c428e850f766b81b04b854ea47db2ecbc6000dd5
[p5sagit/p5-mst-13.2.git] / lib / Test / Harness / t / test-harness.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't';
6         @INC = ('../lib', 'lib');
7     }
8     else {
9         unshift @INC, 't/lib';
10     }
11 }
12
13 my $SAMPLE_TESTS = $ENV{PERL_CORE} ? "lib/sample-tests" : "t/sample-tests";
14
15 use strict;
16
17 # For shutting up Test::Harness.
18 # Has to work on 5.004 which doesn't have Tie::StdHandle.
19 package My::Dev::Null;
20
21 sub WRITE  {}
22 sub PRINT  {}
23 sub PRINTF {}
24 sub TIEHANDLE {
25     my $class = shift;
26     my $fh    = do { local *HANDLE;  \*HANDLE };
27     return bless $fh, $class;
28 }
29 sub READ {}
30 sub READLINE {}
31 sub GETC {}
32
33
34 package main;
35
36 use Test::More;
37
38 my $IsVMS = $^O eq 'VMS';
39
40 # VMS uses native, not POSIX, exit codes.
41 my $die_estat = $IsVMS ? 44 : 1;
42
43 my %samples = (
44             simple            => {
45                                   total => {
46                                             bonus      => 0,
47                                             max        => 5,
48                                             'ok'       => 5,
49                                             files      => 1,
50                                             bad        => 0,
51                                             good       => 1,
52                                             tests      => 1,
53                                             sub_skipped=> 0,
54                                             'todo'     => 0,
55                                             skipped    => 0,
56                                            },
57                                   failed => { },
58                                   all_ok => 1,
59                                  },
60             simple_fail      => {
61                                  total => {
62                                            bonus       => 0,
63                                            max         => 5,
64                                            'ok'        => 3,
65                                            files       => 1,
66                                            bad         => 1,
67                                            good        => 0,
68                                            tests       => 1,
69                                            sub_skipped => 0,
70                                            'todo'      => 0,
71                                            skipped     => 0,
72                                           },
73                                  failed => {
74                                             canon      => '2 5',
75                                            },
76                                  all_ok => 0,
77                                 },
78             descriptive       => {
79                                   total => {
80                                             bonus      => 0,
81                                             max        => 5,
82                                             'ok'       => 5,
83                                             files      => 1,
84                                             bad        => 0,
85                                             good       => 1,
86                                             tests      => 1,
87                                             sub_skipped=> 0,
88                                             'todo'     => 0,
89                                             skipped    => 0,
90                                            },
91                                   failed => { },
92                                   all_ok => 1,
93                                  },
94             no_nums           => {
95                                   total => {
96                                             bonus      => 0,
97                                             max        => 5,
98                                             'ok'       => 4,
99                                             files      => 1,
100                                             bad        => 1,
101                                             good       => 0,
102                                             tests      => 1,
103                                             sub_skipped=> 0,
104                                             'todo'     => 0,
105                                             skipped    => 0,
106                                            },
107                                   failed => {
108                                              canon     => '3',
109                                             },
110                                   all_ok => 0,
111                                  },
112             'todo'            => {
113                                   total => {
114                                             bonus      => 1,
115                                             max        => 5,
116                                             'ok'       => 5,
117                                             files      => 1,
118                                             bad        => 0,
119                                             good       => 1,
120                                             tests      => 1,
121                                             sub_skipped=> 0,
122                                             'todo'     => 2,
123                                             skipped    => 0,
124                                            },
125                                   failed => { },
126                                   all_ok => 1,
127                                  },
128             todo_inline       => {
129                                   total => {
130                                             bonus       => 1,
131                                             max         => 3,
132                                             'ok'        => 3,
133                                             files       => 1,
134                                             bad         => 0,
135                                             good        => 1,
136                                             tests       => 1,
137                                             sub_skipped => 0,
138                                             'todo'      => 2,
139                                             skipped     => 0,
140                                            },
141                                   failed => { },
142                                   all_ok => 1,
143                                  },
144             'skip'            => {
145                                   total => {
146                                             bonus      => 0,
147                                             max        => 5,
148                                             'ok'       => 5,
149                                             files      => 1,
150                                             bad        => 0,
151                                             good       => 1,
152                                             tests      => 1,
153                                             sub_skipped=> 1,
154                                             'todo'     => 0,
155                                             skipped    => 0,
156                                            },
157                                   failed => { },
158                                   all_ok => 1,
159                                  },
160             'skip_nomsg'      => {
161                                   total => {
162                                             bonus      => 0,
163                                             max        => 1,
164                                             'ok'       => 1,
165                                             files      => 1,
166                                             bad        => 0,
167                                             good       => 1,
168                                             tests      => 1,
169                                             sub_skipped=> 1,
170                                             'todo'     => 0,
171                                             skipped    => 0,
172                                            },
173                                   failed => { },
174                                   all_ok => 1,
175                                  },
176             bailout           => 0,
177             combined          => {
178                                   total => {
179                                             bonus      => 1,
180                                             max        => 10,
181                                             'ok'       => 8,
182                                             files      => 1,
183                                             bad        => 1,
184                                             good       => 0,
185                                             tests      => 1,
186                                             sub_skipped=> 1,
187                                             'todo'     => 2,
188                                             skipped    => 0
189                                            },
190                                   failed => {
191                                              canon     => '3 9',
192                                             },
193                                   all_ok => 0,
194                                  },
195             duplicates        => {
196                                   total => {
197                                             bonus      => 0,
198                                             max        => 10,
199                                             'ok'       => 11,
200                                             files      => 1,
201                                             bad        => 1,
202                                             good       => 0,
203                                             tests      => 1,
204                                             sub_skipped=> 0,
205                                             'todo'     => 0,
206                                             skipped    => 0,
207                                            },
208                                   failed => {
209                                              canon     => '??',
210                                             },
211                                   all_ok => 0,
212                                  },
213             head_end          => {
214                                   total => {
215                                             bonus      => 0,
216                                             max        => 4,
217                                             'ok'       => 4,
218                                             files      => 1,
219                                             bad        => 0,
220                                             good       => 1,
221                                             tests      => 1,
222                                             sub_skipped=> 0,
223                                             'todo'     => 0,
224                                             skipped    => 0,
225                                            },
226                                   failed => { },
227                                   all_ok => 1,
228                                  },
229             head_fail         => {
230                                   total => {
231                                             bonus      => 0,
232                                             max        => 4,
233                                             'ok'       => 3,
234                                             files      => 1,
235                                             bad        => 1,
236                                             good       => 0,
237                                             tests      => 1,
238                                             sub_skipped=> 0,
239                                             'todo'     => 0,
240                                             skipped    => 0,
241                                            },
242                                   failed => {
243                                              canon      => '2',
244                                             },
245                                   all_ok => 0,
246                                  },
247             skipall          => {
248                                   total => {
249                                             bonus      => 0,
250                                             max        => 0,
251                                             'ok'       => 0,
252                                             files      => 1,
253                                             bad        => 0,
254                                             good       => 1,
255                                             tests      => 1,
256                                             sub_skipped=> 0,
257                                             'todo'     => 0,
258                                             skipped    => 1,
259                                            },
260                                   failed => { },
261                                   all_ok => 1,
262                                  },
263             skipall_nomsg   => {
264                                   total => {
265                                             bonus      => 0,
266                                             max        => 0,
267                                             'ok'       => 0,
268                                             files      => 1,
269                                             bad        => 0,
270                                             good       => 1,
271                                             tests      => 1,
272                                             sub_skipped=> 0,
273                                             'todo'     => 0,
274                                             skipped    => 1,
275                                            },
276                                   failed => { },
277                                   all_ok => 1,
278                                  },
279             with_comments     => {
280                                   total => {
281                                             bonus      => 2,
282                                             max        => 5,
283                                             'ok'       => 5,
284                                             files      => 1,
285                                             bad        => 0,
286                                             good       => 1,
287                                             tests      => 1,
288                                             sub_skipped=> 0,
289                                             'todo'     => 4,
290                                             skipped    => 0,
291                                            },
292                                   failed => { },
293                                   all_ok => 1,
294                                  },
295             taint             => {
296                                   total => {
297                                             bonus      => 0,
298                                             max        => 1,
299                                             'ok'       => 1,
300                                             files      => 1,
301                                             bad        => 0,
302                                             good       => 1,
303                                             tests      => 1,
304                                             sub_skipped=> 0,
305                                             'todo'     => 0,
306                                             skipped    => 0,
307                                            },
308                                   failed => { },
309                                   all_ok => 1,
310                                  },
311
312             'die'             => {
313                                   total => {
314                                             bonus      => 0,
315                                             max        => 0,
316                                             'ok'       => 0,
317                                             files      => 1,
318                                             bad        => 1,
319                                             good       => 0,
320                                             tests      => 1,
321                                             sub_skipped=> 0,
322                                             'todo'     => 0,
323                                             skipped    => 0,
324                                            },
325                                   failed => {
326                                              estat      => $die_estat,
327                                              max        => '??',
328                                              failed     => '??',
329                                              canon      => '??',
330                                             },
331                                   all_ok => 0,
332                                  },
333
334             die_head_end      => {
335                                   total => {
336                                             bonus      => 0,
337                                             max        => 0,
338                                             'ok'       => 4,
339                                             files      => 1,
340                                             bad        => 1,
341                                             good       => 0,
342                                             tests      => 1,
343                                             sub_skipped=> 0,
344                                             'todo'     => 0,
345                                             skipped    => 0,
346                                            },
347                                   failed => {
348                                              estat      => $die_estat,
349                                              max        => '??',
350                                              failed     => '??',
351                                              canon      => '??',
352                                             },
353                                   all_ok => 0,
354                                  },
355
356             die_last_minute   => {
357                                   total => {
358                                             bonus      => 0,
359                                             max        => 4,
360                                             'ok'       => 4,
361                                             files      => 1,
362                                             bad        => 1,
363                                             good       => 0,
364                                             tests      => 1,
365                                             sub_skipped=> 0,
366                                             'todo'     => 0,
367                                             skipped    => 0,
368                                            },
369                                   failed => {
370                                              estat      => $die_estat,
371                                              max        => 4,
372                                              failed     => 0,
373                                              canon      => '??',
374                                             },
375                                   all_ok => 0,
376                                  },
377             bignum            => {
378                                   total => {
379                                             bonus      => 0,
380                                             max        => 2,
381                                             'ok'       => 4,
382                                             files      => 1,
383                                             bad        => 1,
384                                             good       => 0,
385                                             tests      => 1,
386                                             sub_skipped=> 0,
387                                             'todo'     => 0,
388                                             skipped    => 0,
389                                            },
390                                   failed => {
391                                              canon      => '??',
392                                             },
393                                   all_ok => 0,
394                                  },
395             'shbang_misparse' => {
396                                   total => {
397                                             bonus      => 0,
398                                             max        => 2,
399                                             'ok'       => 2,
400                                             files      => 1,
401                                             bad        => 0,
402                                             good       => 1,
403                                             tests      => 1,
404                                             sub_skipped=> 0,
405                                             'todo'     => 0,
406                                             skipped    => 0,
407                                            },
408                                   failed => { },
409                                   all_ok => 1,
410                                  },
411            );
412
413 plan tests => (keys(%samples) * 7) + 1;
414
415 use Test::Harness;
416 use_ok('Test::Harness');
417
418
419 tie *NULL, 'My::Dev::Null' or die $!;
420
421 while (my($test, $expect) = each %samples) {
422     # _run_all_tests() runs the tests but skips the formatting.
423     my($totals, $failed);
424     my $warning;
425     eval {
426         select NULL;    # _run_all_tests() isn't as quiet as it should be.
427         local $SIG{__WARN__} = sub { $warning .= join '', @_; };
428         ($totals, $failed) = 
429           Test::Harness::_run_all_tests("$SAMPLE_TESTS/$test");
430     };
431     select STDOUT;
432
433     SKIP: {
434         skip "special tests for bailout", 1 unless $test eq 'bailout';
435         like( $@, '/Further testing stopped: GERONI/i' );
436     }
437
438     SKIP: {
439         skip "don't apply to a bailout", 5 if $test eq 'bailout';
440         is( $@, '' );
441         is( Test::Harness::_all_ok($totals), $expect->{all_ok},
442                                                   "$test - all ok" );
443         ok( defined $expect->{total},             "$test - has total" );
444         is_deeply( {map { $_=>$totals->{$_} } keys %{$expect->{total}}},
445                    $expect->{total},
446                                                   "$test - totals" );
447         is_deeply( {map { $_=>$failed->{"$SAMPLE_TESTS/$test"}{$_} }
448                     keys %{$expect->{failed}}},
449                    $expect->{failed},
450                                                   "$test - failed" );
451     }
452
453     SKIP: {
454         skip "special tests for bignum", 1 unless $test eq 'bignum';
455         is( $warning, <<WARN );
456 Enourmous test number seen [test 100001]
457 Can't detailize, too big.
458 Enourmous test number seen [test 136211425]
459 Can't detailize, too big.
460 WARN
461
462     }
463
464 }