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