Skip to content

Commit

Permalink
added tests for arbitrary lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
ksurent committed Mar 27, 2012
1 parent 159bb35 commit 724d540
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 4 deletions.
31 changes: 30 additions & 1 deletion t/10_Array_Iterator_test.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 85;
use Test::More tests => 99;

BEGIN {
use_ok('Array::Iterator')
Expand Down Expand Up @@ -133,3 +133,32 @@ ok(!$iterator2->hasNext(), '... we should have no more');
# we should have no more
ok(!$iterator4->hasNext(), '... we should have no more');
}

{
# check arbitrary position lookups
my $iterator5 = Array::Iterator->new(@control);

# when not iterated()
ok($iterator5->has_next, '... we should have next element');
ok($iterator5->has_next(1), '... should be the same as has_next()');
ok($iterator5->has_next(2), '... we should have 2nd next element');
ok($iterator5->has_next(5), '... we should have 5th next element');
ok(!$iterator5->has_next(6), '... we should not have 6th next element');

cmp_ok($iterator5->peek(1), '==', $iterator5->peek, '... should be the same as peek()');
cmp_ok($iterator5->peek(2), '==', 2, '... we should get 2nd next element');
cmp_ok($iterator5->peek(5), '==', 5, '... we should get 5th next element');

ok(!defined($iterator5->peek(6)), '... peek() outside of the bounds should return undef');

$iterator5->next;

# when iterated()
ok($iterator5->has_next(4), '... we should have 4th next element after iterating');
ok(!$iterator5->has_next(5), '... we should not have 5th next element after iterating');

cmp_ok($iterator5->peek(1), '==', $iterator5->peek, '... should be the same as peek() after iterating');
cmp_ok($iterator5->peek(2), '==', 3, '... we should get 2nd next element after iterating');

ok(!defined($iterator5->peek(5)), '... peek() outside of the bounds should return undef after iterating');
}
21 changes: 19 additions & 2 deletions t/20_Array_Iterator_exceptions.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 15;
use Test::More tests => 19;
use Test::Exception;

BEGIN {
Expand Down Expand Up @@ -69,6 +69,24 @@ throws_ok {
} 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 value 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 value doesn't make sense/, '... should not be able to call peek() with negative argument';
}

# check our protected methods
throws_ok {
$iterator->_current_index();
Expand All @@ -81,7 +99,6 @@ throws_ok {
throws_ok {
$iterator->_getItem();
} qr/Illegal Operation/, '... got the error we expected';


# -----------------------------------------------
# NOTE:
Expand Down
37 changes: 36 additions & 1 deletion t/30_Array_Iterator_BiDirectional_test.t
Expand Up @@ -3,7 +3,7 @@
use strict;
use warnings;

use Test::More tests => 32;
use Test::More tests => 48;
use Test::Exception;

BEGIN {
Expand Down Expand Up @@ -72,6 +72,41 @@ throws_ok {
} qr/Out Of Bounds \: no more elements/, '... this should die if i try again';


my $iterator3 = Array::Iterator::BiDirectional->new(@control);

# when not iterated()
ok(!$iterator3->has_previous(1), '... should be the same as has_previous()');
ok(!$iterator3->has_previous(2), '... should not have 2nd previous element');
ok(!$iterator3->has_previous(3), '... should not have 3rd previous element');

ok(!defined($iterator3->look_back(1)), '... should be the same as look_back()');
ok(!defined($iterator3->look_back(2)), '... look_back() outside of the bounds should return undef');
ok(!defined($iterator3->look_back(5)), '... look_back() outside of the bounds should return undef');

$iterator3->next while $iterator3->has_next;

# when iterated()
ok($iterator3->has_previous(1), '... should be the same as has_previous() after iterating');
ok($iterator3->has_previous(2), '... should have 2nd previous element');

cmp_ok($iterator3->look_back(1), '==', $iterator3->look_back, '... should be the same as look_back() after iterating');
cmp_ok($iterator3->look_back(2), '==', 4, '... should get 2nd previous element after iterating');
cmp_ok($iterator3->look_back(3), '==', 3, '... should get 3rd previous element after iterating');
ok(!defined($iterator3->look_back(6)), '... look_back() outside of the bounds should return undef after iterating');

# check arbitrary lookup edge cases
throws_ok {
$iterator3->has_previous(0)
} qr/\Qhas_previous(0) doesn't make sense/, '... should not be able to call has_previous() with zero argument';

throws_ok {
$iterator3->has_previous(-1)
} qr/\Qhas_previous() with negative argument doesn't make sense/, '... should not be able to call has_previous() with negative argument';

throws_ok {
$iterator3->look_back(0)
} qr/\Qlook_back(0) doesn't make sense/, '... should not be able to call look_back() with zero argument';

throws_ok {
$iterator3->look_back(-1)
} qr/\Qlook_back() with negative argument doesn't make sense/, '... should not be able to call look_back() with negative argument';

0 comments on commit 724d540

Please sign in to comment.