]> git.refcnt.org Git - colorize.git/blob - t/merge.t
Set compiler once for tests
[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 [ "\e[123456m", [ 1 ] ], # tightly coupled to ALLOC_COMPLETE_PART_LINE
44 );
45 # sequence, buffer size
46 my @merge_fail = (
47 [ "\e30m", 1 ], # missing bracket
48 [ "\e[am", 2 ], # not a digit nor ; nor m
49 );
50 # sequence
51 my @buffer = (
52 "\e[30mz",
53 "\e[31mz",
54 "\e[32mz",
55 "\e[33mz",
56 "\e[34mz",
57 "\e[35mz",
58 "\e[36mz",
59 "\e[37mz",
60 "\e[39mz",
61 "\e[1;30mz",
62 "\e[1;31mz",
63 "\e[1;32mz",
64 "\e[1;33mz",
65 "\e[1;34mz",
66 "\e[1;35mz",
67 "\e[1;36mz",
68 "\e[1;37mz",
69 "\e[1;39mz",
70 "\e[40mz",
71 "\e[41mz",
72 "\e[42mz",
73 "\e[43mz",
74 "\e[44mz",
75 "\e[45mz",
76 "\e[46mz",
77 "\e[47mz",
78 "\e[49mz",
79 "\e[0mz",
80 "\e[mz",
81 "\e[;;mz",
82 );
83 # sequence, buffer size
84 my @pushback = (
85 [ "\ezm", 1 ],
86 [ "\e[z", 2 ],
87 );
88
89 my $tests = 0;
90 foreach (@merge_success) {
91 $tests += @{$_->[1]};
92 }
93 $tests += @merge_fail;
94 $tests += @buffer;
95 $tests += @pushback;
96
97 my $source = 'colorize.c';
98 my $compiler = 'gcc';
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_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 plan tests => $tests;
121
122 foreach my $test (@merge_success) {
123 foreach my $buf_size (@{$test->[1]}) {
124 SKIP: {
125 skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
126 ok(qx(printf %s "$test->[0]" | $programs{$buf_size} --clean) eq $test->[0], 'merge success: ' . $test_name->($test->[0], $buf_size));
127 }
128 }
129 }
130 foreach my $test (@merge_fail) {
131 my $buf_size = $test->[1];
132 SKIP: {
133 skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
134 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));
135 }
136 }
137 foreach my $test (@buffer) {
138 my $buf_size = length($test) - 1;
139 SKIP: {
140 skip 'compiling failed (merge part line)', 1 unless $compile->($buf_size);
141 ok(qx(printf %s "$test" | $programs{$buf_size} --clean) eq substr($test, 0, $buf_size), 'buffer: ' . $test_name->($test, $buf_size));
142 }
143 }
144 foreach my $test (@pushback) {
145 my $buf_size = $test->[1];
146 SKIP: {
147 my $program = tmpnam();
148 skip 'compiling failed (merge part line)', 1 unless system("$compiler -DBUF_SIZE=$buf_size -o $program $source") == 0;
149 ok(qx(printf %s "$test->[0]" | $program --clean) eq $test->[0], 'pushback: ' . $test_name->($test->[0], $buf_size));
150 unlink $program;
151 }
152 }
153
154 unlink $programs{$_} foreach keys %programs;