1#!/usr/bin/env perl
2#
3##########################################################################
4#
5# Name:         02-color-wrappers.t
6# Version:      1.20
7# Author:       Rene Uittenbogaard
8# Date:         2010-10-04
9# Requires:     Term::ScreenColor
10# Description:  Tests for string wrapping methods in Term::ScreenColor
11#
12
13##########################################################################
14# declarations
15
16use strict;
17use Test::More tests => 208;
18
19require Term::ScreenColor;
20
21my $scr;
22
23my %NORMALS = (
24	"\e[m"      => 1,
25	"\e[0m"     => 1,
26	"\e[m\cO"   => 1,
27	"\e[0m\cO"  => 1,
28	"\e(B\e[m"  => 1,
29	"\e(B\e[0m" => 1,
30);
31
32my %FLASHES = (
33	"\e[?5h\e[?5l" => 1,
34	""             => 1,
35);
36
37my @tests = (
38	# ansi colors: 'colorizable' turns them on/off
39	{ chapter => 'simple ansi: ', color => 'clear                       ', 0 => "", 1 => "\e[0m"  },
40	{ chapter => 'simple ansi: ', color => 'reset                       ', 0 => "", 1 => "\e[0m"  },
41	{ chapter => 'simple ansi: ', color => 'ansibold                    ', 0 => "", 1 => "\e[1m"  },
42	{ chapter => 'simple ansi: ', color => 'italic                      ', 0 => "", 1 => "\e[3m"  },
43	{ chapter => 'simple ansi: ', color => 'underscore                  ', 0 => "", 1 => "\e[4m"  },
44	{ chapter => 'simple ansi: ', color => 'blink                       ', 0 => "", 1 => "\e[5m"  },
45	{ chapter => 'simple ansi: ', color => 'inverse                     ', 0 => "", 1 => "\e[7m"  },
46	{ chapter => 'simple ansi: ', color => 'concealed                   ', 0 => "", 1 => "\e[8m"  },
47
48	{ chapter => 'simple ansi: ', color => 'noansibold                  ', 0 => "", 1 => "\e[22m" },
49	{ chapter => 'simple ansi: ', color => 'noitalic                    ', 0 => "", 1 => "\e[23m" },
50	{ chapter => 'simple ansi: ', color => 'nounderscore                ', 0 => "", 1 => "\e[24m" },
51	{ chapter => 'simple ansi: ', color => 'noblink                     ', 0 => "", 1 => "\e[25m" },
52	{ chapter => 'simple ansi: ', color => 'noinverse                   ', 0 => "", 1 => "\e[27m" },
53	{ chapter => 'simple ansi: ', color => 'noconcealed                 ', 0 => "", 1 => "\e[28m" },
54
55	{ chapter => 'simple ansi color: ', color => 'black                 ', 0 => "", 1 => "\e[30m" },
56	{ chapter => 'simple ansi color: ', color => 'red                   ', 0 => "", 1 => "\e[31m" },
57	{ chapter => 'simple ansi color: ', color => 'green                 ', 0 => "", 1 => "\e[32m" },
58	{ chapter => 'simple ansi color: ', color => 'yellow                ', 0 => "", 1 => "\e[33m" },
59	{ chapter => 'simple ansi color: ', color => 'blue                  ', 0 => "", 1 => "\e[34m" },
60	{ chapter => 'simple ansi color: ', color => 'magenta               ', 0 => "", 1 => "\e[35m" },
61	{ chapter => 'simple ansi color: ', color => 'cyan                  ', 0 => "", 1 => "\e[36m" },
62	{ chapter => 'simple ansi color: ', color => 'white                 ', 0 => "", 1 => "\e[37m" },
63
64	{ chapter => 'simple ansi color: ', color => 'on_black              ', 0 => "", 1 => "\e[40m" },
65	{ chapter => 'simple ansi color: ', color => 'on_red                ', 0 => "", 1 => "\e[41m" },
66	{ chapter => 'simple ansi color: ', color => 'on_green              ', 0 => "", 1 => "\e[42m" },
67	{ chapter => 'simple ansi color: ', color => 'on_yellow             ', 0 => "", 1 => "\e[43m" },
68	{ chapter => 'simple ansi color: ', color => 'on_blue               ', 0 => "", 1 => "\e[44m" },
69	{ chapter => 'simple ansi color: ', color => 'on_magenta            ', 0 => "", 1 => "\e[45m" },
70	{ chapter => 'simple ansi color: ', color => 'on_cyan               ', 0 => "", 1 => "\e[46m" },
71	{ chapter => 'simple ansi color: ', color => 'on_white              ', 0 => "", 1 => "\e[47m" },
72
73	{ chapter => 'complex ansi: ', color => '33;41                      ', 0 => "", 1 => "\e[33;41m" },
74	{ chapter => 'complex ansi: ', color => 'ansibold yellow            ', 0 => "", 1 => "\e[1;33m"  },
75	{ chapter => 'complex ansi: ', color => 'ansibold on red            ', 0 => "", 1 => "\e[1;41m"  },
76	{ chapter => 'complex ansi: ', color => 'cyan on black              ', 0 => "", 1 => "\e[36;40m" },
77	{ chapter => 'complex ansi: ', color => 'white on cyan              ', 0 => "", 1 => "\e[37;46m" },
78	{ chapter => 'complex ansi: ', color => 'green on yellow            ', 0 => "", 1 => "\e[32;43m" },
79	{ chapter => 'complex ansi: ', color => 'magenta inverse            ', 0 => "", 1 => "\e[35;7m"  },
80	{ chapter => 'complex ansi: ', color => 'on_red blink               ', 0 => "", 1 => "\e[41;5m"  },
81	{ chapter => 'complex ansi: ', color => 'yellow on blue             ', 0 => "", 1 => "\e[33;44m" },
82	{ chapter => 'complex ansi: ', color => 'red underscore             ', 0 => "", 1 => "\e[31;4m"  },
83
84	# termcap codes (non-ansi), so 'colorizable' makes no difference
85	{ chapter => 'simple termcap: ', color => 'bold                     ', 0 => "\e[1m", 1 => "\e[1m" },
86	{ chapter => 'simple termcap: ', color => 'underline                ', 0 => "\e[4m", 1 => "\e[4m" },
87	{ chapter => 'simple termcap: ', color => 'reverse                  ', 0 => "\e[7m", 1 => "\e[7m" },
88
89	# if 'colorizable' is off, only the non-ansi component passes
90	{ chapter => 'termcap/ansi combination: ', color => 'bold yellow    ', 0 => "\e[1m", 1 => "\e[1;33m" },
91	{ chapter => 'termcap/ansi combination: ', color => 'bold on red    ', 0 => "\e[1m", 1 => "\e[1;41m" },
92	{ chapter => 'termcap/ansi combination: ', color => 'magenta reverse', 0 => "\e[7m", 1 => "\e[35;7m" },
93	{ chapter => 'termcap/ansi combination: ', color => 'red underline  ', 0 => "\e[4m", 1 => "\e[31;4m" },
94);
95
96##########################################################################
97# test instantiation
98
99sub init {
100	$ENV{TERM} = 'xterm';
101	open NULL, ">/dev/null";
102	# intercept STDOUT as this interferes with test output
103	{
104		local *STDOUT = *NULL;
105		$scr = new Term::ScreenColor();
106		system "stty cooked echo"; # nicer output on terminal
107	}
108	return $scr;
109}
110
111##########################################################################
112# test Term::ScreenColor
113
114sub main {
115	my ($scr) = @_;
116	my ($i, @descriptions, @results, $result, $able, $actual);
117
118	my $teststring = 'blurk';
119
120#	$i = 1; @descriptions = grep { $i++ % 2 } @tests;
121#	$i = 0; @results      = grep { $i++ % 2 } @tests;
122#
123	# ---------- colorizable off ----------
124	ok($scr->colorizable($able = 0), 'turn colorizable off');
125
126	ok($scr->bold2esc()      eq "\e[1m",
127			'direct fetch simple termcap: bold                (colorizable=no)');
128	ok($scr->underline2esc() eq "\e[4m",
129			'direct fetch simple termcap: underline           (colorizable=no)');
130	ok($scr->reverse2esc()   eq "\e[7m",
131			'direct fetch simple termcap: reverse             (colorizable=no)');
132	ok($FLASHES{$scr->flash2esc()},
133			'direct fetch simple termcap: flash               (colorizable=no)');
134	ok($NORMALS{$scr->normal2esc()},
135			'direct fetch simple termcap: normal              (colorizable=no)');
136	ok($scr->color2esc('')               eq "",
137			'fetch ansi color: empty string                   (colorizable=no)');
138	ok($scr->colored('', $teststring)          eq $teststring,
139			'apply ansi color: empty string                   (colorizable=no)');
140	ok($NORMALS{$scr->color2esc('normal on green')},
141			'fetch ansi/termcap combination: normal on green  (colorizable=no)');
142	$result = $scr->colored('normal on green', $teststring);
143	$result =~ /^(.*)$teststring/;
144	ok($NORMALS{$1},'apply ansi/termcap combination: normal on green  (colorizable=no)');
145
146	foreach $i (0 .. $#tests) {
147		$result = $tests[$i]{$able};
148		ok($scr->color2esc($tests[$i]{color}) eq $result,
149			"fetch $tests[$i]{chapter}$tests[$i]{color}  (colorizable=no)");
150		ok($scr->colored($tests[$i]{color}, $teststring) eq
151			($result ? "$result$teststring\e[0m" : $teststring),
152			"apply $tests[$i]{chapter}$tests[$i]{color}  (colorizable=no)");
153	}
154
155	# ---------- colorizable on ----------
156	ok($scr->colorizable($able = 1), 'turn colorizable on');
157
158	ok($scr->bold2esc()      eq "\e[1m",
159			'direct fetch simple termcap: bold                (colorizable=yes)');
160	ok($scr->underline2esc() eq "\e[4m",
161			'direct fetch simple termcap: underline           (colorizable=yes)');
162	ok($scr->reverse2esc()   eq "\e[7m",
163			'direct fetch simple termcap: reverse             (colorizable=yes)');
164	ok($FLASHES{$scr->flash2esc()},
165			'direct fetch simple termcap: flash               (colorizable=yes)');
166	ok($NORMALS{$scr->normal2esc()},
167			'direct fetch simple termcap: normal              (colorizable=yes)');
168	ok($scr->color2esc('')               eq "",
169			'fetch ansi color: empty string                   (colorizable=yes)');
170	ok($scr->colored('', $teststring)    eq $teststring,
171			'apply ansi color: empty string                   (colorizable=yes)');
172	ok($scr->color2esc('normal on green') eq "\e[0;42m",
173			'fetch ansi/termcap combination: normal on green  (colorizable=yes)');
174	ok($scr->colored('normal on green', $teststring) eq "\e[0;42m$teststring\e[0m",
175			'apply ansi/termcap combination: normal on green  (colorizable=yes)');
176
177	foreach $i (0 .. $#tests) {
178		$result = $tests[$i]{$able};
179		ok($scr->color2esc($tests[$i]{color}) eq $result,
180			"fetch $tests[$i]{chapter}$tests[$i]{color}  (colorizable=yes)");
181		ok($scr->colored($tests[$i]{color}, $teststring) eq
182			($result ? "$result$teststring\e[0m" : $teststring),
183			"apply $tests[$i]{chapter}$tests[$i]{color}  (colorizable=yes)");
184	}
185
186}
187
188##########################################################################
189# main
190
191$scr = init();
192main($scr);
193
194__END__
195
196