3 # t/basic.t -- Test suite for the Term::ANSIColor Perl module.
5 # Copyright 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2009
6 # Russ Allbery <rra@stanford.edu>
8 # This program is free software; you may redistribute it and/or modify it
9 # under the same terms as Perl itself.
12 use Test::More tests => 47;
15 delete $ENV{ANSI_COLORS_DISABLED};
16 use_ok ('Term::ANSIColor',
17 qw/:pushpop color colored uncolor colorstrip colorvalid/);
20 # Various basic tests.
21 is (color ('blue on_green', 'bold'), "\e[34;42;1m", 'Simple attributes');
22 is (colored ('testing', 'blue', 'bold'), "\e[34;1mtesting\e[0m", 'colored');
23 is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting", 'Constants');
24 $Term::ANSIColor::AUTORESET = 1;
25 is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting\e[0m\e[0m", 'AUTORESET');
26 $Term::ANSIColor::EACHLINE = "\n";
27 is (colored ("test\n\ntest", 'bold'), "\e[1mtest\e[0m\n\n\e[1mtest\e[0m",
29 $Term::ANSIColor::EACHLINE = "\r\n";
30 is (colored ("test\ntest\r\r\n\r\n", 'bold'),
31 "\e[1mtest\ntest\r\e[0m\r\n\r\n",
32 'EACHLINE with multiple delimiters');
33 $Term::ANSIColor::EACHLINE = "\n";
34 is (colored (['bold', 'on_green'], "test\n", "\n", "test"),
35 "\e[1;42mtest\e[0m\n\n\e[1;42mtest\e[0m",
36 'colored with reference to array');
37 is_deeply ([ uncolor ('1;42', "\e[m", '', "\e[0m") ],
38 [ qw/bold on_green clear/ ], 'uncolor');
40 # Several tests for ANSI_COLORS_DISABLED.
41 $ENV{ANSI_COLORS_DISABLED} = 1;
42 is (color ('blue'), '', 'color support for ANSI_COLORS_DISABLED');
43 is (colored ('testing', 'blue', 'on_red'), 'testing',
44 'colored support for ANSI_COLORS_DISABLED');
45 is ((GREEN 'testing'), 'testing', 'Constant support for ANSI_COLORS_DISABLED');
46 delete $ENV{ANSI_COLORS_DISABLED};
48 # Make sure DARK is exported. This was omitted in versions prior to 1.07.
49 is ((DARK "testing"), "\e[2mtesting\e[0m", 'DARK');
51 # Check faint as a synonym for dark.
52 is (colored ('test', 'faint'), "\e[2mtest\e[0m", 'colored supports faint');
53 is ((FAINT "test"), "\e[2mtest\e[0m", '...and the FAINT constant works');
55 # Test colored with 0 and EACHLINE.
56 $Term::ANSIColor::EACHLINE = "\n";
57 is (colored ('0', 'blue', 'bold'), "\e[34;1m0\e[0m",
58 'colored with 0 and EACHLINE');
59 is (colored ("0\n0\n\n", 'blue', 'bold'), "\e[34;1m0\e[0m\n\e[34;1m0\e[0m\n\n",
60 'colored with 0, EACHLINE, and multiple lines');
62 # Test colored with the empty string and EACHLINE.
63 is (colored ('', 'blue', 'bold'), '',
64 'colored with an empty string and EACHLINE');
66 # Test push and pop support.
67 $Term::ANSIColor::AUTORESET = 0;
68 is ((PUSHCOLOR RED ON_GREEN "text"), "\e[31m\e[42mtext",
69 'PUSHCOLOR does not break constants');
70 is ((PUSHCOLOR BLUE "text"), "\e[34mtext", '...and adding another level');
71 is ((RESET BLUE "text"), "\e[0m\e[34mtext", '...and using reset');
72 is ((POPCOLOR "text"), "\e[31m\e[42mtext", '...and POPCOLOR works');
73 is ((LOCALCOLOR GREEN ON_BLUE "text"), "\e[32m\e[44mtext\e[31m\e[42m",
75 $Term::ANSIColor::AUTOLOCAL = 1;
76 is ((ON_BLUE "text"), "\e[44mtext\e[31m\e[42m", 'AUTOLOCAL');
77 $Term::ANSIColor::AUTOLOCAL = 0;
78 is ((POPCOLOR "text"), "\e[0mtext", 'POPCOLOR with empty stack');
80 # Test push and pop support with the syntax from the original openmethods.com
81 # submission, which uses a different coding style.
82 is (PUSHCOLOR (RED ON_GREEN), "\e[31m\e[42m",
83 'PUSHCOLOR with explict argument');
84 is (PUSHCOLOR (BLUE), "\e[34m", '...and another explicit argument');
85 is (RESET . BLUE . "text", "\e[0m\e[34mtext",
86 '...and constants with concatenation');
87 is (POPCOLOR . "text", "\e[31m\e[42mtext",
88 '...and POPCOLOR works without an argument');
89 is (LOCALCOLOR(GREEN . ON_BLUE . "text"), "\e[32m\e[44mtext\e[31m\e[42m",
90 'LOCALCOLOR with two arguments');
91 is (POPCOLOR . "text", "\e[0mtext", 'POPCOLOR with no arguments');
94 is (colorstrip ("\e[1mBold \e[31;42mon green\e[0m\e[m"), 'Bold on green',
95 'Basic color stripping');
96 is (colorstrip ("\e[1m", 'bold', "\e[0m"), 'bold',
97 'Color stripping across multiple strings');
98 is_deeply ([ colorstrip ("\e[1m", 'bold', "\e[0m") ],
99 [ '', 'bold', '' ], '...and in an array context');
100 is (colorstrip ("\e[2cSome other code\e and stray [0m stuff"),
101 "\e[2cSome other code\e and stray [0m stuff",
102 'colorstrip does not remove non-color stuff');
105 is (colorvalid ("blue bold dark", "blink on_green"), 1,
106 'colorvalid returns true for valid attributes');
107 is (colorvalid ("green orange"), undef,
108 '...and false for invalid attributes');
110 # Test error handling.
111 my $output = eval { color 'chartreuse' };
112 is ($output, undef, 'color on unknown color name fails');
113 like ($@, qr/^Invalid attribute name chartreuse at /,
114 '...with the right error');
115 $output = eval { colored "Stuff", 'chartreuse' };
116 is ($output, undef, 'colored on unknown color name fails');
117 like ($@, qr/^Invalid attribute name chartreuse at /,
118 '...with the right error');
119 $output = eval { uncolor "\e[28m" };
120 is ($output, undef, 'uncolor on unknown color code fails');
121 like ($@, qr/^No name for escape sequence 28 at /, '...with the right error');
122 $output = eval { uncolor "\e[foom" };
123 is ($output, undef, 'uncolor on bad escape sequence fails');
124 like ($@, qr/^Bad escape sequence foo at /, '...with the right error');
126 # Test error reporting when calling unrecognized Term::ANSIColor subs that go
128 eval { Term::ANSIColor::RSET () };
129 like ($@, qr/^undefined subroutine \&Term::ANSIColor::RSET called at /,
130 'Correct error from an attribute that is not defined');
131 eval { Term::ANSIColor::reset () };
132 like ($@, qr/^undefined subroutine \&Term::ANSIColor::reset called at /,
133 'Correct error from a lowercase attribute');