File: group_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 (203 lines) | stat: -rw-r--r-- 5,287 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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};
my $authors    = $literature->{authors};
my $reviews    = $literature->{reviews};

subtest group_by__method => sub {
    note "ArrayRef call, list context result";

    my $book_object = {
        "Leviathan Wakes"       => $books->[0],
        "Caliban's War"         => $books->[1],
        "The Tree-Body Problem" => $books->[2],
        "The Name of the Wind"  => $books->[3],
    };
    eq_or_diff(
        { $books->group_by("title") },
        $book_object,
        "Group by simple method call works",
    );

    note "list call, list context result";
    my @books = @$books;
    my $genre_exists = @books->group_by("title");
    eq_or_diff(
        $genre_exists,
        $book_object,
        "Group by simple method call works",
    );

    note "Call with arrayref accessor";
    eq_or_diff(
        { $books->group_by([ "title" ]) },
        $book_object,
        "Group by simple method call works",
    );
};

subtest group_by__key => sub {
    note "ArrayRef key lookup";
    eq_or_diff(
        { $reviews->group_by("id") },
        {
            1 => { id => 1, score => 7 },
            2 => { id => 2, score => 6 },
            3 => { id => 3, score => 9 },
        },
        "Group by simple hash key lookup works",
    );
};

subtest group_by__key__with_args => sub {
    note "ArrayRef key lookup";
    throws_ok(
        sub { $reviews->group_by("id" => 32) },
        qr{ group_by .+? 'id' .+? \@args .+? \(32\) .+? only[ ]supported[ ]for .+? t.group_by.t}x,
        "Group by simple hash key lookup with args dies as expected",
    );
};

subtest group_by_count__method => sub {
    note "ArrayRef call, list context result";

    my $genre_count = {
        "Sci-fi"  => 3,
        "Fantasy" => 1,
    };

    eq_or_diff(
        { $books->group_by_count("genre") },
        $genre_count,
        "Group by simple method call works",
    );

    note "list call, list context result";
    my @books = @$books;
    my $genre_exists = @books->group_by_count("genre");
    eq_or_diff(
        $genre_exists,
        $genre_count,
        "Group by simple method call works",
    );
};



subtest group_by__missing_method => sub {
    throws_ok(
        sub { $books->group_by() },
        qr{^->group_by\(\)[ ]missing[ ]argument:[ ]\$accessor \s at .+? t.group_by.t }x,
        "Missing arg croaks from the caller, not from the lib"
    );
};

subtest group_by__not_a_method => sub {
    # Invalid arg, not a method
    throws_ok(
        sub { $books->group_by("not_a_method") },
        qr{ not_a_method .+? Book .+? t.group_by.t }x,
        "Missing method croaks from the caller, not from the lib",
    );
};

subtest group_by__method__args => sub {
    eq_or_diff(
        { $authors->group_by_count(publisher_affiliation => ["with"]) },
        {
            'James A. Corey with Orbit'     => 1,
            'Cixin Liu with Head of Zeus'   => 1,
            'Patrick Rothfuss with Gollanz' => 1,
        },
        "group_by with argument list",
    );
    eq_or_diff(
        { $authors->group_by_count([publisher_affiliation => "with"]) },
        {
            'James A. Corey with Orbit'     => 1,
            'Cixin Liu with Head of Zeus'   => 1,
            'Patrick Rothfuss with Gollanz' => 1,
        },
        "group_by with argument list",
    );
};

subtest group_by__method__args__invalid_type => sub {
    throws_ok(
        sub { $authors->group_by(publisher_affiliation => 342) },
        qr{ group_by .+? 'publisher_affiliation' .+? \@args .+? \(342\) .+? list .+? t.group_by.t}x,
        "group_by with argument which isn't an array ref",
    );
};

subtest group_by_count__method__args__invalid_type => sub {
    throws_ok(
        sub { $authors->group_by_count(publisher_affiliation => 342) },
        qr{ group_by .+? 'publisher_affiliation' .+? \@args .+? \(342\) .+? list .+? t.group_by.t}x,
        "group_by with argument which isn't an array ref",
    );
};

subtest group_by__sub_ref => sub {
    eq_or_diff(
        { $books->group_by("genre", [], sub { 1 }) },
        {
            "Sci-fi"  => 1,
            "Fantasy" => 1,
        },
        "group_by with sub_ref works",
    );
    eq_or_diff(
        { $books->group_by([ "genre" ], sub { 1 }) },
        {
            "Sci-fi"  => 1,
            "Fantasy" => 1,
        },
        "group_by with sub_ref works",
    );
};

subtest group_by__method__array => sub {
    my $genre_books = $books->group_by_array("genre");
    my $genre_books2 = $books->group_by_array([ "genre" ]);
    eq_or_diff($genre_books, $genre_books2, "Same output");

    my $genre_book_titles = {
        map {
            $_ => $genre_books->{$_}->map_by("title")->sort->join(", ");
        }
        $genre_books->keys
    };

    eq_or_diff(
        $genre_book_titles,
        {
            "Sci-fi"  => "Caliban's War, Leviathan Wakes, The Tree-Body Problem",
            "Fantasy" => "The Name of the Wind",
        },
        "group_by_array work",
    );
};

subtest examples => sub {
    ok(1);
};




done_testing();