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 func‐
19       tions to make the habit of writing assertions even easier.
20
21       Everything in here is effectively syntactic sugar.  There's no techni‐
22       cal 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 program‐
34       mers 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
45       Asserts that $string matches $match.
46
47       assert_isnt( $string, $unmatch [,$name] )
48
49       Asserts that $string does NOT match $unmatch.
50
51       assert_like( $string, qr/regex/ [,$name] )
52
53       Asserts that $string matches qr/regex/.
54
55       assert_defined( $this [, $name] )
56
57       Asserts that $this is defined.
58
59       assert_nonblank( $this [, $name] )
60
61       Asserts that $this is not blank and not a reference.
62

NUMERIC ASSERTIONS

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

REFERENCE ASSERTIONS

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

SET AND HASH MEMBERSHIP

195       assert_in( $string, \@inlist [,$name] );
196
197       Asserts that $string is defined and matches one of the elements of
198       \@inlist.
199
200       \@inlist must be an array reference of defined strings.
201
202       assert_exists( \%hash, $key [,$name] )
203
204       assert_exists( \%hash, \@keylist [,$name] )
205
206       Asserts that %hash is indeed a hash, and that $key exists in %hash, or
207       that all of the keys in @keylist exist in %hash.
208
209           assert_exists( \%custinfo, 'name', 'Customer has a name field' );
210
211           assert_exists( \%custinfo, [qw( name addr phone )],
212                                   'Customer has name, address and phone' );
213
214       assert_lacks( \%hash, $key [,$name] )
215
216       assert_lacks( \%hash, \@keylist [,$name] )
217
218       Asserts that %hash is indeed a hash, and that $key does NOT exist in
219       %hash, or that none of the keys in @keylist exist in %hash.
220
221           assert_lacks( \%users, 'root', 'Root is not in the user table' );
222
223           assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' );
224

UTILITY ASSERTIONS

226       assert_fail( [$name] )
227
228       Assertion that always fails.  "assert_fail($msg)" is exactly the same
229       as calling "assert(0,$msg)", but it eliminates that case where you
230       accidentally use "assert($msg)", which of course never fires.
231
233       Copyright (c) 2005 Andy Lester. All rights reserved. This program is
234       free software; you can redistribute it and/or modify it under the same
235       terms as Perl itself.
236

ACKNOWLEDGEMENTS

238       Thanks to Bob Diss, Pete Krawczyk, David Storrs, Dan Friedman, and
239       Allard Hoeve for code and fixes.
240
241
242
243perl v5.8.8                       2005-10-14                           More(3)
Impressum