1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#!perl
use warnings;
use strict;
use Test::More tests => 10;
use Carp::Assert::More;
use Test::Exception;
my $FAILED = qr/Assertion failed/;
my $api = \&assert_positive_integer;
MAIN: {
# {} is not an arrayref.
throws_ok( sub { assert_arrayref_all( {}, $api ) }, $FAILED );
# A ref to a hash with stuff in it is not an arrayref.
my $ref = { foo => 'foo', bar => 'bar' };
throws_ok( sub { assert_arrayref_all( $ref, $api ) }, $FAILED );
# 3 is not an arrayref.
throws_ok( sub { assert_arrayref_all( 3, $api ) }, $FAILED );
# [] is a nonempty arrayref.
lives_ok( sub { assert_arrayref_all( [ 3 ], $api ) } );
# [] is an empty arrayref.
throws_ok( sub { assert_arrayref_all( [], $api ) }, $FAILED );
my @empty_ary = ();
throws_ok( sub { assert_arrayref_all( \@empty_ary, $api ) }, qr/Array contains no elements/ );
# A coderef is not an arrayref.
my $coderef = sub {};
throws_ok( sub { assert_arrayref_all( $coderef, $api ) }, $FAILED );
# An arrayref is not a coderef.
throws_ok( sub { assert_arrayref_all( \@empty_ary, [] ) }, qr/assert_arrayref_all requires a code reference/ );
}
MASS_ASSERTIONS: {
my @things = ( 1, 2, 4.3 );
throws_ok(
sub { assert_arrayref_all( \@things, $api ) },
qr/assert_arrayref_all: Element #2/,
'Automatic name comes back OK'
);
throws_ok(
sub { assert_arrayref_all( \@things, $api, 'All gotta be posint' ) },
qr/All gotta be posint: Element #2/,
'Automatic name comes back OK'
);
@things = 1..400;
assert_arrayref_all( \@things, $api, 'Must all be positive integer' );
}
exit 0;
|