PrePAN

Sign in to PrePAN

Test::Contract Evaluate Test::More-like assertions in production code

Good

Synopsis

use Test::Contract;

my $c = Test::Contract->new;
$c->is( $answer, 42, "Life, Universe, and Everything" );
$c->like( $user_input, qr/f?o?r?m?a?t?/, "Input as expected");
$c->refute( $what_exactly_went_wrong, $why_we_care_about_it );

if ($c->get_pass) {
    # continue
} else {
    warn $c->get_tap;
};

Description

1) Sometimes I feel like applying a series Test::More-like checks to user input, dynamically loaded plugin, object instance being passed etc. Unfortunately, Test::Builder makes it impossible without turning the whole application into a test script. Hence this OO interface.

2) Instead of using ok($condition, $message) as a foundation, this module uses a refute statement which is an inverted assertion. I.e. if everything is OK, we only need 1 bit of information; if something went wrong, we need more details. Think of Unix programs returning 0 on success and different error codes otherwise.

This way, extending the test arsenal becomes much simpler: a test function may know nothing about the test framework, it ONLY needs to try hard to find a discrepancy in its own arguments (aka pure function). A builder module is available that imports user's test subroutines into the main module.

3) Also supported: subcontracts to group complex checks, functional interface (Test::More compatible), checking that contract is fulfilled to exactly the given extent (useful for testing the test routines themselves).

Comments

Please sign up to post a review.