1More(3)               User Contributed Perl Documentation              More(3)
2
3
4

NAME

6       Carp::Assert::More - convenience wrappers around Carp::Assert
7

VERSION

9       Version 1.12
10

SYNOPSIS

12           use Carp::Assert::More;
13
14           my $obj = My::Object;
15           assert_isa( $obj, 'My::Object', 'Got back a correct object' );
16

DESCRIPTION

18       Carp::Assert::More is a set of wrappers around the Carp::Assert
19       functions to make the habit of writing assertions even easier.
20
21       Everything in here is effectively syntactic sugar.  There's no
22       technical reason to use
23
24           assert_isa( $foo, 'HTML::Lint' );
25
26       instead of
27
28           assert( defined $foo );
29           assert( ref($foo) eq 'HTML::Lint' );
30
31       other than readability and simplicity of the code.
32
33       My intent here is to make common assertions easy so that we as
34       programmers have no excuse to not use them.
35

CAVEATS

37       I haven't specifically done anything to make Carp::Assert::More be
38       backwards compatible with anything besides Perl 5.6.1, much less back
39       to 5.004.  Perhaps someone with better testing resources in that area
40       can help me out here.
41

SIMPLE ASSERTIONS

43   assert_is( $string, $match [,$name] )
44       Asserts that $string matches $match.
45
46   assert_isnt( $string, $unmatch [,$name] )
47       Asserts that $string does NOT match $unmatch.
48
49   assert_like( $string, qr/regex/ [,$name] )
50       Asserts that $string matches qr/regex/.
51
52   assert_defined( $this [, $name] )
53       Asserts that $this is defined.
54
55   assert_nonblank( $this [, $name] )
56       Asserts that $this is not blank and not a reference.
57

NUMERIC ASSERTIONS

59   assert_integer( $this [, $name ] )
60       Asserts that $this is an integer, which may be zero or negative.
61
62           assert_integer( 0 );    # pass
63           assert_integer( -14 );  # pass
64           assert_integer( '14.' );  # FAIL
65
66   assert_nonzero( $this [, $name ] )
67       Asserts that the numeric value of $this is not zero.
68
69           assert_nonzero( 0 );    # FAIL
70           assert_nonzero( -14 );  # pass
71           assert_nonzero( '14.' );  # pass
72
73       Asserts that the numeric value of $this is not zero.
74
75   assert_positive( $this [, $name ] )
76       Asserts that the numeric value of $this is greater than zero.
77
78           assert_positive( 0 );    # FAIL
79           assert_positive( -14 );  # FAIL
80           assert_positive( '14.' );  # pass
81
82   assert_nonnegative( $this [, $name ] )
83       Asserts that the numeric value of $this is greater than or equal to
84       zero.  Since non-numeric strings evaluate to zero, this means that any
85       non-numeric string will pass.
86
87           assert_nonnegative( 0 );    # pass
88           assert_nonnegative( -14 );  # FAIL
89           assert_nonnegative( '14.' );  # pass
90           assert_nonnegative( 'dog' );  # pass
91
92   assert_negative( $this [, $name ] )
93       Asserts that the numeric value of $this is less than zero.
94
95           assert_negative( 0 );       # FAIL
96           assert_negative( -14 );     # pass
97           assert_negative( '14.' );   # FAIL
98
99   assert_nonzero_integer( $this [, $name ] )
100       Asserts that the numeric value of $this is not zero, and that $this is
101       an integer.
102
103           assert_nonzero_integer( 0 );    # FAIL
104           assert_nonzero_integer( -14 );  # pass
105           assert_nonzero_integer( '14.' );  # FAIL
106
107   assert_positive_integer( $this [, $name ] )
108       Asserts that the numeric value of $this is greater than zero, and that
109       $this is an integer.
110
111           assert_positive_integer( 0 );     # FAIL
112           assert_positive_integer( -14 );   # FAIL
113           assert_positive_integer( '14.' ); # FAIL
114           assert_positive_integer( '14' );  # pass
115
116   assert_nonnegative_integer( $this [, $name ] )
117       Asserts that the numeric value of $this is not less than zero, and that
118       $this is an integer.
119
120           assert_nonnegative_integer( 0 );    # pass
121           assert_nonnegative_integer( -14 );  # pass
122           assert_nonnegative_integer( '14.' );  # FAIL
123
124   assert_negative_integer( $this [, $name ] )
125       Asserts that the numeric value of $this is less than zero, and that
126       $this is an integer.
127
128           assert_negative_integer( 0 );    # FAIL
129           assert_negative_integer( -14 );  # pass
130           assert_negative_integer( '14.' );  # FAIL
131

REFERENCE ASSERTIONS

133   assert_isa( $this, $type [, $name ] )
134       Asserts that $this is an object of type $type.
135
136   assert_nonempty( $this [, $name ] )
137       $this must be a ref to either a hash or an array.  Asserts that that
138       collection contains at least 1 element.  Will assert (with its own
139       message, not $name) unless given a hash or array ref.   It is OK if
140       $this has been blessed into objecthood, but the semantics of checking
141       an object to see if it has keys (for a hashref) or returns >0 in scalar
142       context (for an array ref) may not be what you want.
143
144           assert_nonempty( 0 );       # FAIL
145           assert_nonempty( 'foo' );   # FAIL
146           assert_nonempty( undef );   # FAIL
147           assert_nonempty( {} );      # FAIL
148           assert_nonempty( [] );      # FAIL
149           assert_nonempty( {foo=>1} );# pass
150           assert_nonempty( [1,2,3] ); # pass
151
152   assert_nonref( $this [, $name ] )
153       Asserts that $this is not undef and not a reference.
154
155   assert_hashref( $ref [,$name] )
156       Asserts that $ref is defined, and is a reference to a (possibly empty)
157       hash.
158
159       NB: This method returns false for objects, even those whose underlying
160       data is a hashref. This is as it should be, under the assumptions that:
161
162       (a) you shouldn't rely on the underlying data structure of a particular
163           class, and
164
165       (b) you should use "assert_isa" instead.
166
167   assert_listref( $ref [,$name] )
168       Asserts that $ref is defined, and is a reference to a (possibly empty)
169       list.
170
171       NB: The same caveat about objects whose underlying structure is a hash
172       (see "assert_hashref") applies here; this method returns false even for
173       objects whose underlying structure is an array.
174

SET AND HASH MEMBERSHIP

176   assert_in( $string, \@inlist [,$name] );
177       Asserts that $string is defined and matches one of the elements of
178       \@inlist.
179
180       \@inlist must be an array reference of defined strings.
181
182   assert_exists( \%hash, $key [,$name] )
183   assert_exists( \%hash, \@keylist [,$name] )
184       Asserts that %hash is indeed a hash, and that $key exists in %hash, or
185       that all of the keys in @keylist exist in %hash.
186
187           assert_exists( \%custinfo, 'name', 'Customer has a name field' );
188
189           assert_exists( \%custinfo, [qw( name addr phone )],
190                                   'Customer has name, address and phone' );
191
192   assert_lacks( \%hash, $key [,$name] )
193   assert_lacks( \%hash, \@keylist [,$name] )
194       Asserts that %hash is indeed a hash, and that $key does NOT exist in
195       %hash, or that none of the keys in @keylist exist in %hash.
196
197           assert_lacks( \%users, 'root', 'Root is not in the user table' );
198
199           assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' );
200

UTILITY ASSERTIONS

202   assert_fail( [$name] )
203       Assertion that always fails.  "assert_fail($msg)" is exactly the same
204       as calling "assert(0,$msg)", but it eliminates that case where you
205       accidentally use "assert($msg)", which of course never fires.
206
208       Copyright (c) 2005 Andy Lester. All rights reserved. This program is
209       free software; you can redistribute it and/or modify it under the same
210       terms as Perl itself.
211

ACKNOWLEDGEMENTS

213       Thanks to Bob Diss, Pete Krawczyk, David Storrs, Dan Friedman, and
214       Allard Hoeve for code and fixes.
215
216
217
218perl v5.12.0                      2005-10-14                           More(3)
Impressum