selftest: Remove testsuite parsing.
[npower/samba.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, $fh) = @_;
29         my $expected_fail = 0;
30         my $open_tests = [];
31
32         while(<$fh>) {
33                 if (/^test: (.+)\n/) {
34                         $msg_ops->control_msg($_);
35                         $msg_ops->start_test($1);
36                         push (@$open_tests, $1);
37                 } elsif (/^(success|successful|failure|fail|skip|knownfail|error|xfail): (.*?)( \[)?([ \t]*)( multipart)?\n/) {
38                         $msg_ops->control_msg($_);
39                         my $result = $1;
40                         my $testname = $2;
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                                         $msg_ops->end_test($testname, "error",
53                                                                "reason ($result) interrupted\n");
54                                         return 1;
55                                 }
56                         }
57                         if ($result eq "success" or $result eq "successful") {
58                                 pop(@$open_tests); #FIXME: Check that popped value == $testname 
59                                 $msg_ops->end_test($testname, "success", $reason);
60                         } elsif ($result eq "xfail" or $result eq "knownfail") {
61                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
62                                 $msg_ops->end_test($testname, "xfail", $reason);
63                                 $expected_fail++;
64                         } elsif ($result eq "failure" or $result eq "fail") {
65                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
66                                 $msg_ops->end_test($testname, "failure", $reason);
67                         } elsif ($result eq "skip") {
68                                 # Allow tests to be skipped without prior announcement of test
69                                 my $last = pop(@$open_tests);
70                                 if (defined($last) and $last ne $testname) {
71                                         push (@$open_tests, $testname);
72                                 }
73                                 $msg_ops->end_test($testname, "skip", $reason);
74                         } elsif ($result eq "error") {
75                                 pop(@$open_tests); #FIXME: Check that popped value == $testname
76                                 $msg_ops->end_test($testname, "error", $reason);
77                         }
78                 } else {
79                         $msg_ops->output_msg($_);
80                 }
81         }
82
83         while ($#$open_tests+1 > 0) {
84                 $msg_ops->end_test(pop(@$open_tests), "error",
85                                    "was started but never finished!\n");
86         }
87 }
88
89 sub start_test($)
90 {
91         my ($testname) = @_;
92         print "test: $testname\n";
93 }
94
95 sub end_test($$;$)
96 {
97         my $name = shift;
98         my $result = shift;
99         my $reason = shift;
100         if ($reason) {
101                 print "$result: $name [\n";
102                 print $reason;
103                 if (substr($reason, -1, 1) ne "\n") { print "\n"; }
104                 print "]\n";
105         } else {
106                 print "$result: $name\n";
107         }
108 }
109
110 sub skip_test($;$)
111 {
112         my $name = shift;
113         my $reason = shift;
114         end_test($name, "skip", $reason);
115 }
116
117 sub fail_test($;$)
118 {
119         my $name = shift;
120         my $reason = shift;
121         end_test($name, "fail", $reason);
122 }
123
124 sub success_test($;$)
125 {
126         my $name = shift;
127         my $reason = shift;
128         end_test($name, "success", $reason);
129 }
130
131 sub xfail_test($;$)
132 {
133         my $name = shift;
134         my $reason = shift;
135         end_test($name, "xfail", $reason);
136 }
137
138 sub report_time($)
139 {
140         my ($time) = @_;
141         my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime($time);
142         $sec = ($time - int($time) + $sec);
143         my $msg = sprintf("%f", $sec);
144         if (substr($msg, 1, 1) eq ".") {
145                 $msg = "0" . $msg;
146         }
147         printf "time: %04d-%02d-%02d %02d:%02d:%s\n", $year+1900, $mon+1, $mday, $hour, $min, $msg;
148 }
149
150 sub progress_pop()
151 {
152         print "progress: pop\n";
153 }
154
155 sub progress_push()
156 {
157         print "progress: push\n";
158 }
159
160 sub progress($;$)
161 {
162         my ($count, $whence) = @_;
163
164         unless(defined($whence)) {
165                 $whence = "";
166         }
167
168         print "progress: $whence$count\n";
169 }
170
171 # The following are Samba extensions:
172
173 sub start_testsuite($)
174 {
175         my ($name) = @_;
176         print "testsuite: $name\n";
177 }
178
179 sub skip_testsuite($;$)
180 {
181         my ($name, $reason) = @_;
182         if ($reason) {
183                 print "skip-testsuite: $name [\n$reason\n]\n";
184         } else {
185                 print "skip-testsuite: $name\n";
186         }
187 }
188
189 sub end_testsuite($$;$)
190 {
191         my $name = shift;
192         my $result = shift;
193         my $reason = shift;
194         if ($reason) {
195                 print "testsuite-$result: $name [\n";
196                 print "$reason\n";
197                 print "]\n";
198         } else {
199                 print "testsuite-$result: $name\n";
200         }
201 }
202
203 1;