File: Catmandu-Fix-Condition.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 (75 lines) | stat: -rw-r--r-- 2,186 bytes parent folder | download
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
use strict;
use warnings;
use Catmandu::Fix;
use Test::More;
use Test::Exception;

my $pkg;

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

my $fixer;
my $fixes;

# ALL_MATCH
$fixes = "if all_match('oogly.*', 'doogly') upcase('foo') end";

ok $fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly']}),
    {foo => 'LOW', oogly => ['doogly']}, "if all_match - check all match";

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly', '!doogly!']}),
    {foo => 'LOW', oogly => ['doogly', '!doogly!']},
    "if all_match - check all match (2)";

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly', 'something']}),
    {foo => 'low', oogly => ['doogly', 'something']},
    "if all_match - check not all match";

is_deeply $fixer->fix({foo => 'low'}), {foo => 'low'},
    "if all_match - check no match";

# ANY_MATCH
$fixes = "if any_match('oogly.*', 'doogly') upcase('foo') end";

ok $fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly']}),
    {foo => 'LOW', oogly => ['doogly']}, "if any_match - check all match";

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly', '!doogly!']}),
    {foo => 'LOW', oogly => ['doogly', '!doogly!']},
    "if any_match - check all match (2)";

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly', 'something']}),
    {foo => 'LOW', oogly => ['doogly', 'something']},
    "if any_match - check not all match";

is_deeply $fixer->fix({foo => 'low'}), {foo => 'low'},
    "if any_match - check no match";

# EXISTS
$fixes = "if exists('oogly') upcase('foo') end";

ok $fixer = Catmandu::Fix->new(fixes => [$fixes]);

is_deeply $fixer->fix({foo => 'low', oogly => ['doogly']}),
    {foo => 'LOW', oogly => ['doogly']}, "if exists - check match";

is_deeply $fixer->fix({foo => 'low'}), {foo => 'low'},
    "if exists - check no match";

# USE AS INLINE FIX
{
    use Catmandu::Fix::Condition::exists as => 'has_field';
    my $item = {foo => {bar => 1}};
    ok has_field($item,  'foo.bar'), 'inline condition - true';
    ok !has_field($item, 'doz'),     'inline condition - false';
}

done_testing;