selftest: Add copyright headers
[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 => time(),
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 output_msg($$);
45
46 sub start_testsuite($$)
47 {
48         my ($self, $name) = @_;
49
50         $self->{index}++;
51         $self->{NAME} = $name;
52         $self->{START_TIME} = time();
53
54         my $duration = $self->{START_TIME} - $self->{start_time};
55
56         $self->{test_output}->{$name} = "" unless($self->{verbose});
57
58         my $out = "";
59         $out .= "[$self->{index}/$self->{totalsuites} in ".$duration."s";
60         $out .= sprintf(", %d errors", ($#{$self->{suitesfailed}}+1)) if ($#{$self->{suitesfailed}} > -1);
61         $out .= "] $name"; 
62         if ($self->{immediate}) {
63                 print "$out\n";
64         } else {
65                 print "$out: ";
66         }
67 }
68
69 sub output_msg($$)
70 {
71         my ($self, $output) = @_;
72
73         if ($self->{verbose}) {
74                 require FileHandle;
75                 print $output;
76                 STDOUT->flush();
77         } else {
78                 $self->{test_output}->{$self->{NAME}} .= $output;
79         }
80 }
81
82 sub control_msg($$)
83 {
84         my ($self, $output) = @_;
85
86         $self->output_msg($output);
87 }
88
89 sub end_testsuite($$$$$)
90 {
91         my ($self, $name, $result, $unexpected, $reason) = @_;
92         my $out = "";
93
94         if ($unexpected) {
95                 if ($result eq "success" and not defined($reason)) {
96                         $reason = "Expected negative exit code, got positive exit code";
97                 } 
98                 $self->output_msg("ERROR: $reason\n");
99                 push (@{$self->{suitesfailed}}, $name);
100         } else {
101                 $self->{suites_ok}++;
102         }
103
104         if ($unexpected and $self->{immediate} and not $self->{verbose}) {
105                 $out .= $self->{test_output}->{$name};
106         }
107
108         if (not $self->{immediate}) {
109                 if (not $unexpected) {
110                         $out .= " ok\n";
111                 } else {
112                         $out .= " " . uc($result) . "\n";
113                 }
114         }
115
116         print $out;
117 }
118
119 sub start_test($$$)
120 {
121         my ($self, $parents, $testname) = @_;
122
123         if ($#$parents == -1) {
124                 $self->start_testsuite($testname);
125         }
126 }
127
128 sub end_test($$$$$)
129 {
130         my ($self, $parents, $testname, $result, $unexpected, $reason) = @_;
131         
132         if ($#$parents == -1) {
133                 $self->end_testsuite($testname, $result, $unexpected, $reason);
134                 return;
135         }
136
137         my $append = "";
138
139         unless ($unexpected) {
140                 $self->{test_output}->{$self->{NAME}} = "";
141                 if (not $self->{immediate}) {
142                         if ($result eq "failure") { print "f"; }
143                         elsif ($result eq "skip") { print "s"; }
144                         elsif ($result eq "success") { print "."; }
145                         else { print "?($result)"; }
146                 }
147                 return;
148         }
149
150         my $fullname = join(".", @$parents).".$testname";
151
152         $append = "UNEXPECTED($result): $testname ($fullname)\n";
153
154         $self->{test_output}->{$self->{NAME}} .= $append;
155
156         if ($self->{immediate} and not $self->{verbose}) {
157                 print $self->{test_output}->{$self->{NAME}};
158                 $self->{test_output}->{$self->{NAME}} = "";
159         }
160
161         if (not $self->{immediate}) {
162                 if ($result eq "error") { print "E"; } 
163                 elsif ($result eq "failure") { print "F"; }
164                 elsif ($result eq "success") { print "S"; }
165                 else { print "?"; }
166         }
167 }
168
169 sub summary($)
170 {
171         my ($self) = @_;
172
173         open(SUMMARY, ">$self->{summaryfile}");
174
175         if ($#{$self->{suitesfailed}} > -1) {
176                 print SUMMARY "= Failed tests =\n";
177
178                 foreach (@{$self->{suitesfailed}}) {
179                         print SUMMARY "== $_ ==\n";
180                         print SUMMARY $self->{test_output}->{$_}."\n\n";
181                 }
182
183                 print SUMMARY "\n";
184         }
185
186         if (not $self->{immediate} and not $self->{verbose}) {
187                 foreach (@{$self->{suitesfailed}}) {
188                         print "===============================================================================\n";
189                         print "FAIL: $_\n";
190                         print $self->{test_output}->{$_};
191                         print "\n";
192                 }
193         }
194
195         print SUMMARY "= Skipped tests =\n";
196         foreach my $reason (keys %{$self->{skips}}) {
197                 print SUMMARY "$reason\n";
198                 foreach my $name (@{$self->{skips}->{$reason}}) {
199                         print SUMMARY "\t$name\n";
200                 }
201                 print SUMMARY "\n";
202         }
203         close(SUMMARY);
204
205         print "\nA summary with detailed information can be found in:\n  $self->{summaryfile}\n";
206
207         if ($#{$self->{suitesfailed}} == -1) {
208                 my $ok = $self->{statistics}->{TESTS_EXPECTED_OK} + 
209                                  $self->{statistics}->{TESTS_EXPECTED_FAIL};
210                 print "\nALL OK ($ok tests in $self->{suites_ok} testsuites)\n";
211         } else {
212                 print "\nFAILED ($self->{statistics}->{TESTS_UNEXPECTED_FAIL} failures and $self->{statistics}->{TESTS_ERROR} errors in ". ($#{$self->{suitesfailed}}+1) ." testsuites)\n";
213         }
214
215 }
216
217 sub skip_testsuite($$)
218 {
219         my ($self, $name, $reason) = @_;
220
221         push (@{$self->{skips}->{$reason}}, $name);
222
223         $self->{totalsuites}--;
224 }
225
226 1;