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
|
# $Id$
use strict;
use lib 't/lib';
use lib 't/lib/cached';
use Test::More;
use Test::Exception;
use Scalar::Util;
use DodTestUtil;
BEGIN {
DodTestUtil->check_driver;
unless (eval { require Cache::Memory }) {
plan skip_all => 'Tests require Cache::Memory';
}
}
plan tests => 17;
use_ok 'Data::ObjectDriver::Iterator';
my $iter;
$iter = Data::ObjectDriver::Iterator->new(sub {});
ok Scalar::Util::blessed($iter), "blessed obj";
isa_ok $iter, "CODE", "it's a subref";
isa_ok $iter, "Data::ObjectDriver::Iterator";
can_ok $iter, "next";
is $iter->next, undef;
is $iter->(), undef;
my $i = 0;
my $sub = sub {
return undef if $i >= 10;
return $i++;
};
$iter = Data::ObjectDriver::Iterator->new($sub);
is $iter->next, 0;
is $iter->(), 1;
$i = 11;
is $iter->(), undef;
$i = 2;
$iter->end(); # do nothing
is $iter->next, 2;
{
my $sub2 = sub { $sub->() }; # new reference
my $iter2 = Data::ObjectDriver::Iterator->new($sub2, sub { $i = 10 });
is $iter2->(), 3;
}
is $i, 10, "end has been called";
{
$i = 0;
my $sub2 = sub { $sub->() }; # new reference
my $iter2 = Data::ObjectDriver::Iterator->new($sub2, sub { $i = 10 });
{
my $sub3 = sub { $sub->() }; # new reference
my $iter3 = Data::ObjectDriver::Iterator->new($sub3, sub { $i = -1 });
is $iter2->(), 0;
is $iter3->(), 1;
}
is $i, -1;
}
is $i, 10;
__END__
use Recipe;
use Ingredient;
setup_dbs({
global => [ qw( recipes ingredients) ],
});
my $iter = $recipe->ingredients;
isa_ok $iter, "CODE", "iterator is also available";
while (my $i = $iter->()) {
isa_ok $i, "Ingredient", "next";
}
my $r = $ingredient->recipe;
is $r->recipe_id, $recipe->recipe_id, "recipe id back using 'parent_method'";
teardown_dbs(qw( global ));
|