Move Test::Simple from lib to ext.
[p5sagit/p5-mst-13.2.git] / ext / Test-Simple / lib / Test / Builder / Tester.pm
CommitLineData
845d7e37 1package Test::Builder::Tester;
2
3use strict;
8f70d4fd 4our $VERSION = "1.18";
845d7e37 5
6use Test::Builder;
7use Symbol;
8use Carp;
9
10=head1 NAME
11
12Test::Builder::Tester - test testsuites that have been built with
13Test::Builder
14
15=head1 SYNOPSIS
16
17 use Test::Builder::Tester tests => 1;
18 use Test::More;
19
1be77ff7 20 test_out("not ok 1 - foo");
21 test_fail(+1);
845d7e37 22 fail("foo");
23 test_test("fail works");
24
25=head1 DESCRIPTION
26
27A module that helps you test testing modules that are built with
28B<Test::Builder>.
29
30The testing system is designed to be used by performing a three step
31process for each test you wish to test. This process starts with using
1be77ff7 32C<test_out> and C<test_err> in advance to declare what the testsuite you
33are testing will output with B<Test::Builder> to stdout and stderr.
845d7e37 34
35You then can run the test(s) from your test suite that call
36B<Test::Builder>. At this point the output of B<Test::Builder> is
37safely captured by B<Test::Builder::Tester> rather than being
38interpreted as real test output.
39
40The final stage is to call C<test_test> that will simply compare what you
41predeclared to what B<Test::Builder> actually outputted, and report the
42results back with a "ok" or "not ok" (with debugging) to the normal
43output.
44
45=cut
46
47####
48# set up testing
49####
50
51my $t = Test::Builder->new;
52
53###
54# make us an exporter
55###
56
57use Exporter;
705e6672 58our @ISA = qw(Exporter);
845d7e37 59
705e6672 60our @EXPORT = qw(test_out test_err test_fail test_diag test_test line_num);
845d7e37 61
62# _export_to_level and import stolen directly from Test::More. I am
63# the king of cargo cult programming ;-)
64
65# 5.004's Exporter doesn't have export_to_level.
ccbd73a4 66sub _export_to_level {
67 my $pkg = shift;
68 my $level = shift;
69 (undef) = shift; # XXX redundant arg
70 my $callpkg = caller($level);
71 $pkg->export( $callpkg, @_ );
845d7e37 72}
73
74sub import {
75 my $class = shift;
76 my(@plan) = @_;
77
78 my $caller = caller;
79
80 $t->exported_to($caller);
81 $t->plan(@plan);
82
83 my @imports = ();
ccbd73a4 84 foreach my $idx ( 0 .. $#plan ) {
845d7e37 85 if( $plan[$idx] eq 'import' ) {
ccbd73a4 86 @imports = @{ $plan[ $idx + 1 ] };
845d7e37 87 last;
88 }
89 }
90
ccbd73a4 91 __PACKAGE__->_export_to_level( 1, __PACKAGE__, @imports );
845d7e37 92}
93
94###
95# set up file handles
96###
97
98# create some private file handles
99my $output_handle = gensym;
100my $error_handle = gensym;
101
102# and tie them to this package
1be77ff7 103my $out = tie *$output_handle, "Test::Builder::Tester::Tie", "STDOUT";
104my $err = tie *$error_handle, "Test::Builder::Tester::Tie", "STDERR";
845d7e37 105
106####
107# exported functions
108####
109
110# for remembering that we're testing and where we're testing at
111my $testing = 0;
112my $testing_num;
113
114# remembering where the file handles were originally connected
115my $original_output_handle;
116my $original_failure_handle;
117my $original_todo_handle;
118
119my $original_test_number;
120my $original_harness_state;
121
122my $original_harness_env;
123
124# function that starts testing and redirects the filehandles for now
ccbd73a4 125sub _start_testing {
845d7e37 126 # even if we're running under Test::Harness pretend we're not
127 # for now. This needed so Test::Builder doesn't add extra spaces
128 $original_harness_env = $ENV{HARNESS_ACTIVE} || 0;
129 $ENV{HARNESS_ACTIVE} = 0;
130
131 # remember what the handles were set to
132 $original_output_handle = $t->output();
133 $original_failure_handle = $t->failure_output();
134 $original_todo_handle = $t->todo_output();
135
136 # switch out to our own handles
137 $t->output($output_handle);
138 $t->failure_output($error_handle);
139 $t->todo_output($error_handle);
140
141 # clear the expected list
142 $out->reset();
143 $err->reset();
144
145 # remeber that we're testing
ccbd73a4 146 $testing = 1;
845d7e37 147 $testing_num = $t->current_test;
148 $t->current_test(0);
149
150 # look, we shouldn't do the ending stuff
151 $t->no_ending(1);
152}
153
68938d83 154=head2 Functions
845d7e37 155
1be77ff7 156These are the six methods that are exported as default.
845d7e37 157
158=over 4
159
1be77ff7 160=item test_out
845d7e37 161
1be77ff7 162=item test_err
845d7e37 163
1be77ff7 164Procedures for predeclaring the output that your test suite is
165expected to produce until C<test_test> is called. These procedures
166automatically assume that each line terminates with "\n". So
845d7e37 167
1be77ff7 168 test_out("ok 1","ok 2");
845d7e37 169
1be77ff7 170is the same as
171
172 test_out("ok 1\nok 2");
173
174which is even the same as
845d7e37 175
1be77ff7 176 test_out("ok 1");
177 test_out("ok 2");
178
179Once C<test_out> or C<test_err> (or C<test_fail> or C<test_diag>) have
180been called once all further output from B<Test::Builder> will be
181captured by B<Test::Builder::Tester>. This means that your will not
182be able perform further tests to the normal output in the normal way
183until you call C<test_test> (well, unless you manually meddle with the
184output filehandles)
845d7e37 185
186=cut
187
ccbd73a4 188sub test_out {
1be77ff7 189 # do we need to do any setup?
190 _start_testing() unless $testing;
191
ccbd73a4 192 $out->expect(@_);
845d7e37 193}
194
ccbd73a4 195sub test_err {
1be77ff7 196 # do we need to do any setup?
197 _start_testing() unless $testing;
845d7e37 198
ccbd73a4 199 $err->expect(@_);
1be77ff7 200}
845d7e37 201
1be77ff7 202=item test_fail
68938d83 203
845d7e37 204Because the standard failure message that B<Test::Builder> produces
205whenever a test fails will be a common occurrence in your test error
206output, and because has changed between Test::Builder versions, rather
207than forcing you to call C<test_err> with the string all the time like
208so
209
210 test_err("# Failed test ($0 at line ".line_num(+1).")");
211
68938d83 212C<test_fail> exists as a convenience function that can be called
845d7e37 213instead. It takes one argument, the offset from the current line that
214the line that causes the fail is on.
215
216 test_fail(+1);
217
1be77ff7 218This means that the example in the synopsis could be rewritten
219more simply as:
845d7e37 220
1be77ff7 221 test_out("not ok 1 - foo");
222 test_fail(+1);
223 fail("foo");
224 test_test("fail works");
845d7e37 225
226=cut
227
ccbd73a4 228sub test_fail {
845d7e37 229 # do we need to do any setup?
1be77ff7 230 _start_testing() unless $testing;
845d7e37 231
232 # work out what line we should be on
ccbd73a4 233 my( $package, $filename, $line ) = caller;
234 $line = $line + ( shift() || 0 ); # prevent warnings
845d7e37 235
236 # expect that on stderr
237 $err->expect("# Failed test ($0 at line $line)");
238}
239
240=item test_diag
241
242As most of the remaining expected output to the error stream will be
243created by Test::Builder's C<diag> function, B<Test::Builder::Tester>
244provides a convience function C<test_diag> that you can use instead of
245C<test_err>.
246
247The C<test_diag> function prepends comment hashes and spacing to the
248start and newlines to the end of the expected output passed to it and
249adds it to the list of expected error output. So, instead of writing
250
251 test_err("# Couldn't open file");
252
253you can write
254
255 test_diag("Couldn't open file");
256
257Remember that B<Test::Builder>'s diag function will not add newlines to
258the end of output and test_diag will. So to check
259
260 Test::Builder->new->diag("foo\n","bar\n");
261
262You would do
263
264 test_diag("foo","bar")
265
266without the newlines.
267
268=cut
269
ccbd73a4 270sub test_diag {
845d7e37 271 # do we need to do any setup?
272 _start_testing() unless $testing;
273
274 # expect the same thing, but prepended with "# "
275 local $_;
ccbd73a4 276 $err->expect( map { "# $_" } @_ );
845d7e37 277}
278
279=item test_test
280
281Actually performs the output check testing the tests, comparing the
282data (with C<eq>) that we have captured from B<Test::Builder> against
283that that was declared with C<test_out> and C<test_err>.
284
285This takes name/value pairs that effect how the test is run.
286
287=over
288
289=item title (synonym 'name', 'label')
290
291The name of the test that will be displayed after the C<ok> or C<not
292ok>.
293
294=item skip_out
295
296Setting this to a true value will cause the test to ignore if the
297output sent by the test to the output stream does not match that
298declared with C<test_out>.
299
300=item skip_err
301
302Setting this to a true value will cause the test to ignore if the
303output sent by the test to the error stream does not match that
304declared with C<test_err>.
305
306=back
307
308As a convience, if only one argument is passed then this argument
309is assumed to be the name of the test (as in the above examples.)
310
311Once C<test_test> has been run test output will be redirected back to
312the original filehandles that B<Test::Builder> was connected to
313(probably STDOUT and STDERR,) meaning any further tests you run
314will function normally and cause success/errors for B<Test::Harness>.
315
316=cut
317
ccbd73a4 318sub test_test {
319 # decode the arguements as described in the pod
320 my $mess;
321 my %args;
322 if( @_ == 1 ) {
323 $mess = shift
324 }
325 else {
326 %args = @_;
327 $mess = $args{name} if exists( $args{name} );
328 $mess = $args{title} if exists( $args{title} );
329 $mess = $args{label} if exists( $args{label} );
330 }
845d7e37 331
332 # er, are we testing?
333 croak "Not testing. You must declare output with a test function first."
ccbd73a4 334 unless $testing;
845d7e37 335
336 # okay, reconnect the test suite back to the saved handles
337 $t->output($original_output_handle);
338 $t->failure_output($original_failure_handle);
339 $t->todo_output($original_todo_handle);
340
341 # restore the test no, etc, back to the original point
342 $t->current_test($testing_num);
343 $testing = 0;
344
345 # re-enable the original setting of the harness
346 $ENV{HARNESS_ACTIVE} = $original_harness_env;
347
348 # check the output we've stashed
ccbd73a4 349 unless( $t->ok( ( $args{skip_out} || $out->check ) &&
350 ( $args{skip_err} || $err->check ), $mess )
351 )
845d7e37 352 {
ccbd73a4 353 # print out the diagnostic information about why this
354 # test failed
845d7e37 355
ccbd73a4 356 local $_;
845d7e37 357
ccbd73a4 358 $t->diag( map { "$_\n" } $out->complaint )
359 unless $args{skip_out} || $out->check;
845d7e37 360
ccbd73a4 361 $t->diag( map { "$_\n" } $err->complaint )
362 unless $args{skip_err} || $err->check;
845d7e37 363 }
364}
365
366=item line_num
367
368A utility function that returns the line number that the function was
369called on. You can pass it an offset which will be added to the
370result. This is very useful for working out the correct text of
68938d83 371diagnostic functions that contain line numbers.
845d7e37 372
373Essentially this is the same as the C<__LINE__> macro, but the
374C<line_num(+3)> idiom is arguably nicer.
375
376=cut
377
ccbd73a4 378sub line_num {
379 my( $package, $filename, $line ) = caller;
380 return $line + ( shift() || 0 ); # prevent warnings
845d7e37 381}
382
383=back
384
385In addition to the six exported functions there there exists one
386function that can only be accessed with a fully qualified function
387call.
388
389=over 4
390
391=item color
392
393When C<test_test> is called and the output that your tests generate
394does not match that which you declared, C<test_test> will print out
395debug information showing the two conflicting versions. As this
396output itself is debug information it can be confusing which part of
397the output is from C<test_test> and which was the original output from
398your original tests. Also, it may be hard to spot things like
399extraneous whitespace at the end of lines that may cause your test to
400fail even though the output looks similar.
401
402To assist you, if you have the B<Term::ANSIColor> module installed
403(which you should do by default from perl 5.005 onwards), C<test_test>
404can colour the background of the debug information to disambiguate the
405different types of output. The debug output will have it's background
406coloured green and red. The green part represents the text which is
407the same between the executed and actual output, the red shows which
408part differs.
409
410The C<color> function determines if colouring should occur or not.
411Passing it a true or false value will enable or disable colouring
412respectively, and the function called with no argument will return the
413current setting.
414
415To enable colouring from the command line, you can use the
416B<Text::Builder::Tester::Color> module like so:
417
418 perl -Mlib=Text::Builder::Tester::Color test.t
419
420Or by including the B<Test::Builder::Tester::Color> module directly in
421the PERL5LIB.
422
423=cut
424
425my $color;
ccbd73a4 426
427sub color {
428 $color = shift if @_;
429 $color;
845d7e37 430}
431
432=back
433
434=head1 BUGS
435
68938d83 436Calls C<<Test::Builder->no_ending>> turning off the ending tests.
437This is needed as otherwise it will trip out because we've run more
438tests than we strictly should have and it'll register any failures we
439had that we were testing for as real failures.
845d7e37 440
441The color function doesn't work unless B<Term::ANSIColor> is installed
442and is compatible with your terminal.
443
444Bugs (and requests for new features) can be reported to the author
445though the CPAN RT system:
446L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Builder-Tester>
447
448=head1 AUTHOR
449
450Copyright Mark Fowler E<lt>mark@twoshortplanks.comE<gt> 2002, 2004.
451
452Some code taken from B<Test::More> and B<Test::Catch>, written by by
453Michael G Schwern E<lt>schwern@pobox.comE<gt>. Hence, those parts
454Copyright Micheal G Schwern 2001. Used and distributed with
455permission.
456
457This program is free software; you can redistribute it
458and/or modify it under the same terms as Perl itself.
459
460=head1 NOTES
461
462This code has been tested explicitly on the following versions
463of perl: 5.7.3, 5.6.1, 5.6.0, 5.005_03, 5.004_05 and 5.004.
464
465Thanks to Richard Clamp E<lt>richardc@unixbeard.netE<gt> for letting
466me use his testing system to try this module out on.
467
468=head1 SEE ALSO
469
470L<Test::Builder>, L<Test::Builder::Tester::Color>, L<Test::More>.
471
472=cut
473
4741;
475
476####################################################################
477# Helper class that is used to remember expected and received data
478
1be77ff7 479package Test::Builder::Tester::Tie;
845d7e37 480
481##
482# add line(s) to be expected
483
ccbd73a4 484sub expect {
845d7e37 485 my $self = shift;
486
487 my @checks = @_;
488 foreach my $check (@checks) {
489 $check = $self->_translate_Failed_check($check);
ccbd73a4 490 push @{ $self->{wanted} }, ref $check ? $check : "$check\n";
845d7e37 491 }
492}
493
ccbd73a4 494sub _translate_Failed_check {
495 my( $self, $check ) = @_;
845d7e37 496
ba760cef 497 if( $check =~ /\A(.*)# (Failed .*test) \((.*?) at line (\d+)\)\Z(?!\n)/ ) {
498 $check = "/\Q$1\E#\\s+\Q$2\E.*?\\n?.*?\Qat $3\E line \Q$4\E.*\\n?/";
845d7e37 499 }
500
501 return $check;
502}
503
845d7e37 504##
505# return true iff the expected data matches the got data
506
ccbd73a4 507sub check {
845d7e37 508 my $self = shift;
509
510 # turn off warnings as these might be undef
511 local $^W = 0;
512
ccbd73a4 513 my @checks = @{ $self->{wanted} };
514 my $got = $self->{got};
845d7e37 515 foreach my $check (@checks) {
ccbd73a4 516 $check = "\Q$check\E" unless( $check =~ s,^/(.*)/$,$1, or ref $check );
845d7e37 517 return 0 unless $got =~ s/^$check//;
518 }
519
520 return length $got == 0;
521}
522
523##
524# a complaint message about the inputs not matching (to be
525# used for debugging messages)
526
ccbd73a4 527sub complaint {
528 my $self = shift;
845d7e37 529 my $type = $self->type;
530 my $got = $self->got;
ccbd73a4 531 my $wanted = join "\n", @{ $self->wanted };
845d7e37 532
533 # are we running in colour mode?
ccbd73a4 534 if(Test::Builder::Tester::color) {
535 # get color
536 eval { require Term::ANSIColor };
537 unless($@) {
538 # colours
539
540 my $green = Term::ANSIColor::color("black") . Term::ANSIColor::color("on_green");
541 my $red = Term::ANSIColor::color("black") . Term::ANSIColor::color("on_red");
542 my $reset = Term::ANSIColor::color("reset");
543
544 # work out where the two strings start to differ
545 my $char = 0;
546 $char++ while substr( $got, $char, 1 ) eq substr( $wanted, $char, 1 );
547
548 # get the start string and the two end strings
549 my $start = $green . substr( $wanted, 0, $char );
550 my $gotend = $red . substr( $got, $char ) . $reset;
551 my $wantedend = $red . substr( $wanted, $char ) . $reset;
552
553 # make the start turn green on and off
554 $start =~ s/\n/$reset\n$green/g;
555
556 # make the ends turn red on and off
557 $gotend =~ s/\n/$reset\n$red/g;
558 $wantedend =~ s/\n/$reset\n$red/g;
559
560 # rebuild the strings
561 $got = $start . $gotend;
562 $wanted = $start . $wantedend;
563 }
845d7e37 564 }
565
ccbd73a4 566 return "$type is:\n" . "$got\nnot:\n$wanted\nas expected";
845d7e37 567}
568
569##
570# forget all expected and got data
571
ccbd73a4 572sub reset {
845d7e37 573 my $self = shift;
b7f9bbeb 574 %$self = (
ccbd73a4 575 type => $self->{type},
576 got => '',
577 wanted => [],
578 );
845d7e37 579}
580
ccbd73a4 581sub got {
845d7e37 582 my $self = shift;
b7f9bbeb 583 return $self->{got};
845d7e37 584}
585
ccbd73a4 586sub wanted {
845d7e37 587 my $self = shift;
b7f9bbeb 588 return $self->{wanted};
845d7e37 589}
590
ccbd73a4 591sub type {
845d7e37 592 my $self = shift;
b7f9bbeb 593 return $self->{type};
845d7e37 594}
595
596###
597# tie interface
598###
599
ccbd73a4 600sub PRINT {
845d7e37 601 my $self = shift;
b7f9bbeb 602 $self->{got} .= join '', @_;
845d7e37 603}
604
605sub TIEHANDLE {
ccbd73a4 606 my( $class, $type ) = @_;
845d7e37 607
ccbd73a4 608 my $self = bless { type => $type }, $class;
b7f9bbeb 609
845d7e37 610 $self->reset;
611
612 return $self;
613}
614
ccbd73a4 615sub READ { }
616sub READLINE { }
617sub GETC { }
618sub FILENO { }
845d7e37 619
6201;