This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Term-ANSIColor to CPAN version 4.03
[perl5.git] / cpan / Term-ANSIColor / t / module / basic.t
CommitLineData
ba5532d7 1#!/usr/bin/perl
2e589669 2#
ba5532d7 3# Basic test suite for the Term::ANSIColor Perl module.
c23d8173 4#
5e64492f
CBW
5# Copyright 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2009, 2010, 2012, 2014
6# Russ Allbery <rra@cpan.org>
c23d8173
RGS
7#
8# This program is free software; you may redistribute it and/or modify it
9# under the same terms as Perl itself.
e3e5e1ea 10
c23d8173 11use strict;
ba5532d7 12use warnings;
e3e5e1ea 13
ba5532d7
CBW
14use Test::More tests => 152;
15
16# Load the module.
c23d8173 17BEGIN {
ba5532d7 18 delete $ENV{ANSI_COLORS_ALIASES};
c23d8173 19 delete $ENV{ANSI_COLORS_DISABLED};
ba5532d7
CBW
20 use_ok('Term::ANSIColor',
21 qw(:pushpop color colored uncolor colorstrip colorvalid));
e3e5e1ea
GS
22}
23
c23d8173 24# Various basic tests.
ba5532d7
CBW
25is(color('blue on_green', 'bold'), "\e[34;42;1m", 'Simple attributes');
26is(colored('testing', 'blue', 'bold'), "\e[34;1mtesting\e[0m", 'colored');
27is((BLUE BOLD 'testing'), "\e[34m\e[1mtesting", 'Constants');
28is(join(q{}, BLUE, BOLD, 'testing'),
29 "\e[34m\e[1mtesting", 'Constants with commas');
30is((BLUE 'test', 'ing'), "\e[34mtesting", 'Constants with multiple strings');
31
32# Test case variations on attributes.
33is(color('Blue BOLD', 'on_GReeN'), "\e[34;1;42m", 'Attribute case');
34
35# color should return undef if there were no attributes.
36is(color(), undef, 'color returns undef with no attributes');
37
38# Autoreset after the end of a command string.
e3e5e1ea 39$Term::ANSIColor::AUTORESET = 1;
ba5532d7
CBW
40is((BLUE BOLD 'testing'), "\e[34m\e[1mtesting\e[0m\e[0m", 'AUTORESET');
41is((BLUE BOLD, 'te', 'st'), "\e[34m\e[1mtest\e[0m", 'AUTORESET with commas');
42$Term::ANSIColor::AUTORESET = 0;
43
44# Reset after each line terminator.
e3e5e1ea 45$Term::ANSIColor::EACHLINE = "\n";
ba5532d7
CBW
46is(colored("test\n\ntest", 'bold'),
47 "\e[1mtest\e[0m\n\n\e[1mtest\e[0m", 'EACHLINE');
e3e5e1ea 48$Term::ANSIColor::EACHLINE = "\r\n";
ba5532d7
CBW
49is(
50 colored("test\ntest\r\r\n\r\n", 'bold'),
c23d8173 51 "\e[1mtest\ntest\r\e[0m\r\n\r\n",
ba5532d7
CBW
52 'EACHLINE with multiple delimiters'
53);
f63addff 54$Term::ANSIColor::EACHLINE = "\n";
ba5532d7
CBW
55is(
56 colored(['bold', 'on_green'], "test\n", "\n", 'test'),
c23d8173 57 "\e[1;42mtest\e[0m\n\n\e[1;42mtest\e[0m",
ba5532d7
CBW
58 'colored with reference to array'
59);
60
61# Basic tests for uncolor.
62is_deeply([uncolor('1;42', "\e[m", q{}, "\e[0m")],
63 [qw(bold on_green clear)], 'uncolor');
64is_deeply([uncolor("\e[01m")], ['bold'], 'uncolor("\\e[01m")');
65is_deeply([uncolor("\e[m")], [], 'uncolor("\\e[m")');
66is_deeply([uncolor(q{})], [], 'uncolor("")');
110e9fb0 67
c23d8173 68# Several tests for ANSI_COLORS_DISABLED.
ba5532d7
CBW
69local $ENV{ANSI_COLORS_DISABLED} = 1;
70is(color('blue'), q{}, 'color support for ANSI_COLORS_DISABLED');
71is(colored('testing', 'blue', 'on_red'),
72 'testing', 'colored support for ANSI_COLORS_DISABLED');
73is((GREEN 'testing'), 'testing', 'Constant support for ANSI_COLORS_DISABLED');
74delete $ENV{ANSI_COLORS_DISABLED};
75
76# Earlier versions of Term::ANSIColor didn't support ANSI_COLORS_DISABLED if
77# the constant had been created before the environment variable was set. Test
78# all the ones we're going to use to get full test coverage.
79local $ENV{ANSI_COLORS_DISABLED} = 1;
80is((BLUE 'testing'), 'testing', 'ANSI_COLORS_DISABLED with existing constant');
81delete $ENV{ANSI_COLORS_DISABLED};
82
83# If ANSI_COLORS_DISABLED is set to a false value or the empty string, it
84# should not take effect.
85local $ENV{ANSI_COLORS_DISABLED} = 0;
86is(color('bold'), "\e[1m", 'ANSI_COLORS_DISABLED must be true');
87is((BOLD), "\e[1m", '...likewise for constants');
88local $ENV{ANSI_COLORS_DISABLED} = q{};
89is(color('bold'), "\e[1m", '...likewise when set to an empty string');
90is((BOLD), "\e[1m", '...likewise for constants');
2e589669
RGS
91delete $ENV{ANSI_COLORS_DISABLED};
92
93# Make sure DARK is exported. This was omitted in versions prior to 1.07.
ba5532d7 94is((DARK 'testing'), "\e[2mtesting", 'DARK');
51da1d85 95
cdab9eb9 96# Check faint as a synonym for dark.
ba5532d7
CBW
97is(colored('test', 'faint'), "\e[2mtest\e[0m", 'colored supports faint');
98is((FAINT 'test'), "\e[2mtest", '...and the FAINT constant works');
cdab9eb9 99
2c33c767 100# Test bright color support.
ba5532d7
CBW
101is(color('bright_red'), "\e[91m", 'Bright red is supported');
102is((BRIGHT_RED 'test'), "\e[91mtest", '...and as a constant');
103is(color('on_bright_red'), "\e[101m", '...as is on bright red');
104is((ON_BRIGHT_RED 'test'), "\e[101mtest", '...and as a constant');
2c33c767 105
00196a0f 106# Test italic, which was added in 3.02.
ba5532d7
CBW
107is(color('italic'), "\e[3m", 'Italic is supported');
108is((ITALIC 'test'), "\e[3mtest", '...and as a constant');
00196a0f 109
ba5532d7
CBW
110# Test colored with 0 and EACHLINE. Regression test for an incorrect use of a
111# truth check.
51da1d85 112$Term::ANSIColor::EACHLINE = "\n";
ba5532d7
CBW
113is(colored('0', 'blue', 'bold'),
114 "\e[34;1m0\e[0m", 'colored with 0 and EACHLINE');
115is(
116 colored("0\n0\n\n", 'blue', 'bold'),
117 "\e[34;1m0\e[0m\n\e[34;1m0\e[0m\n\n",
118 'colored with 0, EACHLINE, and multiple lines'
119);
51da1d85
RGS
120
121# Test colored with the empty string and EACHLINE.
ba5532d7 122is(colored(q{}, 'blue', 'bold'), q{}, 'colored w/empty string and EACHLINE');
c23d8173
RGS
123
124# Test push and pop support.
ba5532d7
CBW
125is((PUSHCOLOR RED ON_GREEN 'text'),
126 "\e[31m\e[42mtext", 'PUSHCOLOR does not break constants');
127is((PUSHCOLOR BLUE 'text'), "\e[34mtext", '...and adding another level');
128is((RESET BLUE 'text'), "\e[0m\e[34mtext", '...and using reset');
129is((POPCOLOR 'text'), "\e[31m\e[42mtext", '...and POPCOLOR works');
130is((LOCALCOLOR GREEN ON_BLUE 'text'),
131 "\e[32m\e[44mtext\e[31m\e[42m", 'LOCALCOLOR');
c23d8173 132$Term::ANSIColor::AUTOLOCAL = 1;
ba5532d7
CBW
133is((BLUE 'text'), "\e[34mtext\e[31m\e[42m", 'AUTOLOCAL');
134is((BLUE 'te', 'xt'), "\e[34mtext\e[31m\e[42m", 'AUTOLOCAL with commas');
c23d8173 135$Term::ANSIColor::AUTOLOCAL = 0;
ba5532d7
CBW
136is((POPCOLOR 'text'), "\e[0mtext", 'POPCOLOR with empty stack');
137
138# If AUTOLOCAL and AUTORESET are both set, the former takes precedence.
139is((PUSHCOLOR RED ON_GREEN 'text'),
140 "\e[31m\e[42mtext", 'Push some colors onto the stack');
141$Term::ANSIColor::AUTOLOCAL = 1;
142$Term::ANSIColor::AUTORESET = 1;
143is((BLUE 'text'), "\e[34mtext\e[31m\e[42m", 'AUTOLOCAL overrides AUTORESET');
144$Term::ANSIColor::AUTOLOCAL = 0;
145is((BLUE 'text'), "\e[34mtext\e[0m", 'AUTORESET works with stacked colors');
146is((POPCOLOR 'text'), "\e[0mtext\e[0m", 'POPCOLOR with empty stack');
147$Term::ANSIColor::AUTORESET = 0;
c23d8173
RGS
148
149# Test push and pop support with the syntax from the original openmethods.com
150# submission, which uses a different coding style.
ba5532d7
CBW
151is(PUSHCOLOR(RED ON_GREEN), "\e[31m\e[42m", 'PUSHCOLOR with explict argument');
152is(PUSHCOLOR(BLUE), "\e[34m", '...and another explicit argument');
153is(
154 RESET . BLUE . 'text',
155 "\e[0m\e[34mtext",
156 '...and constants with concatenation'
157);
158is(
159 POPCOLOR . 'text',
160 "\e[31m\e[42mtext",
161 '...and POPCOLOR works without an argument'
162);
163is(
164 LOCALCOLOR(GREEN . ON_BLUE . 'text'),
165 "\e[32m\e[44mtext\e[31m\e[42m",
166 'LOCALCOLOR with two arguments'
167);
168is(POPCOLOR . 'text', "\e[0mtext", 'POPCOLOR with no arguments');
169
170# Prior to Term::ANSIColor, PUSHCOLOR, unlike all other constants, didn't take
171# an array, so it could lose colors in some syntax.
172is(PUSHCOLOR(RED, ON_GREEN), "\e[31m\e[42m", 'PUSHCOLOR with two arguments');
173is(
174 LOCALCOLOR(GREEN, 'text'),
175 "\e[32mtext\e[31m\e[42m",
176 'LOCALCOLOR with two arguments'
177);
178is(POPCOLOR(BOLD, 'text'), "\e[0m\e[1mtext", 'POPCOLOR with two arguments');
b5b25974
RGS
179
180# Test colorstrip.
ba5532d7
CBW
181is(
182 colorstrip("\e[1mBold \e[31;42mon green\e[0m\e[m"),
183 'Bold on green',
184 'Basic color stripping'
185);
186is(colorstrip("\e[1m", 'bold', "\e[0m"),
187 'bold', 'Color stripping across multiple strings');
188is_deeply(
189 [colorstrip("\e[1m", 'bold', "\e[0m")],
190 [q{}, 'bold', q{}],
191 '...and in an array context'
192);
193is(colorstrip("foo\e[1m", 'bar', "baz\e[0m"),
194 'foobarbaz', '...and proper joining in scalar context');
195is(
196 colorstrip("\e[2cSome other code\e and stray [0m stuff"),
b5b25974 197 "\e[2cSome other code\e and stray [0m stuff",
ba5532d7
CBW
198 'colorstrip does not remove non-color stuff'
199);
b5b25974 200
cdab9eb9 201# Test colorvalid.
ba5532d7
CBW
202ok(
203 colorvalid('blue bold dark', 'blink on_green'),
204 'colorvalid returns true for valid attributes'
205);
206ok(!colorvalid('green orange'), '...and false for invalid attributes');
207
208# Test error handling in color.
209my $output = eval { color('chartreuse') };
210is($output, undef, 'color on unknown color name fails');
211like(
212 $@,
213 qr{ \A Invalid [ ] attribute [ ] name [ ] chartreuse [ ] at [ ] }xms,
214 '...with the right error'
215);
216
217# Test error handling in colored.
218$output = eval { colored('Stuff', 'chartreuse') };
219is($output, undef, 'colored on unknown color name fails');
220like(
221 $@,
222 qr{ \A Invalid [ ] attribute [ ] name [ ] chartreuse [ ] at [ ] }xms,
223 '...with the right error'
224);
225
226# Test error handling in uncolor.
b5b25974 227$output = eval { uncolor "\e[28m" };
ba5532d7
CBW
228is($output, undef, 'uncolor on unknown color code fails');
229like(
230 $@,
231 qr{ \A No [ ] name [ ] for [ ] escape [ ] sequence [ ] 28 [ ] at [ ] }xms,
232 '...with the right error'
233);
b5b25974 234$output = eval { uncolor "\e[foom" };
ba5532d7
CBW
235is($output, undef, 'uncolor on bad escape sequence fails');
236like(
237 $@,
238 qr{ \A Bad [ ] escape [ ] sequence [ ] foo [ ] at [ ] }xms,
239 '...with the right error'
240);
b5b25974
RGS
241
242# Test error reporting when calling unrecognized Term::ANSIColor subs that go
243# through AUTOLOAD.
ba5532d7
CBW
244ok(!eval { Term::ANSIColor::RSET() }, 'Running invalid constant');
245like(
246 $@,
247 qr{ \A undefined [ ] subroutine [ ] \&Term::ANSIColor::RSET [ ] called
248 [ ] at [ ] }xms,
249 'Correct error from an attribute that is not defined'
250);
251ok(!eval { Term::ANSIColor::reset() }, 'Running invalid sub');
252like(
253 $@,
254 qr{ \A undefined [ ] subroutine [ ] \&Term::ANSIColor::reset [ ] called
255 [ ] at [ ] }xms,
256 'Correct error from a lowercase attribute'
257);
00196a0f
CBW
258
259# Ensure that we still get proper error reporting for unknown constants when
260# when colors are disabled.
ba5532d7
CBW
261local $ENV{ANSI_COLORS_DISABLED} = 1;
262eval { Term::ANSIColor::RSET() };
263like(
264 $@,
265 qr{ \A undefined [ ] subroutine [ ] \&Term::ANSIColor::RSET [ ] called
266 [ ] at [ ] }xms,
267 'Correct error from undefined attribute with disabled colors'
268);
269delete $ENV{ANSI_COLORS_DISABLED};
270
271# These are somewhat redundant, but they ensure we test all the branches in
272# our generated constant subs so that we can use Test::Strict to check test
273# suite coverage.
274is((BOLD 't'), "\e[1mt", 'Basic constant works for BOLD');
275is((BLUE 't'), "\e[34mt", '...and for BLUE');
276is((GREEN 't'), "\e[32mt", '...and for GREEN');
277is((DARK 't'), "\e[2mt", '...and for DARK');
278is((FAINT 't'), "\e[2mt", '...and for FAINT');
279is((BRIGHT_RED 't'), "\e[91mt", '...and for BRIGHT_RED');
280is((ON_BRIGHT_RED 't'), "\e[101mt", '...and for ON_BRIGHT_RED');
281is((ITALIC 't'), "\e[3mt", '...and for ITALIC');
282is((RED 't'), "\e[31mt", '...and for RED');
283is((ON_GREEN 't'), "\e[42mt", '...and for ON_GREEN');
284is((ON_BLUE 't'), "\e[44mt", '...and for ON_BLUE');
285is((RESET 't'), "\e[0mt", '...and for RESET');
286
287# Do the same for disabled colors.
288local $ENV{ANSI_COLORS_DISABLED} = 1;
289is(BOLD, q{}, 'ANSI_COLORS_DISABLED works for BOLD');
290is(BLUE, q{}, '...and for BLUE');
291is(GREEN, q{}, '...and for GREEN');
292is(DARK, q{}, '...and for DARK');
293is(FAINT, q{}, '...and for FAINT');
294is(BRIGHT_RED, q{}, '...and for BRIGHT_RED');
295is(ON_BRIGHT_RED, q{}, '...and for ON_BRIGHT_RED');
296is(ITALIC, q{}, '...and for ITALIC');
297is(RED, q{}, '...and for RED');
298is(ON_GREEN, q{}, '...and for ON_GREEN');
299is(ON_BLUE, q{}, '...and for ON_BLUE');
300is(RESET, q{}, '...and for RESET');
00196a0f 301delete $ENV{ANSI_COLORS_DISABLED};
ba5532d7
CBW
302
303# Do the same for AUTORESET.
304$Term::ANSIColor::AUTORESET = 1;
305is((BOLD 't'), "\e[1mt\e[0m", 'AUTORESET works for BOLD');
306is((BLUE 't'), "\e[34mt\e[0m", '...and for BLUE');
307is((GREEN 't'), "\e[32mt\e[0m", '...and for GREEN');
308is((DARK 't'), "\e[2mt\e[0m", '...and for DARK');
309is((FAINT 't'), "\e[2mt\e[0m", '...and for FAINT');
310is((BRIGHT_RED 't'), "\e[91mt\e[0m", '...and for BRIGHT_RED');
311is((ON_BRIGHT_RED 't'), "\e[101mt\e[0m", '...and for ON_BRIGHT_RED');
312is((ITALIC 't'), "\e[3mt\e[0m", '...and for ITALIC');
313is((RED 't'), "\e[31mt\e[0m", '...and for RED');
314is((ON_GREEN 't'), "\e[42mt\e[0m", '...and for ON_GREEN');
315is((ON_BLUE 't'), "\e[44mt\e[0m", '...and for ON_BLUE');
316is((RESET 't'), "\e[0mt\e[0m", '...and for RESET');
317is((BOLD), "\e[1m", 'AUTORESET without text for BOLD');
318is((BLUE), "\e[34m", '...and for BLUE');
319is((GREEN), "\e[32m", '...and for GREEN');
320is((DARK), "\e[2m", '...and for DARK');
321is((FAINT), "\e[2m", '...and for FAINT');
322is((BRIGHT_RED), "\e[91m", '...and for BRIGHT_RED');
323is((ON_BRIGHT_RED), "\e[101m", '...and for ON_BRIGHT_RED');
324is((ITALIC), "\e[3m", '...and for ITALIC');
325is((RED), "\e[31m", '...and for RED');
326is((ON_GREEN), "\e[42m", '...and for ON_GREEN');
327is((ON_BLUE), "\e[44m", '...and for ON_BLUE');
328is((RESET), "\e[0m", '...and for RESET');
329$Term::ANSIColor::AUTORESET = 0;
330
331# Do the same for AUTOLOCAL.
332$Term::ANSIColor::AUTOLOCAL = 1;
333is((BOLD 't'), "\e[1mt\e[0m", 'AUTOLOCAL works for BOLD');
334is((BLUE 't'), "\e[34mt\e[0m", '...and for BLUE');
335is((GREEN 't'), "\e[32mt\e[0m", '...and for GREEN');
336is((DARK 't'), "\e[2mt\e[0m", '...and for DARK');
337is((FAINT 't'), "\e[2mt\e[0m", '...and for FAINT');
338is((BRIGHT_RED 't'), "\e[91mt\e[0m", '...and for BRIGHT_RED');
339is((ON_BRIGHT_RED 't'), "\e[101mt\e[0m", '...and for ON_BRIGHT_RED');
340is((ITALIC 't'), "\e[3mt\e[0m", '...and for ITALIC');
341is((RED 't'), "\e[31mt\e[0m", '...and for RED');
342is((ON_GREEN 't'), "\e[42mt\e[0m", '...and for ON_GREEN');
343is((ON_BLUE 't'), "\e[44mt\e[0m", '...and for ON_BLUE');
344is((RESET 't'), "\e[0mt\e[0m", '...and for RESET');
345is((BOLD), "\e[1m", 'AUTOLOCAL without text for BOLD');
346is((BLUE), "\e[34m", '...and for BLUE');
347is((GREEN), "\e[32m", '...and for GREEN');
348is((DARK), "\e[2m", '...and for DARK');
349is((FAINT), "\e[2m", '...and for FAINT');
350is((BRIGHT_RED), "\e[91m", '...and for BRIGHT_RED');
351is((ON_BRIGHT_RED), "\e[101m", '...and for ON_BRIGHT_RED');
352is((ITALIC), "\e[3m", '...and for ITALIC');
353is((RED), "\e[31m", '...and for RED');
354is((ON_GREEN), "\e[42m", '...and for ON_GREEN');
355is((ON_BLUE), "\e[44m", '...and for ON_BLUE');
356is((RESET), "\e[0m", '...and for RESET');
357$Term::ANSIColor::AUTOLOCAL = 0;
358
359# Force an internal error inside the AUTOLOAD stub by creating an attribute
360# that will generate a syntax error. This is just for coverage purposes.
361# Disable warnings since our syntax error will spew otherwise.
362local $SIG{__WARN__} = sub { };
363$Term::ANSIColor::ATTRIBUTES{yellow} = q{'ERROR'};
364ok(!eval { YELLOW 't' }, 'Caught internal AUTOLOAD error');
365like(
366 $@,
367 qr{ \A failed [ ] to [ ] generate [ ] constant [ ] YELLOW: [ ] }xms,
368 '...with correct error message'
369);