File Coverage

blib/lib/Test/Mini/Assertions.pm
Criterion Covered Total %
statement 241 244 98.7
branch 72 82 87.8
condition 54 74 72.9
subroutine 49 50 98.0
pod 33 34 97.0
total 449 484 92.7


line stmt bran cond sub pod time code
1             # Raised on Test Error.
2             # @api private
3             package Test::Mini::Exception;
4 4     4   7227 use base 'Exception::Class::Base';
  4         9  
  4         1900  
5              
6             # Raised on Test Failure.
7             # @api private
8             package Test::Mini::Exception::Assert;
9 4     4   24759 use base 'Test::Mini::Exception';
  4         9  
  4         3189  
10              
11             # Raised on Test Skip.
12             # @api private
13             package Test::Mini::Exception::Skip;
14 4     4   27 use base 'Test::Mini::Exception::Assert';
  4         7  
  4         3146  
15              
16             # Basic Assertions for Test::Mini.
17             package Test::Mini::Assertions;
18 4     4   28 use strict;
  4         8  
  4         150  
19 4     4   23 use warnings;
  4         8  
  4         163  
20              
21 4     4   24 use Scalar::Util qw/ looks_like_number refaddr reftype /;
  4         8  
  4         876  
22 4     4   27 use List::Util qw/ min /;
  4         7  
  4         452  
23 4     4   20933 use List::MoreUtils qw/ any /;
  4         5445  
  4         367  
24 4     4   3521 use Data::Inspect;
  4         90637  
  4         1061  
25              
26             # Formats error messages, appending periods and defaulting undefs as
27             # appropriate.
28             #
29             # @param $default [String] The default message to use.
30             # @param $msg [String] A message to use in place of the default.
31             # @return A well-formatted message.
32             sub message {
33             my ($default, $msg) = @_;
34              
35             $msg .= $msg ? ".\n" : '';
36             $msg .= "$default.";
37              
38             return sub { return $msg };
39             }
40              
41             # Dereferences the given argument, if possible.
42             #
43             # @param $ref The argument to dereference.
44             # @return The referenced value or values.
45             sub deref {
46             my ($ref) = @_;
47             return %$ref if reftype($ref) eq 'HASH';
48             return @$ref if reftype($ref) eq 'ARRAY';
49             return $$ref if reftype($ref) eq 'SCALAR';
50             return $$ref if reftype($ref) eq 'REF';
51             return refaddr($ref);
52             }
53              
54             # Produce a more useful string representation of the given argument.
55             #
56             # @param $obj The object to describe.
57             # @return [String] A description of the given object.
58             sub inspect {
59             Data::Inspect->new()->inspect(@_);
60             }
61              
62             my $assertion_count = 0;
63              
64 4     4   4764 use namespace::clean;
  4         129944  
  4         29  
65              
66             # Pulls all of the test-related methods into the calling package.
67             sub import {
68 7     7   216 my ($class) = @_;
69 7         21 my $caller = caller;
70              
71 4     4   1474 no strict 'refs';
  4         15  
  4         28917  
72 7         16 *{"$caller\::count_assertions"} = \&_count_assertions;
  7         55  
73 7         24 *{"$caller\::reset_assertions"} = \&_reset_assertions;
  7         39  
74              
75 7 100       10 my @asserts = grep { /^(assert|refute|skip$|flunk$)/ && defined &{$_} } keys %{"$class\::"};
  294         844  
  238         810  
  7         81  
76              
77 7         41 for my $assertion (@asserts) {
78 238         248 *{"$caller\::$assertion"} = \&{$assertion};
  238         3718  
  238         534  
79             }
80             }
81              
82 49     49   509 sub _count_assertions { return $assertion_count }
83             sub _reset_assertions {
84 54     54   74 my $final_count = $assertion_count;
85 54         79 $assertion_count = 0;
86 54         181 return $final_count;
87             }
88              
89             # @group Exported Functions
90              
91             # Asserts that +$test+ is truthy, and throws a {Test::Mini::Exception::Assert}
92             # if that assertion fails.
93             #
94             # @example
95             # assert 1;
96             # @example
97             # assert 'true', 'Truth should shine clear';
98             #
99             # @param $test The value to test.
100             # @param [String] $msg An optional description.
101             sub assert ($;$) {
102 273     273 1 3434 my ($test, $msg) = @_;
103 273   100     611 $msg ||= 'Assertion failed; no message given.';
104 273 100       582 $msg = $msg->() if ref $msg eq 'CODE';
105              
106 273         337 $assertion_count++;
107              
108 273 100       2829 return 1 if $test;
109              
110 3         50 Test::Mini::Exception::Assert->throw(
111             message => $msg,
112             ignore_package => [__PACKAGE__],
113             );
114             }
115              
116             # Asserts that +$test+ is falsey, and throws a {Test::Mini::Exception::Assert}
117             # if that assertion fails.
118             #
119             # @example
120             # refute 0;
121             # @example
122             # refute undef, 'Deny the untruths';
123             #
124             # @param $test The value to test.
125             # @param [String] $msg An optional description.
126             sub refute ($;$) {
127 107     107 1 254 my ($test, $msg) = @_;
128 107   100     242 $msg ||= 'Refutation failed; no message given.';
129 107         365 return assert(!$test, $msg);
130             }
131              
132             # Asserts that the given code reference returns a truthy value.
133             #
134             # @deprecated This assertion offers little advantage over the base {#assert}.
135             # This will be removed in v2.0.0.
136             #
137             # @example
138             # assert_block { 'true' };
139             # @example
140             # assert_block \&some_sub, 'expected better from &some_sub';
141             #
142             # @param [CODE] $block The code reference to test.
143             # @param [String] $msg An optional description.
144             sub assert_block (&;$) {
145 6     6 1 173 my ($block, $msg) = @_;
146 6         608 warn '#assert_block is deprecated; please use #assert instead.';
147 6 50 33     61 ($msg, $block) = ($block, $msg) if $msg && ref $block ne 'CODE';
148 6         19 $msg = message('Expected block to return true value', $msg);
149 6         19 assert_instance_of($block, 'CODE');
150 6         50 assert($block->(), $msg);
151             }
152              
153             # Asserts that the given code reference returns a falsey value.
154             #
155             # @deprecated This assertion offers little advantage over the base {#refute}.
156             # This will be removed in v2.0.0.
157             #
158             # @example
159             # refute_block { '' };
160             # @example
161             # refute_block \&some_sub, 'expected worse from &some_sub';
162             #
163             # @param [CODE] $block The code reference to test.
164             # @param [String] $msg An optional description.
165             sub refute_block (&;$) {
166 6     6 1 165 my ($block, $msg) = @_;
167 6         496 warn '#refute_block is deprecated; please use #refute instead.';
168 6 50 33     48 ($msg, $block) = ($block, $msg) if $msg && ref $block ne 'CODE';
169 6         18 $msg = message('Expected block to return false value', $msg);
170 6         17 assert_instance_of($block, 'CODE');
171 6         46 refute($block->(), $msg);
172             }
173              
174             # Verifies that the given +$obj+ is capable of responding to the given
175             # +$method+ name.
176             #
177             # @example
178             # assert_can $date, 'day_of_week';
179             # @example
180             # assert_can $time, 'seconds', '$time cannot respond to #seconds';
181             #
182             # @param $obj The object being tested.
183             # @param [String] $method The method name being checked for.
184             # @param [String] $msg An optional description.
185             sub assert_can ($$;$) {
186 24     24 1 92 my ($obj, $method, $msg) = @_;
187 24   50     36 $msg = message("Expected @{[inspect($obj)]} (@{[ref $obj || 'SCALAR']}) to respond to #$method", $msg);
  24         50  
  24         3337  
188 24         260 assert($obj->can($method), $msg);
189             }
190              
191             # Verifies that the given +$obj+ is *not* capable of responding to the given
192             # +$method+ name.
193             #
194             # @example
195             # refute_can $date, 'to_time';
196             # @example
197             # refute_can $time, 'day', '$time cannot respond to #day';
198             #
199             # @param $obj The object being tested.
200             # @param [String] $method The method name being checked.
201             # @param [String] $msg An optional description.
202             sub refute_can ($$;$) {
203 4     4 1 50 my ($obj, $method, $msg) = @_;
204 4   50     8 $msg = message("Expected @{[inspect($obj)]} (@{[ref $obj || 'SCALAR']}) to not respond to #$method", $msg);
  4         9  
  4         364  
205 4         51 refute($obj->can($method), $msg);
206             }
207              
208             # Verifies that the given +$collection+ contains the given +$obj+ as a member.
209             #
210             # @example
211             # assert_contains [qw/ 1 2 3 /], 2;
212             # @example
213             # assert_contains { a => 'b' }, 'a'; # 'b' also contained
214             # @example
215             # assert_contains 'expectorate', 'xp';
216             # @example
217             # assert_contains Collection->new(1, 2, 3), 2; # if Collection->contains(2)
218             #
219             # @param [Array|Hash|String|#contains] $collection The collection to test.
220             # @param $obj The needle to find.
221             # @param [String] $msg An optional description.
222             sub assert_contains ($$;$) {
223 31     31 1 4247 my ($collection, $obj, $msg) = @_;
224 31         50 my $m = message("Expected @{[inspect($collection)]} to contain @{[inspect($obj)]}", $msg);
  31         63  
  31         4992  
225 31 100       282 if (ref $collection eq 'ARRAY') {
    100          
    100          
226 10 50   20   51 my $search = any {defined $obj ? $_ eq $obj : defined $_ } @$collection;
  20         46  
227 10         48 assert($search, $m);
228             }
229             elsif (ref $collection eq 'HASH') {
230 6         28 &assert_contains([%$collection], $obj, $msg);
231             }
232             elsif (ref $collection) {
233 6         17 assert_can($collection, 'contains');
234 6         104 assert($collection->contains($obj), $m);
235             }
236             else {
237 9         41 assert(index($collection, $obj) != -1, $m);
238             }
239             }
240              
241             # Verifies that the given +$collection+ does not contain the given +$obj+ as a
242             # member.
243             #
244             # @example
245             # refute_contains [qw/ 1 2 3 /], 5;
246             # @example
247             # refute_contains { a => 'b' }, 'x';
248             # @example
249             # refute_contains 'expectorate', 'spec';
250             # @example
251             # refute_contains Collection->new(1, 2, 3), 5; # unless Collection->contains(5)
252             #
253             # @param [Array|Hash|String|#contains] $collection The collection to test.
254             # @param $obj The needle to look for.
255             # @param [String] $msg An optional description.
256             sub refute_contains ($$;$) {
257 26     26 1 217 my ($collection, $obj, $msg) = @_;
258 26         35 my $m = message("Expected @{[inspect($collection)]} to not contain @{[inspect($obj)]}", $msg);
  26         64  
  26         4538  
259 26 100       240 if (ref $collection eq 'ARRAY') {
    100          
    100          
260 10 50   20   58 my $search = any {defined $obj ? $_ eq $obj : defined $_ } @$collection;
  20         50  
261 10         42 refute($search, $m);
262             }
263             elsif (ref $collection eq 'HASH') {
264 6         36 &refute_contains([%$collection], $obj, $msg);
265             }
266             elsif (ref $collection) {
267 6         17 assert_can($collection, 'contains');
268 6         80 refute($collection->contains($obj), $m);
269             }
270             else {
271 4         17 refute(index($collection, $obj) != -1, $m);
272             }
273             }
274              
275             # Validates that the given +$obj+ is defined.
276             #
277             # @example
278             # assert_defined $value; # if defined $value
279             #
280             # @param $obj The value to check.
281             # @param [String] $msg An optional description.
282             sub assert_defined ($;$) {
283 12     12 1 111 my ($obj, $msg) = @_;
284 12         19 $msg = message("Expected @{[inspect($obj)]} to be defined", $msg);
  12         21  
285 12         96 assert(defined $obj, $msg);
286             }
287              
288             # Validates that the given +$obj+ is not defined.
289             # @alias #assert_undef
290 6     6 1 99 sub refute_defined ($;$) { goto &assert_undef }
291              
292             # Tests that the supplied code block dies, and fails if it succeeds. If
293             # +$error+ is provided, the error message in +$@+ must contain it.
294             #
295             # @example
296             # assert_dies { die 'LAGHLAGHLAGHL' };
297             # @example
298             # assert_dies { die 'Failure on line 27 in Foo.pm' } 'line 27';
299             #
300             # @param [CODE] $sub The code that should die.
301             # @param [String] $error The (optional) error substring expected.
302             # @param [String] $msg An optional description.
303             sub assert_dies (&;$$) {
304 5     5 1 104 my ($sub, $error, $msg) = @_;
305 5 100       15 $error = '' unless defined $error;
306              
307 5         10 $msg = message("Expected @{[inspect($sub)]} to die matching /$error/", $msg);
  5         12  
308 5         35 my ($full_error, $dies);
309             {
310 5         8 local $@;
  5         6  
311 5         10 $dies = not eval { $sub->(); return 1; };
  5         17  
  2         8  
312 5         14 $full_error = $@;
313             }
314 5         15 assert($dies, $msg);
315 5         391 assert_contains("$full_error", $error);
316             }
317              
318             # Verifies the emptiness of a collection.
319             #
320             # @example
321             # assert_empty [];
322             # @example
323             # assert_empty {};
324             # @example
325             # assert_empty '';
326             # @example
327             # assert_empty Collection->new(); # if Collection->new()->is_empty()
328             #
329             # @param [Array|Hash|String|#is_empty] $collection The collection under scrutiny.
330             # @param [String] $msg An optional description.
331             sub assert_empty ($;$) {
332 10     10 1 280 my ($collection, $msg) = @_;
333 10         15 $msg = message("Expected @{[inspect($collection)]} to be empty", $msg);
  10         22  
334 10 100       95 if (ref $collection eq 'ARRAY') {
    100          
    100          
335 2         7 refute(@$collection, $msg);
336             }
337             elsif (ref $collection eq 'HASH') {
338 2         9 refute(keys %$collection, $msg);
339             }
340             elsif (ref $collection) {
341 4         13 assert_can($collection, 'is_empty');
342 4         66 assert($collection->is_empty(), $msg);
343             }
344             else {
345 2         6 refute(length $collection, $msg);
346             }
347             }
348              
349             # Verifies the non-emptiness of a collection.
350             #
351             # @example
352             # refute_empty [ 1 ];
353             # @example
354             # refute_empty { a => 1 };
355             # @example
356             # refute_empty 'full';
357             # @example
358             # refute_empty Collection->new(); # unless Collection->new()->is_empty()
359             #
360             # @param [Array|Hash|String|#is_empty] $collection The collection under scrutiny.
361             # @param [String] $msg An optional description.
362             sub refute_empty ($;$) {
363 10     10 1 265 my ($collection, $msg) = @_;
364 10         17 $msg = message("Expected @{[inspect($collection)]} to not be empty", $msg);
  10         21  
365 10 100       108 if (ref $collection eq 'ARRAY') {
    100          
    100          
366 2         8 assert(@$collection, $msg);
367             }
368             elsif (ref $collection eq 'HASH') {
369 2         10 assert(keys %$collection, $msg);
370             }
371             elsif (ref $collection) {
372 4         14 assert_can($collection, 'is_empty');
373 4         64 refute($collection->is_empty(), $msg);
374             }
375             else {
376 2         12 assert(length $collection, $msg);
377             }
378             }
379              
380             # Checks two given arguments for equality.
381             # @alias #assert_equal
382 25     25 1 567 sub assert_eq { goto &assert_equal }
383              
384             # Checks two given arguments for inequality.
385             # @alias #refute_equal
386 25     25 1 519 sub refute_eq { goto &refute_equal }
387              
388             # Checks two given arguments for equality.
389             #
390             # @example
391             # assert_equal 3.000, 3;
392             # @example
393             # assert_equal lc('FOO'), 'foo';
394             # @example
395             # assert_equal [qw/ 1 2 3 /], [ 1, 2, 3 ];
396             # @example
397             # assert_equal { a => 'eh' }, { a => 'eh' };
398             # @example
399             # assert_equal Class->new(), $expected; # if $expected->equals(Class->new())
400             #
401             # @param $actual The value under test.
402             # @param $expected The expected value.
403             # @param [String] $msg An optional description.
404             sub assert_equal ($$;$) {
405 74     74 1 2296 my ($actual, $expected, $msg) = @_;
406 74         97 $msg = message("Got @{[inspect($actual)]}\nnot @{[inspect($expected)]}", $msg);
  74         150  
  74         81027  
407              
408 74         657 my @expected = ($expected);
409 74         116 my @actual = ($actual);
410              
411 74         94 my $passed = 1;
412              
413 74   66     325 while ($passed && (@actual || @expected)) {
      66        
414 1156         2417 ($actual, $expected) = (shift(@actual), shift(@expected));
415              
416 1156 100 66     3433 next if ref $actual && ref $expected && refaddr($actual) == refaddr($expected);
      100        
417              
418 1150 100 66     13796 if (UNIVERSAL::can($expected, 'equals')) {
    100 66        
    100 66        
    100 100        
    100 100        
    100          
419 8         29 $passed = $expected->equals($actual);
420             }
421             elsif (ref $actual eq 'ARRAY' && ref $expected eq 'ARRAY') {
422 24         49 $passed = (@$actual == @$expected);
423 24         46 unshift @actual, @$actual;
424 24         98 unshift @expected, @$expected;
425             }
426             elsif (ref $actual eq 'HASH' && ref $expected eq 'HASH') {
427 10         29 $passed = (keys %$actual == keys %$expected);
428 10         352 unshift @actual, map {$_, $actual->{$_} } sort keys %$actual;
  520         1050  
429 10         378 unshift @expected, map {$_, $expected->{$_}} sort keys %$expected;
  520         1253  
430             }
431             elsif (ref $actual && ref $expected) {
432 18         34 $passed = (ref $actual eq ref $expected);
433 18         45 unshift @actual, [ deref($actual) ];
434 18         42 unshift @expected, [ deref($expected) ];
435             }
436             elsif (looks_like_number($actual) && looks_like_number($expected)) {
437 1038         4573 $passed = ($actual == $expected);
438             }
439             elsif (defined $actual && defined $expected) {
440 44         202 $passed = ($actual eq $expected);
441             }
442             else {
443 8   66     54 $passed = !(defined $actual || defined $expected);
444             }
445             }
446              
447 74         315 assert($passed, $msg);
448             }
449              
450             # Checks two given arguments for inequality.
451             #
452             # @example
453             # refute_equal 3.001, 3;
454             # @example
455             # refute_equal lc('FOOL'), 'foo';
456             # @example
457             # refute_equal [qw/ 1 23 /], [ 1, 2, 3 ];
458             # @example
459             # refute_equal { a => 'ae' }, { a => 'eh' };
460             # @example
461             # refute_equal Class->new(), $expected; # unless $expected->equals(Class->new())
462             #
463             # @param $actual The value under test.
464             # @param $expected The tested value.
465             # @param [String] $msg An optional description.
466             sub refute_equal ($$;$) {
467 50     50 1 537 my ($actual, $unexpected, $msg) = @_;
468 50         99 $msg = message("The given values were unexpectedly equal", $msg);
469              
470 50         97 my @unexpected = ($unexpected);
471 50         70 my @actual = ($actual);
472              
473 50         69 my $passed = 1;
474              
475 50   66     295 while ($passed && (@actual || @unexpected)) {
      66        
476 108         187 ($actual, $unexpected) = (shift(@actual), shift(@unexpected));
477              
478 108 100 66     579 next if ref $actual && ref $unexpected && refaddr($actual) == refaddr($unexpected);
      100        
479              
480 104 100 66     1214 if (UNIVERSAL::can($unexpected, 'equals')) {
    100 66        
    100 66        
    100 100        
    100 100        
    100          
481 4         14 $passed = $unexpected->equals($actual);
482             }
483             elsif (ref $actual eq 'ARRAY' && ref $unexpected eq 'ARRAY') {
484 24         37 $passed = (@$actual == @$unexpected);
485 24         48 unshift @actual, @$actual;
486 24         94 unshift @unexpected, @$unexpected;
487             }
488             elsif (ref $actual eq 'HASH' && ref $unexpected eq 'HASH') {
489 8         23 $passed = (keys %$actual == keys %$unexpected);
490 8         27 unshift @actual, %$actual;
491 8         44 unshift @unexpected, %$unexpected;
492             }
493             elsif (ref $actual && ref $unexpected) {
494 18         36 $passed = (ref $actual eq ref $unexpected);
495 18         37 unshift @actual, [ deref($actual) ];
496 18         40 unshift @unexpected, [ deref($unexpected) ];
497             }
498             elsif (looks_like_number($actual) && looks_like_number($unexpected)) {
499 28         135 $passed = ($actual == $unexpected);
500             }
501             elsif (defined $actual && defined $unexpected) {
502 17         64 $passed = ($actual eq $unexpected);
503             }
504             else {
505 5   66     33 $passed = !(defined $actual || defined $unexpected);
506             }
507             }
508              
509 50         120 refute($passed, $msg);
510             }
511              
512             # Checks that the difference between +$actual+ and +$expected+ is less than
513             # +$delta+.
514             #
515             # @example
516             # assert_in_delta 1.001, 1;
517             # @example
518             # assert_in_delta 104, 100, 5;
519             #
520             # @param [Number] $actual The tested value.
521             # @param [Number] $expected The static value.
522             # @param [Number] $delta The expected delta. Defaults to 0.001.
523             # @param [String] $msg An optional description.
524             sub assert_in_delta ($$;$$) {
525 11     11 1 58 my ($actual, $expected, $delta, $msg) = @_;
526 11 50       31 $delta = 0.001 unless defined $delta;
527 11         144 my $n = abs($actual - $expected);
528 11         124 $msg = message("Expected $actual - $expected ($n) to be < $delta", $msg);
529 11         56 assert($delta >= $n, $msg);
530             }
531              
532             # Checks that the difference between +$actual+ and +$expected+ is greater than
533             # +$delta+.
534             #
535             # @example
536             # refute_in_delta 1.002, 1;
537             # @example
538             # refute_in_delta 106, 100, 5;
539             #
540             # @param [Number] $actual The tested value.
541             # @param [Number] $expected The static value.
542             # @param [Number] $delta The delta +$actual+ and +$expected+ are expected to
543             # differ by. Defaults to 0.001.
544             # @param [String] $msg An optional description.
545             sub refute_in_delta ($$;$$) {
546 5     5 1 54 my ($actual, $expected, $delta, $msg) = @_;
547 5 50       16 $delta = 0.001 unless defined $delta;
548 5         12 my $n = abs($actual - $expected);
549 5         42 $msg = message("Expected $actual - $expected ($n) to be > $delta", $msg);
550 5         15 refute($delta >= $n, $msg);
551             }
552              
553             # Checks that the difference between +$actual+ and +$expected+ is less than
554             # a given fraction of the smaller of the two numbers.
555             #
556             # @example
557             # assert_in_epsilon 22.0 / 7.0, Math::Trig::pi;
558             # @example
559             # assert_in_epsilon 220, 200, 0.10
560             #
561             # @param [Number] $actual The tested value.
562             # @param [Number] $expected The static value.
563             # @param [Number] $epsilon The expected tolerance factor. Defaults to 0.001.
564             # @param [String] $msg An optional description.
565             sub assert_in_epsilon ($$;$$) {
566 8     8 1 55 my ($actual, $expected, $epsilon, $msg) = @_;
567 8 100       24 $epsilon = 0.001 unless defined $epsilon;
568 8         41 assert_in_delta(
569             $actual,
570             $expected,
571             min(abs($actual), abs($expected)) * $epsilon,
572             $msg,
573             );
574             }
575              
576             # Checks that the difference between +$actual+ and +$expected+ is greater than
577             # a given fraction of the smaller of the two numbers.
578             #
579             # @example
580             # refute_in_epsilon 21.0 / 7.0, Math::Trig::pi;
581             # @example
582             # refute_in_epsilon 220, 200, 0.20
583             #
584             # @param [Number] $actual The tested value.
585             # @param [Number] $expected The static value.
586             # @param [Number] $epsilon The factor by which +$actual+ and +$expected+ are
587             # expected to differ by. Defaults to 0.001.
588             # @param [String] $msg An optional description.
589             sub refute_in_epsilon ($$;$$) {
590 2     2 1 37 my ($actual, $expected, $epsilon, $msg) = @_;
591 2 100       7 $epsilon = 0.001 unless defined $epsilon;
592 2         15 refute_in_delta(
593             $actual,
594             $expected,
595             min(abs($actual), abs($expected)) * $epsilon,
596             $msg,
597             );
598             }
599              
600             # Verifies that the given +$collection+ contains the given +$obj+ as a member.
601             # @alias #assert_contains
602 10     10 1 203 sub assert_includes ($$;$) { goto &assert_contains }
603              
604             # Verifies that the given +$collection+ does not contain the given +$obj+ as a
605             # member.
606             # @alias #refute_includes
607 10     10 0 212 sub refute_includes ($$;$) { goto &refute_contains }
608              
609             # Validates that the given object is an instance of +$type+.
610             #
611             # @example
612             # assert_instance_of MyApp::Person->new(), 'MyApp::Person';
613             #
614             # @param $obj The instance to check.
615             # @param [Class] $type The type to expect.
616             # @param [String] $msg An optional description.
617             # @see #assert_is_a
618             sub assert_instance_of ($$;$) {
619 14     14 1 82 my ($obj, $type, $msg) = @_;
620 14         23 $msg = message("Expected @{[inspect($obj)]} to be an instance of $type, not @{[ref $obj]}", $msg);
  14         68  
  14         805  
621 14         130 assert(ref $obj eq $type, $msg);
622             }
623              
624             # Validates that +$obj+ inherits from +$type+.
625             #
626             # @example
627             # assert_is_a 'Employee', 'Employee';
628             # @example
629             # assert_is_a Employee->new(), 'Employee';
630             # @example
631             # assert_is_a 'Employee', 'Person'; # assuming Employee->isa('Person')
632             # @example
633             # assert_is_a Employee->new(), 'Person';
634             #
635             # @param $obj The instance or class to check.
636             # @param [Class] $type The expected superclass.
637             # @param [String] $msg An optional description.
638             sub assert_is_a($$;$) {
639 12     12 1 276 my ($obj, $type, $msg) = @_;
640 12         19 $msg = message("Expected @{[inspect($obj)]} to inherit from $type", $msg);
  12         26  
641 12         160 assert($obj->isa($type), $msg);
642             }
643              
644             # Validates that +$obj+ inherits from +$type+.
645             # @alias #assert_is_a
646 6     6 1 128 sub assert_isa { goto &assert_is_a }
647              
648             # Validates that the given +$string+ matches the given +$pattern+.
649             #
650             # @example
651             # assert_match 'Four score and seven years ago...', qr/score/;
652             #
653             # @param [String] $string The string to match.
654             # @param [Regex] $pattern The regular expression to match against.
655             # @param [String] $msg An optional description.
656             sub assert_match ($$;$) {
657 2     2 1 37 my ($string, $pattern, $msg) = @_;
658 2         6 $msg = message("Expected qr/$pattern/ to match against @{[inspect($string)]}", $msg);
  2         6  
659 2         24 assert($string =~ $pattern, $msg);
660             }
661              
662             # Validates that the given +$string+ does not match the given +$pattern+.
663             #
664             # @example
665             # refute_match 'Four score and seven years ago...', qr/score/;
666             #
667             # @param [String] $string The string to match.
668             # @param [Regex] $pattern The regular expression to match against.
669             # @param [String] $msg An optional description.
670             sub refute_match ($$;$) {
671 2     2 1 123 my ($string, $pattern, $msg) = @_;
672 2         6 $msg = message("Expected qr/$pattern/ to fail to match against @{[inspect($string)]}", $msg);
  2         6  
673 2         26 refute($string =~ $pattern, $msg);
674             }
675              
676             # Verifies that the given +$obj+ is capable of responding to the given
677             # +$method+ name.
678             # @alias #assert_can
679 2     2 1 52 sub assert_responds_to ($$;$) { goto &assert_can }
680              
681             # Verifies that the given +$obj+ is *not* capable of responding to the given
682             # +$method+ name.
683             # @alias #refute_can
684 2     2 1 54 sub refute_responds_to ($$;$) { goto &refute_can }
685              
686             # Validates that the given +$obj+ is undefined.
687             #
688             # @example
689             # assert_undef $value; # if not defined $value
690             #
691             # @param $obj The value to check.
692             # @param [String] $msg An optional description.
693             sub assert_undef ($;$) {
694 12     12 1 114 my ($obj, $msg) = @_;
695 12         15 $msg = message("Expected @{[inspect($obj)]} to be undefined", $msg);
  12         21  
696 12         88 refute(defined $obj, $msg);
697             }
698              
699             # Validates that the given +$obj+ is not undefined.
700             # @alias #assert_defined
701 6     6 1 101 sub refute_undef ($;$) { goto &assert_defined }
702              
703             # Allows the current test to be bypassed with an indeterminate status.
704             # @param [String] $msg An optional description.
705             sub skip (;$) {
706 1     1 1 8 my ($msg) = @_;
707 1 50       6 $msg = 'Test skipped; no message given.' unless defined $msg;
708 1 50       5 $msg = $msg->() if ref $msg eq 'CODE';
709 1         23 Test::Mini::Exception::Skip->throw(
710             message => $msg,
711             ignore_package => [__PACKAGE__],
712             );
713             }
714              
715             # Causes the current test to exit immediately with a failing status.
716             # @param [String] $msg An optional description.
717             sub flunk (;$) {
718 0     0 1   my ($msg) = @_;
719 0 0         $msg = 'Epic failure' unless defined $msg;
720 0           assert(0, $msg);
721             }
722              
723             1;