home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacPerl 5.0.3 / Preinstalled MacPerl (FAT) / lib / Test / Harness.pm
Encoding:
Perl POD Document  |  1995-03-20  |  2.0 KB  |  82 lines  |  [TEXT/McPL]

  1. package Test::Harness;
  2.  
  3. use Exporter;
  4. use Benchmark;
  5. @ISA=(Exporter);
  6. @EXPORT= qw(&runtests &test_lib);
  7. @EXPORT_OK= qw($verbose $switches);
  8.  
  9. $verbose = 0;
  10. $switches = "-w";
  11.  
  12. sub runtests {
  13.     my(@tests) = @_;
  14.     local($|) = 1;
  15.     my($test,$te,$ok,$next,$max,$totmax, $files,$pct);
  16.     my $bad = 0;
  17.     my $good = 0;
  18.     my $total = @tests;
  19. #    local($ENV{'PERL5LIB'}) = join(':', @INC); # pass -I flags to children
  20.     local($ENV{'PERL5LIB'}) = join(',', @INC); # pass -I flags to children
  21.  
  22.     my $t_start = new Benchmark;
  23.     while ($test = shift(@tests)) {
  24.       $te = $test;
  25.       chop($te);
  26.       print "$te" . '.' x (20 - length($te));
  27.       my $fh = "RESULTS";
  28.       open($fh,"$^X $switches $test|") || (print "can't run. $!\n");
  29.       $ok = 0;
  30.       $next = 0;
  31.       while (<$fh>) {
  32.       if( $verbose ){
  33.           print $_;
  34.       }
  35.           unless (/^#/) {
  36.               if (/^1\.\.([0-9]+)/) {
  37.                   $max = $1;
  38.                   $totmax += $max;
  39.                   $files += 1;
  40.                   $next = 1;
  41.                   $ok = 1;
  42.               } else {
  43.           $next = $1, $ok = 0, last if /^not ok ([0-9]*)/;
  44.                   if (/^ok (.*)/ && $1 == $next) {
  45.                       $next = $next + 1;
  46.                   }
  47.               }
  48.           }
  49.       }
  50.       close($fh); # must close to reap child resource values
  51.       $next -= 1;
  52.       if ($ok && $next == $max) {
  53.           print "ok\n";
  54.           $good += 1;
  55.       } else {
  56.           $next += 1;
  57.           print "FAILED on test $next\n";
  58.           $bad += 1;
  59.           $_ = $test;
  60.       }
  61.     }
  62.     my $t_total = timediff(new Benchmark, $t_start);
  63.  
  64.     if ($bad == 0) {
  65.       if ($ok) {
  66.           print "All tests successful.\n";
  67.       } else {
  68.           die "FAILED--no tests were run for some reason.\n";
  69.       }
  70.     } else {
  71.       $pct = sprintf("%.2f", $good / $total * 100);
  72.       if ($bad == 1) {
  73.           warn "Failed 1 test, $pct% okay.\n";
  74.       } else {
  75.           die "Failed $bad/$total tests, $pct% okay.\n";
  76.       }
  77.     }
  78.     printf("Files=%d,  Tests=%d, %s\n", $files,$totmax, timestr($t_total, 'nop'));
  79. }
  80.  
  81. 1;
  82.