root/1.8.2/trunk/test/TestHarness.pm

Revision 439, 1.7 KB (checked in by pennmush, 2 years ago)

PennMUSH 1.7.7p20 Archival

Line 
1package TestHarness;
2
3require Exporter;
4
5@ISA = qw(Exporter);
6
7@EXPORT = qw(test summary);
8
9my $testcount = 0;
10my @failures = ();
11
12$| = 1;
13
14sub test {
15  my $name = shift;
16  my $conn = shift;
17  my $command = shift;
18  my $patterns = shift;
19
20  $patterns = [$patterns] if ref($patterns) ne "ARRAY";
21
22  print substr("Running $name".(" "x80), 0, 78)."\r";
23
24  my $result = defined($command) ? $conn->command($command) : $conn->listen();
25  my $verdict = 1;
26
27  my $pattern;
28  foreach $pattern (@$patterns) {
29    my $matchpattern = $pattern;
30    my $negate = 0;
31    if ($matchpattern =~ s/^!//o) {
32      $negate = 1;
33    } else {
34      $matchpattern =~ s/^=//o;
35    }
36
37    if ($negate) {
38      $verdict = 0 if $result =~ /$matchpattern/;
39    } else {
40      $verdict = 0 unless $result =~ /$matchpattern/;
41    }
42  }
43
44  $testcount++;
45  unless ($verdict) {
46    push(@failures, $name);
47    print "TEST FAILURE: $name\n";
48    if (defined($command)) {
49      print "  command: $command\n";
50    } else {
51      print "  listening\n";
52    }
53    chomp $result;
54    if ($result =~ /\n/o) {
55      print "  result:\n$result\n";
56    } else {
57      print "  result:  $result\n";
58    }
59    foreach $pattern (@$patterns) {
60      print "  pattern: $pattern\n";
61    }
62    print "\n";
63  }
64}
65
66sub summary {
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";
78    }
79    print "  $str\n";
80  }
81
82  $testcount = 0;
83  @failures = ();
84}
85
86END {
87  summary() if $testcount;
88}
89
901;
Note: See TracBrowser for help on using the browser.