File: Catmandu-Fix-sort_field.t

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (67 lines) | stat: -rw-r--r-- 2,285 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use Test::Exception;

my $pkg;

BEGIN {
    $pkg = 'Catmandu::Fix::sort_field';
    use_ok $pkg;
}

is_deeply $pkg->new('tags')->fix({tags => ["foo", "bar", "bar"]}),
    {tags => ["bar", "bar", "foo"]}, "sort alphanum";

is_deeply $pkg->new('tags', uniq => 1)->fix({tags => ["foo", "bar", "bar"]}),
    {tags => ["bar", "foo"]}, "sort unique";

is_deeply $pkg->new('tags')
    ->fix({tags => [undef, undef, "b", "b", "c", "a"]}),
    {tags => ["a", "b", "b", "c", undef, undef]},
    "sort with undefined values";

is_deeply $pkg->new('tags', undef_position => "last")
    ->fix({tags => ["b", undef, undef, "b", "c", "a"]}),
    {tags => ["a", "b", "b", "c", undef, undef]},
    "sort with undefined values, undef last";

is_deeply $pkg->new('tags', undef_position => "first")
    ->fix({tags => [undef, "b", "b", "c", undef, "a"]}),
    {tags => [undef, undef, "a", "b", "b", "c"]},
    "sort with undefined values, undef first";

is_deeply $pkg->new('tags', undef_position => "delete")
    ->fix({tags => [undef, undef, "b", "b", "c", "a"]}),
    {tags => ["a", "b", "b", "c"]},
    "sort with undefined values, remove undef";

is_deeply $pkg->new('tags', uniq => 1)
    ->fix({tags => [undef, undef, "b", "b", "c", "a"]}),
    {tags => ["a", "b", "c", undef]}, "sort uniq with undefined values";

is_deeply $pkg->new('tags', uniq => 1, undef_position => "last")
    ->fix({tags => ["b", undef, undef, "b", "c", "a"]}),
    {tags => ["a", "b", "c", undef]},
    "sort uniq with undefined values, undef last";

is_deeply $pkg->new('tags', uniq => 1, undef_position => "first")
    ->fix({tags => [undef, "b", "b", "c", undef, "a"]}),
    {tags => [undef, "a", "b", "c"]},
    "sort uniq with undefined values, undef first";

is_deeply $pkg->new('tags', uniq => 1, undef_position => "delete")
    ->fix({tags => [undef, undef, "b", "b", "c", "a"]}),
    {tags => ["a", "b", "c"]},
    "sort uniq with undefined values, remove undef";

is_deeply $pkg->new('tags', uniq => 1, reverse => 1)
    ->fix({tags => ["foo", "bar", "bar"]}), {tags => ["foo", "bar"]},
    "sort unique reverse";

is_deeply $pkg->new('nums', numeric => 1)->fix({nums => [100, 1, 10]}),
    {nums => [1, 10, 100]}, "sort numeric";

done_testing 13;