File: Catmandu-Fix.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 (234 lines) | stat: -rw-r--r-- 7,633 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env perl
use strict;
use utf8;
use warnings;
use Test::More;
use Test::Exception;
use IO::File;
use Catmandu::Importer::Mock;
use Catmandu::Util qw(:is);

my $pkg;

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

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

ok $fixer, 'create a new fixer';

is_deeply $fixer->fix({}), {}, 'fixing hashes';
is_deeply $fixer->fix({name => 'value'}), {name => 'value'};
is_deeply $fixer->fix({name => {name => 'value'}}),
    {name => {name => 'value'}};
is_deeply $fixer->fix({name => [{name => 'value'}]}),
    {name => [{name => 'value'}]};

throws_ok {$fixer->fix(IO::File->new("<t/myfixes.fix"))} 'Catmandu::BadArg',
    'throws Catmandu::BadArg';

is_deeply $fixer->fix([]), [], 'fixing arrays';
is_deeply $fixer->fix([{name => 'value'}]), [{name => 'value'}];
is_deeply $fixer->fix([{name => {name => 'value'}}]),
    [{name => {name => 'value'}}];
is_deeply $fixer->fix([{name => [{name => 'value'}]}]),
    [{name => [{name => 'value'}]}];

ok $fixer->fix(Catmandu::Importer::Mock->new(size => 13)), 'fixing iterators';
my $it = $fixer->fix(Catmandu::Importer::Mock->new(size => 13));
can_ok $it, 'count';
is $it->count, 13;

my $gen_n = 3;
my $ref   = $fixer->fix(
    sub {
        return undef unless $gen_n--;
        return {n => $gen_n};
    }
);
ok $ref, 'fixing a coderef';
ok is_code_ref($ref);
is $ref->()->{n}, 2;
is $ref->()->{n}, 1;
is $ref->()->{n}, 0;
is $ref->(),      undef;

# test logging
can_ok $fixer, 'log';
isa_ok $fixer->log,          'Log::Any::Proxy';
isa_ok $fixer->log->adapter, 'Log::Any::Adapter::Null';

# test error handling
{

    package DieFix;
    use Moo;
    with 'Catmandu::Fix::Base';
    sub emit {'die;'}
}

$fixer = Catmandu::Fix->new(fixes => [DieFix->new]);
throws_ok {
    $fixer->fix({});
}
'Catmandu::FixError';

$fixer = Catmandu::Fix->new(fixes => ['t/myfixes.fix']);
ok $fixer;
is_deeply $fixer->fix({}),
    {utf8_name =>
        'ვეპხის ტყაოსანი შოთა რუსთაველი'
    }, 'fixing utf8';

open(FH, '<:encoding(UTF-8)', 't/myfixes.fix');
$fixer = Catmandu::Fix->new(fixes => [\*FH]);
ok $fixer;
is_deeply $fixer->fix({}),
    {utf8_name =>
        'ვეპხის ტყაოსანი შოთა რუსთაველი'
    }, 'fixing utf8';
close(FH);

$fixer = Catmandu::Fix->new(fixes => [IO::File->new('< t/myfixes.fix')]);
ok $fixer;
is_deeply $fixer->fix({}),
    {utf8_name =>
        'ვეპხის ტყაოსანი შოთა რუსთაველი'
    }, 'fixing utf8';

# get

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data.$first,test)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}),
    {data => [qw(0 1 2)], test => 0}, 'get $first test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data.$last,test)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}),
    {data => [qw(0 1 2)], test => 2}, 'get $last test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data.1,test)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}),
    {data => [qw(0 1 2)], test => 1}, 'get position test arary';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data.1,test)']);
is_deeply $fixer->fix({data => {1 => 1}}), {data => {1 => 1}, test => 1},
    'get position test hash';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data.*,test)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}),
    {data => [qw(0 1 2)], test => 2}, 'get star test arary';

# set

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.1)']);
is_deeply $fixer->fix({data => 1}), {data => 1, test => [undef, 1]},
    'set position test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.$first)']);
is_deeply $fixer->fix({data => 1, test => [qw(0 1 2)]}),
    {data => 1, test => [qw(1 1 2)]}, 'set $first test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.$last)']);
is_deeply $fixer->fix({data => 1, test => [qw(0 1 2)]}),
    {data => 1, test => [qw(0 1 1)]}, 'set $last test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.$prepend)']);
is_deeply $fixer->fix({data => 1, test => [qw(0 1 2)]}),
    {data => 1, test => [qw(1 0 1 2)]}, 'set $prepend test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.$append)']);
is_deeply $fixer->fix({data => 1, test => [qw(0 1 2)]}),
    {data => 1, test => [qw(0 1 2 1)]}, 'set $append test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.*)']);
is_deeply $fixer->fix({data => 1, test => [qw(0 1 2)]}),
    {data => 1, test => [qw(1 1 1)]}, 'set star test';

$fixer = Catmandu::Fix->new(fixes => ['copy_field(data,test.1)']);
is_deeply $fixer->fix({data => 1, test => {}}),
    {data => 1, test => {1 => 1}}, 'set hash test';

# non matching paths are ignored

$fixer = Catmandu::Fix->new(fixes => ['upcase(data.0)']);
is_deeply $fixer->fix({}), {}, 'non matching paths are ignored';

# delete

$fixer = Catmandu::Fix->new(fixes => ['remove_field(data.$first)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(1 2)]},
    'remove $first test';

$fixer = Catmandu::Fix->new(fixes => ['remove_field(data.$last)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(0 1)]},
    'remove $last test';

$fixer = Catmandu::Fix->new(fixes => ['remove_field(data.1)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(0 2)]},
    'remove position test arary';

$fixer = Catmandu::Fix->new(fixes => ['remove_field(data.1)']);
is_deeply $fixer->fix({data => {1 => 1}}), {data => {}},
    'remove position test hash';

$fixer = Catmandu::Fix->new(fixes => ['remove_field(data.*)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => []},
    'remove star test arary';

# retain

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.$first)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(0)]},
    'retain_field $first test';

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.$last)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(2)]},
    'retain_field $last test';

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.1)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(1)]},
    'retain_field position test arary';

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.1)']);
is_deeply $fixer->fix({data => {1 => 1, 2 => 2}}), {data => {1 => 1}},
    'retain_field position test hash';

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.*)']);
is_deeply $fixer->fix({data => [qw(0 1 2)]}), {data => [qw(0 1 2)]},
    'retain_field star test arary';

$fixer = Catmandu::Fix->new(fixes => ['retain_field(data.*)']);
is_deeply $fixer->fix({data => {1 => 1, 2 => 2}}),
    {data => {1 => 1, 2 => 2}}, 'retain_field star test hash';

# path delimiter escapes

$fixer = Catmandu::Fix->new(fixes => [q|add_field('with\.a\.dot', Train)|]);
is_deeply $fixer->fix({}), {'with.a.dot' => 'Train'}, "add field with.a.dot";
$fixer = Catmandu::Fix->new(fixes => [q|copy_field('with\.a.dot', no.dot)|]);
is_deeply $fixer->fix({'with.a' => {'dot' => 'Train'}}),
    {'no' => {'dot' => 'Train'}, 'with.a' => {'dot' => 'Train'}},
    "move field with a dot to one without";

# preprocessing and variables

$fixer = Catmandu::Fix->new(
    fixes      => ['t/variables.fix'],
    preprocess => 1,
    variables  => {
        source => 'field1',
        target => 'field2',
        others => [qw(0 1)],
        beer   => 1,
        milk   => 0,
    }
);

is_deeply $fixer->fix({field1 => 'value'}),
    {field2 => 'value', other_0 => 0, other_1 => 1, drunk => 'yes'},
    'preprocessing: variable interpolation';

done_testing;