| 5 | | @ISA = qw(Exporter); |
|---|
| | 13 | sub new { |
|---|
| | 14 | my $class = shift; |
|---|
| | 15 | my $script = shift; |
|---|
| | 16 | my %self = ( |
|---|
| | 17 | -expected => 0, |
|---|
| | 18 | -depends => [], |
|---|
| | 19 | -test => undef, |
|---|
| | 20 | ); |
|---|
| | 21 | # print "Looking at $script\n"; |
|---|
| | 22 | $script =~ /^test(.*)\.pl$/o; |
|---|
| | 23 | my $name = $1; |
|---|
| | 24 | $self{-name} = $name; |
|---|
| | 25 | warn "Duplicate test $name\n" if exists $tests{$name}; |
|---|
| | 26 | my $code = 'sub { my $god = shift; ' . "\n"; |
|---|
| | 27 | open IN, "<", $script or die "Couldn't open ${script}: $!\n"; |
|---|
| | 28 | while (<IN>) { |
|---|
| | 29 | chomp; |
|---|
| | 30 | next if /^\s*(?:#|$)/o; |
|---|
| | 31 | last if /^run tests:$/o; |
|---|
| | 32 | if (/^depends on (.*)$/o) { |
|---|
| | 33 | push @{$self{-depends}}, $1; |
|---|
| | 34 | } |
|---|
| | 35 | if (/^expect (\d+) failures!$/) { |
|---|
| | 36 | $self{-expected} = $1; |
|---|
| | 37 | print "Expecting $1 failures in $name\n"; |
|---|
| | 38 | } |
|---|
| | 39 | if (/^\s*login mortal$/) { |
|---|
| | 40 | $code .= 'my $mortal = shift;' . "\n"; |
|---|
| | 41 | $use_mortal = 1; |
|---|
| | 42 | } |
|---|
| | 43 | } |
|---|
| | 44 | while (<IN>) { |
|---|
| | 45 | $code .= $_; |
|---|
| | 46 | } |
|---|
| | 47 | close IN; |
|---|
| | 48 | $code .= "}"; |
|---|
| | 49 | # print "Test function for $name:\n$code\n"; |
|---|
| | 50 | $self{-test} = eval $code; |
|---|
| | 51 | my $obj = bless \%self; |
|---|
| | 52 | $tests{$name} = $obj; |
|---|
| | 53 | return $obj; |
|---|
| | 54 | } |
|---|
| 9 | | my $testcount = 0; |
|---|
| 10 | | my @failures = (); |
|---|
| | 62 | foreach my $dep (@{$self->{-depends}}) { |
|---|
| | 63 | my $test = $tests{$dep}; |
|---|
| | 64 | if (defined $test) { |
|---|
| | 65 | $test->run($god, $mortal); |
|---|
| | 66 | } else { |
|---|
| | 67 | warn "Unresolved dependency $dep\n"; |
|---|
| | 68 | } |
|---|
| | 69 | } |
|---|
| | 70 | |
|---|
| | 71 | local ($testcount, @failures) = (0, ()); |
|---|
| | 72 | |
|---|
| | 73 | my $name = $self->{-name}; |
|---|
| | 74 | |
|---|
| | 75 | print "Running tests for ${name}:\n"; |
|---|
| | 76 | |
|---|
| | 77 | &{$self->{-test}}($god, $mortal); |
|---|
| | 78 | |
|---|
| | 79 | $testfiles++; |
|---|
| | 80 | $alltests += $testcount; |
|---|
| | 81 | $allfailures += @failures; |
|---|
| | 82 | $allexpected += $self->{-expected}; |
|---|
| | 83 | |
|---|
| | 84 | summary $self->{-name}, $testcount, \@failures, $self->{-expected}; |
|---|
| | 85 | } |
|---|
| | 86 | |
|---|
| | 87 | END { |
|---|
| | 88 | print "Totals:\n"; |
|---|
| | 89 | summary("all tests run", $alltests, $allfailures, $allexpected) |
|---|
| | 90 | if $testfiles > 1; |
|---|
| | 91 | } |
|---|
| 67 | | print ":"x70, "\n"; |
|---|
| 68 | | print "\n"; |
|---|
| 69 | | my $scount = $testcount - @failures; |
|---|
| 70 | | my $fcount = @failures; |
|---|
| 71 | | print "$testcount tests, $scount succeeded, $fcount failed\n"; |
|---|
| 72 | | if ($fcount) { |
|---|
| 73 | | print "failed tests:\n"; |
|---|
| 74 | | my $str = join(", ", @failures); |
|---|
| 75 | | while (length($str) > 67) { |
|---|
| 76 | | $str =~ s/^(.{1,67}), //o; |
|---|
| 77 | | print " $1,\n"; |
|---|
| | 147 | my ($name, $testcount, $failures, $expected) = @_; |
|---|
| | 148 | print ":"x70, "\n"; |
|---|
| | 149 | print "\n"; |
|---|
| | 150 | my $fcount = 0; |
|---|
| | 151 | if (ref $failures) { |
|---|
| | 152 | $fcount = scalar @$failures; |
|---|
| | 153 | } else { |
|---|
| | 154 | $fcount = $failures; |
|---|
| 79 | | print " $str\n"; |
|---|
| 80 | | } |
|---|
| 81 | | |
|---|
| 82 | | $testcount = 0; |
|---|
| 83 | | @failures = (); |
|---|
| 84 | | } |
|---|
| 85 | | |
|---|
| 86 | | END { |
|---|
| 87 | | summary() if $testcount; |
|---|
| | 156 | my $scount = $testcount - $fcount; |
|---|
| | 157 | print "$testcount tests, $scount succeeded, $fcount failed ($expected expected failures)\n"; |
|---|
| | 158 | if ($fcount != $expected) { |
|---|
| | 159 | print "failed tests:\n"; |
|---|
| | 160 | my $str = join(", ", @$failures); |
|---|
| | 161 | while (length($str) > 67) { |
|---|
| | 162 | $str =~ s/^(.{1,67}), //o; |
|---|
| | 163 | print " $1,\n"; |
|---|
| | 164 | } |
|---|
| | 165 | print " $str\n"; |
|---|
| | 166 | } |
|---|
| | 167 | print "\n"; |
|---|