blob: 5f3c78d70f695b8da354b6bc9a430572a0cd40a5 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use IO::Handle;
7use IPC::Open2 qw( open2 );
8use POSIX qw( WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG );
9
10my $VALGRIND = 0;
Bram Moolenaard4a5f402020-05-17 16:04:44 +020011my $EXECUTABLE = "t/.libs/harness";
Bram Moolenaare4f25e42017-07-07 11:54:15 +020012GetOptions(
13 'valgrind|v+' => \$VALGRIND,
Bram Moolenaard4a5f402020-05-17 16:04:44 +020014 'executable|e=s' => \$EXECUTABLE
Bram Moolenaare4f25e42017-07-07 11:54:15 +020015) or exit 1;
16
17my ( $hin, $hout, $hpid );
18{
19 local $ENV{LD_LIBRARY_PATH} = ".libs";
Bram Moolenaard4a5f402020-05-17 16:04:44 +020020 my @command = $EXECUTABLE;
Bram Moolenaar37e3edc2018-12-15 14:49:34 +010021 unshift @command, "valgrind", "--tool=memcheck", "--leak-check=yes", "--num-callers=25", "--log-file=valgrind.out", "--error-exitcode=126" if $VALGRIND;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020022
23 $hpid = open2 $hout, $hin, @command or die "Cannot open2 harness - $!";
24}
25
26my $exitcode = 0;
27
28my $command;
29my @expect;
30
Bram Moolenaar88d68de2020-05-18 21:51:01 +020031my $linenum = 0;
32
Bram Moolenaare4f25e42017-07-07 11:54:15 +020033sub do_onetest
34{
35 $hin->print( "$command\n" );
36 undef $command;
37
38 my $fail_printed = 0;
39
40 while( my $outline = <$hout> ) {
41 last if $outline eq "DONE\n" or $outline eq "?\n";
42
43 chomp $outline;
44
45 if( !@expect ) {
Bram Moolenaar88d68de2020-05-18 21:51:01 +020046 print "# line $linenum: Test failed\n" unless $fail_printed++;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020047 print "# expected nothing more\n" .
48 "# Actual: $outline\n";
49 next;
50 }
51
52 my $expectation = shift @expect;
53
54 next if $expectation eq $outline;
55
Bram Moolenaar88d68de2020-05-18 21:51:01 +020056 print "# line $linenum: Test failed\n" unless $fail_printed++;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057 print "# Expected: $expectation\n" .
58 "# Actual: $outline\n";
59 }
60
61 if( @expect ) {
Bram Moolenaar88d68de2020-05-18 21:51:01 +020062 print "# line $linenum: Test failed\n" unless $fail_printed++;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020063 print "# Expected: $_\n" .
64 "# didn't happen\n" for @expect;
65 }
66
67 $exitcode = 1 if $fail_printed;
68}
69
70sub do_line
71{
72 my ( $line ) = @_;
73
74 if( $line =~ m/^!(.*)/ ) {
75 do_onetest if defined $command;
76 print "> $1\n";
77 }
78
79 # Commands have capitals
80 elsif( $line =~ m/^([A-Z]+)/ ) {
81 # Some convenience formatting
82 if( $line =~ m/^(PUSH|ENCIN) (.*)$/ ) {
83 # we're evil
84 my $string = eval($2);
85 $line = "$1 " . unpack "H*", $string;
86 }
87
88 do_onetest if defined $command;
89
90 $command = $line;
91 undef @expect;
92 }
93 # Expectations have lowercase
94 elsif( $line =~ m/^([a-z]+)/ ) {
95 # Convenience formatting
96 if( $line =~ m/^(text|encout) (.*)$/ ) {
97 $line = "$1 " . join ",", map sprintf("%x", $_), eval($2);
98 }
99 elsif( $line =~ m/^(output) (.*)$/ ) {
100 $line = "$1 " . join ",", map sprintf("%x", $_), unpack "C*", eval($2);
101 }
102 elsif( $line =~ m/^control (.*)$/ ) {
103 $line = sprintf "control %02x", eval($1);
104 }
105 elsif( $line =~ m/^csi (\S+) (.*)$/ ) {
106 $line = sprintf "csi %02x %s", eval($1), $2; # TODO
107 }
108 elsif( $line =~ m/^(escape|osc|dcs) (.*)$/ ) {
109 $line = "$1 " . join "", map sprintf("%02x", $_), unpack "C*", eval($2);
110 }
111 elsif( $line =~ m/^putglyph (\S+) (.*)$/ ) {
112 $line = "putglyph " . join( ",", map sprintf("%x", $_), eval($1) ) . " $2";
113 }
114 elsif( $line =~ m/^(?:movecursor|scrollrect|moverect|erase|damage|sb_pushline|sb_popline|settermprop|setmousefunc) / ) {
115 # no conversion
116 }
117 else {
118 warn "Unrecognised test expectation '$line'\n";
119 }
120
121 push @expect, $line;
122 }
123 # ?screen_row assertion is emulated here
124 elsif( $line =~ s/^\?screen_row\s+(\d+)\s*=\s*// ) {
125 my $row = $1;
126 my $row1 = $row + 1;
127 my $want = eval($line);
128
129 do_onetest if defined $command;
130
131 # TODO: may not be 80
132 $hin->print( "\?screen_chars $row,0,$row1,80\n" );
133 my $response = <$hout>;
134 chomp $response;
135
136 $response = pack "C*", map hex, split m/,/, $response;
137 if( $response ne $want ) {
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200138 print "# line $linenum: Assert ?screen_row $row failed:\n" .
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139 "# Expected: $want\n" .
140 "# Actual: $response\n";
141 $exitcode = 1;
142 }
143 }
144 # Assertions start with '?'
Bram Moolenaard098b822020-05-18 21:12:59 +0200145 elsif( $line =~ s/^\?([a-z]+.*?=)\s*// ) {
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200146 do_onetest if defined $command;
147
148 my ( $assertion ) = $1 =~ m/^(.*)\s+=/;
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200149 my $expectation = $line;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150
151 $hin->print( "\?$assertion\n" );
152 my $response = <$hout>; defined $response or wait, die "Test harness failed - $?\n";
Bram Moolenaard098b822020-05-18 21:12:59 +0200153 chomp $response; $response =~ s/^\s+|\s+$//g;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200155 # Some convenience formatting
156 if( $assertion =~ m/^screen_chars/ and $expectation =~ m/^"/ ) {
157 $expectation = join ",", map sprintf("0x%02x", ord $_), split m//, eval($expectation);
158 }
159
160 if( $response ne $expectation ) {
161 print "# line $linenum: Assert $assertion failed:\n" .
162 "# Expected: $expectation\n" .
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 "# Actual: $response\n";
164 $exitcode = 1;
165 }
166 }
167 # Test controls start with '$'
168 elsif( $line =~ s/\$SEQ\s+(\d+)\s+(\d+):\s*// ) {
169 my ( $low, $high ) = ( $1, $2 );
170 foreach my $val ( $low .. $high ) {
171 ( my $inner = $line ) =~ s/\\#/$val/g;
172 do_line( $inner );
173 }
174 }
175 elsif( $line =~ s/\$REP\s+(\d+):\s*// ) {
176 my $count = $1;
177 do_line( $line ) for 1 .. $count;
178 }
179 else {
180 die "Unrecognised TEST line $line\n";
181 }
182}
183
184open my $test, "<", $ARGV[0] or die "Cannot open test script $ARGV[0] - $!";
185
186while( my $line = <$test> ) {
Bram Moolenaar88d68de2020-05-18 21:51:01 +0200187 $linenum++;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200188 $line =~ s/^\s+//;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200189 chomp $line;
Bram Moolenaar6fc3b592020-05-17 22:27:55 +0200190
191 next if $line =~ m/^(?:#|$)/;
192 last if $line eq "__END__";
193
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200194 do_line( $line );
195}
196
197do_onetest if defined $command;
198
199close $hin;
200close $hout;
201
202waitpid $hpid, 0;
203if( $? ) {
204 printf STDERR "Harness exited %d\n", WEXITSTATUS($?) if WIFEXITED($?);
205 printf STDERR "Harness exit signal %d\n", WTERMSIG($?) if WIFSIGNALED($?);
206 $exitcode = WIFEXITED($?) ? WEXITSTATUS($?) : 125;
207}
208
209exit $exitcode;