File: imap.t

package info (click to toggle)
libiterator-util-perl 0.02%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 136 kB
  • ctags: 12
  • sloc: perl: 234; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,770 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
use strict;
use Test::More tests => 13;
use Iterator::Util;

# Check that imap function works (assumes irange works).

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

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

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

## Parameter error  (4)
eval
{
    $iter = imap { $_ * 2} 'oops';
};

$x = $@;
isnt ($@, q{},   q{Wrong-type; exception thrown});
ok (Iterator::X->caught(), q{Wrong-type base exception type});
ok (Iterator::X::Parameter_Error->caught(), q{Wrong-type specific exception type});
begins_with ($x, q{Argument to imap must be an Iterator object},
                 q{Wrong-type exception formatted properly});

## run an irange through an imap. (1)
eval
{
    $iter = imap { $_ * 2 } irange (0);
};

is ($@, q{},   q{Normal; no exception thrown});

# pull a few numbers out of the hat.  (2)
@vals = ();
eval
{
    push @vals, $iter->value()  for (1..4);
};

is ($@, q{}, q{No exception when imapping});
is_deeply (\@vals, [0, 2, 4, 6], q{imap transformation returned expected result});

# Now do it with a finite irange (2)
@vals = ();
eval
{
    $iter = imap {$_ * $_} irange (1, 4);
    push @vals, $iter->value  while $iter->isnt_exhausted;
};

is ($@, q{}, q{No exception when imapping.});
is_deeply (\@vals, [1, 4, 9, 16], q{Square imap returned expected results});

# Try pushing it further: (4)
eval
{
    push @vals, $iter->value;
};

$x = $@;
isnt ($@, q{},   q{Imapped too far; exception thrown});
ok (Iterator::X->caught(), q{Too-far base exception type});
ok (Iterator::X::Exhausted->caught(), q{Too-far specific exception type});
begins_with ($x, q{Iterator is exhausted},
                 q{Too-far exception formatted properly});