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
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 18;
use Test::Exception;
BEGIN {
use_ok('Array::Iterator')
};
# test the exceptions
# test that the constructor cannot be empty
throws_ok {
my $i = Array::Iterator->new();
} qr/^Insufficient Arguments\: you must provide something to iterate over/,
'... we got the error we expected';
# check that it does not allow non-array ref paramaters
throws_ok {
my $i = Array::Iterator->new({});
} qr/^Incorrect type\: HASH reference must contain the key __array__/,
'... we got the error we expected';
# verify the HASH ref sanity checks
throws_ok {
my $i = Array::Iterator->new({ no_array_key => [] });
} qr/^Incorrect type\: HASH reference must contain the key __array__/,
'... we got the error we expected';
throws_ok {
my $i = Array::Iterator->new({ __array__ => "not an array ref" });
} qr/^Incorrect type\: __array__ value must be an ARRAY reference/,
'... we got the error we expected';
throws_ok {
Array::Iterator->_init(undef, 1);
} qr/^Insufficient Arguments\: you must provide an length and an iteratee/,
'... we got the error we expected';
throws_ok {
Array::Iterator->_init(1);
} qr/^Insufficient Arguments\: you must provide an length and an iteratee/,
'... we got the error we expected';
# now test the next & peek exceptions
my @control = (1 .. 5);
my $iterator = Array::Iterator->new(@control);
isa_ok($iterator, 'Array::Iterator');
my @_control;
push @_control => $iterator->next() while $iterator->hasNext();
ok(!$iterator->hasNext(), '... we are out of elements');
ok(eq_array(\@control, \@_control), '.. make sure all are exhausted');
# test that next will croak if it is called passed the end
throws_ok {
$iterator->next();
} qr/^Out Of Bounds\: no more elements/,
'... we got the error we expected';
# test arbitrary lookups edge cases
{
my $iterator2 = Array::Iterator->new(@control);
throws_ok {
$iterator2->has_next(0)
} qr/\Qhas_next(0) doesn't make sense/, '... should not be able to call has_next() with zero argument';
throws_ok {
$iterator2->has_next(-1)
} qr/\Qhas_next() with negative argument doesn't make sense/, '... should not be able to call has_next() with negative argument';
throws_ok {
$iterator2->peek(0)
} qr/\Qpeek(0) doesn't make sense/, '... should not be able to call peek() with zero argument';
throws_ok {
$iterator2->peek(-1)
} qr/\Qpeek() with negative argument doesn't make sense/, '... should not be able to call peek() with negative argument';
}
# check our protected methods
throws_ok {
$iterator->_current_index();
} qr/Illegal Operation/, '... got the error we expected';
throws_ok {
$iterator->_iteratee();
} qr/Illegal Operation/, '... got the error we expected';
throws_ok {
$iterator->_getItem();
} qr/Illegal Operation/, '... got the error we expected';
# -----------------------------------------------
# NOTE:
# Test removed, peek no longer dies when it reaches
# beyond the iterators bounds, it returns undef instead
# -----------------------------------------------
# test that peek will croak if it is called passed the end
# throws_ok {
# $iterator->peek();
# } qr/^Out Of Bounds\: cannot peek past the end of the array/,
# '... we got the error we expected';
# -----------------------------------------------
|