0bbb795c8c24acbf2a7f63504b43ebb6efc7249c
[ira/wip.git] / selftest / Subunit.pm
1 # Simple Perl module for parsing the Subunit protocol
2 # Copyright (C) 2008 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, $expecting_failure, $open_tests) = @_;
29         my $unexpected_ok = 0;
30         my $expected_fail = 0;
31         my $unexpected_fail = 0;
32         my $unexpected_err = 0;
33         my $orig_open_len = $#$open_tests;
34
35         while(<$fh>) {
36                 if (/^test: (.+)\n/) {
37                         $msg_ops->control_msg($_);
38                         $msg_ops->start_test($open_tests, $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));
42                 } elsif (/^(success|successful|failure|skip|knownfail|error): (.*?)( \[)?([ \t]*)\n/) {
43                         $msg_ops->control_msg($_);
44                         my $reason = undef;
45                         if ($3) {
46                                 $reason = "";
47                                 # reason may be specified in next lines
48                                 my $terminated = 0;
49                                 while(<$fh>) {
50                                         $msg_ops->control_msg($_);
51                                         if ($_ eq "]\n") { $terminated = 1; last; } else { $reason .= $_; }
52                                 }
53                                 
54                                 unless ($terminated) {
55                                         $statistics->{TESTS_ERROR}++;
56                                         $msg_ops->end_test($open_tests, $2, $1, 1, "reason interrupted");
57                                         return 1;
58                                 }
59                         }
60                         my $result = $1;
61                         if ($1 eq "success" or $1 eq "successful") {
62                                 pop(@$open_tests); #FIXME: Check that popped value == $2
63                                 if ($expecting_failure->(join(".", @$open_tests) . ".$2")) {
64                                         $statistics->{TESTS_UNEXPECTED_OK}++;
65                                         $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
66                                         $unexpected_ok++;
67                                 } else {
68                                         $statistics->{TESTS_EXPECTED_OK}++;
69                                         $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
70                                 }
71                         } elsif ($1 eq "failure") {
72                                 pop(@$open_tests); #FIXME: Check that popped value == $2
73                                 if ($expecting_failure->(join(".", @$open_tests) . ".$2")) {
74                                         $statistics->{TESTS_EXPECTED_FAIL}++;
75                                         $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
76                                         $expected_fail++;
77                                 } else {
78                                         $statistics->{TESTS_UNEXPECTED_FAIL}++;
79                                         $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
80                                         $unexpected_fail++;
81                                 }
82                         } elsif ($1 eq "knownfail") {
83                                 pop(@$open_tests); #FIXME: Check that popped value == $2
84                                 $statistics->{TESTS_EXPECTED_FAIL}++;
85                                 $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
86                         } elsif ($1 eq "skip") {
87                                 $statistics->{TESTS_SKIP}++;
88                                 pop(@$open_tests); #FIXME: Check that popped value == $2
89                                 $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
90                         } elsif ($1 eq "error") {
91                                 $statistics->{TESTS_ERROR}++;
92                                 pop(@$open_tests); #FIXME: Check that popped value == $2
93                                 $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
94                                 $unexpected_err++;
95                         }
96                 } else {
97                         $msg_ops->output_msg($_);
98                 }
99         }
100
101         while ($#$open_tests > $orig_open_len) {
102                 $msg_ops->end_test($open_tests, pop(@$open_tests), "error", 1,
103                                    "was started but never finished!");
104                 $statistics->{TESTS_ERROR}++;
105                 $unexpected_err++;
106         }
107
108         return 1 if $unexpected_err > 0;
109         return 1 if $unexpected_fail > 0;
110         return 1 if $unexpected_ok > 0 and $expected_fail > 0;
111         return 0 if $unexpected_ok > 0 and $expected_fail == 0;
112         return 0 if $expected_fail > 0;
113         return 1;
114 }
115
116 sub start_test($)
117 {
118         my ($testname) = @_;
119         print "test: $testname\n";
120 }
121
122 sub end_test($$;$)
123 {
124         my $name = shift;
125         my $result = shift;
126         my $reason = shift;
127         if ($reason) {
128                 print "$result: $name [ $reason ]\n";
129         } else {
130                 print "$result: $name\n";
131         }
132 }
133
134 sub report_time($)
135 {
136         my ($time) = @_;
137         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
138         printf "time: %04d-%02d-%02d %02d:%02d:%02dZ\n", $year+1900, $mon, $mday, $hour, $min, $sec;
139 }
140
141 1;