1 package Test::Builder::Tester;
2 # $Id: /mirror/googlecode/test-more-trunk/lib/Test/Builder/Tester.pm 67223 2008-10-15T03:08:18.888155Z schwern $
13 Test::Builder::Tester - test testsuites that have been built with
18 use Test::Builder::Tester tests => 1;
21 test_out("not ok 1 - foo");
24 test_test("fail works");
28 A module that helps you test testing modules that are built with
31 The testing system is designed to be used by performing a three step
32 process for each test you wish to test. This process starts with using
33 C<test_out> and C<test_err> in advance to declare what the testsuite you
34 are testing will output with B<Test::Builder> to stdout and stderr.
36 You then can run the test(s) from your test suite that call
37 B<Test::Builder>. At this point the output of B<Test::Builder> is
38 safely captured by B<Test::Builder::Tester> rather than being
39 interpreted as real test output.
41 The final stage is to call C<test_test> that will simply compare what you
42 predeclared to what B<Test::Builder> actually outputted, and report the
43 results back with a "ok" or "not ok" (with debugging) to the normal
52 my $t = Test::Builder->new;
59 our @ISA = qw(Exporter);
61 our @EXPORT = qw(test_out test_err test_fail test_diag test_test line_num);
63 # _export_to_level and import stolen directly from Test::More. I am
64 # the king of cargo cult programming ;-)
66 # 5.004's Exporter doesn't have export_to_level.
67 sub _export_to_level {
70 (undef) = shift; # XXX redundant arg
71 my $callpkg = caller($level);
72 $pkg->export( $callpkg, @_ );
81 $t->exported_to($caller);
85 foreach my $idx ( 0 .. $#plan ) {
86 if( $plan[$idx] eq 'import' ) {
87 @imports = @{ $plan[ $idx + 1 ] };
92 __PACKAGE__->_export_to_level( 1, __PACKAGE__, @imports );
99 # create some private file handles
100 my $output_handle = gensym;
101 my $error_handle = gensym;
103 # and tie them to this package
104 my $out = tie *$output_handle, "Test::Builder::Tester::Tie", "STDOUT";
105 my $err = tie *$error_handle, "Test::Builder::Tester::Tie", "STDERR";
111 # for remembering that we're testing and where we're testing at
115 # remembering where the file handles were originally connected
116 my $original_output_handle;
117 my $original_failure_handle;
118 my $original_todo_handle;
120 my $original_test_number;
121 my $original_harness_state;
123 my $original_harness_env;
125 # function that starts testing and redirects the filehandles for now
127 # even if we're running under Test::Harness pretend we're not
128 # for now. This needed so Test::Builder doesn't add extra spaces
129 $original_harness_env = $ENV{HARNESS_ACTIVE} || 0;
130 $ENV{HARNESS_ACTIVE} = 0;
132 # remember what the handles were set to
133 $original_output_handle = $t->output();
134 $original_failure_handle = $t->failure_output();
135 $original_todo_handle = $t->todo_output();
137 # switch out to our own handles
138 $t->output($output_handle);
139 $t->failure_output($error_handle);
140 $t->todo_output($error_handle);
142 # clear the expected list
146 # remeber that we're testing
148 $testing_num = $t->current_test;
151 # look, we shouldn't do the ending stuff
157 These are the six methods that are exported as default.
165 Procedures for predeclaring the output that your test suite is
166 expected to produce until C<test_test> is called. These procedures
167 automatically assume that each line terminates with "\n". So
169 test_out("ok 1","ok 2");
173 test_out("ok 1\nok 2");
175 which is even the same as
180 Once C<test_out> or C<test_err> (or C<test_fail> or C<test_diag>) have
181 been called once all further output from B<Test::Builder> will be
182 captured by B<Test::Builder::Tester>. This means that your will not
183 be able perform further tests to the normal output in the normal way
184 until you call C<test_test> (well, unless you manually meddle with the
190 # do we need to do any setup?
191 _start_testing() unless $testing;
197 # do we need to do any setup?
198 _start_testing() unless $testing;
205 Because the standard failure message that B<Test::Builder> produces
206 whenever a test fails will be a common occurrence in your test error
207 output, and because has changed between Test::Builder versions, rather
208 than forcing you to call C<test_err> with the string all the time like
211 test_err("# Failed test ($0 at line ".line_num(+1).")");
213 C<test_fail> exists as a convenience function that can be called
214 instead. It takes one argument, the offset from the current line that
215 the line that causes the fail is on.
219 This means that the example in the synopsis could be rewritten
222 test_out("not ok 1 - foo");
225 test_test("fail works");
230 # do we need to do any setup?
231 _start_testing() unless $testing;
233 # work out what line we should be on
234 my( $package, $filename, $line ) = caller;
235 $line = $line + ( shift() || 0 ); # prevent warnings
237 # expect that on stderr
238 $err->expect("# Failed test ($0 at line $line)");
243 As most of the remaining expected output to the error stream will be
244 created by Test::Builder's C<diag> function, B<Test::Builder::Tester>
245 provides a convience function C<test_diag> that you can use instead of
248 The C<test_diag> function prepends comment hashes and spacing to the
249 start and newlines to the end of the expected output passed to it and
250 adds it to the list of expected error output. So, instead of writing
252 test_err("# Couldn't open file");
256 test_diag("Couldn't open file");
258 Remember that B<Test::Builder>'s diag function will not add newlines to
259 the end of output and test_diag will. So to check
261 Test::Builder->new->diag("foo\n","bar\n");
265 test_diag("foo","bar")
267 without the newlines.
272 # do we need to do any setup?
273 _start_testing() unless $testing;
275 # expect the same thing, but prepended with "# "
277 $err->expect( map { "# $_" } @_ );
282 Actually performs the output check testing the tests, comparing the
283 data (with C<eq>) that we have captured from B<Test::Builder> against
284 that that was declared with C<test_out> and C<test_err>.
286 This takes name/value pairs that effect how the test is run.
290 =item title (synonym 'name', 'label')
292 The name of the test that will be displayed after the C<ok> or C<not
297 Setting this to a true value will cause the test to ignore if the
298 output sent by the test to the output stream does not match that
299 declared with C<test_out>.
303 Setting this to a true value will cause the test to ignore if the
304 output sent by the test to the error stream does not match that
305 declared with C<test_err>.
309 As a convience, if only one argument is passed then this argument
310 is assumed to be the name of the test (as in the above examples.)
312 Once C<test_test> has been run test output will be redirected back to
313 the original filehandles that B<Test::Builder> was connected to
314 (probably STDOUT and STDERR,) meaning any further tests you run
315 will function normally and cause success/errors for B<Test::Harness>.
320 # decode the arguements as described in the pod
328 $mess = $args{name} if exists( $args{name} );
329 $mess = $args{title} if exists( $args{title} );
330 $mess = $args{label} if exists( $args{label} );
333 # er, are we testing?
334 croak "Not testing. You must declare output with a test function first."
337 # okay, reconnect the test suite back to the saved handles
338 $t->output($original_output_handle);
339 $t->failure_output($original_failure_handle);
340 $t->todo_output($original_todo_handle);
342 # restore the test no, etc, back to the original point
343 $t->current_test($testing_num);
346 # re-enable the original setting of the harness
347 $ENV{HARNESS_ACTIVE} = $original_harness_env;
349 # check the output we've stashed
350 unless( $t->ok( ( $args{skip_out} || $out->check ) &&
351 ( $args{skip_err} || $err->check ), $mess )
354 # print out the diagnostic information about why this
359 $t->diag( map { "$_\n" } $out->complaint )
360 unless $args{skip_out} || $out->check;
362 $t->diag( map { "$_\n" } $err->complaint )
363 unless $args{skip_err} || $err->check;
369 A utility function that returns the line number that the function was
370 called on. You can pass it an offset which will be added to the
371 result. This is very useful for working out the correct text of
372 diagnostic functions that contain line numbers.
374 Essentially this is the same as the C<__LINE__> macro, but the
375 C<line_num(+3)> idiom is arguably nicer.
380 my( $package, $filename, $line ) = caller;
381 return $line + ( shift() || 0 ); # prevent warnings
386 In addition to the six exported functions there there exists one
387 function that can only be accessed with a fully qualified function
394 When C<test_test> is called and the output that your tests generate
395 does not match that which you declared, C<test_test> will print out
396 debug information showing the two conflicting versions. As this
397 output itself is debug information it can be confusing which part of
398 the output is from C<test_test> and which was the original output from
399 your original tests. Also, it may be hard to spot things like
400 extraneous whitespace at the end of lines that may cause your test to
401 fail even though the output looks similar.
403 To assist you, if you have the B<Term::ANSIColor> module installed
404 (which you should do by default from perl 5.005 onwards), C<test_test>
405 can colour the background of the debug information to disambiguate the
406 different types of output. The debug output will have it's background
407 coloured green and red. The green part represents the text which is
408 the same between the executed and actual output, the red shows which
411 The C<color> function determines if colouring should occur or not.
412 Passing it a true or false value will enable or disable colouring
413 respectively, and the function called with no argument will return the
416 To enable colouring from the command line, you can use the
417 B<Text::Builder::Tester::Color> module like so:
419 perl -Mlib=Text::Builder::Tester::Color test.t
421 Or by including the B<Test::Builder::Tester::Color> module directly in
429 $color = shift if @_;
437 Calls C<<Test::Builder->no_ending>> turning off the ending tests.
438 This is needed as otherwise it will trip out because we've run more
439 tests than we strictly should have and it'll register any failures we
440 had that we were testing for as real failures.
442 The color function doesn't work unless B<Term::ANSIColor> is installed
443 and is compatible with your terminal.
445 Bugs (and requests for new features) can be reported to the author
446 though the CPAN RT system:
447 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Builder-Tester>
451 Copyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.
453 Some code taken from B<Test::More> and B<Test::Catch>, written by by
454 Michael G Schwern E<lt>schwern@pobox.comE<gt>. Hence, those parts
455 Copyright Micheal G Schwern 2001. Used and distributed with
458 This program is free software; you can redistribute it
459 and/or modify it under the same terms as Perl itself.
463 This code has been tested explicitly on the following versions
464 of perl: 5.7.3, 5.6.1, 5.6.0, 5.005_03, 5.004_05 and 5.004.
466 Thanks to Richard Clamp E<lt>richardc@unixbeard.netE<gt> for letting
467 me use his testing system to try this module out on.
471 L<Test::Builder>, L<Test::Builder::Tester::Color>, L<Test::More>.
477 ####################################################################
478 # Helper class that is used to remember expected and received data
480 package Test::Builder::Tester::Tie;
483 # add line(s) to be expected
489 foreach my $check (@checks) {
490 $check = $self->_translate_Failed_check($check);
491 push @{ $self->{wanted} }, ref $check ? $check : "$check\n";
495 sub _translate_Failed_check {
496 my( $self, $check ) = @_;
498 if( $check =~ /\A(.*)# (Failed .*test) \((.*?) at line (\d+)\)\Z(?!\n)/ ) {
499 $check = "/\Q$1\E#\\s+\Q$2\E.*?\\n?.*?\Qat $3\E line \Q$4\E.*\\n?/";
506 # return true iff the expected data matches the got data
511 # turn off warnings as these might be undef
514 my @checks = @{ $self->{wanted} };
515 my $got = $self->{got};
516 foreach my $check (@checks) {
517 $check = "\Q$check\E" unless( $check =~ s,^/(.*)/$,$1, or ref $check );
518 return 0 unless $got =~ s/^$check//;
521 return length $got == 0;
525 # a complaint message about the inputs not matching (to be
526 # used for debugging messages)
530 my $type = $self->type;
531 my $got = $self->got;
532 my $wanted = join "\n", @{ $self->wanted };
534 # are we running in colour mode?
535 if(Test::Builder::Tester::color) {
537 eval { require Term::ANSIColor };
541 my $green = Term::ANSIColor::color("black") . Term::ANSIColor::color("on_green");
542 my $red = Term::ANSIColor::color("black") . Term::ANSIColor::color("on_red");
543 my $reset = Term::ANSIColor::color("reset");
545 # work out where the two strings start to differ
547 $char++ while substr( $got, $char, 1 ) eq substr( $wanted, $char, 1 );
549 # get the start string and the two end strings
550 my $start = $green . substr( $wanted, 0, $char );
551 my $gotend = $red . substr( $got, $char ) . $reset;
552 my $wantedend = $red . substr( $wanted, $char ) . $reset;
554 # make the start turn green on and off
555 $start =~ s/\n/$reset\n$green/g;
557 # make the ends turn red on and off
558 $gotend =~ s/\n/$reset\n$red/g;
559 $wantedend =~ s/\n/$reset\n$red/g;
561 # rebuild the strings
562 $got = $start . $gotend;
563 $wanted = $start . $wantedend;
567 return "$type is:\n" . "$got\nnot:\n$wanted\nas expected";
571 # forget all expected and got data
576 type => $self->{type},
589 return $self->{wanted};
594 return $self->{type};
603 $self->{got} .= join '', @_;
607 my( $class, $type ) = @_;
609 my $self = bless { type => $type }, $class;