Test/Data/Sah.pm

filter: Statementstmt 62.7% 123 / 196 Branchbran 35.5% 32 / 90 Conditioncond 46.6% 14 / 30 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 73.9% 17 / 23 Podpod 100% 5 / 5 totaltotal 54.1% 196 / 362 CC 68 SCAR 36.6
CC
68
Cov
53%
run_spectest
88.8
test_sah_cases
55.1
__ANON__
21.5
CRAP 38.7
« Data/Sah/Compiler/Prog.pm ↑ Summary Data/Sah/Compiler.pm »
1 ## no critic: (ControlStructures::ProhibitUnreachableCode)
2  
3 package Test::Data::Sah;
4  
5 22 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 22 ▶use Test::More 0.98;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 22 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Data::Sah qw(gen_validator);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use File::Slurper qw(read_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 22 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 22 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
18 22 our $DATE = '2024-02-16'; # DATE
19 22 our $DIST = 'Data-Sah'; # DIST
20 22 our $VERSION = '0.917'; # VERSION
21  
22 22 our @EXPORT_OK = qw(
23 test_sah_cases
24 run_spectest
25 all_match
26 any_match
27 none_match
28 );
29  
30 # XXX support js & human testing too
31 26 ▶sub test_sah_cases {
Subroutine test_sah_cases: called CC=20 SCAR=40.1
Pod documented
32 26 my $tests = shift;
33 26 ▶ my $opts = shift // {};
Condition100%2/2
shift() // {}
l!l
125
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
34  
35 26 my $sah = Data::Sah->new;
36 26 my $plc = $sah->get_compiler('perl');
37  
38 26 ▶ my $gvopts = $opts->{gen_validator_opts} // {};
Condition100%2/2
$opts->{"gen_validator_opts"} // {}
l!l
125
MC/DC100%1/1
$opts->{"gen_validator_opts"} // {}
$opts->{"gen_validator_opts"}
Truth table100%2/2
A
A= $opts->{"gen_validator_opts"}
Aresult
11
00
39 26 ▶ my $rt = $gvopts->{return_type} // 'bool_valid';
Condition100%2/2
$gvopts->{"return_type"} // "bool_valid"
l!l
125
MC/DC100%1/1
$gvopts->{"return_type"} // "bool_valid"
$gvopts->{"return_type"}
Truth table100%2/2
A
A= $gvopts->{"return_type"}
Aresult
11
00
40  
41 26 for my $test (@$tests) {
42 278 my $v = gen_validator($test->{schema}, $gvopts);
43 278 my $res = $v->($test->{input});
44 my $name = $test->{name} //
45 "data " . dump($test->{input}) . " should".
46 ($test->{valid} ? " pass" : " not pass"). " schema " .
47 278 ▶ dump($test->{schema});
Branch: 1 branch
BranchTrueFalseTotal
$test->{'valid'} ? : 162 116 278
Condition50%1/2
$test->{"name"} // 'data ' . Test::Data::Sah::dump($test->{"input"}) . ' should' . ($test->{"valid"} ? " pass" : " not pass") . ' schema ' . Test::Data::Sah::dump($test->{"schema"})
l!l
0278
MC/DC0%0/1
$test->{"name"} // 'data ' . Test::Data::Sah::dump($test->{"input"}) . ' should' . ($test->{"valid"} ? " pass" : " not pass") . ' schema ' . Test::Data::Sah::dump($test->{"schema"})
$test->{"name"}
Truth table50%1/2
A
A= $test->{"name"}
Aresult
11
00
48 278 my $testres;
49 278 ▶ if ($test->{valid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'valid'}) { } 162 116 278
50 162 ▶ if ($rt eq 'bool_valid') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rt eq 'bool_valid') { } 161 1 162
elsif ($rt eq 'str_errmsg') { } 1 0 1
elsif ($rt eq 'hash_details') { } 0 0 0
51 161 $testres = ok($res, $name);
52 } elsif ($rt eq 'str_errmsg') {
53 1 ▶ $testres = is($res, "", $name) or diag explain $res;
Branch: 1 branch
BranchTrueFalseTotal
$testres = &is($res, "", $name) or diag(explain($res)) 1 0 1
54 } elsif ($rt eq 'hash_details') {
55 0 ▶ $testres = is(scalar keys(%{$res->{errors}}), 0, $name) or diag explain $res;
Branch: 1 branch
BranchTrueFalseTotal
$testres = &is(scalar keys %{$$res{"errors"};}, 0, $name) or diag(explain($res)) 0 0 0
56 }
57 } else {
58 116 ▶ if ($rt eq 'bool_valid') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rt eq 'bool_valid') { } 116 0 116
elsif ($rt eq 'str_errmsg') { } 0 0 0
elsif ($rt eq 'hash_details') { } 0 0 0
59 116 $testres = ok(!$res, $name);
60 } elsif ($rt eq 'str_errmsg') {
61 0 ▶ $testres = isnt($res, "", $name) or diag explain $res;
Branch: 1 branch
BranchTrueFalseTotal
$testres = &isnt($res, "", $name) or diag(explain($res)) 0 0 0
62 } elsif ($rt eq 'hash_details') {
63 0 ▶ $testres = isnt(scalar keys(%{$res->{errors}}), 0, $name) or diag explain $res;
Branch: 1 branch
BranchTrueFalseTotal
$testres = &isnt(scalar keys %{$$res{"errors"};}, 0, $name) or diag(explain($res)) 0 0 0
64 }
65 }
66 278 ▶ next if $testres;
Branch: 1 branch
BranchTrueFalseTotal
if $testres 278 0 278
67  
68 # when test fails, show the validator generated code to help debugging
69 0 my $cd = $plc->compile(schema => $test->{schema});
70 diag "schema compilation result:\n----begin generated code----\n",
71 explain($cd->{result}), "\n----end generated code----\n",
72 "that code should return ", ($test->{valid} ? "true":"false"),
73 0 ▶ " when fed \$data=", dump($test->{input}),
Branch: 1 branch
BranchTrueFalseTotal
$test->{'valid'} ? : 0 0 0
74 " but instead returns ", dump($res);
75  
76 # also show the result for return_type=hash_details
77 0 my $vhash = gen_validator($test->{schema}, {return_type=>"hash_details"});
78 diag "\nvalidator result (hash_details):\n----begin result----\n",
79 0 explain($vhash->($test->{input})), "----end result----";
80 }
81 }
82  
83 14 ▶sub _decode_json {
Subroutine _decode_json: called CC=1 SCAR=0.0
84 14 state $json = do {
85 1 require JSON;
86 1 JSON->new->allow_nonref;
87 };
88 14 $json->decode(@_);
89 }
90  
91 1 ▶sub run_spectest {
Subroutine run_spectest: called CC=21 SCAR=44.9
Pod documented
92 1 require File::ShareDir;
93 1 require File::ShareDir::Tarball;
94 1 require Sah;
95  
96 1 my %args = @_;
97  
98 1 my $sah = Data::Sah->new;
99  
100 1 my $dir;
101 1 ▶ if (version->parse($Sah::VERSION) == version->parse("0.9.27")) {
Branch: 1 branch
BranchTrueFalseTotal
if ('version'->parse($Sah::VERSION) == 'version'->parse('0.9.27')) { } 0 1 1
102 # this version of Sah temporarily uses ShareDir instead of
103 # ShareDir::Tarball due to garbled output problem of tarball.
104 0 $dir = File::ShareDir::dist_dir("Sah");
105 } else {
106 1 $dir = File::ShareDir::Tarball::dist_dir("Sah");
107 }
108 1 ▶ $dir && (-d $dir) or die "Can't find spectest, have you installed Sah?";
Branch: 1 branch
BranchTrueFalseTotal
$dir and -d $dir or die "Can't find spectest, have you installed Sah?" 1 0 1
Condition33%1/3
$dir and -d $dir
!ll&&!rl&&r
001
MC/DC0%0/2
$dir and -d $dir
$dir -d $dir
Truth table33%1/3
A and B
A= $dirB= -d $dir
ABresult
0X0
100
111
109 1 ▶ (-f "$dir/spectest/00-normalize_schema.json")
Branch: 1 branch
BranchTrueFalseTotal
-f "$dir/spectest/00-normalize_schema.json" or die "Something's wrong, spectest doesn't contain the correct files" 1 0 1
110 or die "Something's wrong, spectest doesn't contain the correct files";
111  
112 1 my @specfiles;
113 {
114 1 local $CWD = "$dir/spectest";
115 22 ▶ @specfiles = glob("*.json");
Subroutine BEGIN: called CC=1 SCAR=0.0
116 }
117  
118 # to test certain files only
119 1 my @files;
120 1 ▶ if ($ENV{TEST_SAH_SPECTEST_FILES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'TEST_SAH_SPECTEST_FILES'}) { } 0 1 1
121 0 @files = split /\s*,\s*|\s+/, $ENV{TEST_SAH_SPECTEST_FILES};
122 } else {
123 1 @files = @ARGV;
124 }
125  
126 # to test certain types only
127 1 my @types;
128 1 ▶ if ($ENV{TEST_SAH_SPECTEST_TYPES}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_SAH_SPECTEST_TYPES"}) 0 1 1
129 0 @types = split /\s*,\s*|\s+/, $ENV{TEST_SAH_SPECTEST_TYPES};
130 }
131  
132 # to test only tests that have all matching tags
133 1 my @include_tags;
134 1 ▶ if ($ENV{TEST_SAH_SPECTEST_INCLUDE_TAGS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_SAH_SPECTEST_INCLUDE_TAGS"}) 0 1 1
135 @include_tags = split /\s*,\s*|\s+/,
136 0 $ENV{TEST_SAH_SPECTEST_INCLUDE_TAGS};
137 }
138  
139 # to skip tests that have all matching tags
140 1 my @exclude_tags;
141 1 ▶ if ($ENV{TEST_SAH_SPECTEST_EXCLUDE_TAGS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_SAH_SPECTEST_EXCLUDE_TAGS"}) 0 1 1
142 @exclude_tags = split /\s*,\s*|\s+/,
143 0 $ENV{TEST_SAH_SPECTEST_EXCLUDE_TAGS};
144 }
145  
146 1503 ▶ my $code_test_excluded = sub {
Subroutine __ANON__: called CC=7 SCAR=30.7
147 1503 my $test = shift;
148  
149 1503 ▶ if ($test->{tags} && @exclude_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{"tags"} and @exclude_tags) 0 1503 1503
Condition33%1/3
$test->{"tags"} and @exclude_tags
!ll&&!rl&&r
015030
MC/DC0%0/2
$test->{"tags"} and @exclude_tags
$test->{"tags"} @exclude_tags
Truth table33%1/3
A and B
A= $test->{"tags"}B= @exclude_tags
ABresult
0X0
100
111
150 0 ▶ if (any_match(\@exclude_tags, $test->{tags})) {
Branch: 1 branch
BranchTrueFalseTotal
if (any_match(\@exclude_tags, $test->{"tags"})) 0 0 0
151 0 return "contains excluded tag(s) (".
152 join(", ", @exclude_tags).")";
153 }
154 }
155 1503 ▶ if (@include_tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@include_tags) 0 1503 1503
156 0 ▶ if (!all_match(\@include_tags, $test->{tags} // [])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (all_match(\@include_tags, $test->{"tags"} // [])) 0 0 0
Condition0%0/2
$test->{"tags"} // []
l!l
00
MC/DC0%0/1
$test->{"tags"} // []
$test->{"tags"}
Truth table0%0/2
A
A= $test->{"tags"}
Aresult
11
00
157 0 return "does not contain all include tags (".
158 join(", ", @include_tags).")";
159 }
160 }
161 1503 "";
162 1 };
163  
164 {
165 1 ▶ last unless $args{test_normalize_schema};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"test_normalize_schema"} 1 0 1
166  
167 0 for my $file ("00-normalize_schema.json") {
168 0 ▶ unless (!@files || grep { $_ eq $file } @files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not @files or grep {$_ eq $file;} @files) 0 0 0
Condition0%0/3
not @files or grep {$_ eq $file;} @files
l!l&&r!l&&!r
000
MC/DC0%0/2
not @files or grep {$_ eq $file;} @files
not @files grep {$_ eq $file;} @files
Truth table0%0/3
not(A) or B
A= not @filesB= grep {$_ eq $file;} @files
ABresult
1X1
011
000
169 0 diag "Skipping file $file";
170 0 next;
171 }
172 0 ▶ subtest $file => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
173 0 my $tspec = _decode_json(read_text("$dir/spectest/$file"));
174 0 for my $test (@{ $tspec->{tests} }) {
175 0 ▶ subtest $test->{name} => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
176 0 ▶ if (my $reason = $code_test_excluded->($test)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reason = &$code_test_excluded($test)) 0 0 0
177 0 plan skip_all => "Skipping test $test->{name}: $reason";
178 0 return;
179 }
180 0 eval {
181 is_deeply(normalize_schema($test->{input}),
182 0 $test->{result}, "result");
183 };
184 0 my $eval_err = $@;
185 0 ▶ if ($test->{dies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'dies'}) { } 0 0 0
186 0 ok($eval_err, "dies");
187 } else {
188 0 ▶ ok(!$eval_err, "doesn't die")
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$eval_err, "doesn't die") or diag($eval_err) 0 0 0
189 or diag $eval_err;
190 }
191 0 };
192 }
193 0 ok 1; # an extra dummy ok to pass even if all spectest is skipped
194 0 };
195 }
196 }
197  
198 {
199 1 ▶ last unless $args{test_merge_clause_sets};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"test_merge_clause_sets"} 0 1 1
200  
201 1 for my $file ("01-merge_clause_sets.json") {
202 1 last; # we currently remove _merge_clause_sets() from Data::Sah
203 0 ▶ unless (!@files || grep { $_ eq $file } @files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not @files or grep {$_ eq $file;} @files) 0 0 0
Condition0%0/3
not @files or grep {$_ eq $file;} @files
l!l&&r!l&&!r
000
MC/DC0%0/2
not @files or grep {$_ eq $file;} @files
not @files grep {$_ eq $file;} @files
Truth table0%0/3
not(A) or B
A= not @filesB= grep {$_ eq $file;} @files
ABresult
1X1
011
000
204 0 diag "Skipping file $file";
205 0 next;
206 }
207 0 ▶ subtest $file => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
208 0 my $tspec = _decode_json(scalar read_text("$dir/spectest/$file"));
209 0 for my $test (@{ $tspec->{tests} }) {
210 0 ▶ subtest $test->{name} => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
211 0 ▶ if (my $reason = $code_test_excluded->($test)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reason = &$code_test_excluded($test)) 0 0 0
212 0 plan skip_all => "Skipping test $test->{name}: $reason";
213 0 return;
214 }
215 0 eval {
216 0 is_deeply($sah->_merge_clause_sets(@{ $test->{input} }),
217 0 $test->{result}, "result");
218 };
219 0 my $eval_err = $@;
220 0 ▶ if ($test->{dies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'dies'}) { } 0 0 0
221 0 ok($eval_err, "dies");
222 } else {
223 0 ▶ ok(!$eval_err, "doesn't die")
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$eval_err, "doesn't die") or diag($eval_err) 0 0 0
224 or diag $eval_err;
225 }
226 0 };
227 }
228 0 ok 1; # an extra dummy ok to pass even if all spectest is skipped
229 0 };
230 }
231 }
232  
233 {
234 1 ▶ for my $file (grep {/^10-type-/ || /^20-clause-(prefilters)/} @specfiles) {
Condition100%3/3
/^10-type-/ || /^20-clause-(prefilters)/
l!l&&r!l&&!r
13110
MC/DC100%2/2
/^10-type-/ || /^20-clause-(prefilters)/
/^10-type-/ /^20-clause-(prefilters)/
Truth table100%3/3
A || B
A= /^10-type-/B= /^20-clause-(prefilters)/
ABresult
1X1
011
000
235 0 ▶ unless (!@files || grep { $_ eq $file } @files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not @files or grep {$_ eq $file;} @files) 0 14 14
Condition33%1/3
not @files or grep {$_ eq $file;} @files
l!l&&r!l&&!r
1400
MC/DC0%0/2
not @files or grep {$_ eq $file;} @files
not @files grep {$_ eq $file;} @files
Truth table33%1/3
not(A) or B
A= not @filesB= grep {$_ eq $file;} @files
ABresult
1X1
011
000
236 0 diag "Skipping file $file";
237 0 next;
238 }
239 14 ▶ subtest $file => sub {
Subroutine __ANON__: called CC=9 SCAR=22.5
240 14 diag "Loading $file ...";
241 14 my $tspec = _decode_json(read_text("$dir/spectest/$file"));
242 14 note "Test version: ", $tspec->{version};
243 14 my $tests = $tspec->{tests};
244 14 ▶ if ($args{tests_func}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'tests_func'}) { } 0 14 14
elsif ($args{'test_func'}) { } 14 0 14
245 0 $args{tests_func}->($tests, {
246 parent_args => \%args,
247 code_test_excluded => $code_test_excluded,
248 });
249 } elsif ($args{test_func}) {
250 14 for my $test (@$tests) {
251 1584 my $skip_reason;
252 {
253 1584 ▶ if ($args{skip_if}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_if"}) 1584 0 1584
254 1584 $skip_reason = $args{skip_if}->($test);
255 1584 ▶ last if $skip_reason;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_reason 81 1503 1584
256 }
257 1503 $skip_reason = $code_test_excluded->($test);
258 1503 ▶ last if $skip_reason;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_reason 0 1503 1503
259 }
260 1584 ▶ my $tname = "(tags=".join(", ", sort @{ $test->{tags} // [] }).
Condition50%1/2
$test->{"tags"} // []
l!l
15840
MC/DC0%0/1
$test->{"tags"} // []
$test->{"tags"}
Truth table50%1/2
A
A= $test->{"tags"}
Aresult
11
00
261 ") $test->{name}";
262 1584 ▶ if ($skip_reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_reason) 81 1503 1584
263 81 diag "Skipping test $tname: $skip_reason";
264 81 next;
265 }
266 1503 note explain $test;
267 1503 ▶ subtest $tname => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
268 1503 $args{test_func}->($test);
269 1503 };
270 } # for $test
271 14 ok 1; # an extra dummy ok to pass even if all spectest is skipped
272 } else {
273 0 die "Please specify 'test_func' or 'tests_func'";
274 }
275 14 }; # subtest $file
276 } # for $file
277 }
278  
279 }
280  
281 18582 ▶sub all_match {
Subroutine all_match: called CC=3 SCAR=11.0
Pod documented
282 18582 my ($list1, $list2) = @_;
283  
284 18582 for my $el (@$list1) {
285 18761 ▶ return 0 unless grep { $_ eq $el } @$list2;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $el;} @$list2 18522 239 18761
286 }
287 60 1;
288 }
289  
290 0 ▶sub any_match {
Subroutine any_match: not called CC=3 SCAR=24.8
Pod documented
291 0 my ($list1, $list2) = @_;
292  
293 0 for my $el (@$list1) {
294 0 ▶ return 1 if grep { $_ eq $el } @$list2;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $el;} @$list2 0 0 0
295 }
296 0 0;
297 }
298  
299 0 ▶sub none_match {
Subroutine none_match: not called CC=3 SCAR=24.8
Pod documented
300 0 my ($list1, $list2) = @_;
301  
302 0 for my $el (@$list1) {
303 0 ▶ return 0 if grep { $_ eq $el } @$list2;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $el;} @$list2 0 0 0
304 }
305 0 1;
306 }
307  
308 22 1;
309 # ABSTRACT: Test routines for Data::Sah
310  
311 __END__
« Data/Sah/Compiler/Prog.pm ↑ Summary Data/Sah/Compiler.pm »