This is patch.2b1g to perl5.002beta1.
[p5sagit/p5-mst-13.2.git] / lib / Test / Harness.pm
CommitLineData
a0d0e21e 1package Test::Harness;
2
3use Exporter;
4use Benchmark;
4633a7c4 5use Config;
6
7$Is_OS2 = $Config{'osname'} =~ m|^os/?2$|i ;
8
9$ENV{EMXSHELL} = 'sh' if $Is_OS2; # to run commands
10$path_s = $Is_OS2 ? ';' : ':' ;
11
a0d0e21e 12@ISA=(Exporter);
cb1a09d0 13@EXPORT= qw(&runtests);
a0d0e21e 14@EXPORT_OK= qw($verbose $switches);
15
16$verbose = 0;
17$switches = "-w";
18
19sub runtests {
20 my(@tests) = @_;
21 local($|) = 1;
22 my($test,$te,$ok,$next,$max,$totmax, $files,$pct);
23 my $bad = 0;
24 my $good = 0;
25 my $total = @tests;
4633a7c4 26 local($ENV{'PERL5LIB'}) = join($path_s, @INC); # pass -I flags to children
a0d0e21e 27
28 my $t_start = new Benchmark;
29 while ($test = shift(@tests)) {
30 $te = $test;
31 chop($te);
32 print "$te" . '.' x (20 - length($te));
33 my $fh = "RESULTS";
34 open($fh,"$^X $switches $test|") || (print "can't run. $!\n");
35 $ok = 0;
36 $next = 0;
37 while (<$fh>) {
38 if( $verbose ){
39 print $_;
40 }
41 unless (/^#/) {
42 if (/^1\.\.([0-9]+)/) {
43 $max = $1;
44 $totmax += $max;
45 $files += 1;
46 $next = 1;
47 $ok = 1;
48 } else {
49 $next = $1, $ok = 0, last if /^not ok ([0-9]*)/;
50 if (/^ok (.*)/ && $1 == $next) {
51 $next = $next + 1;
52 }
53 }
54 }
55 }
56 close($fh); # must close to reap child resource values
57 $next -= 1;
58 if ($ok && $next == $max) {
59 print "ok\n";
60 $good += 1;
61 } else {
62 $next += 1;
63 print "FAILED on test $next\n";
64 $bad += 1;
65 $_ = $test;
66 }
67 }
68 my $t_total = timediff(new Benchmark, $t_start);
69
70 if ($bad == 0) {
71 if ($ok) {
72 print "All tests successful.\n";
73 } else {
74 die "FAILED--no tests were run for some reason.\n";
75 }
76 } else {
77 $pct = sprintf("%.2f", $good / $total * 100);
78 if ($bad == 1) {
4633a7c4 79 die "Failed 1 test, $pct% okay.\n";
a0d0e21e 80 } else {
81 die "Failed $bad/$total tests, $pct% okay.\n";
82 }
83 }
84 printf("Files=%d, Tests=%d, %s\n", $files,$totmax, timestr($t_total, 'nop'));
85}
86
871;
cb1a09d0 88__END__
89
90=head1 NAME
91
92Test::Harness - run perl standard test scripts with statistics
93
94=head1 SYNOPSIS
95
96use Test::Harness;
97
98runtests(@tests);
99
100=head1 DESCRIPTION
101
102Perl test scripts print to standard output C<"ok N"> for each single
103test, where C<N> is an increasing sequence of integers. The first line
104output by a standard test scxript is C<"1..M"> with C<M> being the
105number of tests that should be run within the test
106script. Test::Harness::runscripts(@tests) runs all the testscripts
107named as arguments and checks standard output for the expected
108C<"ok N"> strings.
109
110After all tests have been performed, runscripts() prints some
111performance statistics that are computed by the Benchmark module.
112
113=head1 EXPORT
114
115C<&runscripts> is exported by Test::Harness per default.
116
117=head1 DIAGNOSTICS
118
119=over 4
120
121=item C<All tests successful.\nFiles=%d, Tests=%d, %s>
122
123If all tests are successful some statistics about the performance are
124printed.
125
126=item C<Failed 1 test, $pct% okay.>
127
128=item C<Failed %d/%d tests, %.2f%% okay.>
129
130If not all tests were successful, the script dies with one of the
131above messages.
132
133=back
134
135=head1 SEE ALSO
136
137See L<Benchmerk> for the underlying timing routines.
138
139=head1 BUGS
140
141Test::Harness uses $^X to determine the perl binary to run the tests
142with. Test scripts running via the shebang (C<#!>) line may not be portable
143because $^X is not consistent for shebang scripts across
144platforms. This is no problem when Test::Harness is run with an
145absolute path to the perl binary.
146
147=cut