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
|
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 filter_default_true => sub {
note "Default is checking for true";
my $array = [ 0, 1, 2, 3, "", 4, undef, 5 ];
eq_or_diff(
$array->filter->to_ref,
[ 1, 2, 3, 4, 5 ],
"Only true values remain",
);
};
subtest filter_invalid_predicate => sub {
my $strings = [ "abc", "def", "abc" ];
throws_ok(
sub { $strings->filter(\"abc")->to_ref },
qr/->filter .+? \$predicate: .+?\Q is not one of: subref, string, regex/x,
"Invalid predicate type",
);
};
subtest filter_subref => sub {
note "ArrayRef call, list context result, subref predicate";
eq_or_diff(
[ map { $_->name } $authors->filter(sub { $_->is_prolific }) ],
[
"James A. Corey",
],
"filter simple method call works",
);
note "list call, list context result";
my @authors = @$authors;
my $prolific_authors = @authors->filter(sub { $_->is_prolific });
eq_or_diff(
[ map { $_->name } @$prolific_authors ],
[
"James A. Corey",
],
"filter simple method call works",
);
};
subtest filter_string => sub {
my $strings = [ "abc", "def", "abc" ];
eq_or_diff(
$strings->filter("abc")->to_ref,
[ "abc", "abc" ],
"filter scalar string",
);
# TODO: deal with undef comparisons
};
# TODO: Can't work until the old call style is removed.
# subtest filter_undef => sub {
# my $strings = [ "abc", undef, "abc" ];
# eq_or_diff(
# $strings->filter(undef)->to_ref,
# [ undef ],
# "filter undef",
# );
# };
subtest filter_regex => sub {
my $strings = [ "abc", "def", "abc" ];
eq_or_diff(
$strings->filter(qr/a/)->to_ref,
[ "abc", "abc" ],
"filter regex",
);
eq_or_diff(
$strings->filter(qr/A/)->to_ref,
[ ],
"filter regex miss",
);
eq_or_diff(
$strings->filter(qr/A/i)->to_ref,
[ "abc", "abc" ],
"filter regex with flags",
);
# TODO: deal with undef comparisons
};
subtest filter_hashref_keys => sub {
my $strings = [ "abc", "def", "ghi" ];
eq_or_diff(
$strings->filter({ abc => undef, def => 1 })->to_ref,
[ "abc", "def" ],
"filter hashref keys (exists, not true hash value)",
);
# TODO: deal with undef comparisons
};
done_testing();
|