Blame t/module/basic256.t

Packit e6c7a3
#!/usr/bin/perl
Packit e6c7a3
#
Packit e6c7a3
# Tests for 256-color support.
Packit e6c7a3
#
Packit e6c7a3
# Copyright 2012 Kurt Starsinic <kstarsinic@gmail.com>
Packit e6c7a3
# Copyright 2012, 2013, 2016 Russ Allbery <rra@cpan.org>
Packit e6c7a3
#
Packit e6c7a3
# This program is free software; you may redistribute it and/or modify it
Packit e6c7a3
# under the same terms as Perl itself.
Packit e6c7a3
Packit e6c7a3
use strict;
Packit e6c7a3
use warnings;
Packit e6c7a3
Packit e6c7a3
use Test::More tests => 94;
Packit e6c7a3
Packit e6c7a3
# Load the module.
Packit e6c7a3
BEGIN {
Packit e6c7a3
    delete $ENV{ANSI_COLORS_ALIASES};
Packit e6c7a3
    delete $ENV{ANSI_COLORS_DISABLED};
Packit e6c7a3
    use_ok('Term::ANSIColor', qw(:constants256 color uncolor colorvalid));
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Test basic 256-color codes.
Packit e6c7a3
is(color('ansi0'),  "\e[38;5;0m",   'ANSI 0');
Packit e6c7a3
is(color('ansi15'), "\e[38;5;15m",  'ANSI 15');
Packit e6c7a3
is(color('rgb000'), "\e[38;5;16m",  'RGB 000');
Packit e6c7a3
is(color('rgb555'), "\e[38;5;231m", 'RGB 555');
Packit e6c7a3
is(color('grey0'),  "\e[38;5;232m", 'Grey 0');
Packit e6c7a3
is(color('grey23'), "\e[38;5;255m", 'Grey 23');
Packit e6c7a3
Packit e6c7a3
# Errors at boundary cases.
Packit e6c7a3
for my $color (qw(ansi256 rgb600 rgb060 rgb006 rgb666 rgb999 rgb0000 grey24)) {
Packit e6c7a3
    my $output = eval { color($color) };
Packit e6c7a3
    is($output, undef, 'color on unknown color name fails');
Packit e6c7a3
    like(
Packit e6c7a3
        $@,
Packit e6c7a3
        qr{ \A Invalid [ ] attribute [ ] name [ ] \Q$color\E [ ] at [ ] }xms,
Packit e6c7a3
        '...with the right error'
Packit e6c7a3
    );
Packit e6c7a3
    ok(!colorvalid($color), '...and colorvalid says it is invalid');
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Check that various 256-color codes are valid.
Packit e6c7a3
for my $color (qw(ansi0 ansi15 rgb000 rgb555 grey0 grey23)) {
Packit e6c7a3
    ok(colorvalid($color), "Color $color is valid");
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Check uncolor with 256-color codes.
Packit e6c7a3
is_deeply([uncolor('38;5;0')],        ['ansi0'],    'uncolor of ansi0');
Packit e6c7a3
is_deeply([uncolor("\e[38;5;231m")],  ['rgb555'],   'uncolor of rgb555');
Packit e6c7a3
is_deeply([uncolor("\e[48;05;001m")], ['on_ansi1'], 'uncolor with leading 0s');
Packit e6c7a3
is_deeply([uncolor("\e[38;5;233")],   ['grey1'],    'uncolor of grey1');
Packit e6c7a3
Packit e6c7a3
# An invalid 256-color code should report an error on the part that makes it
Packit e6c7a3
# invalid.  Check truncated codes (should report on the 38 or 48), codes with
Packit e6c7a3
# an invalid second part (likewise), and codes with an invalid third part
Packit e6c7a3
# (should report the complete code).
Packit e6c7a3
#
Packit e6c7a3
# This is a hash of test escape sequences to the invalid sequence that should
Packit e6c7a3
# be reported.
Packit e6c7a3
my %uncolor_tests = (
Packit e6c7a3
    "\e[38m"       => 38,
Packit e6c7a3
    "\e[38;5m"     => 38,
Packit e6c7a3
    "\e[38;5;256m" => '38;5;256',
Packit e6c7a3
    "\e[38;5;777m" => '38;5;777',
Packit e6c7a3
    "\e[48m"       => 48,
Packit e6c7a3
    "\e[48;5m"     => 48,
Packit e6c7a3
    "\e[48;5;256m" => '48;5;256',
Packit e6c7a3
    "\e[48;5;777m" => '48;5;777',
Packit e6c7a3
);
Packit e6c7a3
while (my ($escape, $invalid) = each %uncolor_tests) {
Packit e6c7a3
    my $output = eval { uncolor($escape) };
Packit e6c7a3
    is($output, undef, "uncolor on unknown color code \Q$escape\E fails");
Packit e6c7a3
    like(
Packit e6c7a3
        $@,
Packit e6c7a3
        qr{ \A No [ ] name [ ] for [ ] escape [ ] sequence [ ] \Q$invalid\E
Packit e6c7a3
            [ ] at [ ] }xms,
Packit e6c7a3
        '...with the right error'
Packit e6c7a3
    );
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Test all the variations of a few different constants.
Packit e6c7a3
is((ANSI0 't'),   "\e[38;5;0mt",   'Basic constant works for ANSI0');
Packit e6c7a3
is((ANSI15 't'),  "\e[38;5;15mt",  '...and for ANSI15');
Packit e6c7a3
is((ANSI255 't'), "\e[38;5;255mt", '...and for ANSI255');
Packit e6c7a3
is((RGB000 't'),  "\e[38;5;16mt",  '...and for RGB000');
Packit e6c7a3
is((RGB555 't'),  "\e[38;5;231mt", '...and for RGB555');
Packit e6c7a3
is((GREY0 't'),   "\e[38;5;232mt", '...and for GREY0');
Packit e6c7a3
is((GREY23 't'),  "\e[38;5;255mt", '...and for GREY23');
Packit e6c7a3
Packit e6c7a3
# Do the same for disabled colors.
Packit e6c7a3
local $ENV{ANSI_COLORS_DISABLED} = 1;
Packit e6c7a3
is(ANSI0,  q{}, 'ANSI_COLORS_DISABLED works for ANSI0');
Packit e6c7a3
is(ANSI15, q{}, '...and for ANSI15');
Packit e6c7a3
is(RGB000, q{}, '...and for RGB000');
Packit e6c7a3
is(RGB555, q{}, '...and for RGB555');
Packit e6c7a3
is(GREY0,  q{}, '...and for GREY0');
Packit e6c7a3
is(GREY23, q{}, '...and for GREY23');
Packit e6c7a3
delete $ENV{ANSI_COLORS_DISABLED};
Packit e6c7a3
Packit e6c7a3
# Do the same for AUTORESET.
Packit e6c7a3
$Term::ANSIColor::AUTORESET = 1;
Packit e6c7a3
is((ANSI0 't'),  "\e[38;5;0mt\e[0m",   'AUTORESET works for ANSI0');
Packit e6c7a3
is((ANSI15 't'), "\e[38;5;15mt\e[0m",  '...and for ANSI15');
Packit e6c7a3
is((RGB000 't'), "\e[38;5;16mt\e[0m",  '...and for RGB000');
Packit e6c7a3
is((RGB555 't'), "\e[38;5;231mt\e[0m", '...and for RGB555');
Packit e6c7a3
is((GREY0 't'),  "\e[38;5;232mt\e[0m", '...and for GREY0');
Packit e6c7a3
is((GREY23 't'), "\e[38;5;255mt\e[0m", '...and for GREY23');
Packit e6c7a3
is((ANSI0),      "\e[38;5;0m",         'AUTORESET without text for ANSI0');
Packit e6c7a3
is((ANSI15),     "\e[38;5;15m",        '...and for ANSI15');
Packit e6c7a3
is((RGB000),     "\e[38;5;16m",        '...and for RGB000');
Packit e6c7a3
is((RGB555),     "\e[38;5;231m",       '...and for RGB555');
Packit e6c7a3
is((GREY0),      "\e[38;5;232m",       '...and for GREY0');
Packit e6c7a3
is((GREY23),     "\e[38;5;255m",       '...and for GREY23');
Packit e6c7a3
$Term::ANSIColor::AUTORESET = 0;
Packit e6c7a3
Packit e6c7a3
# Do the same for AUTOLOCAL.
Packit e6c7a3
$Term::ANSIColor::AUTOLOCAL = 1;
Packit e6c7a3
is((ANSI0 't'),  "\e[38;5;0mt\e[0m",   'AUTOLOCAL works for ANSI0');
Packit e6c7a3
is((ANSI15 't'), "\e[38;5;15mt\e[0m",  '...and for ANSI15');
Packit e6c7a3
is((RGB000 't'), "\e[38;5;16mt\e[0m",  '...and for RGB000');
Packit e6c7a3
is((RGB555 't'), "\e[38;5;231mt\e[0m", '...and for RGB555');
Packit e6c7a3
is((GREY0 't'),  "\e[38;5;232mt\e[0m", '...and for GREY0');
Packit e6c7a3
is((GREY23 't'), "\e[38;5;255mt\e[0m", '...and for GREY23');
Packit e6c7a3
is((ANSI0),      "\e[38;5;0m",         'AUTOLOCAL without text for ANSI0');
Packit e6c7a3
is((ANSI15),     "\e[38;5;15m",        '...and for ANSI15');
Packit e6c7a3
is((RGB000),     "\e[38;5;16m",        '...and for RGB000');
Packit e6c7a3
is((RGB555),     "\e[38;5;231m",       '...and for RGB555');
Packit e6c7a3
is((GREY0),      "\e[38;5;232m",       '...and for GREY0');
Packit e6c7a3
is((GREY23),     "\e[38;5;255m",       '...and for GREY23');
Packit e6c7a3
$Term::ANSIColor::AUTOLOCAL = 0;