| 1 | # This is a makeover of Test::Harness to allow its tests | ||
| 2 | # to be retrofitted as unit tests. | ||
| 3 | package Test::Unit::UnitHarness; | ||
| 4 | BEGIN { | ||
| 5 | $Test::Unit::UnitHarness::VERSION = '0.25_1325'; # added by dist-tools/SetVersion.pl | ||
| 6 | } | ||
| 7 | |||
| 8 | BEGIN {require 5.002;} | ||
| 9 | use base qw(Test::Unit::Runner Test::Unit::Test Exporter); | ||
| 10 | |||
| 11 | use Config; | ||
| 12 | use Carp; | ||
| 13 | use Class::Inner; | ||
| 14 | use FileHandle; | ||
| 15 | |||
| 16 | use Test::Unit::Debug qw(debug); | ||
| 17 | use Test::Unit::TestCase; | ||
| 18 | use Test::Unit::Exception; | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | |||
| 22 | use vars qw($VERSION $verbose $switches $have_devel_corestack $curtest | ||
| 23 | @EXPORT @EXPORT_OK); | ||
| 24 | $have_devel_corestack = 0; | ||
| 25 | |||
| 26 | # This file was derived from a v1.1502, but that is not the current | ||
| 27 | # version number | ||
| 28 | # | ||
| 29 | # $VERSION = "1.1502"; | ||
| 30 | |||
| 31 | @EXPORT = qw(&runtests); | ||
| 32 | @EXPORT_OK = qw($verbose $switches); | ||
| 33 | |||
| 34 | $verbose = 0; | ||
| 35 | $switches = "-w"; | ||
| 36 | |||
| 37 | # class and object methods | ||
| 38 | |||
| 39 | sub new { | ||
| 40 | my $class = shift; | ||
| 41 | my ($name) = @_; | ||
| 42 | |||
| 43 | my @_Tests = (); | ||
| 44 | my $self = { | ||
| 45 | _Tests => \@_Tests, | ||
| 46 | _Name => $name, | ||
| 47 | _Names => [], | ||
| 48 | }; | ||
| 49 | bless $self, $class; | ||
| 50 | debug(ref($self) . "::new($name) called\n"); | ||
| 51 | |||
| 52 | return $self; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub run { | ||
| 56 | my $self = shift; | ||
| 57 | my $result = shift; | ||
| 58 | my $test = $self->{_Name}; | ||
| 59 | my $fh = new FileHandle; | ||
| 60 | my $next = 1; | ||
| 61 | my $max = 0; | ||
| 62 | my $message = ""; | ||
| 63 | |||
| 64 | # pass -I flags to children | ||
| 65 | my $old5lib = $ENV{PERL5LIB}; | ||
| 66 | local($ENV{'PERL5LIB'}) = join($Config{path_sep}, @INC); | ||
| 67 | |||
| 68 | if ($^O eq 'VMS') { $switches =~ s/-(\S*[A-Z]\S*)/"-$1"/g } | ||
| 69 | |||
| 70 | $fh->open($test) or print "can't open $test. $!\n"; | ||
| 71 | my $first = <$fh>; | ||
| 72 | my $s = $switches; | ||
| 73 | $s .= q[ "-T"] if $first =~ /^#!.*\bperl.*-\w*T/; | ||
| 74 | $fh->close or print "can't close $test. $!\n"; | ||
| 75 | my $cmd = "$^X $s $test|"; | ||
| 76 | $cmd = "MCR $cmd" if $^O eq 'VMS'; | ||
| 77 | $fh->open($cmd) or print "can't run $test. $!\n"; | ||
| 78 | for my $line (<$fh>) { | ||
| 79 | print $line if $verbose; | ||
| 80 | |||
| 81 | if ($line =~ /^1\.\.([0-9]+)/) { | ||
| 82 | # Not supported in Result - It's needed!!! | ||
| 83 | #$result->plan($1); | ||
| 84 | $next = 1; | ||
| 85 | $max = $1; | ||
| 86 | $message = ""; | ||
| 87 | } | ||
| 88 | elsif ($max && $line =~ /^(not\s+)?ok\b/) { | ||
| 89 | my $this = $next; | ||
| 90 | if ($line =~ /^not ok\s*(\d*)/) { | ||
| 91 | $this = $1 if $1 > 0; | ||
| 92 | my $testcase = new Test::Unit::TestCase("$test case $this"); | ||
| 93 | $result->start_test($testcase); | ||
| 94 | $result->add_failure( | ||
| 95 | Test::Unit::UnitHarness::TestCase->new("$test case $this"), | ||
| 96 | Test::Unit::UnitHarness::Exception->new($message) | ||
| 97 | ); | ||
| 98 | $result->end_test($testcase); | ||
| 99 | $message = ""; | ||
| 100 | } | ||
| 101 | elsif ($line =~ /^ok\s*(\d*)/) { | ||
| 102 | $this = $1 if $1; | ||
| 103 | my $testcase = | ||
| 104 | Test::Unit::UnitHarness::TestCase->new("$test case $this"); | ||
| 105 | $result->start_test($testcase); | ||
| 106 | $result->add_pass($testcase); | ||
| 107 | $result->end_test($testcase); | ||
| 108 | $message = ""; | ||
| 109 | } | ||
| 110 | $next++; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | # this is the message, not the medium... | ||
| 114 | # this wasnt part of the Test::Harness protocol, so it | ||
| 115 | # must be output from the program. Collect this, it might | ||
| 116 | # prove useful! | ||
| 117 | $message .= $line; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | $fh->close; # must close to reap child resource values | ||
| 121 | if ($^O eq 'VMS') { | ||
| 122 | if (defined $old5lib) { | ||
| 123 | $ENV{PERL5LIB} = $old5lib; | ||
| 124 | } else { | ||
| 125 | delete $ENV{PERL5LIB}; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub name { | ||
| 131 | my $self = shift; | ||
| 132 | return $self->{_Name}; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub names { | ||
| 136 | my $self = shift; | ||
| 137 | return $self->{_Names}; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub add_test { | ||
| 141 | croak "This is suite is not mutable."; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub add_test_method { | ||
| 145 | croak "This suite is not mutable."; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub count_test_cases { | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub to_string { | ||
| 153 | my $self = shift; | ||
| 154 | return $self->{_Name}; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub warning { | ||
| 158 | my $self = shift; | ||
| 159 | my ($message) = @_; | ||
| 160 | return Class::Inner->new( | ||
| 161 | parent => 'Test::Unit::TestCase', | ||
| 162 | methods => { run_test => sub { (shift)->fail($message) } }, | ||
| 163 | args => ['warning'], | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | |||
| 167 | package Test::Unit::UnitHarness::TestCase; | ||
| 168 | use base qw(Test::Unit::TestCase); | ||
| 169 | |||
| 170 | sub run_test { | ||
| 171 | my $self = shift; | ||
| 172 | my $class = ref($self); | ||
| 173 | my $method = $self->name(); | ||
| 174 | $self->fail("This test is not restartable"); | ||
| 175 | } | ||
| 176 | |||
| 177 | package Test::Unit::UnitHarness::Exception; | ||
| 178 | use base qw(Test::Unit::Exception); | ||
| 179 | use strict; | ||
| 180 | |||
| 181 | sub new { | ||
| 182 | my $class = shift; | ||
| 183 | my ($message) = @_; | ||
| 184 | my $stacktrace = ''; | ||
| 185 | |||
| 186 | $message = '' unless defined($message); | ||
| 187 | $stacktrace = $class . ": Output from external test\n" | ||
| 188 | . $message . "\n"; | ||
| 189 | |||
| 190 | bless { stacktrace => $stacktrace }, $class; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub stacktrace { | ||
| 194 | my $self = shift; | ||
| 195 | return $self->{stacktrace}; | ||
| 196 | } | ||
| 197 | |||
| 198 | 1; | ||
| 199 | |||
| 200 | __END__ |