selftest: Remove unnecessary uses.
[ira/wip.git] / selftest / output / html.pm
1 #!/usr/bin/perl
2 # HTML 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::html;
18 use Exporter;
19 @ISA = qw(Exporter);
20
21 use strict;
22 use warnings;
23
24 use FindBin qw($RealBin);
25 use lib "$RealBin/..";
26
27 sub new($$$) {
28         my ($class, $dirname, $statistics) = @_;
29         my $self = { 
30                 dirname => $dirname,
31                 active_test => undef,
32                 local_statistics => {},
33                 statistics => $statistics,
34                 msg => "",
35                 error_summary => { 
36                         skip => [],
37                         expected_success => [],
38                         unexpected_success => [],
39                         expected_failure => [],
40                         unexpected_failure => [],
41                         skip_testsuites => [],
42                         error => []
43                 }
44         };
45
46         link("$RealBin/output/testresults.css", "$dirname/testresults.css");
47
48         open(INDEX, ">$dirname/index.html");
49
50         bless($self, $class);
51
52         $self->print_html_header("Samba Testsuite Run", *INDEX);
53
54         print INDEX "  <center>";
55         print INDEX "  <table>\n";
56         print INDEX "  <tr>\n";
57         print INDEX "    <td class=\"tableHead\">Test</td>\n";
58         print INDEX "    <td class=\"tableHead\">Result</td>\n";
59         print INDEX "  </tr>\n";
60
61         return $self;
62 }
63
64 sub print_html_header($$$)
65 {
66         my ($self, $title, $fh) = @_;
67
68         print $fh "<html lang=\"en\">\n";
69         print $fh "<head>\n";
70         print $fh "  <title>$title</title>\n";
71         print $fh "  <link rel=\"stylesheet\" type=\"text/css\" href=\"testresults.css\"/>\n";
72         print $fh "</head>\n";
73         print $fh "<body>\n";
74         print $fh "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n";
75         print $fh "  <tr><td class=\"title\">$title</td></tr>\n";
76         print $fh "  <tr><td>\n";
77 }
78
79 sub print_html_footer($$)
80 {
81         my ($self, $fh) = @_;
82
83         print $fh "</td></tr>\n";
84         print $fh "</table>\n";
85         print $fh "</body>\n";
86         print $fh "</html>\n";
87 }
88
89 sub output_msg($$);
90
91 sub start_testsuite($$)
92 {
93         my ($self, $name) = @_;
94
95         $self->{START_TIME} = $self->{last_time};
96
97         $self->{local_statistics} = {
98                 success => 0,
99                 skip => 0,
100                 error => 0,
101                 failure => 0
102         };
103
104         $self->{NAME} = $name;
105         $self->{HTMLFILE} = "$name.html";
106         $self->{HTMLFILE} =~ s/[:\t\n \/]/_/g;
107
108         open(TEST, ">$self->{dirname}/$self->{HTMLFILE}") or die("Unable to open $self->{HTMLFILE} for writing");
109
110         $self->print_html_header("Test Results for $name", *TEST);
111
112         print TEST "<h2>Tests</h2>\n";
113
114         print TEST "  <table>\n";
115 }
116
117 sub control_msg($$)
118 {
119         my ($self, $output) = @_;
120
121         $self->{msg} .=  "<span class=\"control\">$output<br/></span>\n";
122 }
123
124 sub output_msg($$)
125 {
126         my ($self, $output) = @_;
127
128         unless (defined($self->{active_test})) {
129                 print TEST "$output<br/>";
130         } else {
131                 $self->{msg} .= "$output<br/>";
132         }
133 }
134
135 sub end_testsuite($$$$)
136 {
137         my ($self, $name, $result, $unexpected, $reason) = @_;
138
139         print TEST "</table>\n";
140
141         print TEST "<div class=\"duration\">Duration: " . ($self->{last_time} - $self->{START_TIME}) . "s</div>\n";
142
143         $self->print_html_footer(*TEST);
144
145         close(TEST);
146
147         print INDEX "<tr>\n";
148         print INDEX "  <td class=\"testSuite\"><a href=\"$self->{HTMLFILE}\">$name</a></td>\n";
149         my $st = $self->{local_statistics};
150
151         if (not $unexpected) {
152                 if ($result eq "failure") {
153                         print INDEX "  <td class=\"resultExpectedFailure\">";
154                 } else {
155                         print INDEX "  <td class=\"resultOk\">";
156                 }
157         } else {
158                 print INDEX "  <td class=\"resultFailure\">";
159         }
160
161         my $l = 0;
162         if ($st->{success} > 0) {
163                 print INDEX "$st->{success} ok";
164                 $l++;
165         }
166         if ($st->{skip} > 0) {
167                 print INDEX ", " if ($l);
168                 print INDEX "$st->{skip} skipped";
169                 $l++;
170         }
171         if ($st->{failure} > 0) {
172                 print INDEX ", " if ($l);
173                 print INDEX "$st->{failure} failures";
174                 $l++;
175         }
176         if ($st->{error} > 0) {
177                 print INDEX ", " if ($l);
178                 print INDEX "$st->{error} errors";
179                 $l++;
180         }
181
182         if ($l == 0) {
183                 if (not $unexpected) {
184                         print INDEX "OK";
185                 } else {
186                         print INDEX "FAIL";
187                 }
188         }
189
190         print INDEX "</td>";
191                 
192         print INDEX "</tr>\n";
193 }
194
195 sub report_time($$)
196 {
197         my ($self, $time) = @_;
198         $self->{last_time} = $time;
199 }
200
201 sub start_test($$)
202 {
203         my ($self, $testname) = @_;
204
205         $self->{active_test} = $testname;
206         $self->{msg} = "";
207 }
208
209 sub end_test($$$$)
210 {
211         my ($self, $testname, $result, $unexpected, $reason) = @_;
212
213         print TEST "<tr>";
214
215         $self->{local_statistics}->{$result}++;
216
217         my $track_class;
218
219         if ($result eq "skip") {
220                 print TEST "<td class=\"outputSkipped\">\n";
221                 $track_class = "skip";
222         } elsif ($unexpected) {
223                 print TEST "<td class=\"outputFailure\">\n";
224                 if ($result eq "error") {
225                         $track_class = "error";
226                 } else {
227                         $track_class = "unexpected_$result";
228                 }
229         } else {
230                 if ($result eq "failure") {
231                         print TEST "<td class=\"outputExpectedFailure\">\n";
232                 } else {
233                         print TEST "<td class=\"outputOk\">\n";
234                 }
235                 $track_class = "expected_$result";
236         }
237
238         push(@{$self->{error_summary}->{$track_class}}, ,
239                  [$self->{HTMLFILE}, $testname, $self->{NAME}, 
240                   $reason]);
241
242         print TEST "<a name=\"$testname\"><h3>$testname</h3></a>\n";
243
244         print TEST $self->{msg};
245
246         if (defined($reason)) {
247                 print TEST "<div class=\"reason\">$reason</div>\n";
248         }
249
250         print TEST "</td></tr>\n";
251
252         $self->{active_test} = undef;
253 }
254
255 sub summary($)
256 {
257         my ($self) = @_;
258
259         my $st = $self->{statistics};
260         print INDEX "<tr>\n";
261         print INDEX "  <td class=\"testSuiteTotal\">Total</td>\n";
262
263         if ($st->{TESTS_UNEXPECTED_OK} == 0 and 
264             $st->{TESTS_UNEXPECTED_FAIL} == 0 and
265                 $st->{TESTS_ERROR} == 0) {
266                 print INDEX "  <td class=\"resultOk\">";
267         } else {
268                 print INDEX "  <td class=\"resultFailure\">";
269         }
270         print INDEX ($st->{TESTS_EXPECTED_OK} + $st->{TESTS_UNEXPECTED_OK}) . " ok";
271         if ($st->{TESTS_UNEXPECTED_OK} > 0) {
272                 print INDEX " ($st->{TESTS_UNEXPECTED_OK} unexpected)";
273         }
274         if ($st->{TESTS_SKIP} > 0) {
275                 print INDEX ", $st->{TESTS_SKIP} skipped";
276         }
277         if (($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) > 0) {
278                 print INDEX ", " . ($st->{TESTS_UNEXPECTED_FAIL} + $st->{TESTS_EXPECTED_FAIL}) . " failures";
279                 if ($st->{TESTS_UNEXPECTED_FAIL} > 0) {
280                         print INDEX " ($st->{TESTS_EXPECTED_FAIL} expected)";
281                 }
282         }
283         if ($st->{TESTS_ERROR} > 0) {
284                 print INDEX ", $st->{TESTS_ERROR} errors";
285         }
286
287         print INDEX "</td>";
288
289         print INDEX "</tr>\n";
290
291         print INDEX "</table>\n";
292         print INDEX "<a href=\"summary.html\">Summary</a>\n";
293         print INDEX "</center>\n";
294         $self->print_html_footer(*INDEX);
295         close(INDEX);
296
297         my $summ = $self->{error_summary};
298         open(SUMMARY, ">$self->{dirname}/summary.html");
299         $self->print_html_header("Summary", *SUMMARY);
300         sub print_table($$) {
301                 my ($title, $list) = @_;
302                 return if ($#$list == -1);
303                 print SUMMARY "<h3>$title</h3>\n";
304                 print SUMMARY "<table>\n";
305                 print SUMMARY "<tr>\n";
306                 print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
307                 print SUMMARY "  <td class=\"tableHead\">Test</td>\n";
308                 print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
309                 print SUMMARY "</tr>\n";
310
311                 foreach (@$list) {
312                         print SUMMARY "<tr>\n";
313                         print SUMMARY "  <td><a href=\"" . $$_[0] . "\">$$_[2]</a></td>\n";
314                         print SUMMARY "  <td><a href=\"" . $$_[0] . "#$$_[1]\">$$_[1]</a></td>\n";
315                         if (defined($$_[3])) {
316                                 print SUMMARY "  <td>$$_[3]</td>\n";
317                         } else {
318                                 print SUMMARY "  <td></td>\n";
319                         }
320                         print SUMMARY "</tr>\n";
321                 }
322
323                 print SUMMARY "</table>";
324         }
325         print_table("Errors", $summ->{error});
326         print_table("Unexpected successes", $summ->{unexpected_success});
327         print_table("Unexpected failures", $summ->{unexpected_failure});
328         print_table("Skipped tests", $summ->{skip});
329         print_table("Expected failures", $summ->{expected_failure});
330
331         print SUMMARY "<h3>Skipped testsuites</h3>\n";
332         print SUMMARY "<table>\n";
333         print SUMMARY "<tr>\n";
334         print SUMMARY "  <td class=\"tableHead\">Testsuite</td>\n";
335         print SUMMARY "  <td class=\"tableHead\">Reason</td>\n";
336         print SUMMARY "</tr>\n";
337
338         foreach (@{$summ->{skip_testsuites}}) {
339                 print SUMMARY "<tr>\n";
340                 print SUMMARY "  <td>$$_[0]</td>\n";
341                 if (defined($$_[1])) {
342                         print SUMMARY "  <td>$$_[1]</td>\n";
343                 } else {
344                         print SUMMARY "  <td></td>\n";
345                 }
346                 print SUMMARY "</tr>\n";
347         }
348
349         print SUMMARY "</table>";
350
351         $self->print_html_footer(*SUMMARY);
352         close(SUMMARY);
353 }
354
355 sub skip_testsuite($$$$)
356 {
357         my ($self, $name, $reason) = @_;
358
359         push (@{$self->{error_summary}->{skip_testsuites}}, 
360                   [$name, $reason]);
361 }
362
363 1;