selftest/subunit: Add diff command that can diff two subunit streams.
[ira/wip.git] / selftest / Subunit.pm
1 # Perl module for parsing and generating the Subunit protocol
2 # Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package Subunit;
18 use POSIX;
19
20 require Exporter;
21 @ISA = qw(Exporter);
22 @EXPORT_OK = qw(parse_results);
23
24 use strict;
25
26 sub parse_results($$$)
27 {
28         my ($msg_ops, $statistics, $fh) = @_;
29         my $unexpected_ok = 0;
30         my $expected_fail = 0;
31         my $unexpected_fail = 0;
32         my $unexpected_err = 0;
33         my $open_tests = [];
34
35         while(<$fh>) {
36                 if (/^test: (.+)\n/) {
37                         $msg_ops->control_msg($_);
38                         $msg_ops->start_test($1);
39                         push (@$open_tests, $1);
40                 } elsif (/^time: (\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)Z\n/) {
41                         $msg_ops->report_time(mktime($6, $5, $4, $3, $2, $1-1900));
42                 } elsif (/^(success|successful|failure|fail|skip|knownfail|error|xfail|skip-testsuite|testsuite-failure|testsuite-xfail|testsuite-success|testsuite-error): (.*?)( \[)?([ \t]*)\n/) {
43                         $msg_ops->control_msg($_);
44                         my $result = $1;
45                         my $testname = $2;
46                         my $reason = undef;
47                         if ($3) {
48                                 $reason = "";
49                                 # reason may be specified in next lines
50                                 my $terminated = 0;
51                                 while(<$fh>) {
52                                         $msg_ops->control_msg($_);
53                                         if ($_ eq "]\n") { $terminated = 1; last; } else { $reason .= $_; }
54                                 }
55                                 
56                                 unless ($terminated) {
57                                         $statistics->{TESTS_ERROR}++;
58                                         $msg_ops->end_test($testname, "error", 1, "reason ($result) interrupted");
59                                         return 1;
60                                 }
61                         }
62                         if ($result eq "success" or $result eq "successful") {
63                                 pop(@$open_tests); #FIXME: Check that popped value == $testname 
64                                 $statistics->{TESTS_EXPECTED_OK}++;
65                                 $msg_ops->end_test($testname, $result, 0, $reason);
66                         } elsif ($result eq "xfail" or $result eq "knownfail") {
67                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
68                                 $statistics->{TESTS_EXPECTED_FAIL}++;
69                                 $msg_ops->end_test($testname, $result, 0, $reason);
70                                 $expected_fail++;
71                         } elsif ($result eq "failure" or $result eq "fail") {
72                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
73                                 $statistics->{TESTS_UNEXPECTED_FAIL}++;
74                                 $msg_ops->end_test($testname, $result, 1, $reason);
75                                 $unexpected_fail++;
76                         } elsif ($result eq "skip") {
77                                 $statistics->{TESTS_SKIP}++;
78                                 my $last = pop(@$open_tests);
79                                 if (defined($last) and $last ne $testname) {
80                                         push (@$open_tests, $testname);
81                                 }
82                                 $msg_ops->end_test($testname, $result, 0, $reason);
83                         } elsif ($result eq "error") {
84                                 $statistics->{TESTS_ERROR}++;
85                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
86                                 $msg_ops->end_test($testname, $result, 1, $reason);
87                                 $unexpected_err++;
88                         } elsif ($result eq "skip-testsuite") {
89                                 $msg_ops->skip_testsuite($testname);
90                         } elsif ($result eq "testsuite-success") {
91                                 $msg_ops->end_testsuite($testname, "success", $reason);
92                         } elsif ($result eq "testsuite-failure") {
93                                 $msg_ops->end_testsuite($testname, "failure", $reason);
94                         } elsif ($result eq "testsuite-xfail") {
95                                 $msg_ops->end_testsuite($testname, "xfail", $reason);
96                         } elsif ($result eq "testsuite-error") {
97                                 $msg_ops->end_testsuite($testname, "error", $reason);
98                         } 
99                 } elsif (/^testsuite: (.*)\n/) {
100                         $msg_ops->start_testsuite($1);
101                 } elsif (/^testsuite-count: (\d+)\n/) {
102                         $msg_ops->testsuite_count($1);
103                 } else {
104                         $msg_ops->output_msg($_);
105                 }
106         }
107
108         while ($#$open_tests+1 > 0) {
109                 $msg_ops->end_test(pop(@$open_tests), "error", 1,
110                                    "was started but never finished!");
111                 $statistics->{TESTS_ERROR}++;
112                 $unexpected_err++;
113         }
114
115         return 1 if $unexpected_err > 0;
116         return 1 if $unexpected_fail > 0;
117         return 1 if $unexpected_ok > 0 and $expected_fail > 0;
118         return 0 if $unexpected_ok > 0 and $expected_fail == 0;
119         return 0 if $expected_fail > 0;
120         return 1;
121 }
122
123 sub start_test($)
124 {
125         my ($testname) = @_;
126         print "test: $testname\n";
127 }
128
129 sub end_test($$;$)
130 {
131         my $name = shift;
132         my $result = shift;
133         my $reason = shift;
134         if ($reason) {
135                 print "$result: $name [\n";
136                 print "$reason";
137                 print "]\n";
138         } else {
139                 print "$result: $name\n";
140         }
141 }
142
143 sub skip_test($;$)
144 {
145         my $name = shift;
146         my $reason = shift;
147         end_test($name, "skip", $reason);
148 }
149
150 sub fail_test($;$)
151 {
152         my $name = shift;
153         my $reason = shift;
154         end_test($name, "fail", $reason);
155 }
156
157 sub success_test($;$)
158 {
159         my $name = shift;
160         my $reason = shift;
161         end_test($name, "success", $reason);
162 }
163
164 sub xfail_test($;$)
165 {
166         my $name = shift;
167         my $reason = shift;
168         end_test($name, "xfail", $reason);
169 }
170
171 sub report_time($)
172 {
173         my ($time) = @_;
174         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
175         printf "time: %04d-%02d-%02d %02d:%02d:%02dZ\n", $year+1900, $mon, $mday, $hour, $min, $sec;
176 }
177
178 # The following are Samba extensions:
179
180 sub start_testsuite($)
181 {
182         my ($name) = @_;
183         print "testsuite: $name\n";
184 }
185
186 sub skip_testsuite($;$)
187 {
188         my ($name, $reason) = @_;
189         if ($reason) {
190                 print "skip-testsuite: $name [\n$reason\n]\n";
191         } else {
192                 print "skip-testsuite: $name\n";
193         }
194 }
195
196 sub end_testsuite($$;$)
197 {
198         my $name = shift;
199         my $result = shift;
200         my $reason = shift;
201         if ($reason) {
202                 print "testsuite-$result: $name [\n";
203                 print "$reason\n";
204                 print "]\n";
205         } else {
206                 print "testsuite-$result: $name\n";
207         }
208 }
209
210 sub testsuite_count($)
211 {
212         my ($count) = @_;
213         print "testsuite-count: $count\n";
214 }
215
216 1;