File: basic256.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (142 lines) | stat: -rw-r--r-- 5,563 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/perl
#
# Tests for 256-color support.
#
# Copyright 2012 Kurt Starsinic <kstarsinic@gmail.com>
# Copyright 2012-2013, 2016, 2020 Russ Allbery <rra@cpan.org>
#
# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl

use 5.008;
use strict;
use warnings;

use Test::More tests => 100;

# Load the module.
BEGIN {
    delete $ENV{ANSI_COLORS_ALIASES};
    delete $ENV{ANSI_COLORS_DISABLED};
    delete $ENV{NO_COLOR};
    use_ok('Term::ANSIColor', qw(:constants256 color uncolor colorvalid));
}

# Test basic 256-color codes.
is(color('ansi0'),  "\e[38;5;0m",   'ANSI 0');
is(color('ansi15'), "\e[38;5;15m",  'ANSI 15');
is(color('rgb000'), "\e[38;5;16m",  'RGB 000');
is(color('rgb555'), "\e[38;5;231m", 'RGB 555');
is(color('grey0'),  "\e[38;5;232m", 'Grey 0');
is(color('grey23'), "\e[38;5;255m", 'Grey 23');

# Errors at boundary cases.
for my $color (qw(ansi256 rgb600 rgb060 rgb006 rgb666 rgb999 rgb0000 grey24)) {
    my $output = eval { color($color) };
    is($output, undef, 'color on unknown color name fails');
    like(
        $@,
        qr{ \A Invalid [ ] attribute [ ] name [ ] \Q$color\E [ ] at [ ] }xms,
        '...with the right error'
    );
    ok(!colorvalid($color), '...and colorvalid says it is invalid');
}

# Check that various 256-color codes are valid.
for my $color (qw(ansi0 ansi15 rgb000 rgb555 grey0 grey23)) {
    ok(colorvalid($color), "Color $color is valid");
}

# Check uncolor with 256-color codes.
is_deeply([uncolor('38;5;0')],        ['ansi0'],    'uncolor of ansi0');
is_deeply([uncolor("\e[38;5;231m")],  ['rgb555'],   'uncolor of rgb555');
is_deeply([uncolor("\e[48;05;001m")], ['on_ansi1'], 'uncolor with leading 0s');
is_deeply([uncolor("\e[38;5;233")],   ['grey1'],    'uncolor of grey1');

# An invalid 256-color code should report an error on the part that makes it
# invalid.  Check truncated codes (should report on the 38 or 48), codes with
# an invalid second part (likewise), and codes with an invalid third part
# (should report the complete code).
#
# This is a hash of test escape sequences to the invalid sequence that should
# be reported.
my %uncolor_tests = (
    "\e[38m"       => 38,
    "\e[38;5m"     => 38,
    "\e[38;5;256m" => '38;5;256',
    "\e[38;5;777m" => '38;5;777',
    "\e[48m"       => 48,
    "\e[48;5m"     => 48,
    "\e[48;5;256m" => '48;5;256',
    "\e[48;5;777m" => '48;5;777',
);
while (my ($escape, $invalid) = each %uncolor_tests) {
    my $output = eval { uncolor($escape) };
    is($output, undef, "uncolor on unknown color code \Q$escape\E fails");
    like(
        $@,
        qr{ \A No [ ] name [ ] for [ ] escape [ ] sequence [ ] \Q$invalid\E
            [ ] at [ ] }xms,
        '...with the right error'
    );
}

# Test all the variations of a few different constants.
is((ANSI0 't'),   "\e[38;5;0mt",   'Basic constant works for ANSI0');
is((ANSI15 't'),  "\e[38;5;15mt",  '...and for ANSI15');
is((ANSI255 't'), "\e[38;5;255mt", '...and for ANSI255');
is((RGB000 't'),  "\e[38;5;16mt",  '...and for RGB000');
is((RGB555 't'),  "\e[38;5;231mt", '...and for RGB555');
is((GREY0 't'),   "\e[38;5;232mt", '...and for GREY0');
is((GREY23 't'),  "\e[38;5;255mt", '...and for GREY23');

# Do the same for disabled colors.
local $ENV{ANSI_COLORS_DISABLED} = 1;
is(ANSI0,  q{}, 'ANSI_COLORS_DISABLED works for ANSI0');
is(ANSI15, q{}, '...and for ANSI15');
is(RGB000, q{}, '...and for RGB000');
is(RGB555, q{}, '...and for RGB555');
is(GREY0,  q{}, '...and for GREY0');
is(GREY23, q{}, '...and for GREY23');
delete $ENV{ANSI_COLORS_DISABLED};

# Do the same with NO_COLOR.
local $ENV{NO_COLOR} = 0;
is(ANSI0,  q{}, 'NO_COLOR works for ANSI0');
is(ANSI15, q{}, '...and for ANSI15');
is(RGB000, q{}, '...and for RGB000');
is(RGB555, q{}, '...and for RGB555');
is(GREY0,  q{}, '...and for GREY0');
is(GREY23, q{}, '...and for GREY23');
delete $ENV{NO_COLOR};

# Do the same for AUTORESET.
$Term::ANSIColor::AUTORESET = 1;
is((ANSI0 't'),  "\e[38;5;0mt\e[0m",   'AUTORESET works for ANSI0');
is((ANSI15 't'), "\e[38;5;15mt\e[0m",  '...and for ANSI15');
is((RGB000 't'), "\e[38;5;16mt\e[0m",  '...and for RGB000');
is((RGB555 't'), "\e[38;5;231mt\e[0m", '...and for RGB555');
is((GREY0 't'),  "\e[38;5;232mt\e[0m", '...and for GREY0');
is((GREY23 't'), "\e[38;5;255mt\e[0m", '...and for GREY23');
is((ANSI0),      "\e[38;5;0m",         'AUTORESET without text for ANSI0');
is((ANSI15),     "\e[38;5;15m",        '...and for ANSI15');
is((RGB000),     "\e[38;5;16m",        '...and for RGB000');
is((RGB555),     "\e[38;5;231m",       '...and for RGB555');
is((GREY0),      "\e[38;5;232m",       '...and for GREY0');
is((GREY23),     "\e[38;5;255m",       '...and for GREY23');
$Term::ANSIColor::AUTORESET = 0;

# Do the same for AUTOLOCAL.
$Term::ANSIColor::AUTOLOCAL = 1;
is((ANSI0 't'),  "\e[38;5;0mt\e[0m",   'AUTOLOCAL works for ANSI0');
is((ANSI15 't'), "\e[38;5;15mt\e[0m",  '...and for ANSI15');
is((RGB000 't'), "\e[38;5;16mt\e[0m",  '...and for RGB000');
is((RGB555 't'), "\e[38;5;231mt\e[0m", '...and for RGB555');
is((GREY0 't'),  "\e[38;5;232mt\e[0m", '...and for GREY0');
is((GREY23 't'), "\e[38;5;255mt\e[0m", '...and for GREY23');
is((ANSI0),      "\e[38;5;0m",         'AUTOLOCAL without text for ANSI0');
is((ANSI15),     "\e[38;5;15m",        '...and for ANSI15');
is((RGB000),     "\e[38;5;16m",        '...and for RGB000');
is((RGB555),     "\e[38;5;231m",       '...and for RGB555');
is((GREY0),      "\e[38;5;232m",       '...and for GREY0');
is((GREY23),     "\e[38;5;255m",       '...and for GREY23');
$Term::ANSIColor::AUTOLOCAL = 0;