selftest: Add copyright headers
[jra/samba/.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
19 require Exporter;
20 @ISA = qw(Exporter);
21 @EXPORT_OK = qw(parse_results);
22
23 use strict;
24
25 sub parse_results($$$$$)
26 {
27         my ($msg_ops, $statistics, $fh, $expecting_failure, $open_tests) = @_;
28         my $unexpected_ok = 0;
29         my $expected_fail = 0;
30         my $unexpected_fail = 0;
31         my $unexpected_err = 0;
32         my $orig_open_len = $#$open_tests;
33
34         while(<$fh>) {
35                 if (/^test: (.+)\n/) {
36                         $msg_ops->control_msg($_);
37                         $msg_ops->start_test($open_tests, $1);
38                         push (@$open_tests, $1);
39                 } elsif (/^(success|successful|failure|skip|knownfail|error): (.*?)( \[)?([ \t]*)\n/) {
40                         $msg_ops->control_msg($_);
41                         my $reason = undef;
42                         if ($3) {
43                                 $reason = "";
44                                 # reason may be specified in next lines
45                                 my $terminated = 0;
46                                 while(<$fh>) {
47                                         $msg_ops->control_msg($_);
48                                         if ($_ eq "]\n") { $terminated = 1; last; } else { $reason .= $_; }
49                                 }
50                                 
51                                 unless ($terminated) {
52                                         $statistics->{TESTS_ERROR}++;
53                                         $msg_ops->end_test($open_tests, $2, $1, 1, "reason interrupted");
54                                         return 1;
55                                 }
56                         }
57                         my $result = $1;
58                         if ($1 eq "success" or $1 eq "successful") {
59                                 pop(@$open_tests); #FIXME: Check that popped value == $2
60                                 if ($expecting_failure->(join(".", @$open_tests) . ".$2")) {
61                                         $statistics->{TESTS_UNEXPECTED_OK}++;
62                                         $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
63                                         $unexpected_ok++;
64                                 } else {
65                                         $statistics->{TESTS_EXPECTED_OK}++;
66                                         $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
67                                 }
68                         } elsif ($1 eq "failure") {
69                                 pop(@$open_tests); #FIXME: Check that popped value == $2
70                                 if ($expecting_failure->(join(".", @$open_tests) . ".$2")) {
71                                         $statistics->{TESTS_EXPECTED_FAIL}++;
72                                         $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
73                                         $expected_fail++;
74                                 } else {
75                                         $statistics->{TESTS_UNEXPECTED_FAIL}++;
76                                         $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
77                                         $unexpected_fail++;
78                                 }
79                         } elsif ($1 eq "knownfail") {
80                                 pop(@$open_tests); #FIXME: Check that popped value == $2
81                                 $statistics->{TESTS_EXPECTED_FAIL}++;
82                                 $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
83                         } elsif ($1 eq "skip") {
84                                 $statistics->{TESTS_SKIP}++;
85                                 pop(@$open_tests); #FIXME: Check that popped value == $2
86                                 $msg_ops->end_test($open_tests, $2, $1, 0, $reason);
87                         } elsif ($1 eq "error") {
88                                 $statistics->{TESTS_ERROR}++;
89                                 pop(@$open_tests); #FIXME: Check that popped value == $2
90                                 $msg_ops->end_test($open_tests, $2, $1, 1, $reason);
91                                 $unexpected_err++;
92                         }
93                 } else {
94                         $msg_ops->output_msg($_);
95                 }
96         }
97
98         while ($#$open_tests > $orig_open_len) {
99                 $msg_ops->end_test($open_tests, pop(@$open_tests), "error", 1,
100                                    "was started but never finished!");
101                 $statistics->{TESTS_ERROR}++;
102                 $unexpected_err++;
103         }
104
105         return 1 if $unexpected_err > 0;
106         return 1 if $unexpected_fail > 0;
107         return 1 if $unexpected_ok > 0 and $expected_fail > 0;
108         return 0 if $unexpected_ok > 0 and $expected_fail == 0;
109         return 0 if $expected_fail > 0;
110         return 1;
111 }
112
113 1;