File: reject_by.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 (165 lines) | stat: -rw-r--r-- 4,068 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
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
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 $authors    = $literature->{authors};

subtest reject_by => sub {
    note "ArrayRef call, list context result, default predicate (true)";
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("is_prolific") ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by simple method call works",
    );

    note "list call, list context result";
    my @authors = @$authors;
    my $prolific_authors = @authors->reject_by("is_prolific");
    eq_or_diff(
        [ map { $_->name } @$prolific_authors ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by simple method call works",
    );
};

subtest reject_by_with_arrayref_accessor => sub {
    note "Call with arrayref accessor without args";
    eq_or_diff(
        [ map { $_->name } $authors->reject_by([ "is_prolific" ]) ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by simple method call works",
    );
};

subtest reject_by_with_predicate_subref => sub {
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", undef, sub { /Corey/ }) ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by without predicate argument and with predicate sub call works",
    );
    eq_or_diff(
        [
            map { $_->name } $authors->reject_by(
                "publisher_affiliation",
                [ "with" ],
                sub { /Corey/ },
            ),
        ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by with method argument and predicate sub call works",
    );

    note "Arrayref accessor";
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", sub { /Corey/ }) ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by without predicate argument and with predicate sub call works",
    );
    eq_or_diff(
        [
            map { $_->name } $authors->reject_by(
                [ publisher_affiliation => "with" ],
                sub { /Corey/ },
            ),
        ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by with method argument and predicate sub call works",
    );
};

subtest reject_by_with_predicate_string => sub {
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", "James A. Corey") ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by predicate string works",
    );
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", undef, "James A. Corey") ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by predicate string works, with old call style",
    );
};

# TODO: undef can't work until old call style is removed

subtest reject_by_with_predicate_regex => sub {
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", qr/corey/i) ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by predicate regex works",
    );
};

subtest reject_by_with_predicate_hashref => sub {
    eq_or_diff(
        [ map { $_->name } $authors->reject_by("name", { "James A. Corey" => 1 }) ],
        [
            "Cixin Liu",
            "Patrick Rothfuss",
        ],
        "reject_by predicate hashref works",
    );
};



subtest examples => sub {
    my $prolific_author_book_titles = $authors->reject_by("is_prolific")
        ->map_by("books")->flat
        ->map_by("title")->sort;
    eq_or_diff(
        $prolific_author_book_titles,
        [
            "The Name of the Wind",
            "The Tree-Body Problem",
        ],
        "non-prolific_author_book_titles",
    );
};




done_testing();