Support reporting current time in selftest output.
[ira/wip.git] / selftest / output / plain.pm
1 #!/usr/bin/perl
2 # Plain text output for selftest
3 # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 package output::plain;
18 use Exporter;
19 @ISA = qw(Exporter);
20
21 use FindBin qw($RealBin);
22 use lib "$RealBin/..";
23
24 use strict;
25
26 sub new($$$$$$$) {
27         my ($class, $summaryfile, $verbose, $immediate, $statistics, $totaltests) = @_;
28         my $self = { 
29                 verbose => $verbose, 
30                 immediate => $immediate, 
31                 statistics => $statistics,
32                 start_time => undef,
33                 test_output => {},
34                 suitesfailed => [],
35                 suites_ok => 0,
36                 skips => {},
37                 summaryfile => $summaryfile,
38                 index => 0,
39                 totalsuites => $totaltests,
40         };
41         bless($self, $class);
42 }
43
44 sub report_time($$)
45 {
46         my ($self, $time) = @_;
47         unless ($self->{start_time}) {
48                 $self->{start_time} = $time;
49         }
50         $self->{last_time} = $time;
51 }
52
53 sub output_msg($$);
54
55 sub start_testsuite($$)
56 {
57         my ($self, $name) = @_;
58
59         $self->{index}++;
60         $self->{NAME} = $name;
61         $self->{START_TIME} = $self->{last_time};
62
63         my $duration = $self->{START_TIME} - $self->{start_time};
64
65         $self->{test_output}->{$name} = "" unless($self->{verbose});
66
67         my $out = "";
68         $out .= "[$self->{index}/$self->{totalsuites} in ".$duration."s";
69         $out .= sprintf(", %d errors", ($#{$self->{suitesfailed}}+1)) if ($#{$self->{suitesfailed}} > -1);
70         $out .= "] $name"; 
71         if ($self->{immediate}) {
72                 print "$out\n";
73         } else {
74                 print "$out: ";
75         }
76 }
77
78 sub output_msg($$)
79 {
80         my ($self, $output) = @_;
81
82         if ($self->{verbose}) {
83                 require FileHandle;
84                 print $output;
85                 STDOUT->flush();
86         } else {
87                 $self->{test_output}->{$self->{NAME}} .= $output;
88         }
89 }
90
91 sub control_msg($$)
92 {
93         my ($self, $output) = @_;
94
95         $self->output_msg($output);
96 }
97
98 sub end_testsuite($$$$$)
99 {
100         my ($self, $name, $result, $unexpected, $reason) = @_;
101         my $out = "";
102
103         if ($unexpected) {
104                 if ($result eq "success" and not defined($reason)) {
105                         $reason = "Expected negative exit code, got positive exit code";
106                 } 
107                 $self->output_msg("ERROR: $reason\n");
108                 push (@{$self->{suitesfailed}}, $name);
109         } else {
110                 $self->{suites_ok}++;
111         }
112
113         if ($unexpected and $self->{immediate} and not $self->{verbose}) {
114                 $out .= $self->{test_output}->{$name};
115         }
116
117         if (not $self->{immediate}) {
118                 if (not $unexpected) {
119                         $out .= " ok\n";
120                 } else {
121                         $out .= " " . uc($result) . "\n";
122                 }
123         }
124
125         print $out;
126 }
127
128 sub start_test($$$)
129 {
130         my ($self, $parents, $testname) = @_;
131
132         if ($#$parents == -1) {
133                 $self->start_testsuite($testname);
134         }
135 }
136
137 sub end_test($$$$$)
138 {
139         my ($self, $parents, $testname, $result, $unexpected, $reason) = @_;
140         
141         if ($#$parents == -1) {
142                 $self->end_testsuite($testname, $result, $unexpected, $reason);
143                 return;
144         }
145
146         my $append = "";
147
148         unless ($unexpected) {
149                 $self->{test_output}->{$self->{NAME}} = "";
150                 if (not $self->{immediate}) {
151                         if ($result eq "failure") { print "f"; }
152                         elsif ($result eq "skip") { print "s"; }
153                         elsif ($result eq "success") { print "."; }
154                         else { print "?($result)"; }
155                 }
156                 return;
157         }
158
159         my $fullname = join(".", @$parents).".$testname";
160
161         $append = "UNEXPECTED($result): $testname ($fullname)\n";
162
163         $self->{test_output}->{$self->{NAME}} .= $append;
164
165         if ($self->{immediate} and not $self->{verbose}) {
166                 print $self->{test_output}->{$self->{NAME}};
167                 $self->{test_output}->{$self->{NAME}} = "";
168         }
169
170         if (not $self->{immediate}) {
171                 if ($result eq "error") { print "E"; } 
172                 elsif ($result eq "failure") { print "F"; }
173                 elsif ($result eq "success") { print "S"; }
174                 else { print "?"; }
175         }
176 }
177
178 sub summary($)
179 {
180         my ($self) = @_;
181
182         open(SUMMARY, ">$self->{summaryfile}");
183
184         if ($#{$self->{suitesfailed}} > -1) {
185                 print SUMMARY "= Failed tests =\n";
186
187                 foreach (@{$self->{suitesfailed}}) {
188                         print SUMMARY "== $_ ==\n";
189                         print SUMMARY $self->{test_output}->{$_}."\n\n";
190                 }
191
192                 print SUMMARY "\n";
193         }
194
195         if (not $self->{immediate} and not $self->{verbose}) {
196                 foreach (@{$self->{suitesfailed}}) {
197                         print "===============================================================================\n";
198                         print "FAIL: $_\n";
199                         print $self->{test_output}->{$_};
200                         print "\n";
201                 }
202         }
203
204         print SUMMARY "= Skipped tests =\n";
205         foreach my $reason (keys %{$self->{skips}}) {
206                 print SUMMARY "$reason\n";
207                 foreach my $name (@{$self->{skips}->{$reason}}) {
208                         print SUMMARY "\t$name\n";
209                 }
210                 print SUMMARY "\n";
211         }
212         close(SUMMARY);
213
214         print "\nA summary with detailed information can be found in:\n  $self->{summaryfile}\n";
215
216         if ($#{$self->{suitesfailed}} == -1) {
217                 my $ok = $self->{statistics}->{TESTS_EXPECTED_OK} + 
218                                  $self->{statistics}->{TESTS_EXPECTED_FAIL};
219                 print "\nALL OK ($ok tests in $self->{suites_ok} testsuites)\n";
220         } else {
221                 print "\nFAILED ($self->{statistics}->{TESTS_UNEXPECTED_FAIL} failures and $self->{statistics}->{TESTS_ERROR} errors in ". ($#{$self->{suitesfailed}}+1) ." testsuites)\n";
222         }
223
224 }
225
226 sub skip_testsuite($$)
227 {
228         my ($self, $name, $reason) = @_;
229
230         push (@{$self->{skips}->{$reason}}, $name);
231
232         $self->{totalsuites}--;
233 }
234
235 1;