File: value.t

package info (click to toggle)
libiterator-perl 0.03%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 100 kB
  • sloc: perl: 165; makefile: 2
file content (170 lines) | stat: -rw-r--r-- 3,501 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use strict;
use Test::More tests => 33;
use Iterator;

# Check that value() works.
# Also tests is_ and isnt_exhausted.

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

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

my ($iter, $x, $val, $exh, $nex);

# Create iterator for us to work with (1)
eval
{
    my $i = 1;
    my $max = 3;

    $iter = Iterator->new (
    sub
    {
        Iterator::X::Am_Now_Exhausted->throw()
            if ($i > $max);
        return $i++;
    }
    );
};

is $@, q{},   q{Created simple iterator; no exception};

# That iterator should not be exhausted already. (3)
eval
{
    $exh = $iter->is_exhausted;
    $nex = $iter->isnt_exhausted;
};

is ($@, q{}, q{Exhausted check didn't barf.});
ok (!$exh, q{Not exhausted yet.});
ok ( $nex, q{Not exhausted yet.});

# Fetch a value (2)
eval
{
    $val = $iter->value();
};

is $@, q{},   q{Pulled first value from iterator; no exception};
cmp_ok ($val, '==', 1, q{First value is correct});

# That iterator should not be exhausted yet. (3)
eval
{
    $exh = $iter->is_exhausted;
    $nex = $iter->isnt_exhausted;
};

is ($@, q{}, q{Exhausted check didn't barf.});
ok (!$exh, q{Not exhausted yet.});
ok ( $nex, q{Not exhausted yet.});


# Fetch a value (2)
eval
{
    $val = $iter->value();
};

is $@, q{},   q{Pulled second value from iterator; no exception};
cmp_ok ($val, '==', 2, q{Second value is correct});

# That iterator should not be exhausted yet. (3)
eval
{
    $exh = $iter->is_exhausted;
    $nex = $iter->isnt_exhausted;
};

is ($@, q{}, q{Exhausted check didn't barf.});
ok (!$exh, q{Not exhausted yet.});
ok ( $nex, q{Not exhausted yet.});


# Fetch a value (2)
eval
{
    $val = $iter->value();
};

is $@, q{},   q{Pulled third value from iterator; no exception};
cmp_ok ($val, '==', 3, q{Third value is correct});


# Iterator should now be exhausted. (3)
eval
{
    $exh = $iter->is_exhausted;
    $nex = $iter->isnt_exhausted;
};

is ($@, q{}, q{Exhausted check didn't barf.});
ok ( $exh, q{Now exhausted.});
ok (!$nex, q{Now exhausted.});


# Attempt to fetch a value from it (4)
eval
{
    $val = $iter->value();
};

$x = $@;
isnt $@, q{},   q{Pulled fourth value from iterator; got exception};

ok (Iterator::X->caught(), q{Exhausted exception base class ok});

ok (Iterator::X::Exhausted->caught(),  q{Exhausted exception specific class ok});

begins_with $x,
    q{Iterator is exhausted},
    q{Exhausted exception works as a string, too};


# Should still be able to check exhausted state. (3)
eval
{
    $exh = $iter->is_exhausted;
    $nex = $iter->isnt_exhausted;
};

is ($@, q{}, q{Exhausted check didn't barf.});
ok ( $exh, q{Now exhausted.});
ok (!$nex, q{Now exhausted.});


# Test user exception. (7)
eval
{
    my $internal = 1;
    $iter = Iterator->new(sub
    {
        die "what the heck?"
            if $internal > 2;
        return $internal++;
    });
};
is ($@, q{}, q{User-error iterator created fine.});

eval
{
    $val = $iter->value;
};
is ($@, q{}, q{User-error iterator; first value no error.});
cmp_ok ($val, '==', 1, q{User-error iterator; first value correct});

eval
{
    $val = $iter->value;
};
isnt ($@, q{}, q{User-error iterator blew up on time});
$x = $@;
ok (Iterator::X->caught(), q{User-error base exception caught});
ok (Iterator::X::User_Code_Error->caught(), q{User-error specific exception caught});
begins_with ($x, "what the heck?", q{User-error; proper string value.});