With Win32 now building DynaLoader in ext, it is now built with
[p5sagit/p5-mst-13.2.git] / cpan / Term-ANSIColor / t / basic.t
CommitLineData
b5b25974 1#!/usr/bin/perl -Tw
2e589669 2#
cd07961b 3# t/basic.t -- Test suite for the Term::ANSIColor Perl module.
c23d8173 4#
5# Copyright 1997, 1998, 2000, 2001, 2002, 2005, 2006, 2009
6# Russ Allbery <rra@stanford.edu>
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;
cdab9eb9 12use Test::More tests => 47;
e3e5e1ea 13
c23d8173 14BEGIN {
15 delete $ENV{ANSI_COLORS_DISABLED};
cdab9eb9 16 use_ok ('Term::ANSIColor',
17 qw/:pushpop color colored uncolor colorstrip colorvalid/);
e3e5e1ea 18}
19
c23d8173 20# Various basic tests.
21is (color ('blue on_green', 'bold'), "\e[34;42;1m", 'Simple attributes');
22is (colored ('testing', 'blue', 'bold'), "\e[34;1mtesting\e[0m", 'colored');
23is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting", 'Constants');
e3e5e1ea 24$Term::ANSIColor::AUTORESET = 1;
c23d8173 25is ((BLUE BOLD "testing"), "\e[34m\e[1mtesting\e[0m\e[0m", 'AUTORESET');
e3e5e1ea 26$Term::ANSIColor::EACHLINE = "\n";
c23d8173 27is (colored ("test\n\ntest", 'bold'), "\e[1mtest\e[0m\n\n\e[1mtest\e[0m",
28 'EACHLINE');
e3e5e1ea 29$Term::ANSIColor::EACHLINE = "\r\n";
c23d8173 30is (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');
f63addff 33$Term::ANSIColor::EACHLINE = "\n";
c23d8173 34is (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');
37is_deeply ([ uncolor ('1;42', "\e[m", '', "\e[0m") ],
38 [ qw/bold on_green clear/ ], 'uncolor');
110e9fb0 39
c23d8173 40# Several tests for ANSI_COLORS_DISABLED.
110e9fb0 41$ENV{ANSI_COLORS_DISABLED} = 1;
c23d8173 42is (color ('blue'), '', 'color support for ANSI_COLORS_DISABLED');
43is (colored ('testing', 'blue', 'on_red'), 'testing',
44 'colored support for ANSI_COLORS_DISABLED');
45is ((GREEN 'testing'), 'testing', 'Constant support for ANSI_COLORS_DISABLED');
2e589669 46delete $ENV{ANSI_COLORS_DISABLED};
47
48# Make sure DARK is exported. This was omitted in versions prior to 1.07.
c23d8173 49is ((DARK "testing"), "\e[2mtesting\e[0m", 'DARK');
51da1d85 50
cdab9eb9 51# Check faint as a synonym for dark.
52is (colored ('test', 'faint'), "\e[2mtest\e[0m", 'colored supports faint');
53is ((FAINT "test"), "\e[2mtest\e[0m", '...and the FAINT constant works');
54
51da1d85 55# Test colored with 0 and EACHLINE.
56$Term::ANSIColor::EACHLINE = "\n";
c23d8173 57is (colored ('0', 'blue', 'bold'), "\e[34;1m0\e[0m",
58 'colored with 0 and EACHLINE');
59is (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');
51da1d85 61
62# Test colored with the empty string and EACHLINE.
c23d8173 63is (colored ('', 'blue', 'bold'), '',
64 'colored with an empty string and EACHLINE');
65
66# Test push and pop support.
67$Term::ANSIColor::AUTORESET = 0;
68is ((PUSHCOLOR RED ON_GREEN "text"), "\e[31m\e[42mtext",
69 'PUSHCOLOR does not break constants');
70is ((PUSHCOLOR BLUE "text"), "\e[34mtext", '...and adding another level');
71is ((RESET BLUE "text"), "\e[0m\e[34mtext", '...and using reset');
72is ((POPCOLOR "text"), "\e[31m\e[42mtext", '...and POPCOLOR works');
73is ((LOCALCOLOR GREEN ON_BLUE "text"), "\e[32m\e[44mtext\e[31m\e[42m",
74 'LOCALCOLOR');
75$Term::ANSIColor::AUTOLOCAL = 1;
76is ((ON_BLUE "text"), "\e[44mtext\e[31m\e[42m", 'AUTOLOCAL');
77$Term::ANSIColor::AUTOLOCAL = 0;
78is ((POPCOLOR "text"), "\e[0mtext", 'POPCOLOR with empty stack');
79
80# Test push and pop support with the syntax from the original openmethods.com
81# submission, which uses a different coding style.
82is (PUSHCOLOR (RED ON_GREEN), "\e[31m\e[42m",
83 'PUSHCOLOR with explict argument');
84is (PUSHCOLOR (BLUE), "\e[34m", '...and another explicit argument');
85is (RESET . BLUE . "text", "\e[0m\e[34mtext",
86 '...and constants with concatenation');
87is (POPCOLOR . "text", "\e[31m\e[42mtext",
88 '...and POPCOLOR works without an argument');
89is (LOCALCOLOR(GREEN . ON_BLUE . "text"), "\e[32m\e[44mtext\e[31m\e[42m",
90 'LOCALCOLOR with two arguments');
91is (POPCOLOR . "text", "\e[0mtext", 'POPCOLOR with no arguments');
b5b25974 92
93# Test colorstrip.
94is (colorstrip ("\e[1mBold \e[31;42mon green\e[0m\e[m"), 'Bold on green',
95 'Basic color stripping');
96is (colorstrip ("\e[1m", 'bold', "\e[0m"), 'bold',
97 'Color stripping across multiple strings');
98is_deeply ([ colorstrip ("\e[1m", 'bold', "\e[0m") ],
99 [ '', 'bold', '' ], '...and in an array context');
100is (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');
103
cdab9eb9 104# Test colorvalid.
105is (colorvalid ("blue bold dark", "blink on_green"), 1,
106 'colorvalid returns true for valid attributes');
107is (colorvalid ("green orange"), undef,
108 '...and false for invalid attributes');
109
b5b25974 110# Test error handling.
111my $output = eval { color 'chartreuse' };
112is ($output, undef, 'color on unknown color name fails');
113like ($@, qr/^Invalid attribute name chartreuse at /,
114 '...with the right error');
115$output = eval { colored "Stuff", 'chartreuse' };
116is ($output, undef, 'colored on unknown color name fails');
117like ($@, qr/^Invalid attribute name chartreuse at /,
118 '...with the right error');
119$output = eval { uncolor "\e[28m" };
120is ($output, undef, 'uncolor on unknown color code fails');
121like ($@, qr/^No name for escape sequence 28 at /, '...with the right error');
122$output = eval { uncolor "\e[foom" };
123is ($output, undef, 'uncolor on bad escape sequence fails');
124like ($@, qr/^Bad escape sequence foo at /, '...with the right error');
125
126# Test error reporting when calling unrecognized Term::ANSIColor subs that go
127# through AUTOLOAD.
128eval { Term::ANSIColor::RSET () };
129like ($@, qr/^undefined subroutine \&Term::ANSIColor::RSET called at /,
130 'Correct error from an attribute that is not defined');
131eval { Term::ANSIColor::reset () };
132like ($@, qr/^undefined subroutine \&Term::ANSIColor::reset called at /,
133 'Correct error from a lowercase attribute');