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