1 package Carp::Assert::More;
8 use vars qw( $VERSION @ISA @EXPORT );
10 *_fail_msg = *Carp::Assert::_fail_msg;
15 Carp::Assert::More - convenience wrappers around Carp::Assert
40 assert_negative_integer
44 assert_nonnegative_integer
47 assert_nonzero_integer
49 assert_positive_integer
55 use Carp::Assert::More;
58 assert_isa( $obj, 'My::Object', 'Got back a correct object' );
62 Carp::Assert::More is a set of wrappers around the L<Carp::Assert> functions
63 to make the habit of writing assertions even easier.
65 Everything in here is effectively syntactic sugar. There's no technical
68 assert_isa( $foo, 'HTML::Lint' );
72 assert( defined $foo );
73 assert( ref($foo) eq 'HTML::Lint' );
75 other than readability and simplicity of the code.
77 My intent here is to make common assertions easy so that we as programmers
78 have no excuse to not use them.
82 I haven't specifically done anything to make Carp::Assert::More be
83 backwards compatible with anything besides Perl 5.6.1, much less back
84 to 5.004. Perhaps someone with better testing resources in that area
87 =head1 SIMPLE ASSERTIONS
89 =head2 assert_is( $string, $match [,$name] )
91 Asserts that I<$string> matches I<$match>.
100 # undef only matches undef
101 return if !defined($string) && !defined($match);
102 assert_defined( $string, $name );
103 assert_defined( $match, $name );
105 return if $string eq $match;
108 &Carp::confess( _fail_msg($name) );
111 =head2 assert_isnt( $string, $unmatch [,$name] )
113 Asserts that I<$string> does NOT match I<$unmatch>.
117 sub assert_isnt($$;$) {
122 # undef only matches undef
123 return if defined($string) xor defined($unmatch);
125 return if defined($string) && defined($unmatch) && ($string ne $unmatch);
128 &Carp::confess( _fail_msg($name) );
131 =head2 assert_like( $string, qr/regex/ [,$name] )
133 Asserts that I<$string> matches I<qr/regex/>.
137 sub assert_like($$;$) {
142 assert_nonref( $string, $name );
143 assert_isa( $regex, 'Regexp', $name );
144 return if $string =~ $regex;
147 &Carp::confess( _fail_msg($name) );
150 =head2 assert_defined( $this [, $name] )
152 Asserts that I<$this> is defined.
156 sub assert_defined($;$) {
157 return if defined( $_[0] );
160 &Carp::confess( _fail_msg($_[1]) );
163 =head2 assert_nonblank( $this [, $name] )
165 Asserts that I<$this> is not blank and not a reference.
169 sub assert_nonblank($;$) {
173 assert_nonref( $this, $name );
174 return if $this ne "";
177 &Carp::confess( _fail_msg($_[1]) );
180 =head1 NUMERIC ASSERTIONS
182 =head2 assert_integer( $this [, $name ] )
184 Asserts that I<$this> is an integer, which may be zero or negative.
186 assert_integer( 0 ); # pass
187 assert_integer( -14 ); # pass
188 assert_integer( '14.' ); # FAIL
192 sub assert_integer($;$) {
196 assert_nonref( $this, $name );
197 return if $this =~ /^-?\d+$/;
200 &Carp::confess( _fail_msg($name) );
203 =head2 assert_nonzero( $this [, $name ] )
205 Asserts that the numeric value of I<$this> is not zero.
207 assert_nonzero( 0 ); # FAIL
208 assert_nonzero( -14 ); # pass
209 assert_nonzero( '14.' ); # pass
211 Asserts that the numeric value of I<$this> is not zero.
215 sub assert_nonzero($;$) {
220 return if $this+0 != 0;
223 &Carp::confess( _fail_msg($name) );
226 =head2 assert_positive( $this [, $name ] )
228 Asserts that the numeric value of I<$this> is greater than zero.
230 assert_positive( 0 ); # FAIL
231 assert_positive( -14 ); # FAIL
232 assert_positive( '14.' ); # pass
236 sub assert_positive($;$) {
241 return if $this+0 > 0;
244 &Carp::confess( _fail_msg($name) );
247 =head2 assert_nonnegative( $this [, $name ] )
249 Asserts that the numeric value of I<$this> is greater than or equal
250 to zero. Since non-numeric strings evaluate to zero, this means that
251 any non-numeric string will pass.
253 assert_nonnegative( 0 ); # pass
254 assert_nonnegative( -14 ); # FAIL
255 assert_nonnegative( '14.' ); # pass
256 assert_nonnegative( 'dog' ); # pass
260 sub assert_nonnegative($;$) {
265 return if $this+0 >= 0;
268 &Carp::confess( _fail_msg($name) );
271 =head2 assert_negative( $this [, $name ] )
273 Asserts that the numeric value of I<$this> is less than zero.
275 assert_negative( 0 ); # FAIL
276 assert_negative( -14 ); # pass
277 assert_negative( '14.' ); # FAIL
281 sub assert_negative($;$) {
286 return if $this+0 < 0;
289 &Carp::confess( _fail_msg($name) );
292 =head2 assert_nonzero_integer( $this [, $name ] )
294 Asserts that the numeric value of I<$this> is not zero, and that I<$this>
297 assert_nonzero_integer( 0 ); # FAIL
298 assert_nonzero_integer( -14 ); # pass
299 assert_nonzero_integer( '14.' ); # FAIL
303 sub assert_nonzero_integer($;$) {
307 assert_nonzero( $this, $name );
308 assert_integer( $this, $name );
311 =head2 assert_positive_integer( $this [, $name ] )
313 Asserts that the numeric value of I<$this> is greater than zero, and
314 that I<$this> is an integer.
316 assert_positive_integer( 0 ); # FAIL
317 assert_positive_integer( -14 ); # FAIL
318 assert_positive_integer( '14.' ); # FAIL
319 assert_positive_integer( '14' ); # pass
323 sub assert_positive_integer($;$) {
327 assert_positive( $this, $name );
328 assert_integer( $this, $name );
331 =head2 assert_nonnegative_integer( $this [, $name ] )
333 Asserts that the numeric value of I<$this> is not less than zero, and
334 that I<$this> is an integer.
336 assert_nonnegative_integer( 0 ); # pass
337 assert_nonnegative_integer( -14 ); # pass
338 assert_nonnegative_integer( '14.' ); # FAIL
342 sub assert_nonnegative_integer($;$) {
346 assert_nonnegative( $this, $name );
347 assert_integer( $this, $name );
350 =head2 assert_negative_integer( $this [, $name ] )
352 Asserts that the numeric value of I<$this> is less than zero, and that
353 I<$this> is an integer.
355 assert_negative_integer( 0 ); # FAIL
356 assert_negative_integer( -14 ); # pass
357 assert_negative_integer( '14.' ); # FAIL
361 sub assert_negative_integer($;$) {
365 assert_negative( $this, $name );
366 assert_integer( $this, $name );
369 =head1 REFERENCE ASSERTIONS
371 =head2 assert_isa( $this, $type [, $name ] )
373 Asserts that I<$this> is an object of type I<$type>.
377 sub assert_isa($$;$) {
382 assert_defined( $this, $name );
384 # The assertion is true if
385 # 1) For objects, $this is of class $type or of a subclass of $type
386 # 2) For non-objects, $this is a reference to a HASH, SCALAR, ARRAY, etc.
388 require Scalar::Util;
390 return if Scalar::Util::blessed( $this ) && $this->isa( $type );
391 return if ref($this) eq $type;
394 &Carp::confess( _fail_msg($name) );
398 =head2 assert_nonempty( $this [, $name ] )
400 I<$this> must be a ref to either a hash or an array. Asserts that that
401 collection contains at least 1 element. Will assert (with its own message,
402 not I<$name>) unless given a hash or array ref. It is OK if I<$this> has
403 been blessed into objecthood, but the semantics of checking an object to see
404 if it has keys (for a hashref) or returns >0 in scalar context (for an array
405 ref) may not be what you want.
407 assert_nonempty( 0 ); # FAIL
408 assert_nonempty( 'foo' ); # FAIL
409 assert_nonempty( undef ); # FAIL
410 assert_nonempty( {} ); # FAIL
411 assert_nonempty( [] ); # FAIL
412 assert_nonempty( {foo=>1} );# pass
413 assert_nonempty( [1,2,3] ); # pass
417 sub assert_nonempty($;$) {
422 if ( $type eq "HASH" ) {
423 assert_positive( scalar keys %$ref, $name );
425 elsif ( $type eq "ARRAY" ) {
426 assert_positive( scalar @$ref, $name );
429 assert_fail( "Not an array or hash reference" );
433 =head2 assert_nonref( $this [, $name ] )
435 Asserts that I<$this> is not undef and not a reference.
439 sub assert_nonref($;$) {
443 assert_defined( $this, $name );
444 return unless ref( $this );
447 &Carp::confess( _fail_msg($name) );
450 =head2 assert_hashref( $ref [,$name] )
452 Asserts that I<$ref> is defined, and is a reference to a (possibly empty) hash.
454 B<NB:> This method returns I<false> for objects, even those whose underlying
455 data is a hashref. This is as it should be, under the assumptions that:
461 you shouldn't rely on the underlying data structure of a particular class, and
465 you should use C<assert_isa> instead.
471 sub assert_hashref($;$) {
475 return assert_isa( $ref, 'HASH', $name );
478 =head2 assert_listref( $ref [,$name] )
480 Asserts that I<$ref> is defined, and is a reference to a (possibly empty) list.
482 B<NB:> The same caveat about objects whose underlying structure is a
483 hash (see C<assert_hashref>) applies here; this method returns false
484 even for objects whose underlying structure is an array.
488 sub assert_listref($;$) {
492 return assert_isa( $ref, 'ARRAY', $name );
495 =head1 SET AND HASH MEMBERSHIP
497 =head2 assert_in( $string, \@inlist [,$name] );
499 Asserts that I<$string> is defined and matches one of the elements
502 I<\@inlist> must be an array reference of defined strings.
506 sub assert_in($$;$) {
508 my $arrayref = shift;
511 assert_nonref( $string, $name );
512 assert_isa( $arrayref, 'ARRAY', $name );
513 foreach my $element (@{$arrayref}) {
514 assert_nonref( $element, $name );
515 return if $string eq $element;
518 &Carp::confess( _fail_msg($name) );
521 =head2 assert_exists( \%hash, $key [,$name] )
523 =head2 assert_exists( \%hash, \@keylist [,$name] )
525 Asserts that I<%hash> is indeed a hash, and that I<$key> exists in
526 I<%hash>, or that all of the keys in I<@keylist> exist in I<%hash>.
528 assert_exists( \%custinfo, 'name', 'Customer has a name field' );
530 assert_exists( \%custinfo, [qw( name addr phone )],
531 'Customer has name, address and phone' );
535 sub assert_exists($$;$) {
540 assert_isa( $hash, 'HASH', $name );
541 my @list = ref($key) ? @$key : ($key);
544 if ( !exists( $hash->{$_} ) ) {
546 &Carp::confess( _fail_msg($name) );
551 =head2 assert_lacks( \%hash, $key [,$name] )
553 =head2 assert_lacks( \%hash, \@keylist [,$name] )
555 Asserts that I<%hash> is indeed a hash, and that I<$key> does NOT exist
556 in I<%hash>, or that none of the keys in I<@keylist> exist in I<%hash>.
558 assert_lacks( \%users, 'root', 'Root is not in the user table' );
560 assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' );
564 sub assert_lacks($$;$) {
569 assert_isa( $hash, 'HASH', $name );
570 my @list = ref($key) ? @$key : ($key);
573 if ( exists( $hash->{$_} ) ) {
575 &Carp::confess( _fail_msg($name) );
580 =head1 UTILITY ASSERTIONS
582 =head2 assert_fail( [$name] )
584 Assertion that always fails. C<assert_fail($msg)> is exactly the same
585 as calling C<assert(0,$msg)>, but it eliminates that case where you
586 accidentally use C<assert($msg)>, which of course never fires.
590 sub assert_fail(;$) {
592 &Carp::confess( _fail_msg($_[0]) );
598 Copyright (c) 2005 Andy Lester. All rights reserved. This program is
599 free software; you can redistribute it and/or modify it under the same
600 terms as Perl itself.
602 =head1 ACKNOWLEDGEMENTS
614 "I stood on the porch in a tie."