Skip to content

Commit

Permalink
Merge pull request #133 from paultcochrane/pr/add-more-iterator-tests
Browse files Browse the repository at this point in the history
Add more iterator tests
  • Loading branch information
racke committed Aug 27, 2015
2 parents 8c37cbd + 42de4a0 commit 6f77b02
Showing 1 changed file with 112 additions and 13 deletions.
125 changes: 112 additions & 13 deletions t/iterators/01-simple.t
Expand Up @@ -8,22 +8,121 @@ use Test::More tests => 4;

use Template::Flute::Iterator;

my ($cart, $iter);
subtest "Initialisation with a cart of items" => sub {
plan tests => 3;

$cart = [{isbn => '978-0-2016-1622-4', title => 'The Pragmatic Programmer',
quantity => 1},
{isbn => '978-1-4302-1833-3',
title => 'Pro Git', quantity => 1},
];
my $cart = [
{
isbn => '978-0-2016-1622-4',
title => 'The Pragmatic Programmer',
quantity => 1
},
{
isbn => '978-1-4302-1833-3',
title => 'Pro Git',
quantity => 1
},
];

$iter = new Template::Flute::Iterator($cart);
isa_ok($iter, 'Template::Flute::Iterator');
my $iter = Template::Flute::Iterator->new($cart);
isa_ok($iter, 'Template::Flute::Iterator');

ok($iter->count == 2);
ok($iter->count == 2, "Item count is correct");

isa_ok($iter->next, 'HASH');
isa_ok($iter->next, 'HASH', "Next item is a hash reference");
};

$iter->seed({isbn => '978-0-9779201-5-0', title => 'Modern Perl',
quantity => 10});
subtest "Initialisation with a seed item" => sub {
plan tests => 1;

ok($iter->count == 1);
my $iter = Template::Flute::Iterator->new;
$iter->seed(
{
isbn => '978-0-9779201-5-0',
title => 'Modern Perl',
quantity => 10
}
);

ok($iter->count == 1, "Item count is correct");
};

subtest "Sort an iterator" => sub {
plan tests => 4;

my $cart = [
{
isbn => '978-0-2016-1622-4',
title => 'The Pragmatic Programmer',
quantity => 1
},
{
isbn => '978-1-4302-1833-3',
title => 'Pro Git',
quantity => 1
},
{
isbn => '978-0-9779201-5-0',
title => 'Modern Perl',
quantity => 10
}
];

my $iter = Template::Flute::Iterator->new($cart);
ok($iter->count == 3, "Item count is correct");

$iter->sort('title');
my $item = $iter->next;
is $item->{'title'}, "Modern Perl",
"Expected item title from sorted iterator";
$item = $iter->next;
is $item->{'title'}, "Pro Git",
"Expected item title from sorted iterator";
$item = $iter->next;
is $item->{'title'}, "The Pragmatic Programmer",
"Expected item title from sorted iterator";
};

subtest "Sort in iterator; uniquely" => sub {
plan tests => 5;

my $cart = [
{
isbn => '978-0-2016-1622-4',
title => 'The Pragmatic Programmer',
quantity => 1
},
{
isbn => '978-1-4302-1833-3',
title => 'Pro Git',
quantity => 1
},
{
isbn => '978-0-9779201-5-0',
title => 'Modern Perl',
quantity => 10
},
{
isbn => '978-1-4302-1833-3',
title => 'Pro Git',
quantity => 5
},
];

my $iter = Template::Flute::Iterator->new($cart);
ok($iter->count == 4, "Item count is correct");

$iter->sort('title', unique => 1);

my $item = $iter->next;
is $item->{'title'}, "Modern Perl",
"Expected item title from sorted iterator";
$item = $iter->next;
is $item->{'title'}, "Pro Git",
"Expected item title from sorted iterator";
$item = $iter->next;
is $item->{'title'}, "The Pragmatic Programmer",
"Expected item title from sorted iterator";

is $iter->next, undef, "No further items in iterator";
};

0 comments on commit 6f77b02

Please sign in to comment.