blib/lib/Data/Domain.pm

filter: Statementstmt 94.1% 785 / 834 Branchbran 82.8% 328 / 396 Conditioncond 67.5% 123 / 182 MC/DCmcdc 50.4% 52 / 103 Subroutinesub 94.1% 162 / 172 Podpod 100% 11 / 11 totaltotal 86.0% 1461 / 1698 CC 274 SCAR 26.1
CC
274
Cov
88%
inspect
60.4
node_from_path
30.0
_inspect
29.0
CRAP 13.6
↑ Summary
1 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 #======================================================================
5 package Data::Domain; # documentation at end of file
6 #======================================================================
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Scalar::Does 0.007;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use List::Util qw/max uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use List::MoreUtils qw/part natatime any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use if $] < 5.037, experimental => 'smartmatch'; # smartmatch no longer experimental after 5.037
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use overload '""' => \&_stringify,
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.037 ? : 0 5 5
18 5 $] < 5.037 ? ('~~' => \&_matches) : (); # fully deprecated, so cannot be overloaded
19 5 ▶use match::simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 5 our $VERSION = "1.16";
22  
23 5 our $MESSAGE; # global var for last message from _matches()
24 5 our $MAX_DEEP = 100; # limit for recursive calls to inspect()
25 5 our $GLOBAL_MSGS; # table of default messages -- see below method messages()
26 5 our $USE_OLD_MSG_API; # flag for backward compatibility
27  
28 #----------------------------------------------------------------------
29 # exports
30 #----------------------------------------------------------------------
31  
32 # lists of symbols to export
33 5 my @CONSTRUCTORS;
34 my %SHORTCUTS;
35  
36 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
37 5 @CONSTRUCTORS = qw/Whatever Empty
38 Num Int Nat Date Time String Handle
39 Enum List Struct Struict One_of All_of/;
40 5 %SHORTCUTS = (
41 True => [ -true => 1 ],
42 False => [ -true => 0 ],
43 Defined => [ -defined => 1 ],
44 Undef => [ -defined => 0 ],
45 Blessed => [ -blessed => 1 ],
46 Unblessed => [ -blessed => 0 ],
47 Ref => [ -ref => 1 ],
48 Unref => [ -ref => 0 ],
49 Regexp => [ -does => 'Regexp' ],
50 Obj => [ -blessed => 1 ],
51 Class => [ -package => 1 ],
52 Coderef => [ -does => 'CODE' ],
53 );
54 }
55  
56 # setup exports through Sub::Exporter API
57 5 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
58 exports => [ 'node_from_path', # no longer used, but still present for backwards compat
59 75 (map {$_ => \&_wrap_domain } @CONSTRUCTORS ),
60 5 (map {$_ => \&_wrap_shortcut_options} keys %SHORTCUTS) ],
61 groups => { constructors => \@CONSTRUCTORS,
62 shortcuts => [keys %SHORTCUTS] },
63 collectors => { INIT => \&_sub_exporter_init },
64 installer => \&_sub_exporter_installer,
65 5 };
66  
67 # customize Sub::Exporter to support "bang-syntax" for excluding symbols
68 # see https://rt.cpan.org/Public/Bug/Display.html?id=80234
69 5 { my @dont_export;
70  
71 # detect symbols prefixed by '!' and remember them in @dont_export
72 5 ▶ sub _sub_exporter_init {
Subroutine _sub_exporter_init: called CC=1 SCAR=0.0
73 5 my ($collection, $context) = @_;
74 5 my $args = $context->{import_args};
75 my ($exclude, $regular_args)
76 5 ▶ = part {!ref $_->[0] && $_->[0] =~ /^!/ ? 0 : 1} @$args;
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
!ref($_->[0]) && $_->[0] =~ /^!/ ? : 1 5 6
Condition66%2/3
not ref $_->[0] and $_->[0] =~ /^!/
!ll&&!rl&&r
051
MC/DC50%1/2
not ref $_->[0] and $_->[0] =~ /^!/
not ref $_->[0] $_->[0] =~ /^!/
Truth table66%2/3
not(A) and B
A= not ref $_->[0]B= $_->[0] =~ /^!/
ABresult
0X0
100
111
77 5 @$args = @$regular_args;
78 5 @dont_export = map {substr($_->[0], 1)} @$exclude;
79 5 1;
80 }
81  
82 # install symbols, except those that belong to @dont_export
83 5 ▶ sub _sub_exporter_installer {
Subroutine _sub_exporter_installer: called CC=1 SCAR=0.0
84 5 my ($arg, $to_export) = @_;
85 5 my %export_hash = @$to_export;
86 5 delete @export_hash{@dont_export};
87 5 Sub::Exporter::default_installer($arg, [%export_hash]);
88 }
89 }
90  
91 # constructors group : for each domain constructor, we export a closure
92 # that just calls new() on the corresponding subclass. For example,
93 # Num(@args) is just equivalent to Data::Domain::Num->new(@args).
94 5 ▶sub _wrap_domain {
Subroutine _wrap_domain: called CC=1 SCAR=0.0
95 75 my ($class, $name, $args, $coll) = @_;
96 75 ▶ return sub {return "Data::Domain::$name"->new(@_)};
Subroutine __ANON__: called CC=1 SCAR=0.0
97 }
98  
99  
100 # # shortcuts group : calling 'Whatever' with various pre-built options
101 60 ▶sub _wrap_shortcut_options {
Subroutine _wrap_shortcut_options: called CC=1 SCAR=0.0
102 60 my ($class, $name, $args, $coll) = @_;
103 60 ▶ return sub {return Data::Domain::Whatever->new(@{$SHORTCUTS{$name}}, @_)};
Subroutine __ANON__: called CC=1 SCAR=0.0
104 }
105  
106  
107  
108 #----------------------------------------------------------------------
109 # messages
110 #----------------------------------------------------------------------
111  
112 80 ▶sub _msg_bool { # small closure generator for various messages below
Subroutine _msg_bool: called CC=1 SCAR=0.0
113 80 my ($must_be, $if_true, $if_false) = @_;
114 20 ▶ return sub {my ($name, $msg_id, $expected) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
115 80 ▶ "$name: $must_be " . ($expected ? $if_true : $if_false)};
Branch: 1 branch
BranchTrueFalseTotal
$expected ? : 12 8 20
116 }
117  
118  
119 5 my $builtin_msgs = {
120 english => {
121 Generic => {
122 UNDEFINED => "undefined data",
123 INVALID => "invalid",
124 TOO_SMALL => "smaller than minimum '%s'",
125 TOO_BIG => "bigger than maximum '%s'",
126 EXCLUSION_SET => "belongs to exclusion set",
127 MATCH_TRUE => _msg_bool("must be", "true", "false"),
128 MATCH_ISA => "is not a '%s'",
129 MATCH_CAN => "does not have method '%s'",
130 MATCH_DOES => "does not do '%s'",
131 MATCH_BLESSED => _msg_bool("must be", "blessed", "unblessed"),
132 MATCH_PACKAGE => _msg_bool("must be", "a package", "a non-package"),
133 MATCH_REF => _msg_bool("must be", "a reference", "a non-reference"),
134 MATCH_SMART => "does not smart-match '%s'",
135 MATCH_ISWEAK => _msg_bool("must be", "a weak reference", "a strong reference"),
136 MATCH_READONLY=> _msg_bool("must be", "readonly", "non-readonly"),
137 MATCH_TAINTED => _msg_bool("must be", "tainted", "untainted"),
138 },
139 Whatever => {
140 MATCH_DEFINED => _msg_bool("must be", "defined", "undefined"),
141 },
142 Num => {INVALID => "invalid number",},
143 Date => {INVALID => "invalid date",},
144 String => {
145 TOO_SHORT => "less than %d characters",
146 TOO_LONG => "more than %d characters",
147 SHOULD_MATCH => "should match '%s'",
148 SHOULD_NOT_MATCH => "should not match '%s'",
149 },
150 Handle => {INVALID => "is not an open filehandle"},
151 Enum => {NOT_IN_LIST => "not in enumeration list",},
152 List => {
153 NOT_A_LIST => "is not an arrayref",
154 TOO_SHORT => "less than %d items",
155 TOO_LONG => "more than %d items",
156 ANY => "should have at least one '%s'",
157 },
158 Struct => {
159 NOT_A_HASH => "is not a hashref",
160 FORBIDDEN_FIELD => "contains forbidden field(s): %s"
161 },
162 },
163  
164 "français" => {
165 Generic => {
166 UNDEFINED => "donnée non définie",
167 INVALID => "incorrect",
168 TOO_SMALL => "plus petit que le minimum '%s'",
169 TOO_BIG => "plus grand que le maximum '%s'",
170 EXCLUSION_SET => "fait partie des valeurs interdites",
171 MATCH_TRUE => _msg_bool("doit être", "vrai", "faux"),
172 MATCH_ISA => "n'est pas un '%s'",
173 MATCH_CAN => "n'a pas la méthode '%s'",
174 MATCH_DOES => "ne se comporte pas comme un '%s'",
175 MATCH_BLESSED => _msg_bool("doit être", "blessed", "unblessed"),
176 MATCH_PACKAGE => _msg_bool("doit être", "un package", "un non-package"),
177 MATCH_REF => _msg_bool("doit être", "une référence", "une non-référence"),
178 MATCH_SMART => "n'obéit pas au smart-match '%s'",
179 MATCH_ISWEAK => _msg_bool("doit être", "une weak reference", "une strong reference"),
180 MATCH_READONLY=> _msg_bool("doit être", "readonly", "non-readonly"),
181 MATCH_TAINTED => _msg_bool("doit être", "tainted", "untainted"),
182 },
183 Whatever => {
184 MATCH_DEFINED => _msg_bool("doit être", "défini", "non-défini"),
185 },
186 Num => {INVALID => "nombre incorrect",},
187 Date => {INVALID => "date incorrecte",},
188 String => {
189 TOO_SHORT => "moins de %d caractères",
190 TOO_LONG => "plus de %d caractères",
191 SHOULD_MATCH => "devrait être reconnu par la regex '%s'",
192 SHOULD_NOT_MATCH => "ne devrait pas être reconnu par la regex '%s'",
193 },
194 Handle => {INVALID => "n'est pas une filehandle ouverte"},
195 Enum => {NOT_IN_LIST => "n'appartient pas à la liste énumérée",},
196 List => {
197 NOT_A_LIST => "n'est pas une arrayref",
198 TOO_SHORT => "moins de %d éléments",
199 TOO_LONG => "plus de %d éléments",
200 ANY => "doit avoir au moins un '%s'",
201 },
202 Struct => {
203 NOT_A_HASH => "n'est pas une hashref",
204 FORBIDDEN_FIELD => "contient le(s) champ(s) interdit(s): %s",
205 },
206 },
207 };
208  
209 # some domains inherit messages from their parent domain
210 5 foreach my $language (keys %$builtin_msgs) {
211 $builtin_msgs->{$language}{$_} = $builtin_msgs->{$language}{Num}
212 10 for qw/Int Nat/;
213 10 $builtin_msgs->{$language}{Struict} = $builtin_msgs->{$language}{Struct};
214 }
215  
216 # default messages : english
217 5 $GLOBAL_MSGS = $builtin_msgs->{english};
218  
219 #----------------------------------------------------------------------
220 # PUBLIC METHODS
221 #----------------------------------------------------------------------
222  
223 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
224 0 croak "Data::Domain is an abstract class; use subclassses for instantiating domains";
225 }
226  
227  
228 4 ▶sub messages { # class method
Subroutine messages: called CC=5 SCAR=17.8
Pod documented
229 4 my ($class, $new_messages) = @_;
230 4 ▶ croak "messages() is a class method in Data::Domain"
Branch: 1 branch
BranchTrueFalseTotal
if ref $class or $class ne "Data::Domain" 0 4 4
Condition33%1/3
ref $class or $class ne "Data::Domain"
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $class or $class ne "Data::Domain"
ref $class $class ne "Data::Domain"
Truth table33%1/3
A or B
A= ref $classB= $class ne "Data::Domain"
ABresult
1X1
011
000
231 if ref $class or $class ne 'Data::Domain';
232  
233 $GLOBAL_MSGS = (ref $new_messages) ? $new_messages
234 4 ▶ : $builtin_msgs->{$new_messages}
Branch: 2 branches
BranchTrueFalseTotal
$Data::Domain::GLOBAL_MSGS = ref $new_messages ? $new_messages : $builtin_msgs->{$new_messages} or croak("no such builtin messages: $new_messages") 4 0 4
ref $new_messages ? : 2 2 4
235 or croak "no such builtin messages: $new_messages";
236 }
237  
238  
239 1072 ▶sub inspect {
Subroutine inspect: called CC=46 SCAR=41.0
Pod documented
240 1072 my ($self, $data, $context, $is_absent) = @_;
241 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
242  
243 # build a context if this is the top-level call
244 1072 ▶ $context ||= $self->_initial_inspect_context($data);
Condition66%2/3
$context ||= $self->_initial_inspect_context($data)
l!l&&r!l&&!r
8672050
MC/DC0%0/2
$context ||= $self->_initial_inspect_context($data)
$context $self->_initial_inspect_context($data)
Truth table66%2/3
A ||= B
A= $contextB= $self->_initial_inspect_context($data)
ABresult
1X1
011
000
245  
246 1072 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) { } 53 1019 1072
247  
248 # in validation mode, insert the default value into the tree of valid data
249 53 ▶ if (exists $context->{gather_valid_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $context->{"gather_valid_data"}) 15 38 53
250 13 ▶ my $apply_default = sub {my $default = $self->{$_[0]};
Subroutine __ANON__: called CC=2 SCAR=6.9
251 15 ▶ does($default, 'CODE') ? $default->($context) : $default};
Branch: 1 branch
BranchTrueFalseTotal
&does($default, 'CODE') ? : 4 9 13
252 $context->{gather_valid_data} = exists $self->{-default} ? $apply_default->('-default')
253 15 ▶ : $is_absent && exists $self->{-if_absent} ? $apply_default->('-if_absent')
Branch: 2 branches
BranchTrueFalseTotal
exists $self->{'-default'} ? : 12 3 15
$is_absent && exists $self->{'-if_absent'} ? : 1 2 3
Condition66%2/3
$is_absent and exists $self->{"-if_absent"}
!ll&&!rl&&r
201
MC/DC50%1/2
$is_absent and exists $self->{"-if_absent"}
$is_absent exists $self->{"-if_absent"}
Truth table66%2/3
A and B
A= $is_absentB= exists $self->{"-if_absent"}
ABresult
0X0
100
111
254 : undef;
255 }
256  
257 # success if data was optional;
258 53 ▶ return if $self->{-optional} or exists $self->{-default} or exists $self->{-if_absent};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"-optional"} or exists $self->{"-default"} or exists $self->{"-if_absent"} 24 29 53
Condition100%6/6
$self->{"-optional"} or exists $self->{"-default"} or exists $self->{"-if_absent"}
l!l&&r!l&&!r
22229
$self->{"-optional"} or exists $self->{"-default"}
l!l&&r!l&&!r
101231
MC/DC100%3/3
$self->{"-optional"} or exists $self->{"-default"} or exists $self->{"-if_absent"}
$self->{"-optional"} exists $self->{"-default"} exists $self->{"-if_absent"}
Truth table100%4/4
A or B or C
A= $self->{"-optional"}B= exists $self->{"-default"}C= exists $self->{"-if_absent"}
ABCresult
1XX1
01X1
0011
0000
259  
260 # otherwise fail, except for the 'Whatever' domain which is the only one to accept undef
261 29 ▶ return $self->msg(UNDEFINED => '')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa("Data::Domain::Whatever") 18 11 29
262 unless $self->isa("Data::Domain::Whatever");
263 }
264 else { # if $data is defined
265  
266 # remember the value within the tree of valid data
267 1019 ▶ $context->{gather_valid_data} = $data if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 27 992 1019
268  
269 # check some general properties
270 1019 ▶ if (my $isa = $self->{-isa}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $isa = $self->{"-isa"}) 2 1017 1019
271 2 ▶ try {$data->isa($isa)}
Subroutine __ANON__: called CC=1 SCAR=0.0
272 2 ▶ or return $self->msg(MATCH_ISA => $isa);
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { $data->isa($isa); } ) or return $self->msg("MATCH_ISA", $isa) 1 1 2
273 }
274 1018 ▶ if (my $role = $self->{-does}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $role = $self->{"-does"}) 6 1012 1018
275 6 ▶ does($data, $role)
Branch: 1 branch
BranchTrueFalseTotal
&does($data, $role) or return $self->msg("MATCH_DOES", $role) 3 3 6
276 or return $self->msg(MATCH_DOES => $role);
277 }
278 1015 ▶ if (my $can = $self->{-can}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $can = $self->{"-can"}) 3 1012 1015
279 3 ▶ $can = [$can] unless does($can, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless &does($can, "ARRAY") 1 2 3
280 3 foreach my $method (@$can) {
281 5 ▶ try {$data->can($method)}
Subroutine __ANON__: called CC=1 SCAR=0.0
282 5 ▶ or return $self->msg(MATCH_CAN => $method);
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { $data->can($method); } ) or return $self->msg("MATCH_CAN", $method) 4 1 5
283 }
284 }
285 1014 ▶ if (my $match_target = $self->{-matches}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match_target = $self->{"-matches"}) 2 1012 1014
286 2 ▶ match::simple::match($data, $match_target)
Branch: 1 branch
BranchTrueFalseTotal
match::simple::match($data, $match_target) or return $self->msg("MATCH_SMART", $match_target) 1 1 2
287 or return $self->msg(MATCH_SMART => $match_target);
288 }
289 1013 ▶ if ($self->{-has}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-has"}) 1 1012 1013
290 # EXPERIMENTAL: check methods results
291 1 my @msgs = $self->_check_has($data, $context);
292 1 ▶ return {HAS => \@msgs} if @msgs;
Branch: 1 branch
BranchTrueFalseTotal
if @msgs 1 0 1
293 }
294 1012 ▶ if (defined $self->{-blessed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-blessed"}) 6 1006 1012
295 return $self->msg(MATCH_BLESSED => $self->{-blessed})
296 6 ▶ if Scalar::Util::blessed($data) xor $self->{-blessed};
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) xor $self->{"-blessed"} 3 3 6
Condition100%4/4
builtin::blessed($data) xor $self->{"-blessed"}
l&&rl&&!r!l&&r!l&&!r
2121
MC/DC100%2/2
builtin::blessed($data) xor $self->{"-blessed"}
builtin::blessed($data) $self->{"-blessed"}
Truth table100%4/4
A xor B
A= builtin::blessed($data)B= $self->{"-blessed"}
ABresult
110
101
011
000
297 }
298 1009 ▶ if (defined $self->{-package}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-package"}) 3 1006 1009
299 return $self->msg(MATCH_PACKAGE => $self->{-package})
300 3 ▶ if (!ref($data) && $data->isa($data)) xor $self->{-package};
Branch: 1 branch
BranchTrueFalseTotal
if not ref $data and $data->isa($data) xor $self->{"-package"} 2 1 3
Condition71%5/7
not ref $data and $data->isa($data) xor $self->{"-package"}
l&&rl&&!r!l&&r!l&&!r
1020
!ref($data) && $data->isa($data)
!ll&&!rl&&r
111
MC/DC66%2/3
not ref $data and $data->isa($data) xor $self->{"-package"}
!ref($data) $data->isa($data) $self->{"-package"}
Truth table50%3/6
not(A) && B xor C
A= !ref($data)B= $data->isa($data)C= $self->{"-package"}
ABCresult
1110
1101
0X11
1011
0X00
1000
301 }
302 1007 ▶ if (defined $self->{-isweak}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-isweak"}) 0 1007 1007
303 return $self->msg(MATCH_ISWEAK => $self->{-isweak})
304 0 ▶ if Scalar::Util::isweak($data) xor $self->{-isweak};
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_weak($data) xor $self->{"-isweak"} 0 0 0
Condition0%0/4
builtin::is_weak($data) xor $self->{"-isweak"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
builtin::is_weak($data) xor $self->{"-isweak"}
builtin::is_weak($data) $self->{"-isweak"}
Truth table0%0/4
A xor B
A= builtin::is_weak($data)B= $self->{"-isweak"}
ABresult
110
101
011
000
305 }
306 1007 ▶ if (defined $self->{-readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-readonly"}) 0 1007 1007
307 return $self->msg(MATCH_READONLY => $self->{-readonly})
308 0 ▶ if Scalar::Util::readonly($data) xor $self->{-readonly};
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::readonly($data) xor $self->{"-readonly"} 0 0 0
Condition0%0/4
&Scalar::Util::readonly($data) xor $self->{"-readonly"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
&Scalar::Util::readonly($data) xor $self->{"-readonly"}
&Scalar::Util::readonly($data) $self->{"-readonly"}
Truth table0%0/4
A xor B
A= &Scalar::Util::readonly($data)B= $self->{"-readonly"}
ABresult
110
101
011
000
309 }
310 1007 ▶ if (defined $self->{-tainted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-tainted"}) 0 1007 1007
311 return $self->msg(MATCH_TAINTED => $self->{-tainted})
312 0 ▶ if Scalar::Util::tainted($data) xor $self->{-tainted};
Branch: 1 branch
BranchTrueFalseTotal
if &Scalar::Util::tainted($data) xor $self->{"-tainted"} 0 0 0
Condition0%0/4
&Scalar::Util::tainted($data) xor $self->{"-tainted"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
&Scalar::Util::tainted($data) xor $self->{"-tainted"}
&Scalar::Util::tainted($data) $self->{"-tainted"}
Truth table0%0/4
A xor B
A= &Scalar::Util::tainted($data)B= $self->{"-tainted"}
ABresult
110
101
011
000
313 }
314 }
315  
316 # properties that must be checked against both defined and undef data
317 1018 ▶ if (defined $self->{-true}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-true"}) 13 1005 1018
318 return $self->msg(MATCH_TRUE => $self->{-true})
319 13 ▶ if $data xor $self->{-true};
Branch: 1 branch
BranchTrueFalseTotal
if $data xor $self->{"-true"} 6 7 13
Condition100%4/4
$data xor $self->{"-true"}
l&&rl&&!r!l&&r!l&&!r
2335
MC/DC100%2/2
$data xor $self->{"-true"}
$data $self->{"-true"}
Truth table100%4/4
A xor B
A= $dataB= $self->{"-true"}
ABresult
110
101
011
000
320 }
321 1012 ▶ if (defined $self->{-ref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-ref"}) 6 1006 1012
322 return $self->msg(MATCH_REF => $self->{-ref})
323 6 ▶ if ref $data xor $self->{-ref};
Branch: 1 branch
BranchTrueFalseTotal
if ref $data xor $self->{"-ref"} 3 3 6
Condition100%4/4
ref $data xor $self->{"-ref"}
l&&rl&&!r!l&&r!l&&!r
1122
MC/DC100%2/2
ref $data xor $self->{"-ref"}
ref $data $self->{"-ref"}
Truth table100%4/4
A xor B
A= ref $dataB= $self->{"-ref"}
ABresult
110
101
011
000
324 }
325  
326 # now call domain-specific _inspect()
327 1009 return $self->_inspect($data, $context)
328 }
329  
330  
331 14 ▶sub validate {
Subroutine validate: called CC=2 SCAR=6.9
Pod documented
332 14 my ($self, $data) = @_;
333  
334 # inspect the data
335 14 my $context = $self->_initial_inspect_context($data, gather_valid_data => 1);
336 14 my $msg = $self->inspect($data, $context);
337
338 # return the validated data tree if there is no error message
339 14 ▶ return $context->{gather_valid_data} if !$msg;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg 13 1 14
340  
341 # otherwise, die with the error message
342 1 croak $self->name . ": invalid data because " . $self->stringify_msg($msg);
343 }
344  
345  
346 3 ▶sub stringify_msg {
Subroutine stringify_msg: called CC=3 SCAR=11.8
Pod documented
347 3 my ($self, $msg) = @_;
348  
349 0 ▶ return does($msg, 'ARRAY') ? join ", ", map {$self->stringify_msg($_)} grep {$_} @$msg
Branch: 1 branch
BranchTrueFalseTotal
&does($msg, 'HASH') ? : 1 2 3
350 3 ▶ : does($msg, 'HASH') ? join ", ", map {"$_:" . $self->stringify_msg($msg->{$_})} grep {$msg->{$_}} sort keys %$msg
Branch: 1 branch
BranchTrueFalseTotal
&does($msg, 'ARRAY') ? : 0 3 3
351 : $msg;
352 }
353  
354  
355  
356 1 ▶sub func_signature {
Subroutine func_signature: called CC=1 SCAR=0.0
Pod documented
357 1 my ($self) = @_;
358  
359 # this method is overridden in List() and Struct() for dealing with arrays and hashes
360 1 ▶ return sub {my $params = $self->validate(@_); $params};
Subroutine __ANON__: called CC=1 SCAR=0.0
361 }
362  
363  
364 1 ▶sub meth_signature {
Subroutine meth_signature: called CC=1 SCAR=0.0
Pod documented
365 1 my ($self) = @_;
366 1 my $sig = $self->func_signature;
367  
368 # same as func_signature, but the first param is set apart since it is the invocant of the method
369 1 ▶ return sub {my $obj = shift; return ($obj, &$sig)}; # note: &$sig is equivalent to $sig->(@_)
Subroutine __ANON__: called CC=1 SCAR=0.0
370 }
371  
372  
373  
374  
375 #----------------------------------------------------------------------
376 # METHODS FOR INTERNAL USE
377 #----------------------------------------------------------------------
378 # Note : methods without initial underscore could possibly be useful for subclasses, either through
379 # invocation or through subclassing. Methods with initial underscore are really internal mechanics;
380 # I doubt that anybody else would want to invoke or subclass them ... but nothing prevents you from
381 # doing so !
382  
383  
384  
385 253 ▶sub msg {
Subroutine msg: called CC=12 SCAR=25.2
Pod documented
386 253 my ($self, $msg_id, @args) = @_;
387 253 my $msgs = $self->{-messages};
388 253 my $name = $self->name;
389  
390 # if using a coderef, these args will be passed to it
391 253 my @msgs_call_args = ($name, $msg_id, @args);
392 253 ▶ shift @msgs_call_args if $USE_OLD_MSG_API; # because older versions did not pass the $name arg
Branch: 1 branch
BranchTrueFalseTotal
if $Data::Domain::USE_OLD_MSG_API 1 252 253
393  
394 # perl v5.22 and above warns if there are too many @args for sprintf.
395 # The line below prevents that warning
396 5 ▶ no if $] ge '5.022000', warnings => 'redundant';
Subroutine BEGIN: called CC=1 SCAR=0.0
397  
398 # if there is a user-defined message, return it
399 253 ▶ if (defined $msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $msgs) 11 242 253
400 11 for (ref $msgs) {
401 11 ▶ /^CODE/ and return $msgs->(@msgs_call_args); # user function
Branch: 1 branch
BranchTrueFalseTotal
/^CODE/ and return &$msgs(@msgs_call_args) 1 10 11
402 10 ▶ /^$/ and return "$name: $msgs"; # user constant string
Branch: 1 branch
BranchTrueFalseTotal
/^$/ and return "${name}: $msgs" 8 2 10
403 2 ▶ /^HASH/ and do { if (my $msg_string = $msgs->{$msg_id}) { # user hash of msgs
Branch: 2 branches
BranchTrueFalseTotal
/^HASH/ and do { if (my $msg_string = $msgs->{$msg_id}) { return sprintf("${name}: $msg_string", @args); } else { last; } } 2 0 2
if (my $msg_string = $msgs->{$msg_id}) { } 2 0 2
404 2 return sprintf "$name: $msg_string", @args;
405 }
406 else {
407 0 last; # not found in this hash - revert to $GLOBAL_MSGS below
408 }
409 };
410 # otherwise
411 0 croak "-messages option should be a coderef, a hashref or a sprintf string";
412 }
413 }
414  
415 # there was no user-defined message, so use global messages
416 242 ▶ if (ref $GLOBAL_MSGS eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $GLOBAL_MSGS eq 'CODE') { } 2 240 242
417 2 return $GLOBAL_MSGS->(@msgs_call_args);
418 }
419 else {
420 my $msg_entry = $GLOBAL_MSGS->{$self->subclass}{$msg_id}
421 240 ▶ || $GLOBAL_MSGS->{Generic}{$msg_id}
Branch: 1 branch
BranchTrueFalseTotal
my $msg_entry = $$GLOBAL_MSGS{$self->subclass}{$msg_id} || $Data::Domain::GLOBAL_MSGS->{"Generic"}{$msg_id} or croak("no error string for message $msg_id") 240 0 240
Condition66%2/3
$$GLOBAL_MSGS{$self->subclass}{$msg_id} || $Data::Domain::GLOBAL_MSGS->{'Generic'}{$msg_id}
l!l&&r!l&&!r
180600
MC/DC0%0/2
$$GLOBAL_MSGS{$self->subclass}{$msg_id} || $Data::Domain::GLOBAL_MSGS->{'Generic'}{$msg_id}
$$GLOBAL_MSGS{$self->subclass}{$msg_id} $Data::Domain::GLOBAL_MSGS->{'Generic'}{$msg_id}
Truth table66%2/3
A || B
A= $$GLOBAL_MSGS{$self->subclass}{$msg_id}B= $Data::Domain::GLOBAL_MSGS->{'Generic'}{$msg_id}
ABresult
1X1
011
000
422 or croak "no error string for message $msg_id";
423 240 ▶ return ref $msg_entry eq 'CODE' ? $msg_entry->(@msgs_call_args)
Branch: 1 branch
BranchTrueFalseTotal
ref $msg_entry eq 'CODE' ? : 20 220 240
424 : sprintf "$name: $msg_entry", @args;
425 }
426 }
427  
428  
429 444 ▶sub name {
Subroutine name: called CC=2 SCAR=7.1
Pod documented
430 444 my ($self) = @_;
431 444 ▶ return $self->{-name} || $self->subclass;
Condition66%2/3
$self->{'-name'} || $self->subclass
l!l&&r!l&&!r
184260
MC/DC0%0/2
$self->{'-name'} || $self->subclass
$self->{'-name'} $self->subclass
Truth table66%2/3
A || B
A= $self->{'-name'}B= $self->subclass
ABresult
1X1
011
000
432 }
433  
434  
435 671 ▶sub subclass { # returns the class name without initial 'Data::Domain::'
Subroutine subclass: called CC=2 SCAR=7.4
Pod documented
436 671 my ($self) = @_;
437 671 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
67100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
438 671 (my $subclass = $class) =~ s/^Data::Domain:://;
439 671 return $subclass;
440 }
441  
442  
443 219 ▶sub _initial_inspect_context {
Subroutine _initial_inspect_context: called CC=1 SCAR=0.0
444 219 my ($self, $data, %extra) = @_;
445  
446 219 return {root => $data,
447 flat => {},
448 path => [],
449 list => [],
450 %extra,
451 };
452 }
453  
454  
455 1 ▶sub _check_has {
Subroutine _check_has: called CC=5 SCAR=16.8
456 1 my ($self, $data, $context) = @_;
457  
458 1 my @msgs;
459 1 my $iterator = natatime 2, @{$self->{-has}};
460 1 ▶ while (my ($meth_to_call, $expectation) = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 3 1 4
461 3 ▶ my ($meth, @args) = does($meth_to_call, 'ARRAY') ? @$meth_to_call
Branch: 1 branch
BranchTrueFalseTotal
&does($meth_to_call, 'ARRAY') ? : 1 2 3
462 : ($meth_to_call);
463 3 my $msg;
464 3 ▶ if (does($expectation, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&does($expectation, 'ARRAY')) { } 0 3 3
465 0 ▶ $msg = try {my @result = $data->$meth(@args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
466 0 my $domain = List(@$expectation);
467 0 $domain->inspect(\@result)}
468 0 ▶ catch {(my $error_msg = $_) =~ s/\bat\b.*//s; $error_msg};
Subroutine __ANON__: not called CC=1 SCAR=6.9
469 }
470 else {
471 3 ▶ $msg = try {my $result = $data->$meth(@args);
Subroutine __ANON__: called CC=1 SCAR=0.0
472 2 $expectation->inspect($result)}
473 3 ▶ catch {(my $error_msg = $_) =~ s/\bat\b.*//s; $error_msg};
Subroutine __ANON__: called CC=1 SCAR=0.0
474 }
475 3 ▶ push @msgs, $meth_to_call => $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 2 1 3
476 }
477 1 return @msgs;
478 }
479  
480  
481  
482 0 ▶sub _check_returns {
Subroutine _check_returns: not called CC=4 SCAR=30.0
483 0 my ($self, $data, $context) = @_;
484  
485 0 my @msgs;
486 0 my $iterator = natatime 2, @{$self->{-returns}};
487 0 ▶ while (my ($args, $expectation) = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$iterator()) 0 0 0
488 0 my $msg;
489 0 ▶ if (does($expectation, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&does($expectation, 'ARRAY')) { } 0 0 0
490 0 ▶ $msg = try {my @result = $data->(@$args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
491 0 my $domain = List(@$expectation);
492 0 $domain->inspect(\@result)}
493 0 ▶ catch {(my $error_msg = $_) =~ s/\bat\b.*//s; $error_msg};
Subroutine __ANON__: not called CC=1 SCAR=6.9
494 }
495 else {
496 0 ▶ $msg = try {my $result = $data->(@$args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
497 0 $expectation->inspect($result)}
498 0 ▶ catch {(my $error_msg = $_) =~ s/\bat\b.*//s; $error_msg};
Subroutine __ANON__: not called CC=1 SCAR=6.9
499 }
500 0 ▶ push @msgs, $args => $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
501 }
502 0 return @msgs;
503 }
504  
505  
506 185 ▶sub _expand_range {
Subroutine _expand_range: called CC=6 SCAR=18.7
507 185 my ($self, $range_field, $min_field, $max_field) = @_;
508 185 my $name = $self->name;
509  
510 # the range field will be replaced by min and max fields
511 185 ▶ if (my $range = delete $self->{$range_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = delete $self->{$range_field}) 13 172 185
512 13 for ($min_field, $max_field) {
513 26 ▶ not defined $self->{$_}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$_} and croak("${name}: incompatible options: $range_field / $_") 0 26 26
514 or croak "$name: incompatible options: $range_field / $_";
515 }
516 13 ▶ does($range, 'ARRAY') and @$range == 2
Branch: 1 branch
BranchTrueFalseTotal
&does($range, "ARRAY") and @$range == 2 or croak("${name}: invalid argument for $range") 13 0 13
Condition33%1/3
&does($range, "ARRAY") and @$range == 2
!ll&&!rl&&r
0013
MC/DC0%0/2
&does($range, "ARRAY") and @$range == 2
&does($range, "ARRAY") @$range == 2
Truth table33%1/3
A and B
A= &does($range, "ARRAY")B= @$range == 2
ABresult
0X0
100
111
517 or croak "$name: invalid argument for $range";
518 13 @{$self}{$min_field, $max_field} = @$range;
519 }
520 }
521  
522  
523 184 ▶sub _check_min_max {
Subroutine _check_min_max: called CC=7 SCAR=19.5
524 184 my ($self, $min_field, $max_field, $cmp_func) = @_;
525  
526 # choose the appropriate comparison function
527 184 ▶ if ($cmp_func eq '<=') {$cmp_func = sub {$_[0] <= $_[1]}}
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 3 branches
BranchTrueFalseTotal
if ($cmp_func eq '<=') { } 130 54 184
elsif ($cmp_func eq 'le') { } 41 13 54
elsif (&does($cmp_func, 'CODE')) { } 13 0 13
528 41 ▶ elsif ($cmp_func eq 'le') {$cmp_func = sub {$_[0] le $_[1]}}
Subroutine __ANON__: called CC=1 SCAR=0.0
529 elsif (does($cmp_func, 'CODE')) {} # already a coderef, do nothing
530 0 else {croak "inappropriate cmp_func for _check_min_max"}
531  
532 # check that min is smaller than max
533 184 my ($min, $max) = @{$self}{$min_field, $max_field};
534 184 ▶ if (defined $min && defined $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min and defined $max) 21 163 184
Condition100%3/3
defined $min and defined $max
!ll&&!rl&&r
1432021
MC/DC100%2/2
defined $min and defined $max
defined $min defined $max
Truth table100%3/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
535 21 ▶ $cmp_func->($min, $max)
Branch: 1 branch
BranchTrueFalseTotal
&$cmp_func($min, $max) or croak($self->subclass . ": incompatible min/max values ($min/$max)") 16 5 21
536 or croak $self->subclass . ": incompatible min/max values ($min/$max)";
537 }
538 }
539  
540  
541 525 ▶sub _build_subdomain {
Subroutine _build_subdomain: called CC=7 SCAR=19.5
542 525 my ($self, $domain, $context) = @_;
543 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
544  
545 # avoid infinite loop
546 525 ▶ @{$context->{path}} < $MAX_DEEP
Branch: 1 branch
BranchTrueFalseTotal
@{$context->{"path"};} < $MAX_DEEP or croak("inspect() deepness exceeded $MAX_DEEP; " . 'modify $Data::Domain::MAX_DEEP if you need more') 524 1 525
547 or croak "inspect() deepness exceeded $MAX_DEEP; "
548 . "modify \$Data::Domain::MAX_DEEP if you need more";
549  
550 524 ▶ if (does($domain, 'Data::Domain')) {
Branch: 3 branches
BranchTrueFalseTotal
if (&does($domain, 'Data::Domain')) { } 288 236 524
elsif (&does($domain, 'CODE')) { } 230 6 236
elsif (not ref $domain) { } 6 0 6
551 # already a domain, nothing to do
552 }
553 elsif (does($domain, 'CODE')) {
554 # this is a lazy domain, need to call the coderef to get a real domain
555 230 ▶ $domain = try {$domain->($context)}
Subroutine __ANON__: called CC=1 SCAR=0.0
556 1 ▶ catch {(my $error_msg = $_) =~ s/\bat\b.*//s; # remove "at source_file, line ..." from error message
Subroutine __ANON__: called CC=1 SCAR=0.0
557 # return an empty domain that reports the error message
558 1 Data::Domain::Empty->new(-name => "domain parameters",
559 -messages => $error_msg);
560 230 };
561 # did we really get a domain ?
562 230 ▶ does($domain, "Data::Domain")
Branch: 1 branch
BranchTrueFalseTotal
&does($domain, "Data::Domain") or croak("lazy domain coderef returned an invalid domain") 230 0 230
563 or croak "lazy domain coderef returned an invalid domain";
564 }
565 elsif (!ref $domain) {
566 # this is a scalar, build a constant domain with that single value
567 6 ▶ my $subclass = Scalar::Util::looks_like_number($domain) ? 'Num' : 'String';
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($domain) ? : 3 3 6
568 6 $domain = "Data::Domain::$subclass"->new(-min => $domain,
569 -max => $domain,
570 -name => "constant $subclass");
571 }
572 else {
573 0 croak "unknown subdomain : $domain";
574 }
575  
576 524 return $domain;
577 }
578  
579  
580 79 ▶sub _is_proper_subdomain {
Subroutine _is_proper_subdomain: called CC=3 SCAR=14.2
581 79 my ($self, $domain) = @_;
582 79 ▶ return does($_, 'Data::Domain') || does($_, 'CODE') || !ref $_;
Condition33%2/6
&does($_, 'Data::Domain') || &does($_, 'CODE') || !ref($_)
l!l&&r!l&&!r
0790
&does($_, "Data::Domain") or &does($_, "CODE")
l!l&&r!l&&!r
0079
MC/DC0%0/3
&does($_, 'Data::Domain') || &does($_, 'CODE') || !ref($_)
&does($_, "Data::Domain") &does($_, "CODE") !ref($_)
Truth table25%1/4
A or B || not(C)
A= &does($_, "Data::Domain")B= &does($_, "CODE")C= !ref($_)
ABCresult
1XX1
01X1
0011
0000
583 }
584  
585  
586  
587  
588  
589 #----------------------------------------------------------------------
590 # UTILITY FUNCTIONS (NOT METHODS)
591 #----------------------------------------------------------------------
592  
593 # valid options for all subclasses
594 5 my @common_options = qw/-optional -name -messages
595 -true -isa -can -does -matches -ref
596 -has -returns
597 -blessed -package -isweak -readonly -tainted
598 -default -if_absent/;
599  
600 209 ▶sub _parse_args {
Subroutine _parse_args: called CC=9 SCAR=22.2
601 209 my ($args_ref, $options_ref, $default_option, $arg_type) = @_;
602  
603 209 my %parsed;
604  
605 # parse named arguments
606 209 ▶ while (@$args_ref and $args_ref->[0] =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args_ref and $args_ref->[0] =~ /^-/) 161 209 370
Condition100%3/3
@$args_ref and $args_ref->[0] =~ /^-/
!ll&&!rl&&r
17732161
MC/DC100%2/2
@$args_ref and $args_ref->[0] =~ /^-/
@$args_ref $args_ref->[0] =~ /^-/
Truth table100%3/3
A and B
A= @$args_refB= $args_ref->[0] =~ /^-/
ABresult
0X0
100
111
607 161 ▶ any {$args_ref->[0] eq $_} (@$options_ref, @common_options)
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
&any(sub { $args_ref->[0] eq $_; } , @$options_ref, @common_options) or croak("invalid argument: $args_ref->[0]") 161 0 161
608 or croak "invalid argument: $args_ref->[0]";
609 161 my ($key, $val) = (shift @$args_ref, shift @$args_ref);
610 161 $parsed{$key} = $val;
611 }
612  
613 # remaining arguments are mapped to the default option
614 209 ▶ if (@$args_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$args_ref) 32 177 209
615 32 ▶ $default_option or croak "too many args to new()";
Branch: 1 branch
BranchTrueFalseTotal
$default_option or croak("too many args to new()") 32 0 32
616 32 ▶ not exists $parsed{$default_option}
Branch: 1 branch
BranchTrueFalseTotal
exists $parsed{$default_option} and croak("can't have default args if $default_option is set") 0 32 32
617 or croak "can't have default args if $default_option is set";
618 32 ▶ $parsed{$default_option}
Branch: 2 branches
BranchTrueFalseTotal
$arg_type eq 'scalar' ? : 6 26 32
$arg_type eq 'arrayref' ? : 26 0 26
619 = $arg_type eq 'scalar' ? $args_ref->[0]
620 : $arg_type eq 'arrayref' ? $args_ref
621 : croak "unknown type for default option: $arg_type";
622 }
623  
624 209 return \%parsed;
625 }
626  
627  
628 0 ▶sub node_from_path { # no longer used (replaced by Data::Reach); but still present for backwards compat
Subroutine node_from_path: not called CC=5 SCAR=34.0
Pod documented
629 0 my ($root, $path0, @path) = @_;
630 0 ▶ return $root if not defined $path0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path0 0 0 0
631 0 ▶ return undef if not defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 0 0
632 0 ▶ return node_from_path($root->{$path0}, @path)
Branch: 1 branch
BranchTrueFalseTotal
if &does($root, "HASH") 0 0 0
633 if does($root, 'HASH');
634 0 ▶ return node_from_path($root->[$path0], @path)
Branch: 1 branch
BranchTrueFalseTotal
if &does($root, "ARRAY") 0 0 0
635 if does($root, 'ARRAY');
636  
637 # otherwise
638 0 croak "node_from_path: incorrect root/path";
639 }
640  
641 #----------------------------------------------------------------------
642 # implementation for overloaded operators
643 #----------------------------------------------------------------------
644 0 ▶sub _matches {
Subroutine _matches: not called CC=1 SCAR=6.9
645 0 my ($self, $data, $call_order) = @_;
646 0 $Data::Domain::MESSAGE = $self->inspect($data);
647 0 return !$Data::Domain::MESSAGE; # smart match successful if no error message
648 }
649  
650 144 ▶sub _stringify {
Subroutine _stringify: called CC=1 SCAR=0.0
651 144 my ($self) = @_;
652 144 my $dumper = Data::Dumper->new([$self])->Indent(0)->Terse(1);
653 144 return $dumper->Dump;
654 }
655  
656 #======================================================================
657 # END OF PARENT CLASS -- BELOW ARE IMPLEMENTATIONS FOR SPECIFIC DOMAINS
658 #======================================================================
659  
660  
661 #======================================================================
662 package Data::Domain::Whatever;
663 #======================================================================
664 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
665 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
666 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
667 5 ▶use Scalar::Does qw/does/;
Subroutine BEGIN: called CC=1 SCAR=0.0
668 5 our @ISA = 'Data::Domain';
669  
670 27 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
671 27 my $class = shift;
672 27 my @options = qw/-defined/;
673 27 my $self = Data::Domain::_parse_args( \@_, \@options );
674 27 bless $self, $class;
675  
676 not ($self->{-defined } && $self->{-optional})
677 27 ▶ or croak "both -defined and -optional: meaningless!";
Branch: 1 branch
BranchTrueFalseTotal
($self->{"-defined"} and $self->{"-optional"}) and croak("both -defined and -optional: meaningless!") 0 27 27
Condition66%2/3
$self->{"-defined"} and $self->{"-optional"}
!ll&&!rl&&r
2520
MC/DC0%0/2
$self->{"-defined"} and $self->{"-optional"}
$self->{"-defined"} $self->{"-optional"}
Truth table66%2/3
A and B
A= $self->{"-defined"}B= $self->{"-optional"}
ABresult
0X0
100
111
678  
679 27 return $self;
680 }
681  
682 34 ▶sub _inspect {
Subroutine _inspect: called CC=4 SCAR=13.9
683 34 my ($self, $data) = @_;
684  
685 34 ▶ if (defined $self->{-defined}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-defined"}) 10 24 34
686 return $self->msg(MATCH_DEFINED => $self->{-defined})
687 10 ▶ if defined($data) xor $self->{-defined};
Branch: 1 branch
BranchTrueFalseTotal
if defined $data xor $self->{"-defined"} 6 4 10
Condition100%4/4
defined $data xor $self->{"-defined"}
l&&rl&&!r!l&&r!l&&!r
3331
MC/DC100%2/2
defined $data xor $self->{"-defined"}
defined $data $self->{"-defined"}
Truth table100%4/4
A xor B
A= defined $dataB= $self->{"-defined"}
ABresult
110
101
011
000
688 }
689  
690 # otherwise, success
691 28 return;
692 }
693  
694  
695 #======================================================================
696 package Data::Domain::Empty;
697 #======================================================================
698 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
699 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
700 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
701 5 our @ISA = 'Data::Domain';
702  
703 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
704 3 my $class = shift;
705 3 my @options = ();
706 3 my $self = Data::Domain::_parse_args( \@_, \@options );
707 3 bless $self, $class;
708 }
709  
710 7 ▶sub _inspect {
Subroutine _inspect: called CC=1 SCAR=0.0
711 7 my ($self, $data) = @_;
712  
713 7 return $self->msg(INVALID => ''); # always fails
714 }
715  
716  
717 #======================================================================
718 package Data::Domain::Num;
719 #======================================================================
720 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
721 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
722 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
723 5 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
724 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
725  
726 5 our @ISA = 'Data::Domain';
727  
728 68 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
729 68 my $class = shift;
730 68 my @options = qw/-range -min -max -not_in/;
731 68 my $self = Data::Domain::_parse_args(\@_, \@options);
732 68 bless $self, $class;
733  
734 68 $self->_expand_range(qw/-range -min -max/);
735 68 $self->_check_min_max(qw/-min -max <=/);
736  
737 67 ▶ if ($self->{-not_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-not_in"}) 1 66 67
738 1 ▶ try {my $vals = $self->{-not_in};
Subroutine __ANON__: called CC=2 SCAR=7.1
739 1 ▶ @$vals > 0 and not grep {!looks_like_number($_)} @$vals}
Branch: 1 branch
BranchTrueFalseTotal
@$vals > 0 and not grep({not &looks_like_number($_);} @$vals) 1 0 1
740 1 ▶ or croak "-not_in : needs an arrayref of numbers";
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { my $vals = $self->{"-not_in"}; @$vals > 0 and not grep({not &looks_like_number($_);} @$vals); } ) or croak("-not_in : needs an arrayref of numbers") 1 0 1
741 }
742  
743 67 return $self;
744 }
745  
746 321 ▶sub _inspect {
Subroutine _inspect: called CC=8 SCAR=20.8
747 321 my ($self, $data) = @_;
748  
749 321 ▶ looks_like_number($data)
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($data) or return $self->msg("INVALID", $data) 214 107 321
750 or return $self->msg(INVALID => $data);
751  
752 214 ▶ if (defined $self->{-min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-min"}) 32 182 214
753 $data >= $self->{-min}
754 32 ▶ or return $self->msg(TOO_SMALL => $self->{-min});
Branch: 1 branch
BranchTrueFalseTotal
$data >= $self->{"-min"} or return $self->msg("TOO_SMALL", $self->{"-min"}) 25 7 32
755 }
756 207 ▶ if (defined $self->{-max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max"}) 12 195 207
757 $data <= $self->{-max}
758 12 ▶ or return $self->msg(TOO_BIG => $self->{-max});
Branch: 1 branch
BranchTrueFalseTotal
$data <= $self->{"-max"} or return $self->msg("TOO_BIG", $self->{"-max"}) 8 4 12
759 }
760 203 ▶ if (defined $self->{-not_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-not_in"}) 5 198 203
761 5 ▶ grep {$data == $_} @{$self->{-not_in}}
Branch: 1 branch
BranchTrueFalseTotal
grep {$data == $_;} @{$$self{"-not_in"};} and return $self->msg("EXCLUSION_SET", $data) 2 3 5
762 and return $self->msg(EXCLUSION_SET => $data);
763 }
764  
765 201 return;
766 }
767  
768  
769 #======================================================================
770 package Data::Domain::Int;
771 #======================================================================
772 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
773 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
774  
775 5 our @ISA = 'Data::Domain::Num';
776  
777 104 ▶sub _inspect {
Subroutine _inspect: called CC=3 SCAR=11.0
778 104 my ($self, $data) = @_;
779  
780 104 ▶ defined($data) and $data =~ /^-?\d+$/
Branch: 1 branch
BranchTrueFalseTotal
defined $data and $data =~ /^-?\d+$/ or return $self->msg("INVALID", $data) 79 25 104
Condition66%2/3
defined $data and $data =~ /^-?\d+$/
!ll&&!rl&&r
02579
MC/DC50%1/2
defined $data and $data =~ /^-?\d+$/
defined $data $data =~ /^-?\d+$/
Truth table66%2/3
A and B
A= defined $dataB= $data =~ /^-?\d+$/
ABresult
0X0
100
111
781 or return $self->msg(INVALID => $data);
782 79 return $self->SUPER::_inspect($data);
783 }
784  
785  
786 #======================================================================
787 package Data::Domain::Nat;
788 #======================================================================
789 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
790 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
791  
792 5 our @ISA = 'Data::Domain::Num';
793  
794 3 ▶sub _inspect {
Subroutine _inspect: called CC=3 SCAR=11.0
795 3 my ($self, $data) = @_;
796  
797 3 ▶ defined($data) and $data =~ /^\d+$/
Branch: 1 branch
BranchTrueFalseTotal
defined $data and $data =~ /^\d+$/ or return $self->msg("INVALID", $data) 2 1 3
Condition66%2/3
defined $data and $data =~ /^\d+$/
!ll&&!rl&&r
012
MC/DC50%1/2
defined $data and $data =~ /^\d+$/
defined $data $data =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $dataB= $data =~ /^\d+$/
ABresult
0X0
100
111
798 or return $self->msg(INVALID => $data);
799 2 return $self->SUPER::_inspect($data);
800 }
801  
802  
803 #======================================================================
804 package Data::Domain::String;
805 #======================================================================
806 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
807 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
808 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
809 5 our @ISA = 'Data::Domain';
810  
811 41 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
812 41 my $class = shift;
813 41 my @options = qw/-regex -antiregex
814 -range -min -max
815 -length -min_length -max_length
816 -not_in/;
817 41 my $self = Data::Domain::_parse_args(\@_, \@options, -regex => 'scalar');
818 41 bless $self, $class;
819  
820 41 $self->_expand_range(qw/-range -min -max/);
821 41 $self->_check_min_max(qw/-min -max le/);
822  
823 41 $self->_expand_range(qw/-length -min_length -max_length/);
824 41 $self->_check_min_max(qw/-min_length -max_length <=/);
825  
826 40 return $self;
827 }
828  
829 174 ▶sub _inspect {
Subroutine _inspect: called CC=17 SCAR=28.3
830 174 my ($self, $data) = @_;
831  
832 # $data must be Unref or obj with a stringification method
833 174 ▶ !ref($data) || overload::Method($data, '""')
Branch: 1 branch
BranchTrueFalseTotal
not ref $data or overload::Method($data, "\"\"") or return $self->msg("INVALID", $data) 171 3 174
Condition100%3/3
not ref $data or overload::Method($data, "\"\"")
l!l&&r!l&&!r
17013
MC/DC100%2/2
not ref $data or overload::Method($data, "\"\"")
not ref $data overload::Method($data, "\"\"")
Truth table100%3/3
not(A) or B
A= not ref $dataB= overload::Method($data, "\"\"")
ABresult
1X1
011
000
834 or return $self->msg(INVALID => $data);
835 171 ▶ if ($self->{-min_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-min_length"}) 6 165 171
836 length($data) >= $self->{-min_length}
837 6 ▶ or return $self->msg(TOO_SHORT => $self->{-min_length});
Branch: 1 branch
BranchTrueFalseTotal
length $data >= $self->{"-min_length"} or return $self->msg("TOO_SHORT", $self->{"-min_length"}) 5 1 6
838 }
839 170 ▶ if (defined $self->{-max_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max_length"}) 5 165 170
840 length($data) <= $self->{-max_length}
841 5 ▶ or return $self->msg(TOO_LONG => $self->{-max_length});
Branch: 1 branch
BranchTrueFalseTotal
length $data <= $self->{"-max_length"} or return $self->msg("TOO_LONG", $self->{"-max_length"}) 2 3 5
842 }
843 167 ▶ if ($self->{-regex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-regex"}) 133 34 167
844 $data =~ $self->{-regex}
845 133 ▶ or return $self->msg(SHOULD_MATCH => $self->{-regex});
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /$self->{"-regex"}/ or return $self->msg("SHOULD_MATCH", $self->{"-regex"}) 120 13 133
846 }
847 154 ▶ if ($self->{-antiregex}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-antiregex"}) 2 152 154
848 $data !~ $self->{-antiregex}
849 2 ▶ or return $self->msg(SHOULD_NOT_MATCH => $self->{-antiregex});
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /$self->{"-antiregex"}/ and return $self->msg("SHOULD_NOT_MATCH", $self->{"-antiregex"}) 1 1 2
850 }
851 153 ▶ if (defined $self->{-min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-min"}) 4 149 153
852 $data ge $self->{-min}
853 4 ▶ or return $self->msg(TOO_SMALL => $self->{-min});
Branch: 1 branch
BranchTrueFalseTotal
$data ge $self->{"-min"} or return $self->msg("TOO_SMALL", $self->{"-min"}) 3 1 4
854 }
855 152 ▶ if (defined $self->{-max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max"}) 3 149 152
856 $data le $self->{-max}
857 3 ▶ or return $self->msg(TOO_BIG => $self->{-max});
Branch: 1 branch
BranchTrueFalseTotal
$data le $self->{"-max"} or return $self->msg("TOO_BIG", $self->{"-max"}) 2 1 3
858 }
859 151 ▶ if ($self->{-not_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-not_in"}) 1 150 151
860 1 ▶ grep {$data eq $_} @{$self->{-not_in}}
Branch: 1 branch
BranchTrueFalseTotal
grep {$data eq $_;} @{$$self{"-not_in"};} and return $self->msg("EXCLUSION_SET", $data) 0 1 1
861 and return $self->msg(EXCLUSION_SET => $data);
862 }
863  
864 151 return;
865 }
866  
867  
868 #======================================================================
869 package Data::Domain::Date;
870 #======================================================================
871 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
872 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
873 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
874 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
875 5 our @ISA = 'Data::Domain';
876  
877  
878 5 ▶use autouse 'Date::Calc' => qw/Decode_Date_EU Decode_Date_US Date_to_Text
Subroutine BEGIN: called CC=1 SCAR=0.0
879 5 Delta_Days Add_Delta_Days Today check_date/;
880  
881 5 my $date_parser = \&Decode_Date_EU;
882  
883 #----------------------------------------------------------------------
884 # utility functions
885 #----------------------------------------------------------------------
886 3 ▶sub _print_date {
Subroutine _print_date: called CC=1 SCAR=0.0
887 3 my $date = shift;
888 3 $date = _expand_dynamic_date($date);
889 3 return Date_to_Text(@$date);
890 }
891  
892  
893 5 my $dynamic_date = qr/^(today|yesterday|tomorrow)$/;
894  
895 42 ▶sub _expand_dynamic_date {
Subroutine _expand_dynamic_date: called CC=3 SCAR=11.0
896 42 my $date = shift;
897 42 ▶ if (not ref $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $date) 7 35 42
898 $date = {
899 today => [Today],
900 yesterday => [Add_Delta_Days(Today, -1)],
901 tomorrow => [Add_Delta_Days(Today, +1)]
902 7 ▶ }->{$date} or croak "unexpected date : $date";
Branch: 1 branch
BranchTrueFalseTotal
$date = {"today", [Today()], "yesterday", [Add_Delta_Days(Today(), -1)], "tomorrow", [Add_Delta_Days(Today(), 1)]}->{$date} or croak("unexpected date : $date") 7 0 7
903 }
904 42 return $date;
905 }
906  
907 15 ▶sub _date_cmp {
Subroutine _date_cmp: called CC=1 SCAR=0.0
908 15 my ($d1, $d2) = map {_expand_dynamic_date($_)} @_;
909 15 return -Delta_Days(@$d1, @$d2);
910 }
911  
912  
913 #----------------------------------------------------------------------
914 # public API
915 #----------------------------------------------------------------------
916  
917 1 ▶sub parser {
Subroutine parser: called CC=4 SCAR=14.9
918 1 my ($class, $new_parser) = @_;
919 1 ▶ not ref $class or croak "Data::Domain::Date::parser is a class method";
Branch: 1 branch
BranchTrueFalseTotal
ref $class and croak("Data::Domain::Date::parser is a class method") 0 1 1
920  
921 $date_parser =
922 (ref $new_parser eq 'CODE')
923 ? $new_parser
924 : {US => \&Decode_Date_US,
925 1 ▶ EU => \&Decode_Date_EU}->{$new_parser}
Branch: 2 branches
BranchTrueFalseTotal
$date_parser = ref $new_parser eq "CODE" ? $new_parser : {"US", \&Data::Domain::Date::Decode_Date_US, "EU", \&Data::Domain::Date::Decode_Date_EU}->{$new_parser} or croak("unknown date parser : $new_parser") 1 0 1
ref $new_parser eq 'CODE' ? : 1 0 1
926 or croak "unknown date parser : $new_parser";
927 1 return $date_parser;
928 }
929  
930  
931 11 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
932 11 my $class = shift;
933 11 my @options = qw/-range -min -max -not_in/;
934 11 my $self = Data::Domain::_parse_args(\@_, \@options);
935 11 bless $self, $class;
936  
937 11 $self->_expand_range(qw/-range -min -max/);
938  
939 # parse date boundaries into internal representation (arrayrefs)
940 11 for my $bound (qw/-min -max/) {
941 21 ▶ if ($self->{$bound} and $self->{$bound} !~ $dynamic_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$bound} and not $self->{$bound} =~ /$dynamic_date/) 6 15 21
Condition100%3/3
$self->{$bound} and not $self->{$bound} =~ /$dynamic_date/
!ll&&!rl&&r
1416
MC/DC100%2/2
$self->{$bound} and not $self->{$bound} =~ /$dynamic_date/
$self->{$bound} not $self->{$bound} =~ /$dynamic_date/
Truth table100%3/3
A and not(B)
A= $self->{$bound}B= not $self->{$bound} =~ /$dynamic_date/
ABresult
0X0
100
111
942 6 ▶ my @date = $date_parser->($self->{$bound})
Branch: 1 branch
BranchTrueFalseTotal
my(@date) = &$date_parser($self->{$bound}) or croak("invalid date ($bound): $self->{$bound}") 5 1 6
943 or croak "invalid date ($bound): $self->{$bound}";
944 5 $self->{$bound} = \@date;
945 }
946 }
947  
948 # check order of boundaries
949 10 ▶ $self->_check_min_max(qw/-min -max/, sub {_date_cmp($_[0], $_[1]) <= 0});
Subroutine __ANON__: called CC=1 SCAR=0.0
950  
951 # parse dates in the exclusion set into internal representation
952 9 ▶ if ($self->{-not_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-not_in"}) 1 8 9
953 1 my @excl_dates;
954 1 ▶ try {
Subroutine __ANON__: called CC=4 SCAR=13.9
955 1 foreach my $date (@{$self->{-not_in}}) {
956 2 ▶ if ($date =~ $dynamic_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /$dynamic_date/) { } 1 1 2
957 1 push @excl_dates, $date;
958 }
959 else {
960 1 ▶ my @parsed_date = $date_parser->($date) or die "wrong date";
Branch: 1 branch
BranchTrueFalseTotal
my(@parsed_date) = &$date_parser($date) or die "wrong date" 1 0 1
961 1 push @excl_dates, \@parsed_date;
962 }
963 }
964 1 @excl_dates > 0;
965 }
966 1 ▶ or croak "-not_in : needs an arrayref of dates";
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { foreach my $date (@{$$self{"-not_in"};}) { if ($date =~ /$dynamic_date/) { push @excl_dates, $date; } else { my(@parsed_date) = &$date_parser($date) or die "wrong date"; push @excl_dates, \@parsed_date; } ; } ; @excl_dates > 0; } ) or croak("-not_in : needs an arrayref of dates") 1 0 1
967 1 $self->{-not_in} = \@excl_dates;
968 }
969  
970 9 return $self;
971 }
972  
973  
974 18 ▶sub _inspect {
Subroutine _inspect: called CC=11 SCAR=24.0
975 18 my ($self, $data) = @_;
976  
977 18 ▶ my @date = try {$date_parser->($data)};
Subroutine __ANON__: called CC=1 SCAR=0.0
978 18 ▶ @date && check_date(@date)
Branch: 1 branch
BranchTrueFalseTotal
@date and check_date(@date) or return $self->msg("INVALID", $data) 14 4 18
Condition66%2/3
@date and check_date(@date)
!ll&&!rl&&r
4014
MC/DC50%1/2
@date and check_date(@date)
@date check_date(@date)
Truth table66%2/3
A and B
A= @dateB= check_date(@date)
ABresult
0X0
100
111
979 or return $self->msg(INVALID => $data);
980  
981 14 ▶ if (defined $self->{-min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-min"}) 6 8 14
982 6 my $min = _expand_dynamic_date($self->{-min});
983 !check_date(@$min) || (_date_cmp(\@date, $min) < 0)
984 6 ▶ and return $self->msg(TOO_SMALL => _print_date($self->{-min}));
Branch: 1 branch
BranchTrueFalseTotal
!check_date(@$min) || _date_cmp(\@date, $min) < 0 and return $self->msg("TOO_SMALL", _print_date($self->{"-min"})) 2 4 6
Condition66%2/3
not check_date(@$min) or _date_cmp(\@date, $min) < 0
l!l&&r!l&&!r
024
MC/DC50%1/2
not check_date(@$min) or _date_cmp(\@date, $min) < 0
not check_date(@$min) _date_cmp(\@date, $min) < 0
Truth table66%2/3
not(A) or B
A= not check_date(@$min)B= _date_cmp(\@date, $min) < 0
ABresult
1X1
011
000
985 }
986  
987 12 ▶ if (defined $self->{-max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max"}) 3 9 12
988 3 my $max = _expand_dynamic_date($self->{-max});
989 !check_date(@$max) || (_date_cmp(\@date, $max) > 0)
990 3 ▶ and return $self->msg(TOO_BIG => _print_date($self->{-max}));
Branch: 1 branch
BranchTrueFalseTotal
!check_date(@$max) || _date_cmp(\@date, $max) > 0 and return $self->msg("TOO_BIG", _print_date($self->{"-max"})) 1 2 3
Condition66%2/3
not check_date(@$max) or _date_cmp(\@date, $max) > 0
l!l&&r!l&&!r
012
MC/DC50%1/2
not check_date(@$max) or _date_cmp(\@date, $max) > 0
not check_date(@$max) _date_cmp(\@date, $max) > 0
Truth table66%2/3
not(A) or B
A= not check_date(@$max)B= _date_cmp(\@date, $max) > 0
ABresult
1X1
011
000
991 }
992  
993 11 ▶ if ($self->{-not_in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-not_in"}) 2 9 11
994 2 ▶ grep {_date_cmp(\@date, $_) == 0} @{$self->{-not_in}}
Branch: 1 branch
BranchTrueFalseTotal
grep {_date_cmp(\@date, $_) == 0;} @{$$self{"-not_in"};} and return $self->msg("EXCLUSION_SET", $data) 1 1 2
995 and return $self->msg(EXCLUSION_SET => $data);
996 }
997  
998 10 return;
999 }
1000  
1001  
1002 #======================================================================
1003 package Data::Domain::Time;
1004 #======================================================================
1005 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1006 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1007 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1008 5 our @ISA = 'Data::Domain';
1009  
1010 5 my $time_regex = qr/^(\d\d?):?(\d\d?)?:?(\d\d?)?$/;
1011  
1012 9 ▶sub _valid_time {
Subroutine _valid_time: called CC=5 SCAR=17.2
1013 9 my ($h, $m, $s) = @_;
1014 9 ▶ $m ||= 0;
Condition50%1/2
$m ||= 0
l!l
90
MC/DC0%0/1
$m ||= 0
$m
Truth table50%1/2
A
A= $m
Aresult
11
00
1015 9 ▶ $s ||= 0;
Condition50%1/2
$s ||= 0
l!l
09
MC/DC0%0/1
$s ||= 0
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
1016 9 ▶ return ($h <= 23 && $m <= 59 && $s <= 59);
Condition66%4/6
$h <= 23 && $m <= 59 && $s <= 59
!ll&&!rl&&r
108
$h <= 23 and $m <= 59
!ll&&!rl&&r
108
MC/DC33%1/3
$h <= 23 && $m <= 59 && $s <= 59
$h <= 23 $m <= 59 $s <= 59
Truth table50%2/4
A and B && C
A= $h <= 23B= $m <= 59C= $s <= 59
ABCresult
0XX0
10X0
1100
1111
1017 }
1018  
1019  
1020 16 ▶sub _expand_dynamic_time {
Subroutine _expand_dynamic_time: called CC=3 SCAR=15.1
1021 16 my $time = shift;
1022 16 ▶ if (not ref $time) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $time) 0 16 16
1023 0 ▶ $time eq 'now' or croak "unexpected time : $time";
Branch: 1 branch
BranchTrueFalseTotal
$time eq "now" or croak("unexpected time : $time") 0 0 0
1024 0 $time = [(localtime)[2, 1, 0]];
1025 }
1026 16 return $time;
1027 }
1028  
1029  
1030 7 ▶sub _time_cmp {
Subroutine _time_cmp: called CC=7 SCAR=31.9
1031 7 my ($t1, $t2) = map {_expand_dynamic_time($_)} @_;
1032  
1033 7 ▶ return $t1->[0] <=> $t2->[0] # hours
Condition14%2/14
$t1->[0] <=> $t2->[0] || ($t1->[1] || 0) <=> ($t2->[1] || 0) || ($t1->[2] || 0) <=> ($t2->[2] || 0)
l!l&&r!l&&!r
700
$t1->[0] <=> $t2->[0] or ($t1->[1] || 0) <=> ($t2->[1] || 0)
l!l&&r!l&&!r
700
$t1->[1] || 0
l!l
00
$t2->[1] || 0
l!l
00
$t1->[2] || 0
l!l
00
$t2->[2] || 0
l!l
00
MC/DC0%0/3
$t1->[0] <=> $t2->[0] || ($t1->[1] || 0) <=> ($t2->[1] || 0) || ($t1->[2] || 0) <=> ($t2->[2] || 0)
$t1->[0] <=> $t2->[0] $t1->[1] $t1->[2]
MC/DC0%0/1
$t2->[1] || 0
$t2->[1]
MC/DC0%0/1
$t2->[2] || 0
$t2->[2]
Truth table25%1/4
A or B || C
A= $t1->[0] <=> $t2->[0]B= $t1->[1]C= $t1->[2]
ABCresult
1XX1
01X1
0011
0000
Truth table0%0/2
A
A= $t2->[1]
Aresult
11
00
Truth table0%0/2
A
A= $t2->[2]
Aresult
11
00
1034 || ($t1->[1] || 0) <=> ($t2->[1] || 0) # minutes
1035 || ($t1->[2] || 0) <=> ($t2->[2] || 0); # seconds
1036 }
1037  
1038 2 ▶sub _print_time {
Subroutine _print_time: called CC=2 SCAR=6.9
1039 2 my $time = _expand_dynamic_time(shift);
1040 2 ▶ return sprintf "%02d:%02d:%02d", map {$_ || 0} @$time;
Condition100%2/2
$_ || 0
l!l
42
MC/DC100%1/1
$_ || 0
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
1041 }
1042  
1043  
1044 3 ▶sub new {
Subroutine new: called CC=6 SCAR=18.2
1045 3 my $class = shift;
1046 3 my @options = qw/-range -min -max/;
1047 3 my $self = Data::Domain::_parse_args(\@_, \@options);
1048 3 bless $self, $class;
1049  
1050 3 $self->_expand_range(qw/-range -min -max/);
1051  
1052 # parse time boundaries
1053 3 for my $bound (qw/-min -max/) {
1054 6 ▶ if ($self->{$bound} and $self->{$bound} ne 'now') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{$bound} and $self->{$bound} ne "now") 4 2 6
Condition66%2/3
$self->{$bound} and $self->{$bound} ne "now"
!ll&&!rl&&r
204
MC/DC50%1/2
$self->{$bound} and $self->{$bound} ne "now"
$self->{$bound} $self->{$bound} ne "now"
Truth table66%2/3
A and B
A= $self->{$bound}B= $self->{$bound} ne "now"
ABresult
0X0
100
111
1055 4 my @time = ($self->{$bound} =~ $time_regex);
1056 4 ▶ @time && _valid_time(@time)
Branch: 1 branch
BranchTrueFalseTotal
@time and _valid_time(@time) or croak("invalid time ($bound): $self->{$bound}") 4 0 4
Condition33%1/3
@time and _valid_time(@time)
!ll&&!rl&&r
004
MC/DC0%0/2
@time and _valid_time(@time)
@time _valid_time(@time)
Truth table33%1/3
A and B
A= @timeB= _valid_time(@time)
ABresult
0X0
100
111
1057 or croak "invalid time ($bound): $self->{$bound}";
1058 4 $self->{$bound} = \@time;
1059 }
1060 }
1061  
1062 # check order of boundaries
1063 3 ▶ $self->_check_min_max(qw/-min -max/, sub {_time_cmp($_[0], $_[1]) <= 0});
Subroutine __ANON__: called CC=1 SCAR=0.0
1064  
1065 2 return $self;
1066 }
1067  
1068  
1069 6 ▶sub _inspect {
Subroutine _inspect: called CC=7 SCAR=19.5
1070 6 my ($self, $data) = @_;
1071  
1072 6 my @t = ($data =~ $time_regex);
1073 6 ▶ @t and _valid_time(@t)
Branch: 1 branch
BranchTrueFalseTotal
@t and _valid_time(@t) or return $self->msg("INVALID", $data) 4 2 6
Condition100%3/3
@t and _valid_time(@t)
!ll&&!rl&&r
114
MC/DC100%2/2
@t and _valid_time(@t)
@t _valid_time(@t)
Truth table100%3/3
A and B
A= @tB= _valid_time(@t)
ABresult
0X0
100
111
1074 or return $self->msg(INVALID => $data);
1075  
1076 4 ▶ if (defined $self->{-min}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-min"}) 3 1 4
1077 _time_cmp(\@t, $self->{-min}) < 0
1078 3 ▶ and return $self->msg(TOO_SMALL => _print_time($self->{-min}));
Branch: 1 branch
BranchTrueFalseTotal
_time_cmp(\@t, $self->{"-min"}) < 0 and return $self->msg("TOO_SMALL", _print_time($self->{"-min"})) 1 2 3
1079 }
1080  
1081 3 ▶ if (defined $self->{-max}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max"}) 2 1 3
1082 _time_cmp(\@t, $self->{-max}) > 0
1083 2 ▶ and return $self->msg(TOO_BIG => _print_time($self->{-max}));
Branch: 1 branch
BranchTrueFalseTotal
_time_cmp(\@t, $self->{"-max"}) > 0 and return $self->msg("TOO_BIG", _print_time($self->{"-max"})) 1 1 2
1084 }
1085  
1086 2 return;
1087 }
1088  
1089  
1090  
1091 #======================================================================
1092 package Data::Domain::Handle;
1093 #======================================================================
1094 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1095 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1096 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1097 5 our @ISA = 'Data::Domain';
1098  
1099 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
1100 1 my $class = shift;
1101 1 my @options = ();
1102 1 my $self = Data::Domain::_parse_args(\@_, \@options);
1103 1 bless $self, $class;
1104 }
1105  
1106 3 ▶sub _inspect {
Subroutine _inspect: called CC=2 SCAR=6.9
1107 3 my ($self, $data) = @_;
1108 3 ▶ Scalar::Util::openhandle($data)
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Util::openhandle($data) or return $self->msg("INVALID", "") 2 1 3
1109 or return $self->msg(INVALID => '');
1110  
1111 2 return; # otherwise OK, no error
1112 }
1113  
1114  
1115  
1116  
1117 #======================================================================
1118 package Data::Domain::Enum;
1119 #======================================================================
1120 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1121 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1122 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1123 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
1124 5 our @ISA = 'Data::Domain';
1125  
1126 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
1127 5 my $class = shift;
1128 5 my @options = qw/-values/;
1129 5 my $self = Data::Domain::_parse_args(\@_, \@options, -values => 'arrayref');
1130 5 bless $self, $class;
1131  
1132 5 ▶ try {@{$self->{-values}}} or croak "Enum : incorrect set of values";
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
&try(sub { @{$self->{"-values"};}; } ) or croak("Enum : incorrect set of values") 5 0 5
1133  
1134 5 ▶ not grep {! defined $_} @{$self->{-values}}
Branch: 1 branch
BranchTrueFalseTotal
grep {not defined $_;} @{$$self{"-values"};} and croak("Enum : undefined element in values") 1 4 5
1135 or croak "Enum : undefined element in values";
1136  
1137 4 return $self;
1138 }
1139  
1140  
1141 6 ▶sub _inspect {
Subroutine _inspect: called CC=2 SCAR=6.9
1142 6 my ($self, $data) = @_;
1143  
1144 return $self->msg(NOT_IN_LIST => $data)
1145 6 ▶ if not grep {$_ eq $data} @{$self->{-values}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $data;} @{$$self{"-values"};} 2 4 6
1146  
1147 4 return; # otherwise OK, no error
1148 }
1149  
1150  
1151 #======================================================================
1152 package Data::Domain::List;
1153 #======================================================================
1154 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1155 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1156 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1157 5 ▶use List::MoreUtils qw/all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1158 5 ▶use Scalar::Does qw/does/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1159 5 our @ISA = 'Data::Domain';
1160  
1161 21 ▶sub new {
Subroutine new: called CC=11 SCAR=24.6
1162 21 my $class = shift;
1163 21 my @options = qw/-items -size -min_size -max_size -any -all/;
1164 21 my $self = Data::Domain::_parse_args(\@_, \@options, -items => 'arrayref');
1165 21 bless $self, $class;
1166  
1167 21 $self->_expand_range(qw/-size -min_size -max_size/);
1168 21 $self->_check_min_max(qw/-min_size -max_size <=/);
1169  
1170 20 ▶ if ($self->{-items}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"-items"}) 10 10 20
1171 10 ▶ does($self->{-items}, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
&does($self->{"-items"}, "ARRAY") or croak("invalid -items for Data::Domain::List") 10 0 10
1172 or croak "invalid -items for Data::Domain::List";
1173  
1174 # if -items is given, then both -{min,max}_size cannot be shorter
1175 10 for my $bound (qw/-min_size -max_size/) {
1176 croak "$bound does not match -items"
1177 0 ▶ if $self->{$bound} and $self->{$bound} < @{$self->{-items}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{$bound} and $self->{$bound} < @{$self->{"-items"};} 0 20 20
Condition33%1/3
$self->{$bound} and $self->{$bound} < @{$self->{"-items"};}
!ll&&!rl&&r
2000
MC/DC0%0/2
$self->{$bound} and $self->{$bound} < @{$self->{"-items"};}
$self->{$bound} $self->{$bound} < @{$self->{"-items"};}
Truth table33%1/3
A and B
A= $self->{$bound}B= $self->{$bound} < @{$self->{"-items"};}
ABresult
0X0
100
111
1178 }
1179  
1180 # check that all items are associated to proper subdomains
1181 10 my @invalid_fields = grep {!$self->_is_proper_subdomain($self->{-items}[$_])} 0 .. $#{$self->{-items}};
1182 10 ▶ croak "invalid subdomain for field: ", join ", ", @invalid_fields if @invalid_fields;
Branch: 1 branch
BranchTrueFalseTotal
if @invalid_fields 0 10 10
1183 }
1184  
1185 # check that -all or -any are domains or lists of domains
1186 20 for my $arg (qw/-all -any/) {
1187 40 ▶ if (my $dom = $self->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dom = $self->{$arg}) 12 28 40
1188 12 ▶ $dom = [$dom] unless does($dom, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless &does($dom, "ARRAY") 11 1 12
1189 12 ▶ all {does($_, 'Data::Domain') || does($_, 'CODE')} @$dom
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
&all(sub { &does($_, "Data::Domain") or &does($_, "CODE"); } , @$dom) or croak("invalid arg to $arg in Data::Domain::List") 12 0 12
&does($_, "Data::Domain") or &does($_, "CODE") 12 1 13
1190 or croak "invalid arg to $arg in Data::Domain::List";
1191 }
1192 }
1193  
1194 20 return $self;
1195 }
1196  
1197  
1198 46 ▶sub _inspect {
Subroutine _inspect: called CC=29 SCAR=33.7
1199 46 my ($self, $data, $context) = @_;
1200 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1201  
1202 46 ▶ does($data, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
&does($data, "ARRAY") or return $self->msg("NOT_A_LIST", $data) 45 1 46
1203 or return $self->msg(NOT_A_LIST => $data);
1204  
1205 # build a shallow copy of the data, so that default values can be inserted
1206 45 my @valid_data;
1207 45 ▶ @valid_data = @$data if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 4 41 45
1208  
1209  
1210 45 ▶ if (defined $self->{-min_size} && @$data < $self->{-min_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-min_size"} and @$data < $self->{"-min_size"}) 1 44 45
Condition100%3/3
defined $self->{"-min_size"} and @$data < $self->{"-min_size"}
!ll&&!rl&&r
3861
MC/DC100%2/2
defined $self->{"-min_size"} and @$data < $self->{"-min_size"}
defined $self->{"-min_size"} @$data < $self->{"-min_size"}
Truth table100%3/3
A and B
A= defined $self->{"-min_size"}B= @$data < $self->{"-min_size"}
ABresult
0X0
100
111
1211 1 return $self->msg(TOO_SHORT => $self->{-min_size});
1212 }
1213  
1214 44 ▶ if (defined $self->{-max_size} && @$data > $self->{-max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"-max_size"} and @$data > $self->{"-max_size"}) 1 43 44
Condition100%3/3
defined $self->{"-max_size"} and @$data > $self->{"-max_size"}
!ll&&!rl&&r
3851
MC/DC100%2/2
defined $self->{"-max_size"} and @$data > $self->{"-max_size"}
defined $self->{"-max_size"} @$data > $self->{"-max_size"}
Truth table100%3/3
A and B
A= defined $self->{"-max_size"}B= @$data > $self->{"-max_size"}
ABresult
0X0
100
111
1215 1 return $self->msg(TOO_LONG => $self->{-max_size});
1216 }
1217  
1218 43 ▶ return unless $self->{-items} || $self->{-all} || $self->{-any};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-items"} or $self->{"-all"} or $self->{"-any"} 2 41 43
Condition100%6/6
$self->{"-items"} or $self->{"-all"} or $self->{"-any"}
l!l&&r!l&&!r
3562
$self->{"-items"} or $self->{"-all"}
l!l&&r!l&&!r
26413
MC/DC66%2/3
$self->{"-items"} or $self->{"-all"} or $self->{"-any"}
$self->{"-items"} $self->{"-all"} $self->{"-any"}
Truth table75%3/4
A or B or C
A= $self->{"-items"}B= $self->{"-all"}C= $self->{"-any"}
ABCresult
1XX1
01X1
0011
0000
1219  
1220 # prepare context for calling lazy subdomains
1221 41 local $context->{list} = $data;
1222  
1223 # initializing some variables
1224 41 my @msgs;
1225 my $has_invalid;
1226 41 ▶ my $items = $self->{-items} || [];
Condition100%2/2
$self->{'-items'} || []
l!l
2615
MC/DC100%1/1
$self->{'-items'} || []
$self->{'-items'}
Truth table100%2/2
A
A= $self->{'-items'}
Aresult
11
00
1227 41 my $n_items = @$items;
1228 41 my $n_data = @$data;
1229  
1230 # check the -items conditions
1231 41 ▶ for (my $i = 0; $i < $n_items; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n_items) 67 41 108
1232 67 local $context->{path} = [@{$context->{path}}, $i];
1233 67 ▶ my $subdomain = $self->_build_subdomain($items->[$i], $context)
Branch: 1 branch
BranchTrueFalseTotal
my $subdomain = $self->_build_subdomain($items->[$i], $context) or next 67 0 67
1234 or next;
1235 67 $msgs[$i] = $subdomain->inspect($data->[$i], $context, ! exists $data->[$i]);
1236 67 ▶ $has_invalid ||= $msgs[$i];
Condition100%3/3
$has_invalid ||= $msgs[$i]
l!l&&r!l&&!r
8653
MC/DC100%2/2
$has_invalid ||= $msgs[$i]
$has_invalid $msgs[$i]
Truth table100%3/3
A ||= B
A= $has_invalidB= $msgs[$i]
ABresult
1X1
011
000
1237  
1238 # re-inject the valid data for that slot
1239 67 ▶ $valid_data[$i] = $context->{gather_valid_data} if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 8 59 67
1240 }
1241  
1242 # check the -all condition (can be a single domain or an arrayref of domains)
1243 41 ▶ if (my $all = $self->{-all}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $all = $self->{"-all"}) 12 29 41
1244 12 ▶ $all = [$all] unless does($all, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless &does($all, "ARRAY") 12 0 12
1245 12 my $n_all = @$all;
1246 12 ▶ for (my $i = $n_items, my $j = 0; # $i iterates over @$data, $j over @$all
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n_data) 33 12 45
1247 $i < $n_data;
1248 $i++, $j = ($j + 1) % $n_all) {
1249 33 local $context->{path} = [@{$context->{path}}, $i];
1250 33 my $subdomain = $self->_build_subdomain($all->[$j], $context);
1251 33 $msgs[$i] = $subdomain->inspect($data->[$i], $context);
1252 33 ▶ $has_invalid ||= $msgs[$i];
Condition100%3/3
$has_invalid ||= $msgs[$i]
l!l&&r!l&&!r
3525
MC/DC100%2/2
$has_invalid ||= $msgs[$i]
$has_invalid $msgs[$i]
Truth table100%3/3
A ||= B
A= $has_invalidB= $msgs[$i]
ABresult
1X1
011
000
1253  
1254 # re-inject the valid data for that slot
1255 $valid_data[$i] = $context->{gather_valid_data} if exists $context->{gather_valid_data}
1256 33 ▶ && not defined $valid_data[$i];
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} and not defined $valid_data[$i] 2 31 33
Condition100%3/3
exists $context->{"gather_valid_data"} and not defined $valid_data[$i]
!ll&&!rl&&r
3012
MC/DC100%2/2
exists $context->{"gather_valid_data"} and not defined $valid_data[$i]
exists $context->{"gather_valid_data"} not defined $valid_data[$i]
Truth table100%3/3
A and not(B)
A= exists $context->{"gather_valid_data"}B= not defined $valid_data[$i]
ABresult
0X0
100
111
1257 }
1258 }
1259  
1260 # stop here if there was any error message
1261 41 ▶ return \@msgs if $has_invalid;
Branch: 1 branch
BranchTrueFalseTotal
if $has_invalid 11 30 41
1262  
1263 # all other conditions were good, now check the "any" conditions
1264 30 ▶ if (my $any = $self->{-any}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $any = $self->{"-any"}) 13 17 30
1265 13 ▶ $any = [$any] unless does($any, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless &does($any, "ARRAY") 9 4 13
1266  
1267 # there must be data to inspect
1268 13 ▶ $n_data > $n_items
Branch: 1 branch
BranchTrueFalseTotal
$n_data > $n_items or return $self->msg("ANY", $any->[0]->name) 12 1 13
1269 or return $self->msg(ANY => $any->[0]->name);
1270  
1271 # inspect the remaining data for all 'any' conditions
1272 CONDITION:
1273 12 foreach my $condition (@$any) {
1274 15 my $subdomain;
1275 15 ▶ for (my $i = $n_items; $i < $n_data; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n_data) 31 4 35
1276 31 local $context->{path} = [@{$context->{path}}, $i];
1277 31 $subdomain = $self->_build_subdomain($condition, $context);
1278 31 my $error = $subdomain->inspect($data->[$i], $context);
1279 31 ▶ next CONDITION if not $error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 11 20 31
1280 }
1281 4 return $self->msg(ANY => $subdomain->name);
1282 }
1283 }
1284  
1285 # re-inject the whole valid array into the context
1286 25 ▶ $context->{gather_valid_data} = \@valid_data if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 4 21 25
1287  
1288 25 return; # OK, no error
1289 }
1290  
1291  
1292 1 ▶sub func_signature {
Subroutine func_signature: called CC=1 SCAR=0.0
1293 1 my ($self) = @_;
1294  
1295 # override the parent method : pass the parameters list as an arrayref to validate(),
1296 # and return the validated datatree as an array
1297 1 ▶ return sub {my $params = $self->validate(\@_); @$params};
Subroutine __ANON__: called CC=1 SCAR=0.0
1298 }
1299  
1300 #======================================================================
1301 package Data::Domain::Struct;
1302 #======================================================================
1303 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1304 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1305 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1306 5 ▶use Scalar::Does qw/does/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1307 5 our @ISA = 'Data::Domain';
1308  
1309 25 ▶sub new {
Subroutine new: called CC=15 SCAR=27.6
1310 25 my $class = shift;
1311 25 my @options = qw/-fields -exclude -keys -values -may_ignore/;
1312 25 my $self = Data::Domain::_parse_args(\@_, \@options, -fields => 'arrayref');
1313 25 bless $self, $class;
1314  
1315 # parse the -fields option
1316 25 ▶ my $fields = $self->{-fields} || [];
Condition100%2/2
$self->{'-fields'} || []
l!l
232
MC/DC100%1/1
$self->{'-fields'} || []
$self->{'-fields'}
Truth table100%2/2
A
A= $self->{'-fields'}
Aresult
11
00
1317 25 ▶ if (does($fields, 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (&does($fields, 'ARRAY')) { } 23 2 25
elsif (&does($fields, 'HASH')) { } 2 0 2
1318 # transform arrayref into hashref plus an ordered list of keys
1319 23 $self->{-fields_list} = [];
1320 23 $self->{-fields} = {};
1321 23 ▶ for (my $i = 0; $i < @$fields; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$fields) 50 23 73
1322 50 my ($key, $val) = ($fields->[$i], $fields->[$i+1]);
1323 50 push @{$self->{-fields_list}}, $key;
1324 50 $self->{-fields}{$key} = $val;
1325 }
1326 }
1327 elsif (does($fields, 'HASH')) {
1328 # keep given hashref, add list of keys
1329 2 $self->{-fields_list} = [sort keys %$fields];
1330 }
1331 else {
1332 0 croak "invalid data for -fields option";
1333 }
1334  
1335 # check that all fields are associated to proper subdomains
1336 25 my @invalid_fields = grep {!$self->_is_proper_subdomain($self->{-fields}{$_})} @{$self->{-fields_list}};
1337 25 ▶ croak "invalid subdomain for field: ", join ", ", @invalid_fields if @invalid_fields;
Branch: 1 branch
BranchTrueFalseTotal
if @invalid_fields 0 25 25
1338  
1339 # check that -exclude and -may_ignore are an arrayref or a regex or a string
1340 25 for my $opt (qw/-exclude -may_ignore/) {
1341 50 ▶ my $val = $self->{$opt} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $val = $self->{$opt} or next 8 42 50
1342 8 ▶ does($val, 'ARRAY') || does($val, 'Regexp') || !ref($val)
Branch: 1 branch
BranchTrueFalseTotal
&does($val, "ARRAY") or &does($val, "Regexp") or not ref $val or croak("invalid data for $opt option") 8 0 8
Condition83%5/6
&does($val, "ARRAY") or &does($val, "Regexp") or not ref $val
l!l&&r!l&&!r
440
&does($val, "ARRAY") or &does($val, "Regexp")
l!l&&r!l&&!r
224
MC/DC0%0/3
&does($val, "ARRAY") or &does($val, "Regexp") or not ref $val
&does($val, "ARRAY") &does($val, "Regexp") not ref $val
Truth table75%3/4
A or B or not(C)
A= &does($val, "ARRAY")B= &does($val, "Regexp")C= not ref $val
ABCresult
1XX1
01X1
0011
0000
1343 or croak "invalid data for $opt option";
1344 }
1345  
1346 # check that -keys or -values are List domains
1347 25 for my $arg (qw/-keys -values/) {
1348 50 ▶ if (my $dom = $self->{$arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dom = $self->{$arg}) 2 48 50
1349 2 ▶ does($dom, 'Data::Domain::List') or does($dom, 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
&does($dom, "Data::Domain::List") or &does($dom, "CODE") or croak("$arg in Data::Domain::Struct should be a List domain") 2 0 2
Condition33%1/3
&does($dom, "Data::Domain::List") or &does($dom, "CODE")
l!l&&r!l&&!r
200
MC/DC0%0/2
&does($dom, "Data::Domain::List") or &does($dom, "CODE")
&does($dom, "Data::Domain::List") &does($dom, "CODE")
Truth table33%1/3
A or B
A= &does($dom, "Data::Domain::List")B= &does($dom, "CODE")
ABresult
1X1
011
000
1350 or croak "$arg in Data::Domain::Struct should be a List domain";
1351 }
1352 }
1353  
1354 25 return $self;
1355 }
1356  
1357  
1358 151 ▶sub _inspect {
Subroutine _inspect: called CC=16 SCAR=27.7
1359 151 my ($self, $data, $context) = @_;
1360 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1361  
1362 # check that $data is a hashref
1363 151 ▶ does($data, 'HASH')
Branch: 1 branch
BranchTrueFalseTotal
&does($data, "HASH") or return $self->msg("NOT_A_HASH", $data) 149 2 151
1364 or return $self->msg(NOT_A_HASH => $data);
1365  
1366 149 my %msgs;
1367  
1368 # build a shallow copy of the data, so that default values can be inserted
1369 my %valid_data;
1370 149 ▶ %valid_data = %$data if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 6 143 149
1371  
1372  
1373 # check if there are any forbidden fields
1374 149 ▶ if (my $exclude = $self->{-exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $exclude = $self->{"-exclude"}) 12 137 149
1375 12 my @other_fields = grep {!$self->{-fields}{$_}} keys %$data;
1376 12 my @wrong_fields = grep {$self->_field_matches(-exclude => $_)} @other_fields;
1377 12 ▶ $msgs{-exclude} = $self->msg(FORBIDDEN_FIELD => join ", ", map {"'$_'"} sort @wrong_fields)
Branch: 1 branch
BranchTrueFalseTotal
if @wrong_fields 8 4 12
1378 if @wrong_fields;
1379 }
1380  
1381 # prepare context for calling lazy subdomains
1382 149 local $context->{flat} = {%{$context->{flat}}, %$data};
1383  
1384 # check fields of the domain
1385 FIELD:
1386 149 foreach my $field (@{$self->{-fields_list}}) {
1387 396 ▶ next FIELD if not exists $data->{$field} and $self->_field_matches(-may_ignore => $field);
Branch: 1 branch
BranchTrueFalseTotal
if not exists $data->{$field} and $self->_field_matches("-may_ignore", $field) 8 388 396
Condition100%3/3
not exists $data->{$field} and $self->_field_matches("-may_ignore", $field)
!ll&&!rl&&r
374148
MC/DC100%2/2
not exists $data->{$field} and $self->_field_matches("-may_ignore", $field)
not exists $data->{$field} $self->_field_matches("-may_ignore", $field)
Truth table100%3/3
not(A) and B
A= not exists $data->{$field}B= $self->_field_matches("-may_ignore", $field)
ABresult
0X0
100
111
1388 388 local $context->{path} = [@{$context->{path}}, $field];
1389 388 my $field_spec = $self->{-fields}{$field};
1390 388 my $subdomain = $self->_build_subdomain($field_spec, $context);
1391 387 my $msg = $subdomain->inspect($data->{$field}, $context, ! exists $data->{$field});
1392 288 ▶ $msgs{$field} = $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 17 271 288
1393  
1394 # re-inject the valid data for that field
1395 288 ▶ $valid_data{$field} = $context->{gather_valid_data} if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 15 273 288
1396 }
1397  
1398 # check the List domain for keys
1399 49 ▶ if (my $keys_dom = $self->{-keys}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $keys_dom = $self->{"-keys"}) 3 46 49
1400 3 local $context->{path} = [@{$context->{path}}, "-keys"];
1401 3 my $subdomain = $self->_build_subdomain($keys_dom, $context);
1402 3 my $msg = $subdomain->inspect([keys %$data], $context);
1403 3 ▶ $msgs{-keys} = $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 1 2 3
1404 }
1405  
1406 # check the List domain for values
1407 49 ▶ if (my $values_dom = $self->{-values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values_dom = $self->{"-values"}) 3 46 49
1408 3 local $context->{path} = [@{$context->{path}}, "-values"];
1409 3 my $subdomain = $self->_build_subdomain($values_dom, $context);
1410 3 my $msg = $subdomain->inspect([values %$data], $context);
1411 3 ▶ $msgs{-values} = $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 1 2 3
1412 }
1413  
1414 # re-inject the whole valid tree into the context
1415 49 ▶ $context->{gather_valid_data} = \%valid_data if exists $context->{gather_valid_data};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"gather_valid_data"} 6 43 49
1416  
1417 49 ▶ return keys %msgs ? \%msgs : undef;
Branch: 1 branch
BranchTrueFalseTotal
keys %msgs ? : 20 29 49
1418 }
1419  
1420 33 ▶sub _field_matches {
Subroutine _field_matches: called CC=3 SCAR=11.0
1421 33 my ($self, $spec, $field) = @_;
1422  
1423 33 my $spec_content = $self->{$spec};
1424 33 ▶ return $spec_content && (match::simple::match($spec_content, ['*', 'all'])
Condition100%6/6
$spec_content && (match::simple::match($spec_content, ['*', 'all']) || match::simple::match($field, $spec_content))
!ll&&!rl&&r
13317
match::simple::match($spec_content, ["*", "all"]) or match::simple::match($field, $spec_content)
l!l&&r!l&&!r
1163
MC/DC100%3/3
$spec_content && (match::simple::match($spec_content, ['*', 'all']) || match::simple::match($field, $spec_content))
$spec_content match::simple::match($spec_content, ["*", "all"]) match::simple::match($field, $spec_content)
Truth table100%4/4
A && B or C
A= $spec_contentB= match::simple::match($spec_content, ["*", "all"])C= match::simple::match($field, $spec_content)
ABCresult
0XX0
1000
11X1
1011
1425 ||
1426 match::simple::match($field, $spec_content));
1427 }
1428  
1429  
1430 1 ▶sub func_signature {
Subroutine func_signature: called CC=1 SCAR=0.0
1431 1 my ($self) = @_;
1432  
1433 # override the parent method : treat the parameters list as a hash,
1434 # and return the validated datatree as a hashref
1435 1 ▶ return sub {my $params = $self->validate({@_}); %$params};
Subroutine __ANON__: called CC=1 SCAR=0.0
1436 }
1437  
1438  
1439  
1440 #======================================================================
1441 package Data::Domain::Struict; # domain for a strict Struct :-)
1442 #======================================================================
1443 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1444 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1445 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1446 5 ▶use Scalar::Does qw/does/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1447 5 our @ISA = 'Data::Domain::Struct';
1448  
1449 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
1450 3 my $class = shift;
1451 3 my $self = $class->SUPER::new(@_);
1452  
1453 3 ▶ not exists $self->{-exclude} or croak "Struict(...): invalid option: '-exclude'";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"-exclude"} and croak("Struict(...): invalid option: '-exclude'") 1 2 3
1454 2 $self->{-exclude} = '*';
1455  
1456 2 return $self;
1457 }
1458  
1459  
1460 #======================================================================
1461 package Data::Domain::One_of;
1462 #======================================================================
1463 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1464 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1465 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1466 5 our @ISA = 'Data::Domain';
1467  
1468 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
1469 2 my $class = shift;
1470 2 my @options = qw/-options/;
1471 2 my $self = Data::Domain::_parse_args(\@_, \@options, -options => 'arrayref');
1472 2 bless $self, $class;
1473  
1474 2 ▶ Scalar::Does::does($self->{-options}, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Does::does($self->{"-options"}, "ARRAY") or croak("One_of: invalid options") 2 0 2
1475 or croak "One_of: invalid options";
1476  
1477 2 return $self;
1478 }
1479  
1480  
1481 213 ▶sub _inspect {
Subroutine _inspect: called CC=3 SCAR=11.0
1482 213 my ($self, $data, $context) = @_;
1483 213 my @msgs;
1484 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1485  
1486 213 for my $subdomain (@{$self->{-options}}) {
1487 321 ▶ my $msg = $subdomain->inspect($data, $context)
Branch: 1 branch
BranchTrueFalseTotal
my $msg = $subdomain->inspect($data, $context) or return 112 109 221
1488 or return; # $subdomain was successful
1489 112 push @msgs, $msg;
1490 }
1491 4 return \@msgs;
1492 }
1493  
1494  
1495 1 ▶sub func_signature {
Subroutine func_signature: called CC=1 SCAR=0.0
1496 1 my ($self) = @_;
1497  
1498 # take a reference to the func_signature implementation for the
1499 # first option ... assuming all remaining options have the same
1500 # structure. This wil not work in all cases, but is better than nothing.
1501 1 my $first_sig_ref = $self->{-options}[0]->can("func_signature");
1502  
1503 # invoke that implementation on $self
1504 1 return $self->$first_sig_ref;
1505 }
1506  
1507  
1508  
1509 #======================================================================
1510 package Data::Domain::All_of;
1511 #======================================================================
1512 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1513 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1514 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1515 5 our @ISA = 'Data::Domain';
1516  
1517 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
1518 2 my $class = shift;
1519 2 my @options = qw/-options/;
1520 2 my $self = Data::Domain::_parse_args(\@_, \@options, -options => 'arrayref');
1521 2 bless $self, $class;
1522  
1523 2 ▶ Scalar::Does::does($self->{-options}, 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
&Scalar::Does::does($self->{"-options"}, "ARRAY") or croak("All_of: invalid options") 2 0 2
1524 or croak "All_of: invalid options";
1525  
1526 2 return $self;
1527 }
1528  
1529  
1530 4 ▶sub _inspect {
Subroutine _inspect: called CC=4 SCAR=13.9
1531 4 my ($self, $data, $context) = @_;
1532 4 my @msgs;
1533 5 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1534  
1535 4 for my $subdomain (@{$self->{-options}}) {
1536 8 my $msg = $subdomain->inspect($data, $context);
1537 8 ▶ push @msgs, $msg if $msg; # subdomain failed
Branch: 1 branch
BranchTrueFalseTotal
if $msg 2 6 8
1538 }
1539 4 ▶ return @msgs ? \@msgs : undef;
Branch: 1 branch
BranchTrueFalseTotal
@msgs ? : 2 2 4
1540 }
1541  
1542  
1543 # func_signature : reuse the implementation of the "One_of" domain
1544 5 *func_signature = \&Data::Domain::One_of::func_signature;
1545  
1546  
1547 #======================================================================
1548  
1549 5 1;
1550  
1551  
1552 __END__
↑ Summary