]>
Dogcows Code - chaz/p5-File-KDBX/blob - t/iterator.t
9 use File
::KDBX
::Iterator
;
10 use File
::KDBX
::Entry
;
11 use File
::KDBX
::Util
qw(:load);
12 use Iterator
::Simple
qw(:all);
15 subtest
'Basic' => sub {
16 my $it = File
::KDBX
::Iterator-
>new(1..10);
18 is $it->(), 1, 'Get next item (1)';
19 is $it->(), 2, 'Get next item (2)';
21 is $it->(), -5, 'Unget';
22 is $it->peek, 3, 'Peek at next';
23 is $it->(), 3, 'Get next item (3)';
24 is $it->count, 7, 'Get current size';
26 my $limited = $it->limit(3);
27 is $limited->count, 3, 'Get current size';
28 my $enum = ienumerate
$limited;
29 is_deeply
$enum->to_array, [[0, 4], [1, 5], [2, 6]], 'Use Iterator::Simple functions';
31 is $it->(), 7, 'Original iterator is drained by composing iterator';
33 is $it->next(sub { $_ == 9 }), 9, 'Find next matching item';
34 is $it->next, 10, 'Item got skipped while finding next match';
35 is $it->peek, undef, 'No more items (peek)';
36 is $it->next, undef, 'No more items (next)';
39 is_deeply
[$it->each], [qw{10 20 30}], 'Fill buffer and get each item (list)';
40 is $it->(), undef, 'Empty';
42 $it->(my $buffer = [qw{a b c}]);
44 $it->each(sub { push @each, $_ });
45 is_deeply \
@each, [qw{a b c}], 'Fill buffer and get each item (function)';
46 is_deeply
$buffer, [], 'Buffer is empty';
49 subtest
'Sorting' => sub {
51 File
::KDBX
::Iterator-
>new(
52 File
::KDBX
::Entry-
>new(label
=> 'foo', icon_id
=> 1),
53 File
::KDBX
::Entry-
>new(label
=> 'bar', icon_id
=> 5),
54 File
::KDBX
::Entry-
>new(label
=> 'BaZ', icon_id
=> 3),
55 File
::KDBX
::Entry-
>new(label
=> 'qux', icon_id
=> 2),
56 File
::KDBX
::Entry-
>new(label
=> 'Muf', icon_id
=> 4),
60 my @sort = (label
=> collate
=> 0);
63 is_deeply
$it->sort_by(@sort)->map(sub { $_->label })->to_array,
64 [qw{BaZ Muf bar foo qux}], 'Sort text ascending';
67 is_deeply
$it->sort_by(@sort, case
=> 0)->map(sub { $_->label })->to_array,
68 [qw{bar BaZ foo Muf qux}], 'Sort text ascending, ignore-case';
71 is_deeply
$it->sort_by(@sort, ascending
=> 0)->map(sub { $_->label })->to_array,
72 [qw{qux foo bar Muf BaZ}], 'Sort text descending';
75 is_deeply
$it->sort_by(@sort, ascending
=> 0, case
=> 0)->map(sub { $_->label })->to_array,
76 [qw{qux Muf foo BaZ bar}], 'Sort text descending, ignore-case';
79 plan skip_all
=> 'Unicode::Collate required to test collation sorting'
80 if !try_load_optional
('Unicode::Collate');
82 # FIXME I'm missing something....
84 # is_deeply $it->sort_by('label')->map(sub { $_->label })->to_array,
85 # [qw{BaZ Muf bar foo qux}], 'Sort text ascending using Unicode::Collate';
88 is_deeply
$it->sort_by('label', case
=> 0)->map(sub { $_->label })->to_array,
89 [qw{bar BaZ foo Muf qux}], 'Sort text ascending, ignore-case using Unicode::Collate';
93 is_deeply
$it->nsort_by('icon_id')->map(sub { $_->label })->to_array,
94 [qw{foo qux BaZ Muf bar}], 'Sort text numerically, ascending';
97 is_deeply
$it->nsort_by('icon_id', ascending
=> 0)->map(sub { $_->label })->to_array,
98 [qw{bar Muf BaZ qux foo}], 'Sort text numerically, descending';
This page took 0.041729 seconds and 4 git commands to generate.