File: | local/lib/perl5/Test/Most.pm |
Coverage: | 47.2% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
1 | package Test::Most; | |||||
2 | ||||||
3 | 6 6 6 | 17 4 105 | use warnings; | |||
4 | 6 6 6 | 14 5 96 | use strict; | |||
5 | ||||||
6 | 6 6 6 | 885 6 198 | use Test::Most::Exception 'throw_failure'; | |||
7 | ||||||
8 | # XXX don't use 'base' as it can override signal handlers | |||||
9 | 6 6 6 | 768 27414 17 | use Test::Builder::Module; | |||
10 | our ( @ISA, @EXPORT, $DATA_DUMPER_NAMES_INSTALLED ); | |||||
11 | my $HAVE_TIME_HIRES; | |||||
12 | ||||||
13 | BEGIN { | |||||
14 | ||||||
15 | # There's some strange fiddling around with import(), so this allows us to | |||||
16 | # be nicely backwards compatible to earlier versions of Test::More. | |||||
17 | 6 | 1635 | require Test::More; | |||
18 | 6 168 | 12163 104 | @Test::More::EXPORT = grep { $_ ne 'explain' } @Test::More::EXPORT; | |||
19 | 6 | 27 | Test::More->import; | |||
20 | 6 6 6 6 | 851 1713 3758 16 | eval "use Time::HiRes"; | |||
21 | 6 | 361 | $HAVE_TIME_HIRES = 1 unless $@; | |||
22 | } | |||||
23 | ||||||
24 | 6 6 6 | 16 5 96 | use Test::Builder; | |||
25 | my $OK_FUNC; | |||||
26 | BEGIN { | |||||
27 | 6 | 465 | $OK_FUNC = \&Test::Builder::ok; | |||
28 | } | |||||
29 | ||||||
30 - 38 | =head1 NAME Test::Most - Most commonly needed test functions and features. =head1 VERSION Version 0.33 =cut | |||||
39 | ||||||
40 | our $VERSION = '0.33'; | |||||
41 | $VERSION = eval $VERSION; | |||||
42 | ||||||
43 - 437 | =head1 SYNOPSIS Instead of this: use strict; use warnings; use Test::Exception 0.88; use Test::Differences 0.500; use Test::Deep 0.106; use Test::Warn 0.11; use Test::More tests => 42; You type this: use Test::Most tests => 42; =head1 DESCRIPTION L<Test::Most> exists to reduce boilerplate and to make your testing life easier. We provide "one stop shopping" for most commonly used testing modules. In fact, we often require the latest versions so that you get bug fixes through L<Test::Most> and don't have to keep upgrading these modules separately. This module provides you with the most commonly used testing functions, along with automatically turning on strict and warning and gives you a bit more fine-grained control over your test suite. use Test::Most tests => 4, 'die'; ok 1, 'Normal calls to ok() should succeed'; is 2, 2, '... as should all passing tests'; eq_or_diff [3], [4], '... but failing tests should die'; ok 4, '... will never get to here'; As you can see, the C<eq_or_diff> test will fail. Because 'die' is in the import list, the test program will halt at that point. If you do not want strict and warnings enabled, you must explicitly disable them. Thus, you must be explicit about what you want and no longer need to worry about accidentally forgetting them. use Test::Most tests => 4; no strict; no warnings; =head1 EXPORT All functions from the following modules will automatically be exported into your namespace: =over 4 =item * L<Test::More> =item * L<Test::Exception> =item * L<Test::Differences> =item * L<Test::Deep> =item * L<Test::Warn> =back Functions which are I<optionally> exported from any of those modules must be referred to by their fully-qualified name: Test::Deep::render_stack( $var, $stack ); =head1 FUNCTIONS Several other functions are also automatically exported: =head2 C<die_on_fail> die_on_fail; is_deeply $foo, bar, '... we throw an exception if this fails'; This function, if called, will cause the test program to throw a L<Test::Most::Exception>, effectively halting the test. =head2 C<bail_on_fail> bail_on_fail; is_deeply $foo, bar, '... we bail out if this fails'; This function, if called, will cause the test suite to BAIL_OUT() if any tests fail after it. =head2 C<restore_fail> die_on_fail; is_deeply $foo, bar, '... we throw an exception if this fails'; restore_fail; cmp_bag(\@got, \@bag, '... we will not throw an exception if this fails'; This restores the original test failure behavior, so subsequent tests will no longer throw an exception or BAIL_OUT(). =head2 C<set_failure_handler> If you prefer other behavior to 'die_on_fail' or 'bail_on_fail', you can set your own failure handler: set_failure_handler( sub { my $builder = shift; if ( $builder && $builder->{Test_Results}[-1] =~ /critical/ ) { send_admin_email("critical failure in tests"); } } ); It receives the C<< Test::Builder >> instance as its only argument. B<Important>: Note that if the failing test is the very last test run, then the C<$builder> will likely be undefined. This is an unfortunate side effect of how C<Test::Builder> has been designed. =head2 C<explain> Similar to C<note()>, the output will only be seen by the user by using the C<-v> switch with C<prove> or reading the raw TAP. Unlike C<note()>, any reference in the argument list is automatically expanded using C<Data::Dumper>. Thus, instead of this: my $self = Some::Object->new($id); use Data::Dumper; explain 'I was just created', Dumper($self); You can now just do this: my $self = Some::Object->new($id); explain 'I was just created: ', $self; That output will look similar to: I was just created: bless( { 'id' => 2, 'stack' => [] }, 'Some::Object' ) Note that the "dumpered" output has the C<Data::Dumper> variables C<$Indent>, C<Sortkeys> and C<Terse> all set to the value of C<1> (one). This allows for a much cleaner diagnostic output and at the present time cannot be overridden. Note that Test::More's C<explain> acts differently. This C<explain> is equivalent to C<note explain> in Test::More. =head2 C<show> Experimental. Just like C<explain>, but also tries to show you the lexical variable names: my $var = 3; my @array = qw/ foo bar /; show $var, \@array; __END__ $var = 3; @array = [ 'foo', 'bar' ]; It will show C<$VAR1>, C<$VAR2> ... C<$VAR_N> for every variable it cannot figure out the variable name to: my @array = qw/ foo bar /; show @array; __END__ $VAR1 = 'foo'; $VAR2 = 'bar'; Note that this relies on L<Data::Dumper::Names> version 0.03 or greater. If this is not present, it will warn and call L<explain> instead. Also, it can only show the names for lexical variables. Globals such as C<%ENV> or C<%@> are not accessed via PadWalker and thus cannot be shown. It would be nice to find a workaround for this. =head2 C<always_explain> and C<always_show> These are identical to C<explain> and C<show>, but like L<Test::More>'s C<diag> function, these will always emit output, regardless of whether or not you're in verbose mode. =head2 C<all_done> B<DEPRECATED>. Use the new C<done_testing()> (added in L<Test::More|Test::More> since 0.87_01). Instead. We're leaving this in here for a long deprecation cycle. After a while, we might even start warning. If the plan is specified as C<defer_plan>, you may call C<&all_done> at the end of the test with an optional test number. This lets you set the plan without knowing the plan before you run the tests. If you call it without a test number, the tests will still fail if you don't get to the end of the test. This is useful if you don't want to specify a plan but the tests exit unexpectedly. For example, the following would I<pass> with C<no_plan> but fails with C<all_done>. use Test::More 'defer_plan'; ok 1; exit; ok 2; all_done; See L<Deferred plans> for more information. =head1 EXPORT ON DEMAND The following will be exported only if requested: =head2 C<timeit> Prototype: C<timeit(&;$)> This function will warn if C<Time::HiRes> is not installed. The test will still be run, but no timing information will be displayed. use Test::Most 'timeit'; timeit { is expensive_function(), $some_value, $message } "expensive_function()"; timeit { is expensive_function(), $some_value, $message }; C<timeit> accepts a code reference and an optional message. After the test is run, will C<explain> the time of the function using C<Time::HiRes>. If a message is supplied, it will be formatted as: sprintf "$message: took %s seconds" => $time; Otherwise, it will be formatted as: sprintf "$filename line $line: took %s seconds" => $time; =head1 DIE OR BAIL ON FAIL Sometimes you want your test suite to throw an exception or BAIL_OUT() if a test fails. In order to provide maximum flexibility, there are three ways to accomplish each of these. =head2 Import list use Test::Most 'die', tests => 7; use Test::Most qw< no_plan bail >; If C<die> or C<bail> is anywhere in the import list, the test program/suite will throw a C<Test::Most::Exception> or C<BAIL_OUT()> as appropriate the first time a test fails. Calling C<restore_fail> anywhere in the test program will restore the original behavior (not throwing an exception or bailing out). =head2 Functions use Test::Most 'no_plan'; ok $bar, 'The test suite will continue if this passes'; die_on_fail; is_deeply $foo, bar, '... we throw an exception if this fails'; restore_fail; ok $baz, 'The test suite will continue if this passes'; The C<die_on_fail> and C<bail_on_fail> functions will automatically set the desired behavior at runtime. =head2 Environment variables DIE_ON_FAIL=1 prove t/ BAIL_ON_FAIL=1 prove t/ If the C<DIE_ON_FAIL> or C<BAIL_ON_FAIL> environment variables are true, any tests which use C<Test::Most> will throw an exception or call BAIL_OUT on test failure. =head1 MISCELLANEOUS =head2 Moose It used to be that this module would produce a warning when used with Moose: Prototype mismatch: sub main::blessed ($) vs none This was because L<Test::Deep> exported a C<blessed()> function by default, but its prototype did not match the L<Moose> version's prototype. We now exclude the L<Test::Deep> version by default. If you need it, you can call the fully-qualified version or request it on the command line: use Test::Most 'blessed'; =head2 Excluding Test Modules Sometimes you want a exclude a particular test module. For example, L<Test::Deep>, when used with L<Moose>, produces the following warning: Prototype mismatch: sub main::blessed ($) vs none You can exclude this with by adding the module to the import list with a '-' symbol in front: use Test::Most tests => 42, '-Test::Deep'; See L<https://rt.cpan.org/Ticket/Display.html?id=54362&results=e73ff63c5bf9ba0f796efdba5773cf3f> for more information. =head2 Excluding Test Symbols Sometimes you don't want to exclude an entire test module, but just a particular symbol that is causing issues (e.g. see the 'blessed' example above). You can exclude the symbol(s) in the standard way, by specifying the symbol in the import list with a '!' in front: use Test::Most tests => 42, '!blessed'; =head2 Deferred plans B<DEPRECATED>. Use C<done_testing()> from L<Test::More> instead. use Test::Most qw<defer_plan>; use My::Tests; my $test_count = My::Tests->run; all_done($test_count); Sometimes it's difficult to know the plan up front, but you can calculate the plan as your tests run. As a result, you want to defer the plan until the end of the test. Typically, the best you can do is this: use Test::More 'no_plan'; use My::Tests; My::Tests->run; But when you do that, C<Test::Builder> merely asserts that the number of tests you I<ran> is the number of tests. Until now, there was no way of asserting that the number of tests you I<expected> is the number of tests unless you do so before any tests have run. This fixes that problem. =head2 One-stop shopping We generally require the latest stable versions of various test modules. Why? Because they have bug fixes and new features. You don't want to have to keep remembering them, so periodically we'll release new versions of L<Test::Most> just for bug fixes. =head2 C<use ok> We do not bundle L<Test::use::ok>, though it's been requested. That's because C<use_ok> is broken, but L<Test::use::ok> is also subtly broken (and a touch harder to fix). See L<http://use.perl.org/~Ovid/journal/39859> for more information. If you want to test if you can use a module, just use it. If it fails, the test will still fail and that's the desired result. =head1 RATIONALE People want more control over their test suites. Sometimes when you see hundreds of tests failing and whizzing by, you want the test suite to simply halt on the first failure. This module gives you that control. As for the reasons for the four test modules chosen, I ran code over a local copy of the CPAN to find the most commonly used testing modules. Here's the top twenty as of January 2010 (the numbers are different because we're now counting distributions which use a given module rather than simply the number of times a module is used). 1 Test::More 14111 2 Test 1736 3 Test::Exception 744 4 Test::Simple 331 5 Test::Pod 328 6 Test::Pod::Coverage 274 7 Test::Perl::Critic 248 8 Test::Base 228 9 Test::NoWarnings 155 10 Test::Distribution 142 11 Test::Kwalitee 138 12 Test::Deep 128 13 Test::Warn 127 14 Test::Differences 102 15 Test::Spelling 101 16 Test::MockObject 87 17 Test::Builder::Tester 84 18 Test::WWW::Mechanize::Catalyst 79 19 Test::UseAllModules 63 20 Test::YAML::Meta 61 L<Test::Most> is number 24 on that list, if you're curious. See L<http://blogs.perl.org/users/ovid/2010/01/most-popular-testing-modules---january-2010.html>. The modules chosen seemed the best fit for what C<Test::Most> is trying to do. As of 0.02, we've added L<Test::Warn> by request. It's not in the top ten, but it's a great and useful module. =cut | |||||
438 | ||||||
439 | BEGIN { | |||||
440 | 6 | 35 | @ISA = qw(Test::Builder::Module); | |||
441 | 6 | 552 | @EXPORT = ( | |||
442 | @Test::More::EXPORT, | |||||
443 | qw< | |||||
444 | all_done | |||||
445 | bail_on_fail | |||||
446 | die_on_fail | |||||
447 | explain | |||||
448 | always_explain | |||||
449 | last_test_failed | |||||
450 | restore_fail | |||||
451 | set_failure_handler | |||||
452 | show | |||||
453 | always_show | |||||
454 | > | |||||
455 | ); | |||||
456 | } | |||||
457 | ||||||
458 | sub import { | |||||
459 | 6 | 3 | my $bail_set = 0; | |||
460 | ||||||
461 | 6 24 | 6 30 | my %modules_to_load = map { $_ => 1 } qw/ | |||
462 | Test::Differences | |||||
463 | Test::Exception | |||||
464 | Test::Deep | |||||
465 | Test::Warn | |||||
466 | /; | |||||
467 | 6 | 26 | warnings->import; | |||
468 | 6 | 31 | strict->import; | |||
469 | 6 0 0 6 | 208 0 0 128 | eval "use Data::Dumper::Names 0.03"; | |||
470 | 6 | 9 | $DATA_DUMPER_NAMES_INSTALLED = !$@; | |||
471 | ||||||
472 | 6 | 15 | if ( $ENV{BAIL_ON_FAIL} ) { | |||
473 | 0 | 0 | $bail_set = 1; | |||
474 | 0 | 0 | bail_on_fail(); | |||
475 | } | |||||
476 | 6 | 21 | if ( !$bail_set and $ENV{DIE_ON_FAIL} ) { | |||
477 | 0 | 0 | die_on_fail(); | |||
478 | } | |||||
479 | 6 | 10 | for my $i ( 0 .. $#_ ) { | |||
480 | 6 | 12 | if ( 'bail' eq $_[$i] ) { | |||
481 | 0 | 0 | splice @_, $i, 1; | |||
482 | 0 | 0 | bail_on_fail(); | |||
483 | 0 | 0 | $bail_set = 1; | |||
484 | 0 | 0 | last; | |||
485 | } | |||||
486 | } | |||||
487 | 6 | 7 | my $caller = caller; | |||
488 | 6 | 6 | for my $i ( 0 .. $#_ ) { | |||
489 | 6 | 10 | if ( 'timeit' eq $_[$i] ) { | |||
490 | 0 | 0 | splice @_, $i, 1; | |||
491 | 6 6 6 | 18 4 943 | no strict; | |||
492 | 0 0 | 0 0 | *{"${caller}::timeit"} = \&timeit; | |||
493 | 0 | 0 | last; | |||
494 | } | |||||
495 | } | |||||
496 | ||||||
497 | 6 | 4 | my %exclude_symbol; | |||
498 | 6 | 5 | my $i = 0; | |||
499 | ||||||
500 | 6 6 | 6 12 | if ( grep { $_ eq 'blessed' } @_ ) { | |||
501 | 0 0 | 0 0 | @_ = grep { $_ ne 'blessed' } @_; | |||
502 | } | |||||
503 | else { | |||||
504 | 6 | 6 | $exclude_symbol{blessed} = 1; | |||
505 | } | |||||
506 | 6 | 11 | while ($i < @_) { | |||
507 | 6 | 16 | if ( !$bail_set and ( 'die' eq $_[$i] ) ) { | |||
508 | 0 | 0 | splice @_, $i, 1; | |||
509 | 0 | 0 | die_on_fail(); | |||
510 | 0 | 0 | $i = 0; | |||
511 | 0 | 0 | next; | |||
512 | } | |||||
513 | 6 | 12 | if ( $_[$i] =~ /^-(.*)/ ) { | |||
514 | 0 | 0 | my $module = $1; | |||
515 | 0 | 0 | splice @_, $i, 1; | |||
516 | 0 | 0 | unless (exists $modules_to_load{$module}) { | |||
517 | 0 | 0 | require Carp; | |||
518 | 0 | 0 | Carp::croak("Cannot remove non-existent Test::Module ($module)"); | |||
519 | } | |||||
520 | 0 | 0 | delete $modules_to_load{$module}; | |||
521 | 0 | 0 | $i = 0; | |||
522 | 0 | 0 | next; | |||
523 | } | |||||
524 | 6 | 6 | if ( $_[$i] =~ /^!(.*)/ ) { | |||
525 | 0 | 0 | splice @_, $i, 1; | |||
526 | 0 | 0 | $exclude_symbol{$1} = 1; | |||
527 | 0 | 0 | $i = 0; | |||
528 | 0 | 0 | next; | |||
529 | } | |||||
530 | 6 | 18 | if ( 'defer_plan' eq $_[$i] ) { | |||
531 | 0 | 0 | splice @_, $i, 1; | |||
532 | ||||||
533 | 0 | 0 | my $builder = Test::Builder->new; | |||
534 | 0 | 0 | $builder->{Have_Plan} = 1 | |||
535 | ; # don't like setting this directly, but Test::Builder::has_plan doe | |||||
536 | 0 | 0 | $builder->{TEST_MOST_deferred_plan} = 1; | |||
537 | 0 | 0 | $builder->{TEST_MOST_all_done} = 0; | |||
538 | 0 | 0 | $i = 0; | |||
539 | 0 | 0 | next; | |||
540 | } | |||||
541 | 6 | 7 | $i++; | |||
542 | } | |||||
543 | 6 | 14 | foreach my $module (keys %modules_to_load) { | |||
544 | 6 6 6 6 6 6 6 6 6 6 6 6 24 | 1184 7 170 999 5 249 1047 6 120 2004 9 320 527 | eval "use $module"; | |||
545 | ||||||
546 | 24 | 44 | if ( my $error = $@) { | |||
547 | 0 | 0 | require Carp; | |||
548 | 0 | 0 | Carp::croak($error); | |||
549 | } | |||||
550 | 6 6 6 | 27 9 883 | no strict 'refs'; | |||
551 | # Note: export_to_level would be better here. | |||||
552 | 24 372 24 | 14 250 36 | push @EXPORT => grep { !$exclude_symbol{$_} } @{"${module}::EXPORT"}; | |||
553 | } | |||||
554 | ||||||
555 | # 'magic' goto to avoid updating the callstack | |||||
556 | 6 | 22 | goto &Test::Builder::Module::import; | |||
557 | } | |||||
558 | ||||||
559 | sub explain { | |||||
560 | 0 | 0 | _explain(\&Test::More::note, @_); | |||
561 | } | |||||
562 | ||||||
563 | ||||||
564 | sub timeit(&;$) { | |||||
565 | 0 | 0 | my ( $code, $message ) = @_; | |||
566 | 0 | 0 | unless($HAVE_TIME_HIRES) { | |||
567 | 0 | 0 | Test::Most::diag("timeit: Time::HiRes not installed"); | |||
568 | 0 | 0 | $code->(); | |||
569 | } | |||||
570 | 0 | 0 | if ( !$message ) { | |||
571 | 0 | 0 | my ( $package, $filename, $line ) = caller; | |||
572 | 0 | 0 | $message = "$filename line $line"; | |||
573 | } | |||||
574 | 0 | 0 | my $start = [Time::HiRes::gettimeofday()]; | |||
575 | 0 | 0 | $code->(); | |||
576 | 0 | 0 | explain( | |||
577 | sprintf "$message: took %s seconds" => Time::HiRes::tv_interval($start) ); | |||||
578 | } | |||||
579 | ||||||
580 | sub always_explain { | |||||
581 | 0 | 0 | _explain(\&Test::More::diag, @_); | |||
582 | } | |||||
583 | ||||||
584 | sub _explain { | |||||
585 | 0 | 0 | my $diag = shift; | |||
586 | 6 6 6 | 15 7 517 | no warnings 'once'; | |||
587 | $diag->( | |||||
588 | map { | |||||
589 | 0 | 0 | ref $_ | |||
590 | 0 | 0 | ? do { | |||
591 | 0 | 0 | require Data::Dumper; | |||
592 | 0 | 0 | local $Data::Dumper::Indent = 1; | |||
593 | 0 | 0 | local $Data::Dumper::Sortkeys = 1; | |||
594 | 0 | 0 | local $Data::Dumper::Terse = 1; | |||
595 | 0 | 0 | Data::Dumper::Dumper($_); | |||
596 | } | |||||
597 | : $_ | |||||
598 | } @_ | |||||
599 | ); | |||||
600 | } | |||||
601 | ||||||
602 | sub show { | |||||
603 | 0 | 0 | _show(\&Test::More::note, @_); | |||
604 | } | |||||
605 | ||||||
606 | sub always_show { | |||||
607 | 0 | 0 | _show(\&Test::More::diag, @_); | |||
608 | } | |||||
609 | ||||||
610 | sub _show { | |||||
611 | 0 | 0 | unless ( $DATA_DUMPER_NAMES_INSTALLED ) { | |||
612 | 0 | 0 | require Carp; | |||
613 | 0 | 0 | Carp::carp("Data::Dumper::Names 0.03 not found. Use explain() instead of show()"); | |||
614 | 0 | 0 | goto &_explain; | |||
615 | } | |||||
616 | 0 | 0 | my $diag = shift; | |||
617 | 6 6 6 | 13 5 399 | no warnings 'once'; | |||
618 | 0 | 0 | local $Data::Dumper::Indent = 1; | |||
619 | 0 | 0 | local $Data::Dumper::Sortkeys = 1; | |||
620 | 0 | 0 | local $Data::Dumper::Names::UpLevel = $Data::Dumper::Names::UpLevel + 2; | |||
621 | 0 | 0 | $diag->(Data::Dumper::Names::Dumper(@_)); | |||
622 | } | |||||
623 | ||||||
624 | sub die_on_fail { | |||||
625 | 0 0 | 0 0 | set_failure_handler( sub { throw_failure } ); | |||
626 | } | |||||
627 | ||||||
628 | sub bail_on_fail { | |||||
629 | set_failure_handler( | |||||
630 | 0 0 | 0 0 | sub { Test::More::BAIL_OUT("Test failed. BAIL OUT!.\n") } ); | |||
631 | } | |||||
632 | ||||||
633 | sub restore_fail { | |||||
634 | 6 6 6 | 13 3 285 | no warnings 'redefine'; | |||
635 | 0 | 0 | *Test::Builder::ok = $OK_FUNC; | |||
636 | } | |||||
637 | ||||||
638 | sub all_done { | |||||
639 | 0 | 0 | my $builder = Test::Builder->new; | |||
640 | 0 | 0 | if ($builder->{TEST_MOST_deferred_plan}) { | |||
641 | 0 | 0 | $builder->{TEST_MOST_all_done} = 1; | |||
642 | 0 | 0 | $builder->expected_tests(@_ ? $_[0] : $builder->current_test); | |||
643 | } | |||||
644 | } | |||||
645 | ||||||
646 | ||||||
647 | sub set_failure_handler { | |||||
648 | 0 | 0 | my $action = shift; | |||
649 | 6 6 6 | 14 5 385 | no warnings 'redefine'; | |||
650 | 0 | 0 | Test::Builder->new->{TEST_MOST_failure_action} = $action; # for DESTROY | |||
651 | *Test::Builder::ok = sub { | |||||
652 | 0 | 0 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||
653 | 0 | 0 | my $builder = $_[0]; | |||
654 | 0 | 0 | if ( $builder->{TEST_MOST_test_failed} ) { | |||
655 | 0 | 0 | $builder->{TEST_MOST_test_failed} = 0; | |||
656 | 0 | 0 | $action->($builder); | |||
657 | } | |||||
658 | 0 | 0 | $builder->{TEST_MOST_test_failed} = 0; | |||
659 | 0 | 0 | my $result = $OK_FUNC->(@_); | |||
660 | 0 | 0 | $builder->{TEST_MOST_test_failed} = !( $builder->summary )[-1]; | |||
661 | 0 | 0 | return $result; | |||
662 | 0 | 0 | }; | |||
663 | } | |||||
664 | ||||||
665 | { | |||||
666 | 6 6 6 | 13 5 488 | no warnings 'redefine'; | |||
667 | ||||||
668 | # we need this because if the failure is on the final test, we won't have | |||||
669 | # a subsequent test triggering the behavior. | |||||
670 | sub Test::Builder::DESTROY { | |||||
671 | 0 | 0 | my $builder = $_[0]; | |||
672 | 0 | 0 | if ( $builder->{TEST_MOST_test_failed} ) { | |||
673 | 0 | 0 | ( $builder->{TEST_MOST_failure_action} || sub {} )->(); | |||
674 | } | |||||
675 | } | |||||
676 | } | |||||
677 | ||||||
678 | sub _deferred_plan_handler { | |||||
679 | 6 | 13 | my $builder = Test::Builder->new; | |||
680 | 6 | 41 | if ($builder->{TEST_MOST_deferred_plan} and !$builder->{TEST_MOST_all_done}) | |||
681 | { | |||||
682 | 0 | $builder->expected_tests($builder->current_test + 1); | ||||
683 | } | |||||
684 | } | |||||
685 | ||||||
686 | # This should work because the END block defined by Test::Builder should be | |||||
687 | # guaranteed to be run before t one, since we use'd Test::Builder way up top. | |||||
688 | # The other two alternatives would be either to replace Test::Builder::_ending | |||||
689 | # similar to how we did Test::Builder::ok, or to call Test::Builder::no_ending | |||||
690 | # and basically rewrite _ending in our own image. Neither is very palatable, | |||||
691 | # considering _ending's initial underscore. | |||||
692 | ||||||
693 | END { | |||||
694 | 6 | 441 | _deferred_plan_handler(); | |||
695 | } | |||||
696 | ||||||
697 | 1; | |||||
698 | ||||||
699 - 806 | =head1 AUTHOR Curtis Poe, C<< <ovid at cpan.org> >> =head1 BUGS Please report any bugs or feature requests to C<bug-test-extended at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Most>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. =head1 SUPPORT You can find documentation for this module with the perldoc command. perldoc Test::Most You can also look for information at: =over 4 =item * RT: CPAN's request tracker L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Test-Most> =item * AnnoCPAN: Annotated CPAN documentation L<http://annocpan.org/dist/Test-Most> =item * CPAN Ratings L<http://cpanratings.perl.org/d/Test-Most> =item * Search CPAN L<http://search.cpan.org/dist/Test-Most> =back =head1 TODO =head2 Deferred plans Sometimes you don't know the number of tests you will run when you use C<Test::More>. The C<plan()> function allows you to delay specifying the plan, but you must still call it before the tests are run. This is an error: use Test::More; my $tests = 0; foreach my $test ( my $count = run($test); # assumes tests are being run $tests += $count; } plan($tests); The way around this is typically to use 'no_plan' and when the tests are done, C<Test::Builder> merely sets the plan to the number of tests run. We'd like for the programmer to specify this number instead of letting C<Test::Builder> do it. However, C<Test::Builder> internals are a bit difficult to work with, so we're delaying this feature. =head2 Cleaner skip() if ( $some_condition ) { skip $message, $num_tests; } else { # run those tests } That would be cleaner and I might add it if enough people want it. =head1 CAVEATS Because of how Perl handles arguments, and because diagnostics are not really part of the Test Anything Protocol, what actually happens internally is that we note that a test has failed and we throw an exception or bail out as soon as the I<next> test is called (but before it runs). This means that its arguments are automatically evaluated before we can take action: use Test::Most qw<no_plan die>; ok $foo, 'Die if this fails'; ok factorial(123456), '... but wait a loooong time before you throw an exception'; =head1 ACKNOWLEDGEMENTS Many thanks to C<perl-qa> for arguing about this so much that I just went ahead and did it :) Thanks to Aristotle for suggesting a better way to die or bailout. Thanks to 'swillert' (L<http://use.perl.org/~swillert/>) for suggesting a better implementation of my "dumper explain" idea (L<http://use.perl.org/~Ovid/journal/37004>). =head1 COPYRIGHT & LICENSE Copyright 2008 Curtis Poe, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut | |||||
807 | ||||||
808 | 1; |