File: 01_sort_by.t

package info (click to toggle)
liblist-utilsby-xs-perl 0.06-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212 kB
  • sloc: perl: 198; makefile: 3; sh: 1
file content (46 lines) | stat: -rw-r--r-- 1,229 bytes parent folder | download | duplicates (3)
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
use strict;
use warnings;

use Test::More;

use List::UtilsBy::XS qw( sort_by rev_sort_by );

{
    my $expected;
    my @gots;
    my @array;

    is_deeply( [ sort_by { } ], [], 'sort_by with empty list' );
    is_deeply( [ sort_by { $_ } "a" ], [ "a" ], 'unit list' );

    my $obj = { foo => 'bar' };
    $expected = [ { foo => 'bar' } ];
    @gots = sort_by { $_->{bar} } $obj;
    is_deeply(\@gots, $expected, "unit list by hash key");

    is_deeply( [ sort_by { $_ } "a", "b" ], [ "a", "b" ], 'identity function no-op' );
    is_deeply( [ sort_by { $_ } "b", "a" ], [ "a", "b" ], 'identity function on $_' );

    is_deeply( [ sort_by { reverse $_ } "az", "by" ], [ "by", "az" ], 'reverse function' );
}

{
    my $expected;
    my @gots;
    my @array;

    is_deeply( [ sort_by { } ], [], 'rev_sort_by with empty list' );

    $expected = ["b", "a"];
    @gots = rev_sort_by { $_ } "a", "b";
    is_deeply(\@gots, $expected, 'reverse sort identity function');

    push @array, { foo => 'aaa' };
    push @array, { foo => 'bbb' };

    @gots = rev_sort_by { $_->{foo} } @array;
    $expected = [ { foo => "bbb" }, { foo => "aaa" } ];
    is_deeply(\@gots, $expected, "reverse sort by hash key 'foo'");
}

done_testing;