1#!perl -Tw
2
3use warnings;
4use strict;
5
6use Test::More tests => 12;
7use Test::Exception;
8
9use Carp::Assert::More;
10
11use constant PASS => 1;
12use constant FAIL => 0;
13
14my @cases = (
15    [ 0         => FAIL ],
16    [ 'foo'     => FAIL ],
17    [ undef     => FAIL ],
18    [ {}        => PASS ],
19    [ []        => PASS ],
20    [ {foo=>1}  => FAIL ],
21    [ [1,2,3]   => FAIL ],
22);
23
24for my $case ( @cases ) {
25    my ($val,$expected_status) = @$case;
26
27    eval { assert_empty( $val ) };
28    my $desc = 'Checking  ' . ($val // 'undef');
29
30    if ( $expected_status eq FAIL ) {
31        like( $@, qr/Assertion.+failed/, $desc );
32    }
33    else {
34        is( $@, '', $desc );
35    }
36}
37
38NOT_AN_ARRAY: {
39    throws_ok( sub { assert_empty( 27 ) }, qr/Assertion failed!/ );
40}
41
42BLESSED_ARRAY: {
43    my $array_object = bless( [], 'WackyPackage' );
44    lives_ok( sub { assert_empty( $array_object ) } );
45
46    push( @{$array_object}, 14 );
47    throws_ok( sub { assert_empty( $array_object, 'Flooble' ) }, qr/\QAssertion (Flooble) failed!/ );
48}
49
50BLESSED_HASH: {
51    my $hash_object = bless( {}, 'WackyPackage' );
52    lives_ok( sub { assert_empty( $hash_object ) } );
53
54    $hash_object->{foo} = 14;
55    throws_ok( sub { assert_empty( $hash_object, 'Flargle' ) }, qr/\QAssertion (Flargle) failed!/ );
56}
57
58exit 0;
59