]> git.refcnt.org Git - colorize.git/blob - t/merge.t
Compile with -DTEST for testing
[colorize.git] / t / merge.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use lib qw(lib);
6 use constant true => 1;
7 use constant false => 0;
8
9 use Colorize::Common ':defaults';
10 use File::Temp qw(tmpnam);
11 use Test::More;
12
13 # sequence, buffer sizes
14 my @merge_success = (
15 [ "\e[30m", [ 1..4 ] ],
16 [ "\e[31m", [ 1..4 ] ],
17 [ "\e[32m", [ 1..4 ] ],
18 [ "\e[33m", [ 1..4 ] ],
19 [ "\e[34m", [ 1..4 ] ],
20 [ "\e[35m", [ 1..4 ] ],
21 [ "\e[36m", [ 1..4 ] ],
22 [ "\e[37m", [ 1..4 ] ],
23 [ "\e[39m", [ 1..4 ] ],
24 [ "\e[1;30m", [ 1..6 ] ],
25 [ "\e[1;31m", [ 1..6 ] ],
26 [ "\e[1;32m", [ 1..6 ] ],
27 [ "\e[1;33m", [ 1..6 ] ],
28 [ "\e[1;34m", [ 1..6 ] ],
29 [ "\e[1;35m", [ 1..6 ] ],
30 [ "\e[1;36m", [ 1..6 ] ],
31 [ "\e[1;37m", [ 1..6 ] ],
32 [ "\e[1;39m", [ 1..6 ] ],
33 [ "\e[40m", [ 1..4 ] ],
34 [ "\e[41m", [ 1..4 ] ],
35 [ "\e[42m", [ 1..4 ] ],
36 [ "\e[43m", [ 1..4 ] ],
37 [ "\e[44m", [ 1..4 ] ],
38 [ "\e[45m", [ 1..4 ] ],
39 [ "\e[46m", [ 1..4 ] ],
40 [ "\e[47m", [ 1..4 ] ],
41 [ "\e[49m", [ 1..4 ] ],
42 [ "\e[0m", [ 1..3 ] ],
43 [ "\e[m", [ 1..2 ] ],
44 [ "\e[;;m", [ 1..4 ] ],
45 [ "\e[123456m", [ 1 ] ], # tightly coupled to ALLOC_COMPLETE_PART_LINE
46 );
47 # sequence, buffer size
48 my @merge_fail = (
49 [ "\e30m", 1 ], # missing bracket
50 [ "\e[am", 2 ], # not a digit nor ; nor m
51 );
52 # sequence
53 my @buffer = (
54 "\e[30mz",
55 "\e[31mz",
56 "\e[32mz",
57 "\e[33mz",
58 "\e[34mz",
59 "\e[35mz",
60 "\e[36mz",
61 "\e[37mz",
62 "\e[39mz",
63 "\e[1;30mz",
64 "\e[1;31mz",
65 "\e[1;32mz",
66 "\e[1;33mz",
67 "\e[1;34mz",
68 "\e[1;35mz",
69 "\e[1;36mz",
70 "\e[1;37mz",
71 "\e[1;39mz",
72 "\e[40mz",
73 "\e[41mz",
74 "\e[42mz",
75 "\e[43mz",
76 "\e[44mz",
77 "\e[45mz",
78 "\e[46mz",
79 "\e[47mz",
80 "\e[49mz",
81 "\e[0mz",
82 "\e[mz",
83 "\e[;;mz",
84 );
85 # sequence, buffer size
86 my @pushback = (
87 [ "\ezm", 1 ],
88 [ "\e[z", 2 ],
89 );
90
91 my $tests = 0;
92 foreach (@merge_success) {
93 $tests += @{$_->[1]};
94 }
95 $tests += @merge_fail;
96 $tests += @buffer;
97 $tests += @pushback;
98
99 my %programs;
100
101 my $compile = sub
102 {
103 my ($buf_size) = @_;
104 return true if exists $programs{$buf_size};
105 my $program = tmpnam();
106 return false unless system("$compiler -DTEST -DTEST_MERGE_PART_LINE -DBUF_SIZE=$buf_size -o $program $source") == 0;
107 $programs{$buf_size} = $program;
108 return true; # compiling succeeded
109 };
110
111 my $test_name = sub
112 {
113 my ($sequence, $buf_size) = @_;
114 my $substr = substr($sequence, 0, $buf_size);
115 $substr =~ s/^\e/ESC/;
116 $sequence =~ s/^\e/ESC/;
117 return "$sequence: $substr";
118 };
119
120 my $compiling_failed_msg = 'compiling failed (merge part line)';
121
122 plan tests => $tests;
123
124 foreach my $test (@merge_success) {
125 foreach my $buf_size (@{$test->[1]}) {
126 SKIP: {
127 skip $compiling_failed_msg, 1 unless $compile->($buf_size);
128 ok(qx(printf %s "$test->[0]" | $programs{$buf_size} --clean) eq $test->[0], 'merge success: ' . $test_name->($test->[0], $buf_size));
129 }
130 }
131 }
132 foreach my $test (@merge_fail) {
133 my $buf_size = $test->[1];
134 SKIP: {
135 skip $compiling_failed_msg, 1 unless $compile->($buf_size);
136 ok(qx(printf %s "$test->[0]" | $programs{$buf_size} --clean) eq substr($test->[0], 0, $buf_size), 'merge fail: ' . $test_name->($test->[0], $buf_size));
137 }
138 }
139 foreach my $test (@buffer) {
140 my $buf_size = length($test) - 1;
141 SKIP: {
142 skip $compiling_failed_msg, 1 unless $compile->($buf_size);
143 ok(qx(printf %s "$test" | $programs{$buf_size} --clean) eq substr($test, 0, $buf_size), 'buffer: ' . $test_name->($test, $buf_size));
144 }
145 }
146 foreach my $test (@pushback) {
147 my $buf_size = $test->[1];
148 SKIP: {
149 my $program = tmpnam();
150 skip $compiling_failed_msg, 1 unless system("$compiler -DTEST -DBUF_SIZE=$buf_size -o $program $source") == 0;
151 ok(qx(printf %s "$test->[0]" | $program --clean) eq $test->[0], 'pushback: ' . $test_name->($test->[0], $buf_size));
152 unlink $program;
153 }
154 }
155
156 unlink $programs{$_} foreach keys %programs;