3 package Test::Tester::Capture;
5 our $VERSION = '1.302170';
11 @ISA = qw( Test::Builder );
13 # Make Test::Tester::Capture thread-safe for ithreads.
20 my $Curr_Test = 0; share($Curr_Test);
21 my @Test_Results = (); share(@Test_Results);
22 my $Prem_Diag = {diag => ""}; share($Curr_Test);
26 # Test::Tester::Capgture::new used to just return __PACKAGE__
27 # because Test::Builder::new enforced it's singleton nature by
28 # return __PACKAGE__. That has since changed, Test::Builder::new now
29 # returns a blessed has and around version 0.78, Test::Builder::todo
30 # started wanting to modify $self. To cope with this, we now return
31 # a blessed hash. This is a short-term hack, the correct thing to do
32 # is to detect which style of Test::Builder we're dealing with and
36 return bless {}, $class;
40 my($self, $test, $name) = @_;
44 # $test might contain an object which we don't want to accidentally
45 # store, so we turn it into a boolean.
46 $test = $test ? 1 : 0;
51 my($pack, $file, $line) = $self->caller;
53 my $todo = $self->todo();
59 @$result{ 'ok', 'actual_ok' } = ( ( $todo ? 1 : 0 ), 0 );
62 @$result{ 'ok', 'actual_ok' } = ( 1, $test );
66 $name =~ s|#|\\#|g; # # in a name can confuse Test::Harness.
67 $result->{name} = $name;
74 my $what_todo = $todo;
75 $result->{reason} = $what_todo;
76 $result->{type} = 'todo';
79 $result->{reason} = '';
83 $Test_Results[$Curr_Test-1] = $result;
86 my $msg = $todo ? "Failed (TODO)" : "Failed";
87 $result->{fail_diag} = (" $msg test ($file at line $line)\n");
91 $result->{_level} = $Test::Builder::Level;
92 $result->{_depth} = Test::Tester::find_run_tests();
100 my($self, $why) = @_;
103 my $ctx = $self->ctx;
117 _level => $Test::Builder::Level,
118 _depth => Test::Tester::find_run_tests(),
120 $Test_Results[$Curr_Test-1] = \%result;
127 my($self, $why) = @_;
130 my $ctx = $self->ctx;
144 _level => $Test::Builder::Level,
145 _depth => Test::Tester::find_run_tests(),
148 $Test_Results[$Curr_Test-1] = \%result;
155 my($self, @msgs) = @_;
158 # Prevent printing headers when compiling (i.e. -c)
161 my $ctx = $self->ctx;
163 # Escape each line with a #.
165 $_ = 'undef' unless defined;
168 push @msgs, "\n" unless $msgs[-1] =~ /\n\Z/;
170 my $result = $Curr_Test ? $Test_Results[$Curr_Test - 1] : $Prem_Diag;
172 $result->{diag} .= join("", @msgs);
179 return @Test_Results;
183 # Stub. Feel free to send me a patch to implement this.
188 return Test::Builder::explain(@_);
193 return $Prem_Diag->{diag};
200 die "Don't try to change the test number!";
212 $Prem_Diag = {diag => ""};
221 Test::Tester::Capture - Help testing test modules built with Test::Builder
225 This is a subclass of Test::Builder that overrides many of the methods so
226 that they don't output anything. It also keeps track of it's own set of test
227 results so that you can use Test::Builder based modules to perform tests on
228 other Test::Builder based modules.
232 Most of the code here was lifted straight from Test::Builder and then had
233 chunks removed by Fergal Daly <fergal@esatclear.ie>.
237 Under the same license as Perl itself
239 See http://www.perl.com/perl/misc/Artistic.html