Test/Data/Sah/Perl.pm

filter: Statementstmt 97.4% 76 / 78 Branchbran 80.0% 32 / 40 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 93.3% 140 / 150 CC 30 SCAR 28.7
CC
30
Cov
92%
__ANON__
25.7
__ANON__
6.0
run_spectest_for_perl
1.0
CRAP 17.6
« Data/Sah/Compiler/human/TH/HasElems.pm ↑ Summary Data/Sah/Compiler/perl/TH/hash.pm »
1 package Test::Data::Sah::Perl;
2  
3 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Test::Data::Sah qw(run_spectest all_match);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Test::More 0.98;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Data::Sah qw(gen_validator);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
15 2 our $DATE = '2024-02-16'; # DATE
16 2 our $DIST = 'Data-Sah'; # DIST
17 2 our $VERSION = '0.917'; # VERSION
18  
19 2 our @EXPORT_OK = qw(run_spectest_for_perl);
20  
21 1 ▶sub run_spectest_for_perl {
Subroutine run_spectest_for_perl: called CC=1 SCAR=0.0
Pod documented
22 run_spectest(
23 test_merge_clause_sets => 1,
24 1503 ▶ test_func => sub {
Subroutine __ANON__: called CC=25 SCAR=32.5
25 1503 my $test = shift;
26  
27 1503 my $data = $test->{input};
28 1503 my $ho = exists($test->{output}); # has output
29 1503 my $vbool;
30 1503 eval { $vbool = gen_validator(
31 1503 $test->{schema}, {accept_ref=>$ho}) };
32 1503 my $eval_err = $@;
33 1503 ▶ if ($test->{dies}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'dies'}) { } 33 1470 1503
34 33 ok($eval_err, "compile error");
35 33 return;
36 } else {
37 1470 ▶ ok(!$eval_err, "compile success") or do {
Branch: 1 branch
BranchTrueFalseTotal
&ok(!$eval_err, "compile success") or do { diag($eval_err); return } 1470 0 1470
38 0 diag $eval_err;
39 0 return;
40 };
41 }
42  
43 1470 ▶ if ($test->{valid_inputs}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($test->{'valid_inputs'}) { } 25 1445 1470
elsif (exists $test->{'valid'}) { } 1445 0 1445
44 # test multiple inputs, currently only done for rt=bool_valid
45 25 for my $i (0..@{ $test->{valid_inputs} }-1) {
46 51 my $data = $test->{valid_inputs}[$i];
47 51 ▶ ok($vbool->($ho ? \$data : $data), "valid input [$i]");
Branch: 1 branch
BranchTrueFalseTotal
$ho ? : 0 51 51
48 }
49 25 for my $i (0..@{ $test->{invalid_inputs} }-1) {
50 60 my $data = $test->{invalid_inputs}[$i];
51 60 ▶ ok(!$vbool->($ho ? \$data : $data), "invalid input [$i]");
Branch: 1 branch
BranchTrueFalseTotal
$ho ? : 0 60 60
52 }
53 } elsif (exists $test->{valid}) {
54 # test a single input
55 1445 ▶ if ($test->{valid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'valid'}) { } 833 612 1445
56 833 ▶ ok($vbool->($ho ? \$data : $data), "valid (rt=bool_valid)");
Branch: 1 branch
BranchTrueFalseTotal
$ho ? : 6 827 833
57 833 ▶ if ($ho) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ho) 6 827 833
58 6 is_deeply($data, $test->{output}, "output");
59 }
60 } else {
61 612 ▶ ok(!$vbool->($ho ? \$data : $data), "invalid (rt=bool_valid)");
Branch: 1 branch
BranchTrueFalseTotal
$ho ? : 0 612 612
62 }
63 }
64  
65 my $vstr = gen_validator($test->{schema},
66 1470 {return_type=>'str_errmsg'});
67 1470 ▶ if (exists $test->{valid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test->{"valid"}) 1445 25 1470
68 1445 ▶ if ($test->{valid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($test->{'valid'}) { } 833 612 1445
69 833 is($vstr->($test->{input}), "", "valid (rt=str_errmsg)");
70 } else {
71 612 like($vstr->($test->{input}), qr/\S/, "invalid (rt=str_errmsg)");
72 }
73 }
74  
75 my $vfull = gen_validator($test->{schema},
76 1470 {return_type=>'hash_details'});
77 1470 my $res = $vfull->($test->{input});
78 1470 is(ref($res), 'HASH', "validator (rt=hash_details) returns hash");
79 1470 ▶ if (exists($test->{errors}) || exists($test->{warnings}) ||
Branch: 1 branch
BranchTrueFalseTotal
if (exists $test->{"errors"} or exists $test->{"warnings"} or exists $test->{"valid"}) 1445 25 1470
Condition100%6/6
exists $test->{"errors"} or exists $test->{"warnings"} or exists $test->{"valid"}
l!l&&r!l&&!r
282116325
exists $test->{"errors"} or exists $test->{"warnings"}
l!l&&r!l&&!r
27391188
MC/DC100%3/3
exists $test->{"errors"} or exists $test->{"warnings"} or exists $test->{"valid"}
exists $test->{"errors"} exists $test->{"warnings"} exists $test->{"valid"}
Truth table100%4/4
A or B or C
A= exists $test->{"errors"}B= exists $test->{"warnings"}C= exists $test->{"valid"}
ABCresult
1XX1
01X1
0011
0000
80 exists($test->{valid})) {
81 1445 ▶ my $errors = $test->{errors} // ($test->{valid} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$test->{'valid'} ? : 833 339 1172
Condition100%2/2
$test->{"errors"} // ($test->{"valid"} ? 0 : 1)
l!l
2731172
MC/DC100%1/1
$test->{"errors"} // ($test->{"valid"} ? 0 : 1)
$test->{"errors"}
Truth table100%2/2
A
A= $test->{"errors"}
Aresult
11
00
82 1445 ▶ is(scalar(keys %{ $res->{errors} // {} }), $errors, "errors (rt=hash_details)")
Branch: 1 branch
BranchTrueFalseTotal
&is(scalar keys %{$res->{"errors"} // {};}, $errors, "errors (rt=hash_details)") or diag(explain($res)) 1445 0 1445
Condition100%2/2
$res->{"errors"} // {}
l!l
759686
MC/DC100%1/1
$res->{"errors"} // {}
$res->{"errors"}
Truth table100%2/2
A
A= $res->{"errors"}
Aresult
11
00
83 or diag explain $res;
84 1445 ▶ my $warnings = $test->{warnings} // 0;
Condition100%2/2
$test->{"warnings"} // 0
l!l
91436
MC/DC100%1/1
$test->{"warnings"} // 0
$test->{"warnings"}
Truth table100%2/2
A
A= $test->{"warnings"}
Aresult
11
00
85 1445 ▶ is(scalar(keys %{ $res->{warnings} // {} }), $warnings,
Branch: 1 branch
BranchTrueFalseTotal
&is(scalar keys %{$res->{"warnings"} // {};}, $warnings, "warnings (rt=hash_details)") or diag(explain($res)) 1445 0 1445
Condition100%2/2
$res->{"warnings"} // {}
l!l
91436
MC/DC100%1/1
$res->{"warnings"} // {}
$res->{"warnings"}
Truth table100%2/2
A
A= $res->{"warnings"}
Aresult
11
00
86 "warnings (rt=hash_details)")
87 or diag explain $res;
88 }
89 }, # test_func
90  
91 1584 ▶ skip_if => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
92 1584 my $t = shift;
93 1584 ▶ return 0 unless $t->{tags};
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"tags"} 0 1584 1584
94  
95 # disabled temporarily because failing for bool, even though i've
96 # adjust stuffs. but 'between' clause should be very seldomly used
97 # on bool, moreover with op, so i haven't looked into it.
98 return "currently failing"
99 1584 ▶ if all_match([qw/type:bool clause:between op/], $t->{tags});
Branch: 1 branch
BranchTrueFalseTotal
if all_match(["type:bool", "clause:between", "op"], $t->{"tags"}) 28 1556 1584
100  
101 1556 for (qw/
102  
103 check
104 check_each_elem
105 check_each_index
106 check_each_key
107 check_each_value
108 check_prop
109 exists
110 if
111 postfilters
112 prop
113 uniq
114  
115 /) {
116 return "clause $_ not yet implemented"
117 16998 ▶ if all_match(["clause:$_"], $t->{tags});
Branch: 1 branch
BranchTrueFalseTotal
if all_match(["clause:$_"], $t->{"tags"}) 32 16966 16998
118 }
119  
120 return "properties are not yet implemented"
121 1524 ▶ if grep {/^prop:/} @{ $t->{tags} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {/^prop:/;} @{$$t{"tags"};} 21 1503 1524
122  
123 1503 0;
124 }, # skip_if
125  
126 1 );
127 }
128  
129 2 1;
130 # ABSTRACT: Routines for testing Data::Sah (perl compiler)
131  
132 __END__
« Data/Sah/Compiler/human/TH/HasElems.pm ↑ Summary Data/Sah/Compiler/perl/TH/hash.pm »