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
|
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};
my $books = $literature->{books};
my $expected_titles_str = [
"Caliban's War",
"Leviathan Wakes",
"The Name of the Wind",
"The Tree-Body Problem",
];
# order by last char
my $expected_titles_regex = [
"The Name of the Wind",
"The Tree-Body Problem",
"Caliban's War",
"Leviathan Wakes",
];
my $expected_prices_asc = [ 5, 6, 6, 11 ];
subtest order_simple => sub {
eq_or_diff(
$books->map_by("title")->order->to_ref,
$expected_titles_str,
"order default everything, scalar context",
);
eq_or_diff(
[ $books->map_by("title")->order ],
$expected_titles_str,
"order default everything, list context",
);
};
subtest order_num_str => sub {
eq_or_diff(
$books->map_by("price")->order("num")->to_ref,
$expected_prices_asc,
"order num",
);
eq_or_diff(
$books->map_by("title")->order("str")->to_ref,
$expected_titles_str,
"order str",
);
};
subtest order_asc_desc => sub {
eq_or_diff(
$books->map_by("title")->order("asc")->to_ref,
$expected_titles_str,
"order str asc",
);
eq_or_diff(
$books->map_by("title")->order("desc")->to_ref,
$expected_titles_str->reverse->to_ref,
"order str desc",
);
};
subtest order_sub => sub {
eq_or_diff(
$books->map_by("price")->order([ "num", sub { 0 - $_ } ])->to_ref,
$expected_prices_asc->reverse->to_ref,
"order num, subref",
);
};
subtest order_regex => sub {
eq_or_diff(
$books->map_by("title")->order(qr/(.)$/)->to_ref,
$expected_titles_regex->to_ref,
"order regex",
);
};
subtest order_multiple_options__num_desc => sub {
eq_or_diff(
$books->map_by("price")->order([ "num", "desc" ])->to_ref,
$expected_prices_asc->reverse->to_ref,
"order num, desc",
);
};
subtest comparison_args_validation => sub {
throws_ok(
sub { [1]->order("blah")->to_ref },
qr/\Q->order(): Invalid comparison option (blah)/,
"Invalid arg dies ok",
);
throws_ok(
sub { [1]->order([ "asc", "desc" ])->to_ref },
qr/\Q->order(): Conflicting comparison options: (asc) and (desc)/,
"Invalid arg dies ok",
);
# Ignore ugly subref vs regex for now
};
subtest order_multiple_comparisons => sub {
my $words = [
"abc",
"def",
"ABC",
"DEF",
"Abc",
];
# ASCII order is UPPER before lower
my $expected_words = [
"DEF",
"def",
"ABC",
"Abc",
"abc",
];
eq_or_diff(
$words->order(
[ desc => sub { uc($_) } ],
qr/(.+)/,
)->to_ref,
$expected_words,
"First reverse uc, then whole match cmp",
);
};
done_testing();
|