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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
use strict;
use Test::More tests => 18;
use Iterator;
# Check that new() fails when it should.
sub begins_with
{
my ($actual, $expected, $test_name) = @_;
$actual = substr($actual, 0, length $expected);
@_ = ($actual, $expected, $test_name);
goto &is;
}
my ($iter, $x);
# New: too few (4)
eval
{
$iter = Iterator->new();
};
$x = $@;
isnt $x, q{}, q{Too few parameters to new -> exception thrown};
ok (Iterator::X->caught(), q{Too-few exception base class ok});
ok (Iterator::X::Parameter_Error->caught(), q{Too-few exception specific class ok});
begins_with $x,
q{Too few parameters to Iterator->new()},
q{Too-few exception works as a string, too};
# New: too many (4)
eval
{
$iter = Iterator->new(sub {die}, 'whoa there');
};
$x = $@;
isnt $x, q{}, q{Too many parameters to new -> exception thrown};
ok (Iterator::X->caught(), q{Too-many exception base class ok});
ok (Iterator::X::Parameter_Error->caught(), q{Too-many exception specific class ok});
begins_with $x,
q{Too many parameters to Iterator->new()},
q{Too-many exception works as a string, too};
# New: wrong type (4)
eval
{
$iter = Iterator->new('whoa there');
};
$x = $@;
isnt $x, q{}, q{Wrong type of parameter to new -> exception thrown};
ok (Iterator::X->caught(), q{Wrong-type exception base class ok});
ok (Iterator::X::Parameter_Error->caught(), q{Wrong-type exception specific class ok});
begins_with $x,
q{Parameter to Iterator->new() must be code reference},
q{Wrong-type exception works as a string, too};
# New: wrong type (looks like code but isn't) (4)
eval
{
$iter = Iterator->new({qw/whoa there/});
};
$x = $@;
isnt $x, q{}, q{Bad code ref parameter to new -> exception thrown};
ok (Iterator::X->caught(), q{Bad-coderef exception base class ok});
ok (Iterator::X::Parameter_Error->caught(), q{Bad-coderef exception specific class ok});
begins_with $x,
q{Parameter to Iterator->new() must be code reference},
q{Bad-coderef exception works as a string, too};
# New: everything fine (1)
eval
{
my $i = 0;
$iter = Iterator->new( sub {return $i++});
};
$x = $@;
is $x, q{}, q{Simple invocation: no exception};
# New: everything fine (1)
eval
{
my $i = 0;
$iter = Iterator->new( sub {
Iterator::X::Am_Now_Exhausted->throw if $i > 10;
return $i++;
});
};
$x = $@;
is $x, q{}, q{more-complicated invocation: no exception};
|