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