File Coverage

blib/lib/Carp/Assert/More.pm
Criterion Covered Total %
statement 494 501 98.6
branch 266 274 97.0
condition 174 248 70.1
subroutine 57 58 98.2
pod 50 50 100.0
total 1041 1131 92.0


line stmt bran cond sub pod time code
1             package Carp::Assert::More;
2              
3 54     54   7873814 use 5.010;
  54         221  
4 54     54   371 use strict;
  54         134  
  54         3409  
5 54     54   360 use warnings;
  54         118  
  54         3941  
6              
7 54     54   28650 use parent 'Exporter';
  54         18774  
  54         416  
8 54     54   4670 use Scalar::Util qw( looks_like_number );
  54         173  
  54         12334  
9              
10 54     54   370 use vars qw( $VERSION @ISA @EXPORT );
  54         106  
  54         110489  
11              
12             =head1 NAME
13              
14             Carp::Assert::More - Convenience assertions for common situations
15              
16             =head1 VERSION
17              
18             Version 2.9.0
19              
20             =cut
21              
22             our $VERSION = '2.9.0';
23             our @EXPORT = qw(
24             assert
25             assert_all_keys_in
26             assert_and
27             assert_aoh
28             assert_arrayref
29             assert_arrayref_nonempty
30             assert_arrayref_nonempty_of
31             assert_arrayref_of
32             assert_arrayref_all
33             assert_cmp
34             assert_coderef
35             assert_context_list
36             assert_context_nonvoid
37             assert_context_scalar
38             assert_context_void
39             assert_datetime
40             assert_defined
41             assert_empty
42             assert_exists
43             assert_fail
44             assert_hashref
45             assert_hashref_nonempty
46             assert_in
47             assert_integer
48             assert_integer_between
49             assert_is
50             assert_isa
51             assert_isa_in
52             assert_isnt
53             assert_keys_are
54             assert_lacks
55             assert_like
56             assert_listref
57             assert_negative
58             assert_negative_integer
59             assert_nonblank
60             assert_nonempty
61             assert_nonnegative
62             assert_nonnegative_integer
63             assert_nonref
64             assert_nonzero
65             assert_nonzero_integer
66             assert_numeric
67             assert_numeric_between
68             assert_or
69             assert_positive
70             assert_positive_integer
71             assert_regex
72             assert_undefined
73             assert_unlike
74             assert_xor
75             );
76              
77             my $INTEGER = qr/^-?\d+$/;
78              
79             =head1 SYNOPSIS
80              
81             A set of convenience functions for common assertions.
82              
83             use Carp::Assert::More;
84              
85             my $obj = My::Object;
86             assert_isa( $obj, 'My::Object', 'Got back a correct object' );
87              
88             =head1 DESCRIPTION
89              
90             Carp::Assert::More is a convenient set of assertions to make the habit
91             of writing assertions even easier.
92              
93             Everything in here is effectively syntactic sugar. There's no technical
94             difference between calling one of these functions:
95              
96             assert_datetime( $foo );
97             assert_isa( $foo, 'DateTime' );
98              
99             that are provided by Carp::Assert::More and calling these assertions
100             from Carp::Assert
101              
102             assert( defined $foo );
103             assert( ref($foo) eq 'DateTime' );
104              
105             My intent here is to make common assertions easy so that we as programmers
106             have no excuse to not use them.
107              
108             =head1 SIMPLE ASSERTIONS
109              
110             =head2 assert( $condition [, $name] )
111              
112             Asserts that C<$condition> is a true value. This is the same as C
113             in C, provided as a convenience.
114              
115             =cut
116              
117             sub assert($;$) {
118 10     10 1 240011 my $condition = shift;
119 10         20 my $name = shift;
120              
121 10 100       46 return if $condition;
122              
123 3         20 require Carp;
124 3         13 &Carp::confess( _failure_msg($name) );
125             }
126              
127              
128             =head2 assert_is( $string, $match [,$name] )
129              
130             Asserts that I<$string> is the same string value as I<$match>.
131              
132             C is not converted to an empty string. If both strings are
133             C, they match. If only one string is C, they don't match.
134              
135             =cut
136              
137             sub assert_is($$;$) {
138 8     8 1 266574 my $string = shift;
139 8         13 my $match = shift;
140 8         15 my $name = shift;
141              
142 8 100       22 if ( defined($string) ) {
143 6 100 100     49 return if defined($match) && ($string eq $match);
144             }
145             else {
146 2 100       12 return if !defined($match);
147             }
148              
149 3         22 require Carp;
150 3         13 &Carp::confess( _failure_msg($name) );
151             }
152              
153              
154             =head2 assert_isnt( $string, $unmatch [,$name] )
155              
156             Asserts that I<$string> does NOT have the same string value as I<$unmatch>.
157              
158             C is not converted to an empty string.
159              
160             =cut
161              
162             sub assert_isnt($$;$) {
163 8     8 1 182206 my $string = shift;
164 8         18 my $unmatch = shift;
165 8         16 my $name = shift;
166              
167             # undef only matches undef
168 8 100 100     58 return if defined($string) xor defined($unmatch);
169              
170 6 100 66     42 return if defined($string) && defined($unmatch) && ($string ne $unmatch);
      100        
171              
172 5         35 require Carp;
173 5         17 &Carp::confess( _failure_msg($name) );
174             }
175              
176              
177             =head2 assert_cmp( $x, $op, $y [,$name] )
178              
179             Asserts that the relation C<$x $op $y> is true. It lets you know why
180             the comparsison failed, rather than simply that it did fail, by giving
181             better diagnostics than a plain C, as well as showing the
182             operands in the stacktrace.
183              
184             Plain C:
185              
186             assert( $nitems <= 10, 'Ten items or fewer in the express lane' );
187              
188             Assertion (Ten items or fewer in the express lane) failed!
189             Carp::Assert::assert("", "Ten items or fewer in the express lane") called at foo.pl line 12
190              
191             With C:
192              
193             assert_cmp( $nitems, '<=', 10, 'Ten items or fewer in the express lane' );
194              
195             Assertion (Ten items or fewer in the express lane) failed!
196             Failed: 14 <= 10
197             Carp::Assert::More::assert_cmp(14, "<=", 10, "Ten items or fewer in the express lane") called at foo.pl line 11
198              
199             The following operators are supported:
200              
201             =over 4
202              
203             =item * == numeric equal
204              
205             =item * != numeric not equal
206              
207             =item * > numeric greater than
208              
209             =item * >= numeric greater than or equal
210              
211             =item * < numeric less than
212              
213             =item * <= numeric less than or equal
214              
215             =item * lt string less than
216              
217             =item * le string less than or equal
218              
219             =item * gt string less than
220              
221             =item * ge string less than or equal
222              
223             =back
224              
225             There is no support for C or C because those already have
226             C and C, respectively.
227              
228             If either C<$x> or C<$y> is undef, the assertion will fail.
229              
230             If the operator is numeric, and C<$x> or C<$y> are not numbers, the assertion will fail.
231              
232             =cut
233              
234             sub assert_cmp($$$;$) {
235 113     113 1 244523 my $x = shift;
236 113         196 my $op = shift;
237 113         141 my $y = shift;
238 113         147 my $name = shift;
239              
240 113         148 my $why;
241              
242 113 100       480 if ( !defined($op) ) {
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
    100          
243 1         2 $why = 'Invalid operator ';
244             }
245             elsif ( $op eq '==' ) {
246 13 100 100     98 return if looks_like_number($x) && looks_like_number($y) && ($x == $y);
      100        
247             }
248             elsif ( $op eq '!=' ) {
249 13 100 100     99 return if looks_like_number($x) && looks_like_number($y) && ($x != $y);
      100        
250             }
251             elsif ( $op eq '<' ) {
252 17 100 100     122 return if looks_like_number($x) && looks_like_number($y) && ($x < $y);
      100        
253             }
254             elsif ( $op eq '<=' ) {
255 17 100 100     140 return if looks_like_number($x) && looks_like_number($y) && ($x <= $y);
      100        
256             }
257             elsif ( $op eq '>' ) {
258 18 100 100     160 return if looks_like_number($x) && looks_like_number($y) && ($x > $y);
      100        
259             }
260             elsif ( $op eq '>=' ) {
261 17 100 100     124 return if looks_like_number($x) && looks_like_number($y) && ($x >= $y);
      100        
262             }
263             elsif ( $op eq 'lt' ) {
264 2 100 33     16 return if defined($x) && defined($y) && ($x lt $y);
      66        
265             }
266             elsif ( $op eq 'le' ) {
267 2 100 33     27 return if defined($x) && defined($y) && ($x le $y);
      66        
268             }
269             elsif ( $op eq 'gt' ) {
270 2 100 33     16 return if defined($x) && defined($y) && ($x gt $y);
      66        
271             }
272             elsif ( $op eq 'ge' ) {
273 2 100 33     19 return if defined($x) && defined($y) && ($x ge $y);
      66        
274             }
275             else {
276 9         15 $why = qq{Invalid operator "$op"};
277             }
278              
279 71   100     444 $why //= "Failed: " . ($x // 'undef') . ' ' . $op . ' ' . ($y // 'undef');
      100        
      66        
280              
281 71         330 require Carp;
282 71         176 &Carp::confess( _failure_msg($name, $why) );
283             }
284              
285              
286             =head2 assert_like( $string, qr/regex/ [,$name] )
287              
288             Asserts that I<$string> matches I.
289              
290             The assertion fails either the string or the regex are undef.
291              
292             =cut
293              
294             sub assert_like($$;$) {
295 7     7 1 221680 my $string = shift;
296 7         15 my $regex = shift;
297 7         14 my $name = shift;
298              
299 7 100 66     42 if ( defined($string) && !ref($string) ) {
300 6 100       14 if ( ref($regex) ) {
301 5 100       72 return if $string =~ $regex;
302             }
303             }
304              
305 4         31 require Carp;
306 4         14 &Carp::confess( _failure_msg($name) );
307             }
308              
309              
310             =head2 assert_unlike( $string, qr/regex/ [,$name] )
311              
312             Asserts that I<$string> matches I.
313              
314             The assertion fails if the regex is undef.
315              
316             =cut
317              
318             sub assert_unlike($$;$) {
319 7     7 1 245073 my $string = shift;
320 7         11 my $regex = shift;
321 7         12 my $name = shift;
322              
323 7 100       26 return if !defined($string);
324              
325 5 100       14 if ( ref($regex) eq 'Regexp' ) {
326 3 100       25 return if $string !~ $regex;
327             }
328              
329 4         24 require Carp;
330 4         12 &Carp::confess( _failure_msg($name) );
331             }
332              
333              
334             =head2 assert_defined( $this [, $name] )
335              
336             Asserts that I<$this> is defined.
337              
338             =cut
339              
340             sub assert_defined($;$) {
341 9 100   9 1 215231 return if defined( $_[0] );
342              
343 2         23 require Carp;
344 2         15 &Carp::confess( _failure_msg($_[1]) );
345             }
346              
347              
348             =head2 assert_undefined( $this [, $name] )
349              
350             Asserts that I<$this> is not defined.
351              
352             =cut
353              
354             sub assert_undefined($;$) {
355 4 100   4 1 215230 return unless defined( $_[0] );
356              
357 3         19 require Carp;
358 3         8 &Carp::confess( _failure_msg($_[1]) );
359             }
360              
361             =head2 assert_nonblank( $this [, $name] )
362              
363             Asserts that I<$this> is not a reference and is not an empty string.
364              
365             =cut
366              
367             sub assert_nonblank($;$) {
368 7     7 1 235678 my $this = shift;
369 7         14 my $name = shift;
370              
371 7         13 my $why;
372 7 100       20 if ( !defined($this) ) {
373 2         5 $why = 'Value is undef.';
374             }
375             else {
376 5 100       13 if ( ref($this) ) {
377 1         31 $why = 'Value is a reference to ' . ref($this) . '.';
378             }
379             else {
380 4 100       19 return if $this ne '';
381 2         6 $why = 'Value is blank.';
382             }
383             }
384              
385 5         37 require Carp;
386 5         17 &Carp::confess( _failure_msg($name, $why) );
387             }
388              
389              
390             =head1 BOOLEAN ASSERTIONS
391              
392             These boolean assertions help make diagnostics more useful.
393              
394             If you use C with a boolean condition:
395              
396             assert( $x && $y, 'Both X and Y should be true' );
397              
398             you can't tell why it failed:
399              
400             Assertion (Both X and Y should be true) failed!
401             at .../Carp/Assert/More.pm line 123
402             Carp::Assert::More::assert(undef, 'Both X and Y should be true') called at foo.pl line 16
403              
404             But if you use C:
405              
406             assert_and( $x, $y, 'Both X and Y should be true' );
407              
408             the stacktrace tells you which half of the expression failed.
409              
410             Assertion (Both X and Y should be true) failed!
411             at .../Carp/Assert/More.pm line 123
412             Carp::Assert::More::assert_and('thing', undef, 'Both X and Y should be true') called at foo.pl line 16
413              
414             =head2 assert_and( $x, $y [, $name] )
415              
416             Asserts that both C<$x> and C<$y> are true.
417              
418             =cut
419              
420             sub assert_and($$;$) {
421 5     5 1 427494 my $x = shift;
422 5         12 my $y = shift;
423 5         10 my $name = shift;
424              
425 5 100 100     29 return if $x && $y;
426              
427 4         32 require Carp;
428 4         15 &Carp::confess( _failure_msg($name) );
429             }
430              
431              
432             =head2 assert_or( $x, $y [, $name] )
433              
434             Asserts that at least one of C<$x> or C<$y> are true.
435              
436             =cut
437              
438             sub assert_or($$;$) {
439 5     5 1 227997 my $x = shift;
440 5         9 my $y = shift;
441 5         7 my $name = shift;
442              
443 5 100 100     29 return if $x || $y;
444              
445 2         14 require Carp;
446 2         8 &Carp::confess( _failure_msg($name) );
447             }
448              
449             =head2 assert_xor( $x, $y [, $name] )
450              
451             Asserts that C<$x> is true, or C<$y> is true, but not both.
452              
453             =cut
454              
455             sub assert_xor($$;$) {
456 4     4 1 182249 my $x = shift;
457 4         6 my $y = shift;
458 4         5 my $name = shift;
459              
460 4 100 100     19 return if $x && !$y;
461 3 100 100     14 return if $y && !$x;
462              
463 2         14 require Carp;
464 2         10 &Carp::confess( _failure_msg($name) );
465             }
466              
467              
468             =head1 NUMERIC ASSERTIONS
469              
470             =head2 assert_numeric( $n [, $name] )
471              
472             Asserts that C<$n> looks like a number, according to C.
473             C will always fail.
474              
475             =cut
476              
477             sub assert_numeric {
478 21     21 1 236860 my $n = shift;
479 21         28 my $name = shift;
480              
481 21 100       96 return if Scalar::Util::looks_like_number( $n );
482              
483 9         48 require Carp;
484 9         19 &Carp::confess( _failure_msg($name) );
485             }
486              
487              
488             =head2 assert_integer( $this [, $name ] )
489              
490             Asserts that I<$this> is an integer, which may be zero or negative.
491              
492             assert_integer( 0 ); # pass
493             assert_integer( 14 ); # pass
494             assert_integer( -14 ); # pass
495             assert_integer( '14.' ); # FAIL
496              
497             =cut
498              
499             sub assert_integer($;$) {
500 20     20 1 249717 my $this = shift;
501 20         40 my $name = shift;
502              
503 20 100       59 if ( defined($this) ) {
504 18 100       180 return if $this =~ $INTEGER;
505             }
506              
507 14         93 require Carp;
508 14         65 &Carp::confess( _failure_msg($name) );
509             }
510              
511              
512             =head2 assert_nonzero( $this [, $name ] )
513              
514             Asserts that the numeric value of I<$this> is defined and is not zero.
515              
516             assert_nonzero( 0 ); # FAIL
517             assert_nonzero( -14 ); # pass
518             assert_nonzero( '14.' ); # pass
519              
520             =cut
521              
522             sub assert_nonzero($;$) {
523 10     10 1 163324 my $this = shift;
524 10         14 my $name = shift;
525              
526 10 100       44 if ( Scalar::Util::looks_like_number($this) ) {
527 5 100       14 return if $this != 0;
528             }
529              
530 6         33 require Carp;
531 6         17 &Carp::confess( _failure_msg($name) );
532             }
533              
534              
535             =head2 assert_positive( $this [, $name ] )
536              
537             Asserts that I<$this> is defined, numeric and greater than zero.
538              
539             assert_positive( 0 ); # FAIL
540             assert_positive( -14 ); # FAIL
541             assert_positive( '14.' ); # pass
542              
543             =cut
544              
545             sub assert_positive($;$) {
546 10     10 1 180539 my $this = shift;
547 10         21 my $name = shift;
548              
549 10 100       48 if ( Scalar::Util::looks_like_number($this) ) {
550 5 100       25 return if ($this+0 > 0);
551             }
552              
553 7         48 require Carp;
554 7         22 &Carp::confess( _failure_msg($name) );
555             }
556              
557              
558             =head2 assert_nonnegative( $this [, $name ] )
559              
560             Asserts that I<$this> is defined, numeric and greater than or equal
561             to zero.
562              
563             assert_nonnegative( 0 ); # pass
564             assert_nonnegative( -14 ); # FAIL
565             assert_nonnegative( '14.' ); # pass
566             assert_nonnegative( 'dog' ); # pass
567              
568             =cut
569              
570             sub assert_nonnegative($;$) {
571 10     10 1 289995 my $this = shift;
572 10         21 my $name = shift;
573              
574 10 100       73 if ( Scalar::Util::looks_like_number( $this ) ) {
575 5 100       22 return if $this >= 0;
576             }
577              
578 6         58 require Carp;
579 6         23 &Carp::confess( _failure_msg($name) );
580             }
581              
582              
583             =head2 assert_negative( $this [, $name ] )
584              
585             Asserts that the numeric value of I<$this> is defined and less than zero.
586              
587             assert_negative( 0 ); # FAIL
588             assert_negative( -14 ); # pass
589             assert_negative( '14.' ); # FAIL
590              
591             =cut
592              
593             sub assert_negative($;$) {
594 10     10 1 201129 my $this = shift;
595 10         21 my $name = shift;
596              
597 54     54   526 no warnings;
  54         186  
  54         274504  
598 10 100 100     77 return if defined($this) && ($this+0 < 0);
599              
600 9         66 require Carp;
601 9         34 &Carp::confess( _failure_msg($name) );
602             }
603              
604              
605             =head2 assert_nonzero_integer( $this [, $name ] )
606              
607             Asserts that the numeric value of I<$this> is defined, an integer, and not zero.
608              
609             assert_nonzero_integer( 0 ); # FAIL
610             assert_nonzero_integer( -14 ); # pass
611             assert_nonzero_integer( '14.' ); # FAIL
612              
613             =cut
614              
615             sub assert_nonzero_integer($;$) {
616 10     10 1 180164 my $this = shift;
617 10         23 my $name = shift;
618              
619 10 100 100     140 if ( defined($this) && ($this =~ $INTEGER) ) {
620 3 100       14 return if $this != 0;
621             }
622              
623 8         110 require Carp;
624 8         30 &Carp::confess( _failure_msg($name) );
625             }
626              
627              
628             =head2 assert_positive_integer( $this [, $name ] )
629              
630             Asserts that the numeric value of I<$this> is defined, an integer and greater than zero.
631              
632             assert_positive_integer( 0 ); # FAIL
633             assert_positive_integer( -14 ); # FAIL
634             assert_positive_integer( '14.' ); # FAIL
635             assert_positive_integer( '14' ); # pass
636              
637             =cut
638              
639             sub assert_positive_integer($;$) {
640 418     418 1 193260 my $this = shift;
641 418         566 my $name = shift;
642              
643 418 100 100     3265 if ( defined($this) && ($this =~ $INTEGER) ) {
644 409 100       834 return if $this > 0;
645             }
646              
647 11         67 require Carp;
648 11         26 &Carp::confess( _failure_msg($name) );
649             }
650              
651              
652             =head2 assert_nonnegative_integer( $this [, $name ] )
653              
654             Asserts that the numeric value of I<$this> is defined, an integer, and not less than zero.
655              
656             assert_nonnegative_integer( 0 ); # pass
657             assert_nonnegative_integer( -14 ); # FAIL
658             assert_nonnegative_integer( '14.' ); # FAIL
659              
660             =cut
661              
662             sub assert_nonnegative_integer($;$) {
663 10     10 1 254439 my $this = shift;
664 10         21 my $name = shift;
665              
666 10 100 100     132 if ( defined($this) && ($this =~ $INTEGER) ) {
667 3 100       13 return if $this >= 0;
668             }
669              
670 8         62 require Carp;
671 8         25 &Carp::confess( _failure_msg($name) );
672             }
673              
674              
675             =head2 assert_negative_integer( $this [, $name ] )
676              
677             Asserts that the numeric value of I<$this> is defined, an integer, and less than zero.
678              
679             assert_negative_integer( 0 ); # FAIL
680             assert_negative_integer( -14 ); # pass
681             assert_negative_integer( '14.' ); # FAIL
682              
683             =cut
684              
685             sub assert_negative_integer($;$) {
686 11     11 1 229888 my $this = shift;
687 11         17 my $name = shift;
688              
689 11 100 100     131 if ( defined($this) && ($this =~ $INTEGER) ) {
690 3 100       11 return if $this < 0;
691             }
692              
693 10         67 require Carp;
694 10         28 &Carp::confess( _failure_msg($name) );
695             }
696              
697              
698             =head2 assert_numeric_between( $n, $lo, $hi [, $name ] )
699              
700             Asserts that the value of I<$this> is defined, numeric and between C<$lo>
701             and C<$hi>, inclusive.
702              
703             assert_numeric_between( 15, 10, 100 ); # pass
704             assert_numeric_between( 10, 15, 100 ); # FAIL
705             assert_numeric_between( 3.14, 1, 10 ); # pass
706              
707             =cut
708              
709             sub assert_numeric_between($$$;$) {
710 7     7 1 152838 my $n = shift;
711 7         11 my $lo = shift;
712 7         11 my $hi = shift;
713 7         8 my $name = shift;
714              
715 7 100       29 if ( Scalar::Util::looks_like_number( $n ) ) {
716 5 100 66     29 return if $lo <= $n && $n <= $hi;
717             }
718              
719 3         18 require Carp;
720 3         9 &Carp::confess( _failure_msg($name) );
721             }
722              
723              
724             =head2 assert_integer_between( $n, $lo, $hi [, $name ] )
725              
726             Asserts that the value of I<$this> is defined, an integer, and between C<$lo>
727             and C<$hi>, inclusive.
728              
729             assert_integer_between( 15, 10, 100 ); # pass
730             assert_integer_between( 10, 15, 100 ); # FAIL
731             assert_integer_between( 3.14, 1, 10 ); # FAIL
732              
733             =cut
734              
735             sub assert_integer_between($$$;$) {
736 7     7 1 213836 my $n = shift;
737 7         16 my $lo = shift;
738 7         13 my $hi = shift;
739 7         14 my $name = shift;
740              
741 7 100 100     113 if ( defined($n) && $n =~ $INTEGER ) {
742 4 100 66     32 return if $lo <= $n && $n <= $hi;
743             }
744              
745 4         29 require Carp;
746 4         16 &Carp::confess( _failure_msg($name) );
747             }
748              
749              
750             =head1 REFERENCE ASSERTIONS
751              
752             =head2 assert_isa( $this, $type [, $name ] )
753              
754             Asserts that I<$this> is an object of type I<$type>.
755              
756             =cut
757              
758             sub assert_isa($$;$) {
759 6     6 1 253879 my $this = shift;
760 6         14 my $type = shift;
761 6         10 my $name = shift;
762              
763             # The assertion is true if
764             # 1) For objects, $this is of class $type or of a subclass of $type
765             # 2) For non-objects, $this is a reference to a HASH, SCALAR, ARRAY, etc.
766              
767 6 100 66     46 return if Scalar::Util::blessed( $this ) && $this->isa( $type );
768 4 100       18 return if ref($this) eq $type;
769              
770 3         25 require Carp;
771 3         41 &Carp::confess( _failure_msg($name) );
772             }
773              
774              
775             =head2 assert_isa_in( $obj, \@types [, $description] )
776              
777             Assert that the blessed C<$obj> isa one of the types in C<\@types>.
778              
779             assert_isa_in( $obj, [ 'My::Foo', 'My::Bar' ], 'Must pass either a Foo or Bar object' );
780              
781             =cut
782              
783             sub assert_isa_in($$;$) {
784 17     17 1 250500 my $obj = shift;
785 17         28 my $types = shift;
786 17         35 my $name = shift;
787              
788 17 100       57 if ( Scalar::Util::blessed($obj) ) {
789 12         20 for ( @{$types} ) {
  12         27  
790 12 100       126 return if $obj->isa($_);
791             }
792             }
793              
794 8         76 require Carp;
795 8         25 &Carp::confess( _failure_msg($name) );
796             }
797              
798              
799             =head2 assert_empty( $this [, $name ] )
800              
801             I<$this> must be a ref to either a hash or an array. Asserts that that
802             collection contains no elements. Will assert (with its own message,
803             not I<$name>) unless given a hash or array ref. It is OK if I<$this> has
804             been blessed into objecthood, but the semantics of checking an object to see
805             if it does not have keys (for a hashref) or returns 0 in scalar context (for
806             an array ref) may not be what you want.
807              
808             assert_empty( 0 ); # FAIL
809             assert_empty( 'foo' ); # FAIL
810             assert_empty( undef ); # FAIL
811             assert_empty( {} ); # pass
812             assert_empty( [] ); # pass
813             assert_empty( {foo=>1} );# FAIL
814             assert_empty( [1,2,3] ); # FAIL
815              
816             =cut
817              
818             sub assert_empty($;$) {
819 13     13 1 238675 my $ref = shift;
820 13         33 my $name = shift;
821              
822 13         21 my $underlying_type;
823 13 100       33 if ( Scalar::Util::blessed( $ref ) ) {
824 6         13 $underlying_type = Scalar::Util::reftype( $ref );
825             }
826             else {
827 7         17 $underlying_type = ref( $ref );
828             }
829              
830 13         23 my $why;
831             my $n;
832 13 100       37 if ( $underlying_type eq 'HASH' ) {
    100          
833 5 100       9 return if scalar keys %{$ref} == 0;
  5         23  
834 3         7 $n = scalar keys %{$ref};
  3         33  
835 3         8 $why = "Hash contains $n key";
836             }
837             elsif ( $underlying_type eq 'ARRAY' ) {
838 5 100       9 return if @{$ref} == 0;
  5         20  
839 3         5 $n = scalar @{$ref};
  3         6  
840 3         7 $why = "Array contains $n element";
841             }
842             else {
843 3         6 $why = 'Argument is not a hash or array.';
844             }
845              
846 9 100 100     42 $why .= 's' if $n && ($n>1);
847 9         16 $why .= '.';
848              
849 9         59 require Carp;
850 9         29 &Carp::confess( _failure_msg($name, $why) );
851             }
852              
853              
854             =head2 assert_nonempty( $this [, $name ] )
855              
856             I<$this> must be a ref to either a hash or an array. Asserts that that
857             collection contains at least 1 element. Will assert (with its own message,
858             not I<$name>) unless given a hash or array ref. It is OK if I<$this> has
859             been blessed into objecthood, but the semantics of checking an object to see
860             if it has keys (for a hashref) or returns >0 in scalar context (for an array
861             ref) may not be what you want.
862              
863             assert_nonempty( 0 ); # FAIL
864             assert_nonempty( 'foo' ); # FAIL
865             assert_nonempty( undef ); # FAIL
866             assert_nonempty( {} ); # FAIL
867             assert_nonempty( [] ); # FAIL
868             assert_nonempty( {foo=>1} );# pass
869             assert_nonempty( [1,2,3] ); # pass
870              
871             =cut
872              
873             sub assert_nonempty($;$) {
874 13     13 1 262750 my $ref = shift;
875 13         25 my $name = shift;
876              
877 13         54 my $underlying_type;
878 13 100       40 if ( Scalar::Util::blessed( $ref ) ) {
879 4         9 $underlying_type = Scalar::Util::reftype( $ref );
880             }
881             else {
882 9         22 $underlying_type = ref( $ref );
883             }
884              
885 13         23 my $why;
886             my $n;
887 13 100       45 if ( $underlying_type eq 'HASH' ) {
    100          
888 4 100       11 return if scalar keys %{$ref} > 0;
  4         24  
889 2         6 $why = "Hash contains 0 keys.";
890             }
891             elsif ( $underlying_type eq 'ARRAY' ) {
892 4 100       6 return if scalar @{$ref} > 0;
  4         21  
893 2         5 $why = "Array contains 0 elements.";
894             }
895             else {
896 5         12 $why = 'Argument is not a hash or array.';
897             }
898              
899 9         62 require Carp;
900 9         32 &Carp::confess( _failure_msg($name, $why) );
901             }
902              
903              
904             =head2 assert_nonref( $this [, $name ] )
905              
906             Asserts that I<$this> is not undef and not a reference.
907              
908             =cut
909              
910             sub assert_nonref($;$) {
911 5     5 1 282610 my $this = shift;
912 5         11 my $name = shift;
913              
914 5         22 assert_defined( $this, $name );
915 4 100       15 return unless ref( $this );
916              
917 1         9 require Carp;
918 1         5 &Carp::confess( _failure_msg($name) );
919             }
920              
921              
922             =head2 assert_hashref( $ref [,$name] )
923              
924             Asserts that I<$ref> is defined, and is a reference to a (possibly empty) hash.
925              
926             B This method returns I for objects, even those whose underlying
927             data is a hashref. This is as it should be, under the assumptions that:
928              
929             =over 4
930              
931             =item (a)
932              
933             you shouldn't rely on the underlying data structure of a particular class, and
934              
935             =item (b)
936              
937             you should use C instead.
938              
939             =back
940              
941             =cut
942              
943             sub assert_hashref($;$) {
944 7     7 1 171549 my $ref = shift;
945 7         10 my $name = shift;
946              
947 7 100 66     37 if ( ref($ref) eq 'HASH' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'HASH' )) ) {
      66        
948 3         13 return;
949             }
950              
951 4         19 require Carp;
952 4         31 &Carp::confess( _failure_msg($name) );
953             }
954              
955              
956             =head2 assert_hashref_nonempty( $ref [,$name] )
957              
958             Asserts that I<$ref> is defined and is a reference to a hash with at
959             least one key/value pair.
960              
961             =cut
962              
963             sub assert_hashref_nonempty($;$) {
964 10     10 1 238503 my $ref = shift;
965 10         18 my $name = shift;
966              
967 10 100 66     97 if ( ref($ref) eq 'HASH' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'HASH' )) ) {
      66        
968 6 100       13 return if scalar keys %{$ref} > 0;
  6         36  
969             }
970              
971 7         55 require Carp;
972 7         20 &Carp::confess( _failure_msg($name) );
973             }
974              
975              
976             =head2 assert_arrayref( $ref [, $name] )
977              
978             =head2 assert_listref( $ref [,$name] )
979              
980             Asserts that I<$ref> is defined, and is a reference to an array, which
981             may or may not be empty.
982              
983             B The same caveat about objects whose underlying structure is a
984             hash (see C) applies here; this method returns false
985             even for objects whose underlying structure is an array.
986              
987             C is an alias for C and may go away in
988             the future. Use C instead.
989              
990             =cut
991              
992             sub assert_arrayref($;$) {
993 13     13 1 500489 my $ref = shift;
994 13         30 my $name = shift;
995              
996 13 100 66     109 if ( ref($ref) eq 'ARRAY' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'ARRAY' )) ) {
      66        
997 5         29 return;
998             }
999              
1000 8         60 require Carp;
1001 8         29 &Carp::confess( _failure_msg($name) );
1002             }
1003             *assert_listref = *assert_arrayref;
1004              
1005              
1006             =head2 assert_arrayref_nonempty( $ref [, $name] )
1007              
1008             Asserts that I<$ref> is reference to an array that has at least one element in it.
1009              
1010             =cut
1011              
1012             sub assert_arrayref_nonempty($;$) {
1013 11     11 1 381196 my $ref = shift;
1014 11         23 my $name = shift;
1015              
1016 11 100 66     90 if ( ref($ref) eq 'ARRAY' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'ARRAY' )) ) {
      66        
1017 7 100       12 return if scalar @{$ref} > 0;
  7         47  
1018             }
1019              
1020 7         56 require Carp;
1021 7         44 &Carp::confess( _failure_msg($name) );
1022             }
1023              
1024              
1025             =head2 assert_arrayref_of( $ref, $type [, $name] )
1026              
1027             Asserts that I<$ref> is reference to an array, and any/all elements are
1028             of type I<$type>.
1029              
1030             For example:
1031              
1032             my @users = get_users();
1033             assert_arrayref_of( \@users, 'My::User' );
1034              
1035             =cut
1036              
1037             sub assert_arrayref_of($$;$) {
1038 10     10 1 251611 my $ref = shift;
1039 10         15 my $type = shift;
1040 10         11 my $name = shift;
1041              
1042 10         16 my $ok;
1043             my @why;
1044              
1045 10 100 33     40 if ( ref($ref) eq 'ARRAY' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'ARRAY' )) ) {
      66        
1046 6         8 my $n = 0;
1047 6         8 for my $i ( @{$ref} ) {
  6         15  
1048 9 100 66     40 if ( !( ( Scalar::Util::blessed( $i ) && $i->isa( $type ) ) || (ref($i) eq $type) ) ) {
      66        
1049 2         6 push @why, "Element #$n is not of type $type";
1050             }
1051 9         10 ++$n;
1052             }
1053 6         9 $ok = !@why;
1054             }
1055              
1056 10 100       17 if ( !$ok ) {
1057 6         33 require Carp;
1058 6         13 &Carp::confess( _failure_msg($name), @why );
1059             }
1060              
1061 4         14 return;
1062             }
1063              
1064              
1065             =head2 assert_arrayref_nonempty_of( $ref, $type [, $name] )
1066              
1067             Asserts that I<$ref> is reference to an array, that it has at least one
1068             element, and that all elements are of type I<$type>.
1069              
1070             This is the same function as C, except that it also
1071             requires at least one element.
1072              
1073             =cut
1074              
1075             sub assert_arrayref_nonempty_of($$;$) {
1076 10     10 1 247700 my $ref = shift;
1077 10         18 my $type = shift;
1078 10         14 my $name = shift;
1079              
1080 10         18 my $ok;
1081             my @why;
1082              
1083 10 100 33     46 if ( ref($ref) eq 'ARRAY' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'ARRAY' )) ) {
      66        
1084 6 100       24 if ( scalar @{$ref} > 0 ) {
  6         20  
1085 4         6 my $n = 0;
1086 4         5 for my $i ( @{$ref} ) {
  4         6  
1087 9 100 66     45 if ( !( ( Scalar::Util::blessed( $i ) && $i->isa( $type ) ) || (ref($i) eq $type) ) ) {
      66        
1088 2         6 push @why, "Element #$n is not of type $type";
1089             }
1090 9         14 ++$n;
1091             }
1092 4         12 $ok = !@why;
1093             }
1094             else {
1095 2         6 push @why, 'Array contains no elements';
1096             }
1097             }
1098              
1099 10 100       23 if ( !$ok ) {
1100 8         48 require Carp;
1101 8         20 &Carp::confess( _failure_msg($name), @why );
1102             }
1103              
1104 2         14 return;
1105             }
1106              
1107              
1108             =head2 assert_arrayref_all( $aref, $sub [, $name] )
1109              
1110             Asserts that I<$aref> is reference to an array that has at least one
1111             element in it. Each element of the array is passed to subroutine I<$sub>
1112             which is assumed to be an assertion.
1113              
1114             For example:
1115              
1116             my $aref_of_counts = get_counts();
1117             assert_arrayref_all( $aref, \&assert_positive_integer, 'Counts are positive' );
1118              
1119             Whatever is passed as I<$name>, a string saying "Element #N" will be
1120             appended, where N is the zero-based index of the array.
1121              
1122             =cut
1123              
1124             sub assert_arrayref_all($$;$) {
1125 11     11 1 283523 my $aref = shift;
1126 11         20 my $sub = shift;
1127 11         17 my $name = shift;
1128              
1129 11         37 my @why;
1130              
1131 11         95 assert_coderef( $sub, 'assert_arrayref_all requires a code reference' );
1132              
1133 10 100 33     42 if ( ref($aref) eq 'ARRAY' || (Scalar::Util::blessed( $aref ) && $aref->isa( 'ARRAY' )) ) {
      66        
1134 6 100       10 if ( @{$aref} ) {
  6         14  
1135 4 100       55 my $inner_msg = defined($name) ? "$name: " : 'assert_arrayref_all: ';
1136 4         8 my $n = 0;
1137 4         6 for my $i ( @{$aref} ) {
  4         10  
1138 407         1029 $sub->( $i, "${inner_msg}Element #$n" );
1139 405         679 ++$n;
1140             }
1141             }
1142             else {
1143 2         5 push @why, 'Array contains no elements';
1144             }
1145             }
1146             else {
1147 4         10 push @why, 'First argument to assert_arrayref_all was not an array';
1148             }
1149              
1150 8 100       21 if ( @why ) {
1151 6         120 require Carp;
1152 6         36 &Carp::confess( _failure_msg($name), @why );
1153             }
1154              
1155 2         13 return;
1156             }
1157              
1158              
1159             =head2 assert_aoh( $ref [, $name ] )
1160              
1161             Verifies that C<$array> is an arrayref, and that every element is a hashref.
1162              
1163             The array C<$array> can be an empty arraref and the assertion will pass.
1164              
1165             =cut
1166              
1167             sub assert_aoh {
1168 8     8 1 266466 my $ref = shift;
1169 8         22 my $name = shift;
1170              
1171 8         16 my $ok = 0;
1172 8 100 66     61 if ( ref($ref) eq 'ARRAY' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'ARRAY' )) ) {
      66        
1173 4         9 $ok = 1;
1174 4         8 for my $val ( @{$ref} ) {
  4         13  
1175 5 50 33     26 if ( not ( ref($val) eq 'HASH' || (Scalar::Util::blessed( $val) && $val->isa( 'HASH' )) ) ) {
      66        
1176 2         10 $ok = 0;
1177 2         7 last;
1178             }
1179             }
1180             }
1181              
1182 8 100       32 return if $ok;
1183              
1184 6         42 require Carp;
1185 6         19 &Carp::confess( _failure_msg($name) );
1186             }
1187              
1188              
1189             =head2 assert_coderef( $ref [,$name] )
1190              
1191             Asserts that I<$ref> is defined, and is a reference to a closure.
1192              
1193             =cut
1194              
1195             sub assert_coderef($;$) {
1196 18     18 1 238523 my $ref = shift;
1197 18         112 my $name = shift;
1198              
1199 18 100 66     110 if ( ref($ref) eq 'CODE' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'CODE' )) ) {
      100        
1200 12         33 return;
1201             }
1202              
1203 6         46 require Carp;
1204 6         22 &Carp::confess( _failure_msg($name) );
1205             }
1206              
1207              
1208             =head2 assert_regex( $ref [,$name] )
1209              
1210             Asserts that I<$ref> is defined, and is a reference to a regex.
1211              
1212             It is functionally the same as C.
1213              
1214             =cut
1215              
1216             sub assert_regex($;$) {
1217 15     15 1 302594 my $ref = shift;
1218 15         61 my $name = shift;
1219              
1220 15 100       56 if ( ref($ref) eq 'Regexp' ) {
1221 6         32 return;
1222             }
1223              
1224 9         66 require Carp;
1225 9         45 &Carp::confess( _failure_msg($name) );
1226             }
1227              
1228              
1229             =head1 TYPE-SPECIFIC ASSERTIONS
1230              
1231             =head2 assert_datetime( $date )
1232              
1233             Asserts that C<$date> is a DateTime object.
1234              
1235             =cut
1236              
1237             sub assert_datetime($;$) {
1238 0     0 1 0 my $ref = shift;
1239 0         0 my $name = shift;
1240              
1241 0 0 0     0 if ( ref($ref) eq 'DateTime' || (Scalar::Util::blessed( $ref ) && $ref->isa( 'DateTime' )) ) {
      0        
1242 0         0 return;
1243             }
1244              
1245 0         0 require Carp;
1246 0         0 &Carp::confess( _failure_msg($name) );
1247             }
1248              
1249              
1250             =head1 SET AND HASH MEMBERSHIP
1251              
1252             =head2 assert_in( $string, \@inlist [,$name] );
1253              
1254             Asserts that I<$string> matches one of the elements of I<\@inlist>.
1255             I<$string> may be undef.
1256              
1257             I<\@inlist> must be an array reference of non-ref strings. If any
1258             element is a reference, the assertion fails.
1259              
1260             =cut
1261              
1262             sub assert_in($$;$) {
1263 12     12 1 249398 my $needle = shift;
1264 12         25 my $haystack = shift;
1265 12         23 my $name = shift;
1266              
1267 12         22 my $found = 0;
1268              
1269             # String has to be a non-ref scalar, or undef.
1270 12 50       40 if ( !ref($needle) ) {
1271              
1272             # Target list has to be an array...
1273 12 100 33     51 if ( ref($haystack) eq 'ARRAY' || (Scalar::Util::blessed( $haystack ) && $haystack->isa( 'ARRAY' )) ) {
      66        
1274              
1275             # ... and all elements have to be non-refs.
1276 10         18 my $elements_ok = 1;
1277 10         16 foreach my $element (@{$haystack}) {
  10         28  
1278 28 100       64 if ( ref($element) ) {
1279 1         1 $elements_ok = 0;
1280 1         3 last;
1281             }
1282             }
1283              
1284             # Now we can actually do the search.
1285 10 100       27 if ( $elements_ok ) {
1286 9 100       23 if ( defined($needle) ) {
1287 7         12 foreach my $element (@{$haystack}) {
  7         14  
1288 17 100       60 if ( $needle eq $element ) {
1289 5         7 $found = 1;
1290 5         14 last;
1291             }
1292             }
1293             }
1294             else {
1295 2         3 foreach my $element (@{$haystack}) {
  2         6  
1296 5 100       14 if ( !defined($element) ) {
1297 1         2 $found = 1;
1298 1         3 last;
1299             }
1300             }
1301             }
1302             }
1303             }
1304             }
1305              
1306 12 100       44 return if $found;
1307              
1308 6         53 require Carp;
1309 6         21 &Carp::confess( _failure_msg($name) );
1310             }
1311              
1312              
1313             =head2 assert_exists( \%hash, $key [,$name] )
1314              
1315             =head2 assert_exists( \%hash, \@keylist [,$name] )
1316              
1317             Asserts that I<%hash> is indeed a hash, and that I<$key> exists in
1318             I<%hash>, or that all of the keys in I<@keylist> exist in I<%hash>.
1319              
1320             assert_exists( \%custinfo, 'name', 'Customer has a name field' );
1321              
1322             assert_exists( \%custinfo, [qw( name addr phone )],
1323             'Customer has name, address and phone' );
1324              
1325             =cut
1326              
1327             sub assert_exists($$;$) {
1328 10     10 1 261196 my $hash = shift;
1329 10         20 my $key = shift;
1330 10         19 my $name = shift;
1331              
1332 10         16 my $ok = 0;
1333              
1334 10 50 0     45 if ( ref($hash) eq 'HASH' || (Scalar::Util::blessed( $hash ) && $hash->isa( 'HASH' )) ) {
      33        
1335 10 100       25 if ( defined($key) ) {
1336 9 100       29 if ( ref($key) eq 'ARRAY' ) {
    100          
1337 5         15 $ok = (@{$key} > 0);
  5         13  
1338 5         9 for ( @{$key} ) {
  5         15  
1339 7 100       32 if ( !exists( $hash->{$_} ) ) {
1340 2         5 $ok = 0;
1341 2         5 last;
1342             }
1343             }
1344             }
1345             elsif ( !ref($key) ) {
1346 3         8 $ok = exists( $hash->{$key} );
1347             }
1348             else {
1349 1         3 $ok = 0;
1350             }
1351             }
1352             }
1353              
1354 10 100       32 return if $ok;
1355              
1356 6         41 require Carp;
1357 6         39 &Carp::confess( _failure_msg($name) );
1358             }
1359              
1360              
1361             =head2 assert_lacks( \%hash, $key [,$name] )
1362              
1363             =head2 assert_lacks( \%hash, \@keylist [,$name] )
1364              
1365             Asserts that I<%hash> is indeed a hash, and that I<$key> does NOT exist
1366             in I<%hash>, or that none of the keys in I<@keylist> exist in I<%hash>.
1367             The list C<@keylist> cannot be empty.
1368              
1369             assert_lacks( \%users, 'root', 'Root is not in the user table' );
1370              
1371             assert_lacks( \%users, [qw( root admin nobody )], 'No bad usernames found' );
1372              
1373             =cut
1374              
1375             sub assert_lacks($$;$) {
1376 6     6 1 264970 my $hash = shift;
1377 6         7 my $key = shift;
1378 6         8 my $name = shift;
1379              
1380 6         7 my $ok = 0;
1381              
1382 6 50 0     20 if ( ref($hash) eq 'HASH' || (Scalar::Util::blessed( $hash ) && $hash->isa( 'HASH' )) ) {
      33        
1383 6 50       10 if ( defined($key) ) {
1384 6 100       14 if ( ref($key) eq 'ARRAY' ) {
    50          
1385 4         3 $ok = (@{$key} > 0);
  4         7  
1386 4         5 for ( @{$key} ) {
  4         7  
1387 6 100       12 if ( exists( $hash->{$_} ) ) {
1388 1         17 $ok = 0;
1389 1         2 last;
1390             }
1391             }
1392             }
1393             elsif ( !ref($key) ) {
1394 2         4 $ok = !exists( $hash->{$key} );
1395             }
1396             else {
1397 0         0 $ok = 0;
1398             }
1399             }
1400             }
1401              
1402 6 100       16 return if $ok;
1403              
1404 3         18 require Carp;
1405 3         7 &Carp::confess( _failure_msg($name) );
1406             }
1407              
1408              
1409             =head2 assert_all_keys_in( \%hash, \@names [, $name ] )
1410              
1411             Asserts that each key in C<%hash> is in the list of C<@names>.
1412              
1413             This is used to ensure that there are no extra keys in a given hash.
1414              
1415             assert_all_keys_in( $obj, [qw( height width depth )], '$obj can only contain height, width and depth keys' );
1416              
1417             You can pass an empty list of C<@names>.
1418              
1419             =cut
1420              
1421             sub assert_all_keys_in($$;$) {
1422 9     9 1 192379 my $hash = shift;
1423 9         13 my $keys = shift;
1424 9         16 my $name = shift;
1425              
1426 9         13 my @why;
1427 9         16 my $ok = 0;
1428 9 100 33     41 if ( ref($hash) eq 'HASH' || (Scalar::Util::blessed( $hash ) && $hash->isa( 'HASH' )) ) {
      66        
1429 8 100       17 if ( ref($keys) eq 'ARRAY' ) {
1430 7         11 $ok = 1;
1431 7         11 my %keys = map { $_ => 1 } @{$keys};
  15         37  
  7         15  
1432 7         14 for my $key ( keys %{$hash} ) {
  7         20  
1433 18 100       40 if ( !exists $keys{$key} ) {
1434 9         12 $ok = 0;
1435 9         30 push @why, qq{Key "$key" is not a valid key.};
1436             }
1437             }
1438             }
1439             else {
1440 1         3 push @why, 'Argument for array of keys is not an arrayref.';
1441             }
1442             }
1443             else {
1444 1         2 push @why, 'Argument for hash is not a hashref.';
1445             }
1446              
1447 9 100       29 return if $ok;
1448              
1449 6         35 require Carp;
1450 6         39 &Carp::confess( _failure_msg($name, @why) );
1451             }
1452              
1453              
1454             =head2 assert_keys_are( \%hash, \@keys [, $name ] )
1455              
1456             Asserts that the keys for C<%hash> are exactly C<@keys>, no more and no less.
1457              
1458             =cut
1459              
1460             sub assert_keys_are($$;$) {
1461 15     15 1 178677 my $hash = shift;
1462 15         32 my $keys = shift;
1463 15         30 my $name = shift;
1464              
1465 15         24 my @why;
1466 15         1063 my $ok = 0;
1467 15 100 33     78 if ( ref($hash) eq 'HASH' || (Scalar::Util::blessed( $hash ) && $hash->isa( 'HASH' )) ) {
      66        
1468 14 100       35 if ( ref($keys) eq 'ARRAY' ) {
1469 13         28 my %keys = map { $_ => 1 } @{$keys};
  38         130  
  13         35  
1470              
1471             # First check all the keys are allowed.
1472 13         26 $ok = 1;
1473 13         27 for my $key ( keys %{$hash} ) {
  13         45  
1474 33 100       80 if ( !exists $keys{$key} ) {
1475 16         25 $ok = 0;
1476 16         48 push @why, qq{Key "$key" is not a valid key.};
1477             }
1478             }
1479              
1480             # Now check that all the valid keys are represented.
1481 13         30 for my $key ( @{$keys} ) {
  13         30  
1482 38 100       103 if ( !exists $hash->{$key} ) {
1483 21         33 $ok = 0;
1484 21         45 push @why, qq{Key "$key" is not in the hash.};
1485             }
1486             }
1487             }
1488             else {
1489 1         5 push @why, 'Argument for array of keys is not an arrayref.';
1490             }
1491             }
1492             else {
1493 1         4 push @why, 'Argument for hash is not a hashref.';
1494             }
1495              
1496 15 100       55 return if $ok;
1497              
1498 12         86 require Carp;
1499 12         37 &Carp::confess( _failure_msg($name, @why) );
1500             }
1501              
1502              
1503             =head1 CONTEXT ASSERTIONS
1504              
1505             =head2 assert_context_nonvoid( [$name] )
1506              
1507             Verifies that the function currently being executed has not been called
1508             in void context. This is to ensure the calling function is not ignoring
1509             the return value of the executing function.
1510              
1511             Given this function:
1512              
1513             sub something {
1514             ...
1515              
1516             assert_context_nonvoid();
1517              
1518             return $important_value;
1519             }
1520              
1521             These calls to C will pass:
1522              
1523             my $val = something();
1524             my @things = something();
1525              
1526             but this will fail:
1527              
1528             something();
1529              
1530             If the C<$name> argument is not passed, a default message of "
1531             must not be called in void context" is provided.
1532              
1533             =cut
1534              
1535             sub assert_context_nonvoid(;$) {
1536 7     7 1 309499 my (undef, undef, undef, $subroutine, undef, $wantarray) = caller(1);
1537              
1538 7 100       64 return if defined($wantarray);
1539              
1540 3   66     20 my $name = $_[0] // "$subroutine must not be called in void context";
1541              
1542 3         25 require Carp;
1543 3         11 &Carp::confess( _failure_msg($name) );
1544             }
1545              
1546              
1547             =head2 assert_context_void( [$name] )
1548              
1549             Verifies that the function currently being executed has been called
1550             in void context. This is for functions that do not return anything
1551             meaningful.
1552              
1553             Given this function:
1554              
1555             sub something {
1556             ...
1557              
1558             assert_context_void();
1559              
1560             return; # No meaningful value.
1561             }
1562              
1563             These calls to C will fail:
1564              
1565             my $val = something();
1566             my @things = something();
1567              
1568             but this will pass:
1569              
1570             something();
1571              
1572             If the C<$name> argument is not passed, a default message of "
1573             must be called in void context" is provided.
1574              
1575             =cut
1576              
1577             sub assert_context_void(;$) {
1578 7     7 1 197433 my (undef, undef, undef, $subroutine, undef, $wantarray) = caller(1);
1579              
1580 7 100       22 return if not defined($wantarray);
1581              
1582 5   66     17 my $name = $_[0] // "$subroutine must be called in void context";
1583              
1584 5         26 require Carp;
1585 5         11 &Carp::confess( _failure_msg($name) );
1586             }
1587              
1588              
1589             =head2 assert_context_scalar( [$name] )
1590              
1591             Verifies that the function currently being executed has been called in
1592             scalar context. This is to ensure the calling function is not ignoring
1593             the return value of the executing function.
1594              
1595             Given this function:
1596              
1597             sub something {
1598             ...
1599              
1600             assert_context_scalar();
1601              
1602             return $important_value;
1603             }
1604              
1605             This call to C will pass:
1606              
1607             my $val = something();
1608              
1609             but these will fail:
1610              
1611             something();
1612             my @things = something();
1613              
1614             If the C<$name> argument is not passed, a default message of "
1615             must be called in scalar context" is provided.
1616              
1617             =cut
1618              
1619             sub assert_context_scalar(;$) {
1620 7     7 1 163832 my (undef, undef, undef, $subroutine, undef, $wantarray) = caller(1);
1621              
1622 7 100 100     32 return if defined($wantarray) && !$wantarray;
1623              
1624 5   66     20 my $name = $_[0] // "$subroutine must be called in scalar context";
1625              
1626 5         28 require Carp;
1627 5         10 &Carp::confess( _failure_msg($name) );
1628             }
1629              
1630              
1631             =head2 assert_context_list( [$name] )
1632              
1633             Verifies that the function currently being executed has been called in
1634             list context.
1635              
1636             Given this function:
1637              
1638             sub something {
1639             ...
1640              
1641             assert_context_scalar();
1642              
1643             return @values;
1644             }
1645              
1646             This call to C will pass:
1647              
1648             my @vals = something();
1649              
1650             but these will fail:
1651              
1652             something();
1653             my $thing = something();
1654              
1655             If the C<$name> argument is not passed, a default message of "
1656             must be called in list context" is provided.
1657              
1658             =cut
1659              
1660             sub assert_context_list(;$) {
1661 7     7 1 163908 my (undef, undef, undef, $subroutine, undef, $wantarray) = caller(1);
1662              
1663 7 100       36 return if $wantarray;
1664              
1665 5   66     19 my $name = shift // "$subroutine must be called in list context";
1666              
1667 5         26 require Carp;
1668 5         10 &Carp::confess( _failure_msg($name) );
1669             }
1670              
1671              
1672             =head1 UTILITY ASSERTIONS
1673              
1674             =head2 assert_fail( [$name] )
1675              
1676             Assertion that always fails. C is exactly the same
1677             as calling C, but it eliminates that case where you
1678             accidentally use C, which of course never fires.
1679              
1680             =cut
1681              
1682             sub assert_fail(;$) {
1683 1     1 1 220772 require Carp;
1684 1         6 &Carp::confess( _failure_msg($_[0]) );
1685             }
1686              
1687              
1688             # Can't call confess() here or the stack trace will be wrong.
1689             sub _failure_msg {
1690 352     352   854 my ($name, @why) = @_;
1691              
1692 352         968 my $msg = 'Assertion';
1693 352 100       960 $msg .= " ($name)" if defined $name;
1694 352         2546 $msg .= " failed!\n";
1695 352         906 $msg .= "$_\n" for @why;
1696              
1697 352         24334 return $msg;
1698             }
1699              
1700              
1701             =head1 COPYRIGHT & LICENSE
1702              
1703             Copyright 2005-2025 Andy Lester
1704              
1705             This program is free software; you can redistribute it and/or modify
1706             it under the terms of the Artistic License version 2.0.
1707              
1708             =head1 ACKNOWLEDGEMENTS
1709              
1710             Thanks to
1711             Eric A. Zarko,
1712             Bob Diss,
1713             Pete Krawczyk,
1714             David Storrs,
1715             Dan Friedman,
1716             Allard Hoeve,
1717             Thomas L. Shinnick,
1718             and Leland Johnson
1719             for code and fixes.
1720              
1721             =cut
1722              
1723             1;