File: Catmandu-ArrayIterator.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (55 lines) | stat: -rw-r--r-- 856 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::ArrayIterator';
    use_ok $pkg;
}

my $arr = [{n => 1}, {n => 2}, {n => 3}];
my $it  = $pkg->new($arr);

ok $it->does('Catmandu::Iterable');

is_deeply [@{$it}], $arr;

is $it->count, 3;

is_deeply $it->first, $arr->[0];

is $it->contains({n => 2}), 1;

is $it->contains(10), 0;

# test external iteration again because of circular dependency
is_deeply $it->next, {n => 1};
is_deeply $it->next, {n => 2};
$it->rewind;
is_deeply $it->next, {n => 1};

$it->rewind;

my $count = 0;
$it->each(
    sub {
        is shift->{n}, ++$count, "each ($count)";
    }
);

$it->rewind;

$count = 0;
$it->each_until(
    sub {
        is shift->{n}, ++$count, "each ($count)";
        return $count == 2 ? undef : 1;
    }
);

done_testing 15;