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