line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Config::IOD::Common; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
402
|
use 5.010; |
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
24
|
|
5
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
440
|
use Test::More 0.98; |
|
1
|
|
|
|
|
47742
|
|
|
1
|
|
|
|
|
7
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY |
10
|
|
|
|
|
|
|
our $DATE = '2022-05-02'; # DATE |
11
|
|
|
|
|
|
|
our $DIST = 'Config-IOD-Reader'; # DIST |
12
|
|
|
|
|
|
|
our $VERSION = '0.345'; # VERSION |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $CLASS = "Config::IOD::Reader"; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub test_common_iod { |
17
|
|
|
|
|
|
|
|
18
|
1
|
50
|
|
1
|
0
|
110
|
eval "require $CLASS"; die if $@; ## no critic: BuiltinFunctions::ProhibitStringyEval |
|
1
|
|
|
|
|
5
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
subtest "opt: default_section" => sub { |
21
|
1
|
|
|
1
|
|
8852
|
test_read_iod( |
22
|
|
|
|
|
|
|
args => {default_section=>'bawaan'}, |
23
|
|
|
|
|
|
|
input => <<'_', |
24
|
|
|
|
|
|
|
a=1 |
25
|
|
|
|
|
|
|
_ |
26
|
|
|
|
|
|
|
result => {bawaan=>{a=>1}}, |
27
|
|
|
|
|
|
|
); |
28
|
1
|
|
|
|
|
10
|
}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
subtest "opt: allow_directives" => sub { |
31
|
1
|
|
|
1
|
|
661
|
test_read_iod( |
32
|
|
|
|
|
|
|
args => {allow_directives=>['merge']}, |
33
|
|
|
|
|
|
|
input => <<'_', |
34
|
|
|
|
|
|
|
;!noop |
35
|
|
|
|
|
|
|
_ |
36
|
|
|
|
|
|
|
dies => 1, |
37
|
|
|
|
|
|
|
); |
38
|
1
|
|
|
|
|
1147
|
test_read_iod( |
39
|
|
|
|
|
|
|
args => {allow_directives=>['noop']}, |
40
|
|
|
|
|
|
|
input => <<'_', |
41
|
|
|
|
|
|
|
;!noop |
42
|
|
|
|
|
|
|
_ |
43
|
|
|
|
|
|
|
result => {}, |
44
|
|
|
|
|
|
|
); |
45
|
1
|
|
|
|
|
2966
|
}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
subtest "opt: disallow_directives" => sub { |
48
|
1
|
|
|
1
|
|
703
|
test_read_iod( |
49
|
|
|
|
|
|
|
args => {disallow_directives=>['noop']}, |
50
|
|
|
|
|
|
|
input => <<'_', |
51
|
|
|
|
|
|
|
;!noop |
52
|
|
|
|
|
|
|
_ |
53
|
|
|
|
|
|
|
dies => 1, |
54
|
|
|
|
|
|
|
); |
55
|
1
|
|
|
|
|
1079
|
test_read_iod( |
56
|
|
|
|
|
|
|
args => {disallow_directives=>['merge']}, |
57
|
|
|
|
|
|
|
input => <<'_', |
58
|
|
|
|
|
|
|
;!noop |
59
|
|
|
|
|
|
|
_ |
60
|
|
|
|
|
|
|
result => {}, |
61
|
|
|
|
|
|
|
); |
62
|
1
|
|
|
|
|
2561
|
}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
subtest "opt: allow_directives + disallow_directives" => sub { |
65
|
1
|
|
|
1
|
|
659
|
test_read_iod( |
66
|
|
|
|
|
|
|
args => { |
67
|
|
|
|
|
|
|
allow_directives => ['noop'], |
68
|
|
|
|
|
|
|
disallow_directives => ['noop'], |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
input => <<'_', |
71
|
|
|
|
|
|
|
;!noop |
72
|
|
|
|
|
|
|
_ |
73
|
|
|
|
|
|
|
dies => 1, |
74
|
|
|
|
|
|
|
); |
75
|
1
|
|
|
|
|
2603
|
}; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
subtest "opt: enable_directive=0" => sub { |
78
|
1
|
|
|
1
|
|
643
|
test_read_iod( |
79
|
|
|
|
|
|
|
args => {enable_directive=>0}, |
80
|
|
|
|
|
|
|
input => <<'_', |
81
|
|
|
|
|
|
|
[s1] |
82
|
|
|
|
|
|
|
a=1 |
83
|
|
|
|
|
|
|
[s2] |
84
|
|
|
|
|
|
|
;!merge s1 |
85
|
|
|
|
|
|
|
b=2 |
86
|
|
|
|
|
|
|
_ |
87
|
|
|
|
|
|
|
result => {s1=>{a=>1}, s2=>{b=>2}}, |
88
|
|
|
|
|
|
|
); |
89
|
1
|
|
|
|
|
1912
|
test_read_iod( |
90
|
|
|
|
|
|
|
args => {enable_directive=>0}, |
91
|
|
|
|
|
|
|
input => <<'_', |
92
|
|
|
|
|
|
|
[s1] |
93
|
|
|
|
|
|
|
a=1 |
94
|
|
|
|
|
|
|
[s2] |
95
|
|
|
|
|
|
|
!merge s1 |
96
|
|
|
|
|
|
|
b=2 |
97
|
|
|
|
|
|
|
_ |
98
|
|
|
|
|
|
|
dies => 1, |
99
|
|
|
|
|
|
|
); |
100
|
1
|
|
|
|
|
2034
|
}; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
subtest "opt: enable_quoting=0" => sub { |
103
|
1
|
|
|
1
|
|
666
|
test_read_iod( |
104
|
|
|
|
|
|
|
args => {enable_quoting=>0}, |
105
|
|
|
|
|
|
|
input => <<'_', |
106
|
|
|
|
|
|
|
name="1\n2" |
107
|
|
|
|
|
|
|
_ |
108
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>'"1\\n2"'}}, |
109
|
|
|
|
|
|
|
); |
110
|
1
|
|
|
|
|
2132
|
}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
subtest "opt: enable_bracket=0" => sub { |
113
|
1
|
|
|
1
|
|
651
|
test_read_iod( |
114
|
|
|
|
|
|
|
args => {enable_bracket=>0}, |
115
|
|
|
|
|
|
|
input => <<'_', |
116
|
|
|
|
|
|
|
name=[1,2,3] |
117
|
|
|
|
|
|
|
_ |
118
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>'[1,2,3]'}}, |
119
|
|
|
|
|
|
|
); |
120
|
1
|
|
|
|
|
2772
|
}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
subtest "opt: enable_brace=0" => sub { |
123
|
1
|
|
|
1
|
|
666
|
test_read_iod( |
124
|
|
|
|
|
|
|
args => {enable_brace=>0}, |
125
|
|
|
|
|
|
|
input => <<'_', |
126
|
|
|
|
|
|
|
name={"a":1} |
127
|
|
|
|
|
|
|
_ |
128
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>'{"a":1}'}}, |
129
|
|
|
|
|
|
|
); |
130
|
1
|
|
|
|
|
2761
|
}; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
subtest "opt: enable_encoding=0" => sub { |
133
|
1
|
|
|
1
|
|
697
|
test_read_iod( |
134
|
|
|
|
|
|
|
args => {enable_encoding=>0}, |
135
|
|
|
|
|
|
|
input => <<'_', |
136
|
|
|
|
|
|
|
name=!hex 5e5e |
137
|
|
|
|
|
|
|
_ |
138
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>'!hex 5e5e'}}, |
139
|
|
|
|
|
|
|
); |
140
|
1
|
|
|
|
|
2684
|
}; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
subtest "opt: allow_encodings" => sub { |
143
|
1
|
|
|
1
|
|
669
|
test_read_iod( |
144
|
|
|
|
|
|
|
args => {allow_encodings=>['hex']}, |
145
|
|
|
|
|
|
|
input => <<'_', |
146
|
|
|
|
|
|
|
name=!json "1\n2" |
147
|
|
|
|
|
|
|
_ |
148
|
|
|
|
|
|
|
dies => 1, |
149
|
|
|
|
|
|
|
); |
150
|
1
|
|
|
|
|
1079
|
test_read_iod( |
151
|
|
|
|
|
|
|
args => {allow_encodings=>['json']}, |
152
|
|
|
|
|
|
|
input => <<'_', |
153
|
|
|
|
|
|
|
name=!json "1\n2" |
154
|
|
|
|
|
|
|
name2=!j "3\n4" |
155
|
|
|
|
|
|
|
_ |
156
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>"1\n2", name2=>"3\n4"}}, |
157
|
|
|
|
|
|
|
); |
158
|
1
|
|
|
|
|
2714
|
}; |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
subtest "opt: disallow_encodings" => sub { |
161
|
1
|
|
|
1
|
|
688
|
test_read_iod( |
162
|
|
|
|
|
|
|
args => {disallow_encodings=>['json']}, |
163
|
|
|
|
|
|
|
input => <<'_', |
164
|
|
|
|
|
|
|
name=!json "1\n2" |
165
|
|
|
|
|
|
|
_ |
166
|
|
|
|
|
|
|
dies => 1, |
167
|
|
|
|
|
|
|
); |
168
|
1
|
|
|
|
|
1062
|
test_read_iod( |
169
|
|
|
|
|
|
|
args => {disallow_encodings=>['json']}, |
170
|
|
|
|
|
|
|
input => <<'_', |
171
|
|
|
|
|
|
|
name=!j "1\n2" |
172
|
|
|
|
|
|
|
_ |
173
|
|
|
|
|
|
|
dies => 1, |
174
|
|
|
|
|
|
|
); |
175
|
1
|
|
|
|
|
1108
|
test_read_iod( |
176
|
|
|
|
|
|
|
args => {disallow_encodings=>['hex']}, |
177
|
|
|
|
|
|
|
input => <<'_', |
178
|
|
|
|
|
|
|
name=!json "1\n2" |
179
|
|
|
|
|
|
|
_ |
180
|
|
|
|
|
|
|
result => {GLOBAL=>{name=>"1\n2"}}, |
181
|
|
|
|
|
|
|
); |
182
|
1
|
|
|
|
|
2767
|
}; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
subtest "opt: allow_encodings + disallow_encodings" => sub { |
185
|
1
|
|
|
1
|
|
659
|
test_read_iod( |
186
|
|
|
|
|
|
|
args => { |
187
|
|
|
|
|
|
|
allow_encodings =>['json'], |
188
|
|
|
|
|
|
|
disallow_encodings=>['json'], |
189
|
|
|
|
|
|
|
}, |
190
|
|
|
|
|
|
|
input => <<'_', |
191
|
|
|
|
|
|
|
name=!json "1\n2" |
192
|
|
|
|
|
|
|
_ |
193
|
|
|
|
|
|
|
dies => 1, |
194
|
|
|
|
|
|
|
); |
195
|
1
|
|
|
|
|
2804
|
}; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
subtest "opt: allow_bang_only=0" => sub { |
198
|
1
|
|
|
1
|
|
642
|
test_read_iod( |
199
|
|
|
|
|
|
|
args => {allow_bang_only=>0}, |
200
|
|
|
|
|
|
|
input => <<'_', |
201
|
|
|
|
|
|
|
a=1 |
202
|
|
|
|
|
|
|
!noop |
203
|
|
|
|
|
|
|
_ |
204
|
|
|
|
|
|
|
dies => 1, |
205
|
|
|
|
|
|
|
); |
206
|
1
|
|
|
|
|
2070
|
}; |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
subtest "opt: allow_duplicate_key=0" => sub { |
209
|
1
|
|
|
1
|
|
645
|
test_read_iod( |
210
|
|
|
|
|
|
|
args => {allow_duplicate_key=>0}, |
211
|
|
|
|
|
|
|
input => <<'_', |
212
|
|
|
|
|
|
|
a=1 |
213
|
|
|
|
|
|
|
a=2 |
214
|
|
|
|
|
|
|
_ |
215
|
|
|
|
|
|
|
dies => 1, |
216
|
|
|
|
|
|
|
); |
217
|
1
|
|
|
|
|
2059
|
}; |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
subtest "opt: ignore_unknown_directive=1" => sub { |
220
|
1
|
|
|
1
|
|
644
|
test_read_iod( |
221
|
|
|
|
|
|
|
args => {ignore_unknown_directive=>1}, |
222
|
|
|
|
|
|
|
input => <<'_', |
223
|
|
|
|
|
|
|
;!foo bar |
224
|
|
|
|
|
|
|
_ |
225
|
|
|
|
|
|
|
result => {}, |
226
|
|
|
|
|
|
|
); |
227
|
1
|
|
|
|
|
2090
|
}; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# temporarily placed here |
230
|
|
|
|
|
|
|
subtest "expr" => sub { |
231
|
1
|
|
|
1
|
|
652
|
test_read_iod( |
232
|
|
|
|
|
|
|
name => "must be enabled first", |
233
|
|
|
|
|
|
|
args => {}, |
234
|
|
|
|
|
|
|
input => <<'_', |
235
|
|
|
|
|
|
|
a=!e 1+1 |
236
|
|
|
|
|
|
|
_ |
237
|
|
|
|
|
|
|
dies => 1, |
238
|
|
|
|
|
|
|
); |
239
|
1
|
|
|
|
|
1110
|
test_read_iod( |
240
|
|
|
|
|
|
|
name => "must be valid", |
241
|
|
|
|
|
|
|
args => {enable_expr=>1}, |
242
|
|
|
|
|
|
|
input => <<'_', |
243
|
|
|
|
|
|
|
a=!e 1+ |
244
|
|
|
|
|
|
|
_ |
245
|
|
|
|
|
|
|
dies => 1, |
246
|
|
|
|
|
|
|
); |
247
|
1
|
|
|
|
|
1186
|
test_read_iod( |
248
|
|
|
|
|
|
|
args => {enable_expr=>1}, |
249
|
|
|
|
|
|
|
input => <<'_', |
250
|
|
|
|
|
|
|
a=!e 1+1 |
251
|
|
|
|
|
|
|
[sect] |
252
|
|
|
|
|
|
|
b=!e val("GLOBAL.a")*3 |
253
|
|
|
|
|
|
|
c=!e val("b") x 3 |
254
|
|
|
|
|
|
|
_ |
255
|
|
|
|
|
|
|
result => {GLOBAL=>{a=>2}, sect=>{b=>6, c=>666}}, |
256
|
|
|
|
|
|
|
); |
257
|
1
|
|
|
|
|
2594
|
}; |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sub test_read_iod { |
261
|
24
|
|
|
24
|
0
|
112
|
my %args = @_; |
262
|
|
|
|
|
|
|
|
263
|
24
|
|
|
|
|
53
|
my $parser_args = $args{args}; |
264
|
|
|
|
|
|
|
my $test_name = $args{name} // |
265
|
|
|
|
|
|
|
"{". join(", ", |
266
|
24
|
|
66
|
|
|
146
|
(map {"$_=$parser_args->{$_}"} |
|
24
|
|
|
|
|
198
|
|
267
|
|
|
|
|
|
|
sort keys %$parser_args), |
268
|
|
|
|
|
|
|
) . "}"; |
269
|
|
|
|
|
|
|
subtest $test_name => sub { |
270
|
|
|
|
|
|
|
|
271
|
24
|
|
|
24
|
|
16164
|
my $parser = $CLASS->new(%$parser_args); |
272
|
|
|
|
|
|
|
|
273
|
24
|
|
|
|
|
40
|
my $res; |
274
|
24
|
|
|
|
|
39
|
eval { |
275
|
24
|
50
|
|
|
|
58
|
if ($CLASS eq 'Config::IOD') { |
276
|
0
|
|
|
|
|
0
|
$res = $parser->read_string($args{input})->dump; |
277
|
|
|
|
|
|
|
} else { |
278
|
24
|
|
|
|
|
95
|
$res = $parser->read_string($args{input}); |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
}; |
281
|
24
|
|
|
|
|
50
|
my $err = $@; |
282
|
24
|
100
|
|
|
|
50
|
if ($args{dies}) { |
283
|
12
|
50
|
|
|
|
45
|
ok($err, "dies") or diag explain $res; |
284
|
12
|
|
|
|
|
3985
|
return; |
285
|
|
|
|
|
|
|
} else { |
286
|
|
|
|
|
|
|
ok(!$err, "doesn't die") |
287
|
12
|
50
|
|
|
|
55
|
or do { diag explain "err=$err"; return }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
288
|
12
|
50
|
|
|
|
4090
|
is_deeply($res, $args{result}, 'result') |
289
|
|
|
|
|
|
|
or diag explain $res; |
290
|
|
|
|
|
|
|
} |
291
|
24
|
|
|
|
|
152
|
}; |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
1; |
295
|
|
|
|
|
|
|
# ABSTRACT: Common tests for Config::IOD and Config::IOD::Reader |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
__END__ |