File: hash-map_each.t

package info (click to toggle)
libautobox-transform-perl 1.035-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 296 kB
  • sloc: perl: 717; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download
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
use strict;
use warnings;

use Test::More;
use Test::Differences;
use Test::Exception;

use autobox::Core;

use lib "lib";
use autobox::Transform;

use lib "t/lib";
use Literature;

my $literature = Literature::literature();
my $books      = $literature->{books};

subtest map_each_missing_subref => sub {
    throws_ok(
        sub { scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each() },
        qr/map_each\(\$key_value_subref\): \$key_value_subref \(\) is not a sub ref at/,
        "map_each dies without subref",
    );
    throws_ok(
        sub { scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each("abc") },
        qr/map_each\(\$key_value_subref\): \$key_value_subref \(abc\) is not a sub ref at/,
        "map_each dies without subref",
    );
};

subtest map_each_subref_returns_wrong_number_of_items => sub {
    throws_ok(
        sub { scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each(sub { return (1) }) },
        qr|returned odd number of keys/values at|,
        "map_each, 1 return value",
    );
    throws_ok(
        sub { scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each(sub { return (1, 2, 3) }) },
        qr|returned odd number of keys/values at|,
        "map_each, 3 return values",
    );
};

subtest map_each_basic => sub {
    eq_or_diff(
        scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each(
            sub { $_[0] . ( $_[1] // "undef" ), ( $_ // "UNDEF" ) },
        ),
        { zero0 => 0, one1 => 1, two2 => 2, undefinedundef => "UNDEF" },
        "map_each with key, value, topic variable",
    );
    eq_or_diff(
        scalar { one => 1, zero => 0, two => 2, undefined => undef }->map_each(
            sub { ($_[0] => $_, ($_ // "") => $_[0] ) },
        ),
        {
            one => 1, zero => 0, two => 2, undefined => undef,
            1 => "one", 0 => "zero", 2 => "two", "" => "undefined",
        },
        "map_each with multiple return pairs",
    );
};

subtest examples => sub {
    # Upper-case the genre name, and make the count say "n books"
    eq_or_diff(
        {
            $books->group_by_count("genre")->map_each(sub { uc($_[0]) => "$_ books" })
        },
        {
            "FANTASY" => "1 books",
            "SCI-FI"  => "3 books",
        },
        "Book count",
    );

    ok(1);
};


done_testing();