NAME
Carp::Assert::More - convenience wrappers around Carp::Assert
VERSION
Version 0.03
$Header: /home/cvs/carp-assert-more/More.pm,v 1.16 2002/08/21 22:53:34 andy Exp $
SYNOPSIS
use Carp::Assert::More;
my $parser = HTML::Parser->new();
assert_isa( $parser, 'HTML::Parser', 'Got back a correct object' );
DESCRIPTION
Carp::Assert::More is a set of wrappers around the Carp::Assert functions to make the habit of writing assertions even easier.
Everything in here is effectively syntactic sugar. There's no technical reason to use
assert_isa( $foo, 'HTML::Lint' );
instead of
assert( defined $foo );
assert( ref($foo) eq 'HTML::Lint' );
other than readability and simplicity of the code.
My intent here is to make common assertions easy so that we as programmers have no excuse to not use them.
TODO
Many more functions. This is just the first proof of concept.
CAVEATS
I haven't specifically done anything to make Carp::Assert::More be backwards compatible with anything besides Perl 5.6.1, much less back to 5.004. Perhaps someone with better testing resources in that area can help me out here.
FUNCTIONS
Please note that there is no assert_string
function. A string is just a non-reference, for which we have assert_nonref
.
assert_fail( [$name] )
Assertion that always fails. assert_fail($msg)
is exactly the same as calling assert(0,$msg)
, but it eliminates that case where you accidentally use assert($msg)
, which of course never fires.
assert_defined( $this [, $name] )
Asserts that $this is defined.
assert_nonref( $this [, $name ] )
Asserts that $this is not undef and not a reference.
assert_nonblank( $this [, $name] )
Asserts that $this is not blank and not a reference.
assert_integer( $this [, $name ] )
Asserts that $this is an integer, which may be zero or negative.
assert_integer( 0 ); # pass
assert_integer( -14 ); # pass
assert_integer( '14.' ); # FAIL
assert_nonzero( $this [, $name ] )
Asserts that the numeric value of $this is not zero.
assert_nonzero( 0 ); # FAIL
assert_nonzero( -14 ); # pass
assert_nonzero( '14.' ); # pass
Asserts that the numeric value of $this is not zero.
assert_nonzero_integer( $this [, $name ] )
Asserts that the numeric value of $this is not zero, and that $this is an integer.
assert_nonzero_integer( 0 ); # FAIL
assert_nonzero_integer( -14 ); # pass
assert_nonzero_integer( '14.' ); # FAIL
assert_isa( $this, $type [, $name ] )
Asserts that $this is an object of type $type.
assert_exists( \%hash, $key [,$name] )
assert_exists( \%hash, \@keylist [,$name] )
Asserts that $key exists in %hash, or that all of the keys in @keylist exist in %this.
assert_exists( \%custinfo, 'name', 'Customer has a name field' );
assert_exists( \%custinfo, [qw( name addr phone )],
'Customer has name, address and phone' );
AUTHOR
Andy Lester <andy@petdance.com>