blib/lib/Data/Sah/Normalize.pm

filter: Statementstmt 90.0% 90 / 100 Branchbran 63.4% 52 / 82 Conditioncond 65.3% 17 / 26 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 75.3% 174 / 231 CC 52 SCAR 37.0
CC
52
Cov
76%
normalize_clset
47.2
normalize_schema
33.0
BEGIN
1.0
CRAP 40.4
↑ Summary
1 package Data::Sah::Normalize;
2  
3 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 2 our $DATE = '2021-07-29'; # DATE
5 2 our $DIST = 'Data-Sah-Normalize'; # DIST
6 2 our $VERSION = '0.051'; # VERSION
7  
8 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 require Exporter;
13 2 our @ISA = qw(Exporter);
14 2 our @EXPORT_OK = qw(
15 normalize_clset
16 normalize_schema
17  
18 $type_re
19 $clause_name_re
20 $clause_re
21 $attr_re
22 $funcset_re
23 $compiler_re
24 );
25  
26 2 our $type_re = qr/\A(?:[A-Za-z_]\w*::)*[A-Za-z_]\w*\z/;
27 2 our $clause_name_re = qr/\A[A-Za-z_]\w*\z/;
28 2 our $clause_re = qr/\A[A-Za-z_]\w*(?:\.[A-Za-z_]\w*)*\z/;
29 2 our $attr_re = $clause_re;
30 2 our $funcset_re = qr/\A(?:[A-Za-z_]\w*::)*[A-Za-z_]\w*\z/;
31 2 our $compiler_re = qr/\A[A-Za-z_]\w*\z/;
32 2 our $clause_attr_on_empty_clause_re = qr/\A(?:\.[A-Za-z_]\w*)+\z/;
33  
34 3 ▶sub normalize_clset($;$) {
Subroutine normalize_clset: called CC=36 SCAR=38.5
Pod documented
35 3 my ($clset0, $opts) = @_;
36 3 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
21
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
37  
38 3 my $clset = {};
39 3 for my $c (sort keys %$clset0) {
40 5 my $c0 = $c;
41  
42 5 my $v = $clset0->{$c};
43  
44 # ignore expression
45 5 my $expr;
46 5 ▶ if ($c =~ s/=\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ s/=\z//) 1 4 5
47 1 $expr++;
48 # XXX currently can't disregard merge prefix when checking
49 # conflict
50 1 ▶ die "Conflict between '$c=' and '$c'" if exists $clset0->{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $clset0->{$c} 0 1 1
51 1 $clset->{"$c.is_expr"} = 1;
52 }
53  
54 5 my $sc = "";
55 5 my $cn;
56 {
57 5 my $errp = "Invalid clause name syntax '$c0'"; # error prefix
58 5 ▶ if (!$expr && $c =~ s/\A!(?=.)//) {
Branch: 5 branches
BranchTrueFalseTotal
if (not $expr and $c =~ s/\A!(?=.)//) { } 1 4 5
elsif (not $expr and $c =~ s/(?<=.)\|\z//) { } 1 3 4
elsif (not $expr and $c =~ s/(?<=.)\&\z//) { } 1 2 3
elsif (not $expr and $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/) { } 1 1 2
elsif (not $c =~ /$Data::Sah::Normalize::clause_re/ || $c =~ /$Data::Sah::Normalize::clause_attr_on_empty_clause_re/) { } 0 1 1
Condition100%3/3
not $expr and $c =~ s/\A!(?=.)//
!ll&&!rl&&r
131
MC/DC100%2/2
not $expr and $c =~ s/\A!(?=.)//
not $expr $c =~ s/\A!(?=.)//
Truth table100%3/3
not(A) and B
A= not $exprB= $c =~ s/\A!(?=.)//
ABresult
0X0
100
111
59 1 ▶ die "$errp, syntax should be !CLAUSE"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$Data::Sah::Normalize::clause_name_re/ 0 1 1
60 unless $c =~ $clause_name_re;
61 1 $sc = "!";
62 ▶ } elsif (!$expr && $c =~ s/(?<=.)\|\z//) {
Condition100%3/3
not $expr and $c =~ s/(?<=.)\|\z//
!ll&&!rl&&r
121
MC/DC100%2/2
not $expr and $c =~ s/(?<=.)\|\z//
not $expr $c =~ s/(?<=.)\|\z//
Truth table100%3/3
not(A) and B
A= not $exprB= $c =~ s/(?<=.)\|\z//
ABresult
0X0
100
111
63 1 ▶ die "$errp, syntax should be CLAUSE|"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$Data::Sah::Normalize::clause_name_re/ 0 1 1
64 unless $c =~ $clause_name_re;
65 1 $sc = "|";
66 ▶ } elsif (!$expr && $c =~ s/(?<=.)\&\z//) {
Condition100%3/3
not $expr and $c =~ s/(?<=.)\&\z//
!ll&&!rl&&r
111
MC/DC100%2/2
not $expr and $c =~ s/(?<=.)\&\z//
not $expr $c =~ s/(?<=.)\&\z//
Truth table100%3/3
not(A) and B
A= not $exprB= $c =~ s/(?<=.)\&\z//
ABresult
0X0
100
111
67 1 ▶ die "$errp, syntax should be CLAUSE&"
Branch: 1 branch
BranchTrueFalseTotal
unless $c =~ /$Data::Sah::Normalize::clause_name_re/ 0 1 1
68 unless $c =~ $clause_name_re;
69 1 $sc = "&";
70 ▶ } elsif (!$expr && $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/) {
Condition66%2/3
not $expr and $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/
!ll&&!rl&&r
101
MC/DC50%1/2
not $expr and $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/
not $expr $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/
Truth table66%2/3
not(A) and B
A= not $exprB= $c =~ /\A([^.]+)(?:\.(.+))?\((\w+)\)\z/
ABresult
0X0
100
111
71 1 my ($c2, $a, $lang) = ($1, $2, $3);
72 1 ▶ die "$errp, syntax should be CLAUSE(LANG) or C.ATTR(LANG)"
Branch: 1 branch
BranchTrueFalseTotal
unless $c2 =~ /$Data::Sah::Normalize::clause_name_re/ and !defined($a) || $a =~ /$Data::Sah::Normalize::attr_re/ 0 1 1
Condition33%2/6
$c2 =~ /$Data::Sah::Normalize::clause_name_re/ and !defined($a) || $a =~ /$Data::Sah::Normalize::attr_re/
!ll&&!rl&&r
001
not defined $a or $a =~ /$Data::Sah::Normalize::attr_re/
l!l&&r!l&&!r
100
MC/DC0%0/3
$c2 =~ /$Data::Sah::Normalize::clause_name_re/ and !defined($a) || $a =~ /$Data::Sah::Normalize::attr_re/
$c2 =~ /$Data::Sah::Normalize::clause_name_re/ not defined $a $a =~ /$Data::Sah::Normalize::attr_re/
Truth table25%1/4
A and not(B) or C
A= $c2 =~ /$Data::Sah::Normalize::clause_name_re/B= not defined $aC= $a =~ /$Data::Sah::Normalize::attr_re/
ABCresult
0XX0
1000
11X1
1011
73 unless $c2 =~ $clause_name_re &&
74 (!defined($a) || $a =~ $attr_re);
75 1 $sc = "(LANG)";
76 1 ▶ $cn = $c2 . (defined($a) ? ".$a" : "") . ".alt.lang.$lang";
Branch: 1 branch
BranchTrueFalseTotal
defined $a ? : 0 1 1
77 ▶ } elsif ($c !~ $clause_re &&
Condition33%1/3
$c =~ /$Data::Sah::Normalize::clause_re/ || $c =~ /$Data::Sah::Normalize::clause_attr_on_empty_clause_re/
l!l&&r!l&&!r
100
MC/DC0%0/2
$c =~ /$Data::Sah::Normalize::clause_re/ || $c =~ /$Data::Sah::Normalize::clause_attr_on_empty_clause_re/
$c =~ /$Data::Sah::Normalize::clause_re/ $c =~ /$Data::Sah::Normalize::clause_attr_on_empty_clause_re/
Truth table33%1/3
A || B
A= $c =~ /$Data::Sah::Normalize::clause_re/B= $c =~ /$Data::Sah::Normalize::clause_attr_on_empty_clause_re/
ABresult
1X1
011
000
78 $c !~ $clause_attr_on_empty_clause_re) {
79 0 die "$errp, please use letter/digit/underscore only";
80 }
81 }
82  
83 # XXX can't disregard merge prefix when checking conflict
84 5 ▶ if ($sc eq '!') {
Branch: 4 branches
BranchTrueFalseTotal
if ($sc eq '!') { } 1 4 5
elsif ($sc eq '&') { } 1 3 4
elsif ($sc eq '|') { } 1 2 3
elsif ($sc eq '(LANG)') { } 1 1 2
85 die "Conflict between clause shortcuts '!$c' and '$c'"
86 1 ▶ if exists $clset0->{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $clset0->{$c} 0 1 1
87 die "Conflict between clause shortcuts '!$c' and '$c|'"
88 1 ▶ if exists $clset0->{"$c|"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$clset0{"$c|"} 0 1 1
89 die "Conflict between clause shortcuts '!$c' and '$c&'"
90 1 ▶ if exists $clset0->{"$c&"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$clset0{"$c&"} 0 1 1
91 1 $clset->{$c} = $v;
92 1 $clset->{"$c.op"} = "not";
93 } elsif ($sc eq '&') {
94 die "Conflict between clause shortcuts '$c&' and '$c'"
95 1 ▶ if exists $clset0->{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $clset0->{$c} 0 1 1
96 die "Conflict between clause shortcuts '$c&' and '$c|'"
97 1 ▶ if exists $clset0->{"$c|"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$clset0{"$c|"} 0 1 1
98 1 ▶ die "Clause 'c&' value must be an array"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 1 1
99 unless ref($v) eq 'ARRAY';
100 1 $clset->{$c} = $v;
101 1 $clset->{"$c.op"} = "and";
102 } elsif ($sc eq '|') {
103 die "Conflict between clause shortcuts '$c|' and '$c'"
104 1 ▶ if exists $clset0->{$c};
Branch: 1 branch
BranchTrueFalseTotal
if exists $clset0->{$c} 0 1 1
105 1 ▶ die "Clause 'c|' value must be an array"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "ARRAY" 0 1 1
106 unless ref($v) eq 'ARRAY';
107 1 $clset->{$c} = $v;
108 1 $clset->{"$c.op"} = "or";
109 } elsif ($sc eq '(LANG)') {
110 die "Conflict between clause '$c' and '$cn'"
111 1 ▶ if exists $clset0->{$cn};
Branch: 1 branch
BranchTrueFalseTotal
if exists $clset0->{$cn} 0 1 1
112 1 $clset->{$cn} = $v;
113 } else {
114 1 $clset->{$c} = $v;
115 }
116  
117 }
118 3 ▶ $clset->{req} = 1 if $opts->{has_req};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"has_req"} 0 3 3
119  
120 # XXX option to recursively normalize clset, any's of, all's of, ...
121 #if ($clset->{clset}) {
122 # local $opts->{has_req};
123 # if ($clset->{'clset.op'} && $clset->{'clset.op'} =~ /and|or/) {
124 # # multiple clause sets
125 # $clset->{clset} = map { $self->normalize_clset($_, $opts) }
126 # @{ $clset->{clset} };
127 # } else {
128 # $clset->{clset} = $self->normalize_clset($_, $opts);
129 # }
130 #}
131  
132 3 $clset;
133 }
134  
135 4 ▶sub normalize_schema($) {
Subroutine normalize_schema: called CC=17 SCAR=35.0
Pod documented
136 4 my $s = shift;
137  
138 4 my $ref = ref($s);
139 4 ▶ if (!defined($s)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $s) { } 0 4 4
elsif (not $ref) { } 2 2 4
elsif ($ref eq 'ARRAY') { } 2 0 2
140  
141 0 die "Schema is missing";
142  
143 } elsif (!$ref) {
144  
145 2 my $has_req = $s =~ s/\*\z//;
146 2 ▶ $s =~ $type_re or die "Invalid type syntax $s, please use ".
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /$Data::Sah::Normalize::type_re/ or die "Invalid type syntax $s, please use " . 'letter/digit/underscore only' 2 0 2
147 "letter/digit/underscore only";
148 2 ▶ return [$s, $has_req ? {req=>1} : {}];
Branch: 1 branch
BranchTrueFalseTotal
$has_req ? : 1 1 2
149  
150 } elsif ($ref eq 'ARRAY') {
151  
152 2 my $t = $s->[0];
153 2 ▶ my $has_req = $t && $t =~ s/\*\z//;
Condition33%1/3
$t && $t =~ s/\*\z//
!ll&&!rl&&r
020
MC/DC0%0/2
$t && $t =~ s/\*\z//
$t $t =~ s/\*\z//
Truth table33%1/3
A && B
A= $tB= $t =~ s/\*\z//
ABresult
0X0
100
111
154 2 ▶ if (!defined($t)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $t) { } 0 2 2
elsif (ref $t) { } 0 2 2
155 0 die "For array form, at least 1 element is needed for type";
156 } elsif (ref $t) {
157 0 die "For array form, first element must be a string";
158 }
159 2 ▶ $t =~ $type_re or die "Invalid type syntax $s, please use ".
Branch: 1 branch
BranchTrueFalseTotal
$t =~ /$Data::Sah::Normalize::type_re/ or die "Invalid type syntax $s, please use " . 'letter/digit/underscore only' 2 0 2
160 "letter/digit/underscore only";
161  
162 2 my $clset0;
163 my $extras;
164 2 ▶ if (defined($s->[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s->[1]) { } 2 0 2
165 2 ▶ if (ref($s->[1]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s->[1] eq 'HASH') { } 2 0 2
166 2 $clset0 = $s->[1];
167 2 $extras = $s->[2];
168 2 ▶ die "For array form, there should not be more than 3 elements"
Branch: 1 branch
BranchTrueFalseTotal
if @$s > 3 0 2 2
169 if @$s > 3;
170 } else {
171 # flattened clause set [t, c=>1, c2=>2, ...]
172 0 ▶ die "For array in the form of [t, c1=>1, ...], there must be ".
Branch: 1 branch
BranchTrueFalseTotal
unless @$s % 2 0 0 0
173 "3 elements (or 5, 7, ...)"
174 unless @$s % 2;
175 0 $clset0 = { @{$s}[1..@$s-1] };
176 }
177 } else {
178 0 $clset0 = {};
179 }
180  
181 # check clauses and parse shortcuts (!c, c&, c|, c=)
182 2 my $clset = normalize_clset($clset0, {has_req=>$has_req});
183 2 ▶ if (defined $extras) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extras) { } 2 0 2
184 2 ▶ die "For array form with 3 elements, extras must be hash"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $extras eq "HASH" 0 2 2
185 unless ref($extras) eq 'HASH';
186 2 ▶ die "Extras must be empty hashref (Sah 0.9.47)" if keys %$extras;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$extras 1 1 2
187 # we remove extras to comply with Sah 0.9.47+
188 1 return [$t, $clset];
189 } else {
190 0 return [$t, $clset];
191 }
192 }
193  
194 0 die "Schema must be a string or arrayref (not $ref)";
195 }
196  
197 2 1;
198 # ABSTRACT: Normalize Sah schema
199  
200 __END__
↑ Summary