File: doc.t

package info (click to toggle)
libiterator-perl 0.03%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 104 kB
  • ctags: 11
  • sloc: perl: 165; makefile: 2
file content (85 lines) | stat: -rw-r--r-- 2,178 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
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
use strict;
use Test::More tests => 13;
use Iterator;

# Check that the documentation examples work.

sub begins_with
{
    my ($actual, $expected, $test_name) = @_;

    $actual = substr($actual, 0, length $expected);
    @_ =  ($actual, $expected, $test_name);
    goto &is;
}

my ($iter, $it, $x, @vals);

# from the README (13)

sub upto
{
    my ($m, $n) = @_;

    return Iterator->new( sub {
                              return $m++  if $m <= $n;
                              Iterator::X::Am_Now_Exhausted->throw();
                          });
}

@vals = ();
eval
{
    $it = upto (3, 5);
};

is ($@, q{}, q{README iterator created, no exception});

eval
{
    push @vals, $it->value;     #  returns 3
    push @vals, $it->value;     #  returns 4
    push @vals, $it->value;     #  returns 5
};

is ($@, q{}, q{README iterator; first three okay});
is_deeply (\@vals, [3, 4, 5], q{README iterator: expected values ok});

eval
{
    my $i = $it->value;     #  throws an Iterator::X::Exhausted exception.
};

isnt ($@, q{}, q{README iterator: exception thrown});
ok (Iterator::X->caught(), q{README exception: correct base type});
ok (Iterator::X::Exhausted->caught(), q{README exception: correct specific type});
begins_with ($@, q{Iterator is exhausted}, q{README iterator exception formatted propertly.});

{
    my $another_it;

    @vals = ();
    eval
    {
        $another_it = upto (7, 10);
        while ($another_it->isnt_exhausted)
        {
            push @vals, $another_it->value;
        }
        # The above [pushes] 7, 8, 9, 10 and throws no exceptions.
    };

    is ($@, q{}, q{$another_it: no exception thrown});
    is_deeply (\@vals, [7, 8, 9, 10], q{$another_it: expected values});

    eval
    {
        # Another call to $another_it->value would throw an exception.
        $another_it->value
    };

    isnt ($@, q{}, q{$another_it iterator: exception thrown});
    ok (Iterator::X->caught(), q{$another_it exception: correct base type});
    ok (Iterator::X::Exhausted->caught(), q{$another_it exception: correct specific type});
    begins_with ($@, q{Iterator is exhausted}, q{$another_it iterator exception formatted propertly.});
}