Test-Functional

 view release on metacpan or  search on metacpan

lib/Test/Functional.pm  view on Meta::CPAN

check the result).

In most cases, a test passes if the code block doesn't die, and if the condition
is true (or absent). There is a special condition I<dies> which expects the code
block to die, and fails unless it does so.

Whether the test passes or fails, I<test> returns the value generated by
I<BLOCK>.

=cut
sub test(&$;$) {
    return _test(0, @_);
}

=item B<pretest { BLOCK } [CONDITION,] NAME>

This works like I<test> except that if it fails, it will short-circuit all
testing at the current level. This means that top-level I<pretest> calls will
halt the entire test if they fail. One obvious example for this is:

    BEGIN { pretest { use Foo::Bar } "test-use" }
    test { Foo::Bar::double(2) } eqv(4), "double(2)";
    test { Foo::Bar::double(3) } eqv(6), "double(3)";
    test { Foo::Bar::double(4) } eqv(8), "double(4)";

If the C<use Foo::Bar> fails, the information that all the other tests are
failing is less useful. I<pretest> can also be combined with I<group>
(described later) to short-circuit a small set of related tests.

=cut
sub pretest(&$;$) {
    return _test(1, @_);
}

=item B<notest { BLOCK } [CONDITION,] NAME>

This is has exactly the same semantics as I<test>; the only difference is that
it normally doesn't run. If C<< Test::Functional::Conf->unstable >> is true,
then this test will run, otherwise it won't, and will just return undef.

For test-driven development, it is useful to create failing tests using
I<notest> blocks; this prevents test regression. Once the implementation starts
working I<notest> can be switched to I<test>.

=cut
sub notest(&$;$) {
    if($UNSTABLE) {
        return _test(0, @_);
    } else {
        my $t = __PACKAGE__->builder();
        $t->skip("$_[-1]");
        return undef;
    }
}

# $dies is a special code ref that we can test for equality. this code doesn't

lib/Test/Functional.pm  view on Meta::CPAN

        test { $a->add() } isundef, "not a number";

    } "adder";

If C<< Adder->new >> fails, the rest of the tests aren't producing useful
results, so they will be skipped. See the L<ETHOS> section for a more in-depth
discussion of the package in general, and the implications of test
short-circuiting in particular.

=cut
sub group(&$) {
    my ($func, $name) = @_;

    push(@STACK, $name);
    eval { &$func() };
    pop(@STACK);

    die if $@ && @STACK;
}

=back



( run in 2.388 seconds using v1.01-cache-2.11-cpan-49f99fa48dc )