blib/lib/Scalar/Validation.pm

filter: Statementstmt 70.3% 483 / 687 Branchbran 67.0% 173 / 258 Conditioncond 64.2% 52 / 81 MC/DCmcdc 36.7% 18 / 49 Subroutinesub 70.0% 117 / 167 Podpod 14.2% 4 / 28 totaltotal 66.6% 847 / 1270 CC 167 SCAR 23.3
CC
167
Cov
69%
_api_doc_get_argument_info
110.0
__ANON__
31.9
list_meta_info
30.0
CRAP 10.2
↑ Summary
1 # perl
2 #
3 # Class Scalar::Validation
4 #
5 # Simple rule based validation package for scalar values
6 #
7 # Ralf Peine, Sat Oct 25 15:10:22 2014
8 #
9 # More documentation at the end of file
10 #------------------------------------------------------------------------------
11  
12 1 $VERSION = "0.700";
13  
14 package Scalar::Validation;
15  
16 1 ▶use base qw (Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our @EXPORT = qw();
22 1 our @EXPORT_OK = qw (validate is_valid validate_and_correct npar named_parameter par parameter
23 get_rules rule_known declare_rule delete_rule replace_rule enum Enum enum_explained Enum_explained
24 greater_than greater_equal less_than less_equal equal_to g_t g_e l_t l_e
25 is_a
26 p_start parameters_start convert_to_named_params parameters_end p_end
27 validation_trouble get_validation_trouble_level add_validation_trouble
28 validation_messages get_and_reset_validation_messages prepare_validation_mode
29 meta_info_clear build_meta_info_for_module end_meta_info_gen get_meta_info list_meta_info
30 );
31  
32 1 our %EXPORT_TAGS = (
33 all => [qw(validate is_valid validate_and_correct npar named_parameter par parameter
34 get_rules rule_known declare_rule delete_rule replace_rule enum Enum enum_explained Enum_explained
35 greater_than greater_equal less_than less_equal equal_to g_t g_e l_t l_e
36 is_a
37 p_start parameters_start convert_to_named_params parameters_end p_end
38 validation_trouble get_validation_trouble_level add_validation_trouble
39 validation_messages get_and_reset_validation_messages prepare_validation_mode
40 meta_info_clear build_meta_info_for_module end_meta_info_gen get_meta_info list_meta_info
41 )],);
42  
43 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 # use Data::Dumper;
45  
46 # ------------------------------------------------------------------------------
47 #
48 # Initiliazation
49 #
50 # ------------------------------------------------------------------------------
51  
52 1 our $ignore_callers = { __PACKAGE__ , 1 };
53 1 our $ignore_caller_pattern;
54  
55 1 ▶sub _update_caller_pattern {
Subroutine _update_caller_pattern: called CC=1 SCAR=0.0
56 1 $ignore_caller_pattern = eval ("qr/^(".join("|",keys (%$ignore_callers)).")/o");
57 }
58  
59 0 ▶sub ignore_caller {
Subroutine ignore_caller: not called CC=3 SCAR=24.8
Pod undocumented
60 0 my $module_string = shift;
61  
62 0 ▶ die "not a module string: '$module_string'" unless $module_string || $module_string =~ /^[\w:]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $module_string or $module_string =~ /^[\w:]+$/ 0 0 0
Condition0%0/3
$module_string or $module_string =~ /^[\w:]+$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$module_string or $module_string =~ /^[\w:]+$/
$module_string $module_string =~ /^[\w:]+$/
Truth table0%0/3
A or B
A= $module_stringB= $module_string =~ /^[\w:]+$/
ABresult
1X1
011
000
63
64 0 $ignore_callers->{$module_string} = 1;
65 0 update_caller_pattern();
66 }
67  
68 1 _update_caller_pattern();
69  
70 1 _init_private_calls();
71 1 _init_run_API();
72  
73 # _init_doc_API();
74  
75 # ------------------------------------------------------------------------------
76 #
77 # default actions, not changable
78 #
79 # ------------------------------------------------------------------------------
80  
81 1 our $call_info = '';
82  
83 67 ▶my $croak_sub = sub { croak "Error: ",@_; };
Subroutine __ANON__: called CC=1 SCAR=0.0
84 129 ▶my $get_caller_info_default = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
85 129 my ($module, $file_name, $line, $sub_name);
86 129 $sub_name = __PACKAGE__;
87 129 my $call_level_iter = 1;
88  
89 129 ▶ while ($sub_name =~ $ignore_caller_pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_name =~ /$Scalar::Validation::ignore_caller_pattern/) 478 129 607
90 478 ($module, $file_name, $line, $sub_name) = caller($call_level_iter++);
91 478 ▶ $sub_name = '' unless $sub_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_name 57 421 478
92 }
93
94 129 ▶ $sub_name = "MAIN" unless $sub_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub_name 57 72 129
95 129 return $sub_name;
96 1 };
97  
98 # ------------------------------------------------------------------------------
99 #
100 # variables my be overwritten by user
101 #
102 # ------------------------------------------------------------------------------
103  
104 1 our $message_store = undef; # local $Scalar::Validation::message_store = []; # to start storing messages
105 1 our $trouble_level = 0; # to count failed validations. Not affected by is_valid(...)
106 1 our $off = 0; # no validation checks if $off == 1
107 1 our $validate_defaults = 1; # validate default values defined by -Default rule, if set
108  
109 # ------------------------------------------------------------------------------
110 #
111 # actions, changable
112 #
113 # ------------------------------------------------------------------------------
114  
115 1 our $fail_action = $croak_sub;
116 1 our $get_caller_info = $get_caller_info_default;
117  
118 # ------------------------------------------------------------------------------
119 #
120 # private vars of Validation "Instance"
121 #
122 # ------------------------------------------------------------------------------
123 1 my $non_blessed = {
124 REF => 1,
125 ARRAY => 1,
126 HASH => 1,
127 };
128  
129 1 my $special_rules;
130 my $rule_store;
131 1 my $get_content_subs;
132  
133  
134 1 my $class_meta_model = {};
135 1 my $doc_class_name;
136 my $doc_class_method;
137  
138 # ------------------------------------------------------------------------------
139 #
140 # normal rules, can be added, replaced, removed
141 #
142 # ------------------------------------------------------------------------------
143  
144 $rule_store = {
145  
146 # --- This rules are needed for Validation.pm to work, don't delete or change! ---
147
148 Defined => { -name => 'Defined',
149 6 ▶ -where => sub { defined $_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
150 0 ▶ -message => sub { "value is not defined" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 -owner => 'CPAN',
152 -description => "Value is defined",
153 },
154 Filled => { -name => 'Filled',
155 199 ▶ -where => sub { defined $_ and ref($_) eq '' and $_ ne '' },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and ref $_ eq "" and $_ ne "" 193 6 199
Condition100%3/3
defined $_ and ref $_ eq ""
!ll&&!rl&&r
51193
MC/DC100%2/2
defined $_ and ref $_ eq ""
defined $_ ref $_ eq ""
Truth table100%3/3
A and B
A= defined $_B= ref $_ eq ""
ABresult
0X0
100
111
156 2 ▶ -message => sub { "value is not set" },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 -owner => 'CPAN',
158 -description => "Value is Scalar and defined and not empty ('')",
159 },
160 Empty => { -name => 'Empty',
161 5 ▶ -where => sub { !defined $_ or $_ eq '' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
not defined $_ or $_ eq "" 1 4 5
162 3 ▶ -message => sub { "value $_ has to be empty" },
Subroutine __ANON__: called CC=1 SCAR=0.0
163 -owner => 'CPAN',
164 -description => "Value is not defined or ''",
165 },
166 Optional => { -name => 'Optional',
167 6 ▶ -where => sub { 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
168 0 ▶ -message => sub { "value is optional" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
169 -owner => 'CPAN',
170 -description => "Value is optional, cannot fail. Use as last entry in -Or rule.",
171 },
172 String => { -name => 'String',
173 44 ▶ -where => sub { defined $_ and ref($_) eq '' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and ref $_ eq "" 43 1 44
174 1 ▶ -message => sub { "value $_ is not a string" },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 -owner => 'CPAN',
176 -description => "Values is a Scalar and defined",
177 },
178 Int => { -name => 'Int',
179 -as => 'Filled',
180 84 ▶ -where => sub { /^[\+\-]?\d+$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
181 13 ▶ -message => sub { "value $_ is not an integer" },
Subroutine __ANON__: called CC=1 SCAR=0.0
182 -owner => 'CPAN',
183 -description => "Value is an integer",
184 },
185 Even => { -name => 'Even',
186 -as => 'Int',
187 17 ▶ -where => sub { $_ % 2 ? 0: 1; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ % 2 ? : 1 16 17
188 0 ▶ -message => sub { "value $_ is not an integer or not even"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 -owner => 'CPAN',
190 -description => 'Value is an even integer ($_ % 2 == 0)',
191 },
192 Scalar => { -name => 'Scalar',
193 197 ▶ -where => sub { ref($_) eq '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
194 3 ▶ -message => sub { "value $_ is not a scalar" },
Subroutine __ANON__: called CC=1 SCAR=0.0
195 -owner => 'CPAN',
196 -description => 'Value is a Scalar : ref ($_) eq ""',
197 },
198 Ref => { -name => 'Ref',
199 2 ▶ -where => sub { $_ and ref($_) ne '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref $_ ne "" 2 0 2
200 0 ▶ -message => sub { "value $_ is not a reference" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 -owner => 'CPAN',
202 -description => "Value is a reference and not a scalar.",
203 },
204 ArrayRef => { -name => 'ArrayRef',
205 20 ▶ -where => sub { $_ and ref($_) eq 'ARRAY' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref $_ eq "ARRAY" 19 1 20
206 1 ▶ -message => sub { "value $_ is not a array reference" },
Subroutine __ANON__: called CC=1 SCAR=0.0
207 -owner => 'CPAN',
208 -description => "Value is an Array reference.",
209 },
210 HashRef => { -name => 'HashRef',
211 134 ▶ -where => sub { $_ and ref($_) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref $_ eq "HASH" 134 0 134
212 27 ▶ -message => sub { "value $_ is not a hash reference" },
Subroutine __ANON__: called CC=1 SCAR=0.0
213 -owner => 'CPAN',
214 -description => "Value is a Hash reference.",
215 },
216 CodeRef => { -name => 'CodeRef',
217 57 ▶ -where => sub { $_ and ref($_) eq 'CODE' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_ and ref $_ eq "CODE" 55 2 57
218 0 ▶ -message => sub { "value $_ is not a code reference" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 -owner => 'CPAN',
220 -description => "Value is a Code reference.",
221 },
222 Class => { -name => 'Class',
223 10 ▶ -where => sub { return 0 unless $_;
Subroutine __ANON__: called CC=4 SCAR=13.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 2 8 10
224 8 my $type_name = ref($_);
225 8 ▶ !$type_name || $non_blessed->{$type_name} ? 0: 1;
Branch: 1 branch
BranchTrueFalseTotal
!$type_name || $non_blessed->{$type_name} ? : 5 3 8
Condition100%3/3
not $type_name or $non_blessed->{$type_name}
l!l&&r!l&&!r
233
MC/DC100%2/2
not $type_name or $non_blessed->{$type_name}
not $type_name $non_blessed->{$type_name}
Truth table100%3/3
not(A) or B
A= not $type_nameB= $non_blessed->{$type_name}
ABresult
1X1
011
000
226 },
227 1 ▶ -message => sub { "value $_ is not a reference" },
Subroutine __ANON__: called CC=1 SCAR=0.0
228 -owner => 'CPAN',
229 -description => "Value is a reference and not a scalar.",
230 },
231 ModuleName => { -name => 'ModuleName',
232 -as => Filled =>
233 0 ▶ -where => sub { /^\w+(::\w+)*$/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
234 },
235 0 ▶ -message => sub { "value $_ is not a module name like A::Ba::Cba" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 -owner => 'CPAN',
237 -description => "Value is a module name like A::Ba::Cba.",
238 },
239  
240 # --- Some additional global rules --------------------
241
242 ExistingFile => { -name => 'ExistingFile',
243 -as => 'Filled',
244 0 ▶ -where => sub { -f $_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 0 ▶ -message => sub { "$_ is not a valid name of an existing file"},
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 -owner => 'CPAN',
247 -description => "File with given file name has to exist"
248 },
249  
250 Bool => { -name => 'Bool',
251 6 ▶ -where => sub { ref ($_) ? 0: 1 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 6 6
252 0 ▶ -message => sub { "value $_ is not a bool value" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
253 -owner => 'CPAN',
254 -description => "Value is a Scalar, all values including undef allowed",
255 },
256 PositiveInt => { -name => 'PositiveInt',
257 -as => 'Int',
258 6 ▶ -where => sub { $_ >= 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
259 10 ▶ -message => sub { "value $_ is not a positive integer" },
Subroutine __ANON__: called CC=1 SCAR=0.0
260 -owner => 'CPAN',
261 -description => "Value is a positive integer",
262 },
263 NegativeInt => { -name => 'NegativeInt',
264 -as => 'Int',
265 2 ▶ -where => sub { $_ < 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
266 1 ▶ -message => sub { "value $_ is not a negative integer" },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 -owner => 'CPAN',
268 -description => "Value is a negative Integer",
269 },
270 Float => { -name => 'Float',
271 -as => 'Filled',
272 61 ▶ -where => sub { /^[\+\-]?\d+(\.\d+)?([Ee][\+-]?\d+)?$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
273 7 ▶ -message => sub { "value $_ is not a float" },
Subroutine __ANON__: called CC=1 SCAR=0.0
274 -owner => 'CPAN',
275 -description => "Value is a floating number with optional exponent",
276 },
277 PositiveFloat => { -name => 'PositiveFloat',
278 -as => 'Float',
279 8 ▶ -where => sub { $_ > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
280 3 ▶ -message => sub { "value $_ is not a positive float" },
Subroutine __ANON__: called CC=1 SCAR=0.0
281 -owner => 'CPAN',
282 -description => "Value is a positive floating number with optional exponent",
283 },
284 NegativeFloat => { -name => 'NegativeFloat',
285 -as => 'Float',
286 0 ▶ -where => sub { $_ < 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
287 0 ▶ -message => sub { "value $_ is not a negative float" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
288 1 -owner => 'CPAN',
289 -description => "Value is a negative floating number with optional exponent",
290 },
291 };
292  
293 # ------------------------------------------------------------------------------
294 #
295 # gets content of ref container
296 #
297 # ------------------------------------------------------------------------------
298  
299 $get_content_subs = {
300 1 ▶ HASH => sub { my @keys = keys %$_; return scalar @keys ? \@keys: undef; },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
scalar @keys ? : 1 0 1
301 1 ▶ ARRAY => sub { return scalar @$_ ? $_: undef; },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
scalar @$_ ? : 1 0 1
302 1 };
303  
304 # ------------------------------------------------------------------------------
305 #
306 # special rules (combined and other), not changable
307 #
308 # ------------------------------------------------------------------------------
309  
310 $special_rules = {
311 -Optional => {
312 -value_position => 3,
313 51 ▶ -code => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
314 # my $subject_info = shift || '';
315 # my $rule_info = shift;
316 51 local $_ = $_[2];
317  
318 # skip one param if special rule follows
319 51 my $special_rule = $special_rules->{$_[1]};
320 51 ▶ if ($special_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_rule) 13 38 51
321 13 $_ = $_[$special_rule->{-value_position}];
322 }
323 ;
324
325 51 ▶ return $_ if !defined $_; # value not set
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 29 22 51
326 22 return validate(@_);
327 },
328 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
329 0 eval {
330 0 my $rule = shift;
331
332 0 return "-Optional [$rule]";
333 }
334 }
335 },
336 -Default => {
337 -value_position => 4,
338 40 ▶ -code => sub {
Subroutine __ANON__: called CC=9 SCAR=23.2
339 40 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
400
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
340 40 my $default = shift;
341 40 my $rule_info = shift;
342 # local $_ = $_[3];
343  
344 40 my $value_idx = 0;
345  
346 # --- skip x params if special rule follows ---
347 40 my $special_rule = $special_rules->{$rule_info};
348 40 ▶ if ($special_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_rule) 4 36 40
349 4 my $special_idx = $special_rule->{-value_position};
350 4 ▶ $value_idx = $special_idx - 2 if $special_idx >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $special_idx >= 0 4 0 4
351 }
352  
353 40 my $value = $_[$value_idx];
354  
355 # --- value set, validate it ---
356 40 ▶ return validate($subject_info, $rule_info, @_)
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and $value ne "" 2 38 40
Condition100%3/3
defined $value and $value ne ""
!ll&&!rl&&r
3712
MC/DC100%2/2
defined $value and $value ne ""
defined $value $value ne ""
Truth table100%3/3
A and B
A= defined $valueB= $value ne ""
ABresult
0X0
100
111
357 if (defined $value && $value ne '');
358  
359 # --- value not set ----
360 38 ▶ unless (defined $default) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $default) 0 38 38
361 0 $trouble_level++;
362 0 $fail_action->("Rules: Default value for rule -Default not set!");
363 0 return $_;
364 }
365  
366 38 ▶ if (ref($default) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default eq "CODE") 0 38 38
367 0 $default = $default->();
368 }
369  
370 38 ▶ return $default unless $validate_defaults;
Branch: 1 branch
BranchTrueFalseTotal
unless $Scalar::Validation::validate_defaults 0 38 38
371  
372 38 my @args = @_;
373
374 38 $args[$value_idx] = $default;
375  
376 # --- default has also to be validated!! ---
377 38 return validate($subject_info, $rule_info, @args)
378 },
379 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
380 0 eval {
381 0 my $default = shift;
382 0 my $rule = shift;
383
384 0 return "-Default = '$default' [$rule]";
385 }
386 }
387 },
388 -And => {
389 -value_position => 3,
390 14 ▶ -code => sub {
Subroutine __ANON__: called CC=8 SCAR=23.0
391 14 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
140
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
392 14 my $rule_list = shift;
393 14 local $_ = shift;
394 14 my $message_ref = shift;
395
396 14 my $rule_exists = 0;
397 14 my $orig_value = $_;
398
399 14 foreach my $rule (@$rule_list) {
400 25 ▶ if (!defined $rule || $rule eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rule or $rule eq "") 0 25 25
Condition33%1/3
not defined $rule or $rule eq ""
l!l&&r!l&&!r
0025
MC/DC0%0/2
not defined $rule or $rule eq ""
not defined $rule $rule eq ""
Truth table33%1/3
not(A) or B
A= not defined $ruleB= $rule eq ""
ABresult
1X1
011
000
401 0 $trouble_level++;
402 0 $fail_action->("Rules: rule for validation not set");
403 next # in case of fail action doesn't die
404 0 }
405
406 25 ▶ if ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule) 20 5 25
407 20 my $special_rule = $special_rules->{$rule};
408
409 20 ▶ if ($special_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_rule) 0 20 20
410 0 $trouble_level++;
411 0 $fail_action->("Rules: cannot call any special rule inside of rule '-And'\n");
412 0 next;
413 }
414  
415 20 validate ($subject_info, $rule, $_, $message_ref);
416 19 $rule_exists = 1;
417 }
418 }
419
420 13 $trouble_level++;
421 13 ▶ $fail_action->("Rules: No rule found in list to be validated") unless $rule_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_exists 2 11 13
422
423 11 return $orig_value;
424 },
425 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
426 0 my $rule_list = shift;
427
428 0 ▶ delete $rule_list->[$#$rule_list] unless $rule_list->[$#$rule_list];
Branch: 1 branch
BranchTrueFalseTotal
unless $$rule_list[$rule_list->$#*] 0 0 0
429 0 my $arg_info = join (', ', @$rule_list);
430 0 ▶ $arg_info = 'no argument informations' unless $arg_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_info 0 0 0
431  
432 0 return "-And [$arg_info]";
433 }
434 },
435 -Or => {
436 -value_position => 3,
437 24 ▶ -code => sub {
Subroutine __ANON__: called CC=19 SCAR=34.6
438 24 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
240
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
439 24 my $rule_list = shift;
440 24 local $_ = shift;
441 24 my $message_ref = shift;
442
443 24 my $rule_exists = 0;
444 24 my $orig_value = $_;
445
446 24 foreach my $rule_info (@$rule_list) {
447 36 ▶ if (!defined $rule_info || $rule_info eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rule_info or $rule_info eq "") 0 36 36
Condition33%1/3
not defined $rule_info or $rule_info eq ""
l!l&&r!l&&!r
0036
MC/DC0%0/2
not defined $rule_info or $rule_info eq ""
not defined $rule_info $rule_info eq ""
Truth table33%1/3
not(A) or B
A= not defined $rule_infoB= $rule_info eq ""
ABresult
1X1
011
000
448 0 $trouble_level++;
449 0 $fail_action->("Rules: rule for validation not set");
450 next # in case of fail action doesn't die
451 0 }
452
453 36 ▶ next unless $rule_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_info 4 32 36
454
455 32 my $rule_ref = $rule_store->{$rule_info};
456 32 ▶ unless ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_ref) 4 28 32
457 4 my $special_rule = $special_rules->{$rule_info};
458
459 4 ▶ if ($special_rule) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special_rule) 0 4 4
460 0 $trouble_level++;
461 0 $fail_action->("Rules: cannot call any special rule inside of rule '-Or'\n");
462 0 next;
463 }
464  
465 4 my $ref_type = ref ($rule_info);
466
467 4 ▶ unless ($ref_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_type) { } 4 0 4
if (do { $ref_type eq 'HASH' }) { } 2 2 4
elsif ($ref_type eq 'CODE') { } 2 0 2
468 0 $trouble_level++;
469 0 $fail_action->("Rules: unknown rule '$rule_info' for validation");
470 0 next; # in case of fail action doesn't die
471 } elsif ($ref_type eq 'HASH') { # given rule
472 2 $rule_ref = $rule_info;
473 # TODO: validate rule ...
474 } elsif ($ref_type eq 'CODE') { # where condition for rule
475 $rule_ref = {
476 -where => $rule_info,
477 0 ▶ -message => sub { "$_ does not match free defined rule" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
478 2 };
479 } else {
480 0 $trouble_level++;
481 0 $fail_action->("Rules: cannot handle ref type '$ref_type' of rule '$rule_info' for validation");
482 0 next; # in case of fail action doesn't die
483 }
484 }
485  
486 32 ▶ if ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_ref) 32 0 32
487 32 ▶ my $test_message_ref = $message_ref || $rule_ref->{-message};
Condition66%2/3
$message_ref || $rule_ref->{'-message'}
l!l&&r!l&&!r
4280
MC/DC0%0/2
$message_ref || $rule_ref->{'-message'}
$message_ref $rule_ref->{'-message'}
Truth table66%2/3
A || B
A= $message_refB= $rule_ref->{'-message'}
ABresult
1X1
011
000
488  
489 my $parent_is_valid = defined $rule_ref->{-as}
490 32 ▶ ? _check_parent_rules($rule_ref->{-as}, $_)
Branch: 1 branch
BranchTrueFalseTotal
defined $rule_ref->{'-as'} ? : 13 19 32
491 : 1;
492 32 ▶ return $orig_value if $parent_is_valid && $rule_ref->{-where}->();
Branch: 1 branch
BranchTrueFalseTotal
if $parent_is_valid and $rule_ref->{"-where"}->() 19 13 32
Condition100%3/3
$parent_is_valid and $rule_ref->{"-where"}->()
!ll&&!rl&&r
21119
MC/DC100%2/2
$parent_is_valid and $rule_ref->{"-where"}->()
$parent_is_valid $rule_ref->{"-where"}->()
Truth table100%3/3
A and B
A= $parent_is_validB= $rule_ref->{"-where"}->()
ABresult
0X0
100
111
493  
494 13 $rule_exists = 1;
495 }
496 }
497
498 5 $trouble_level++;
499 5 ▶ $fail_action->("Rules: No rule found in list to be validated") unless $rule_exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $rule_exists 0 5 5
500  
501 5 ▶ my $result = _do_fail($subject_info, $message_ref || sub { "No rule matched of [".join(', ', @$rule_list)."]";});
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$message_ref || sub { 'No rule matched of [' . join(', ', @$rule_list) . ']'; }
l!l&&r!l&&!r
230
MC/DC0%0/2
$message_ref || sub { 'No rule matched of [' . join(', ', @$rule_list) . ']'; }
$message_ref sub { 'No rule matched of [' . join(', ', @$rule_list) . ']'; }
Truth table66%2/3
A || B
A= $message_refB= sub { 'No rule matched of [' . join(', ', @$rule_list) . ']'; }
ABresult
1X1
011
000
502 1 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 1 0 1
503  
504 0 return $orig_value;
505 },
506 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
507 0 my $rule_list = shift;
508
509 0 ▶ delete $rule_list->[$#$rule_list] unless $rule_list->[$#$rule_list];
Branch: 1 branch
BranchTrueFalseTotal
unless $$rule_list[$rule_list->$#*] 0 0 0
510 0 my $arg_info = join (', ', @$rule_list);
511 0 ▶ $arg_info = 'no argument informations' unless $arg_info;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_info 0 0 0
512  
513 0 return "-Or [$arg_info]";
514 }
515 },
516 -Enum => {
517 -value_position => 3,
518 15 ▶ -code => sub {
Subroutine __ANON__: called CC=8 SCAR=21.6
519 15 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
150
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
520 15 my $enum_ref = shift;
521 15 local $_ = shift;
522 15 my $message_ref = shift;
523
524 15 my $orig_value = $_;
525  
526 15 my $arg_type = ref ($enum_ref);
527 15 ▶ if ($arg_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_type eq 'ARRAY') { } 7 8 15
elsif ($arg_type ne 'HASH') { } 0 8 8
528 7 $enum_ref = { map {$_=> 1} @$enum_ref };
529 }
530 elsif ($arg_type ne 'HASH') {
531 0 ▶ _do_fail($subject_info, sub {"-Enum needs HASH_ref as second parameter";});
Subroutine __ANON__: not called CC=1 SCAR=6.9
532 }
533  
534 15 ▶ unless (defined $_ && $enum_ref->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_ and $enum_ref->{$_}) 4 11 15
Condition100%3/3
defined $_ and $enum_ref->{$_}
!ll&&!rl&&r
1311
MC/DC100%2/2
defined $_ and $enum_ref->{$_}
defined $_ $enum_ref->{$_}
Truth table100%3/3
A and B
A= defined $_B= $enum_ref->{$_}
ABresult
0X0
100
111
535 my $result = _do_fail($subject_info, $message_ref ||
536 4 ▶ sub { "value $_ unknown, allowed values are: [ "
Subroutine __ANON__: called CC=1 SCAR=0.0
537 .join (", ", sort (keys(%$enum_ref)))." ]"; }
538 4 ▶ );
Condition33%1/3
$message_ref || sub { "value $_ unknown, allowed values are: [ " . join(', ', sort(keys %$enum_ref)) . ' ]'; }
l!l&&r!l&&!r
040
MC/DC0%0/2
$message_ref || sub { "value $_ unknown, allowed values are: [ " . join(', ', sort(keys %$enum_ref)) . ' ]'; }
$message_ref sub { "value $_ unknown, allowed values are: [ " . join(', ', sort(keys %$enum_ref)) . ' ]'; }
Truth table33%1/3
A || B
A= $message_refB= sub { "value $_ unknown, allowed values are: [ " . join(', ', sort(keys %$enum_ref)) . ' ]'; }
ABresult
1X1
011
000
539 1 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 1 0 1
540  
541 }
542 11 return $orig_value;
543 },
544 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
545 0 my $enum_ref = shift;
546  
547 0 my $arg_info = 'no argument informations';
548 0 my $arg_type = ref ($enum_ref);
549 0 ▶ if ($arg_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_type eq 'ARRAY') { } 0 0 0
elsif ($arg_type eq 'HASH') { } 0 0 0
550 0 $arg_info = join (', ', @$enum_ref);
551 }
552 elsif ($arg_type eq 'HASH') {
553 0 $arg_info = join (', ', sort(keys(%$enum_ref)));
554 }
555 0 return "Enum [$arg_info]";
556 }
557 },
558 -Range => {
559 -value_position => 4,
560 37 ▶ -code => sub {
Subroutine __ANON__: called CC=11 SCAR=24.1
561 37 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
370
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
562 37 my $range_ref = shift;
563 37 my $rule = shift;
564 37 local $_ = shift;
565 37 my $message_ref = shift;
566
567 my $wrong_call_message_sub_ref
568 37 ▶ = sub { "-Range needs ARRAY_ref containing two values [min max] as second parameter" };
Subroutine __ANON__: called CC=1 SCAR=0.0
569  
570 37 my $orig_value = $_;
571
572 37 ▶ unless (ref($range_ref) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $range_ref eq "ARRAY") 1 36 37
573 1 _do_fail($subject_info, $wrong_call_message_sub_ref);
574 0 return $orig_value;
575 }
576  
577 36 ▶ unless (scalar @$range_ref == 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$range_ref == 2) 1 35 36
578 1 _do_fail($subject_info, $wrong_call_message_sub_ref);
579 0 return $orig_value;
580 }
581  
582 35 my ($min, $max) = @$range_ref;
583 35 ▶ if ($min > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $max) 1 34 35
584 1 ▶ _do_fail($subject_info, sub { "(min) $min > $max (max) in range definition"; });
Subroutine __ANON__: called CC=1 SCAR=0.0
585 0 return $orig_value;
586 }
587  
588 # type check by is_valid to return here if fails
589 34 my @messages;
590 my $is_valid;
591 {
592 34 local ($message_store) = [];
593 34 $is_valid = is_valid ($subject_info, $rule, $_, $message_ref);
594 34 @messages = @{validation_messages()};
595 }
596
597 34 ▶ unless ($is_valid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_valid) 10 24 34
598 10 my $message = join ("\n", @messages);
599 10 ▶ push (@$message_store, $message) if $message_store;
Branch: 1 branch
BranchTrueFalseTotal
if $Scalar::Validation::message_store 2 8 10
600 10 $trouble_level++;
601 10 my $result = $fail_action->($message);
602 7 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 5 2 7
603  
604 2 return $orig_value;
605 }
606
607 24 ▶ unless ($min <= $_ && $_<= $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($min <= $_ and $_ <= $max) 7 17 24
Condition100%3/3
$min <= $_ and $_ <= $max
!ll&&!rl&&r
4317
MC/DC100%2/2
$min <= $_ and $_ <= $max
$min <= $_ $_ <= $max
Truth table100%3/3
A and B
A= $min <= $_B= $_ <= $max
ABresult
0X0
100
111
608 7 ▶ my $result = _do_fail($subject_info, sub {"value $_ is out of range [$min,$max]"});
Subroutine __ANON__: called CC=1 SCAR=0.0
609 5 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 5 0 5
610 0 return $orig_value;
611 }
612  
613 17 return $orig_value;
614 },
615 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
616 0 my $range_ref = shift;
617 0 my $rule = shift;
618 0 my $min = $range_ref->[0];
619 0 my $max = $range_ref->[1];
620 0 return "Range [$min,$max] of type $rule";
621 }
622 },
623 -RefEmpty => {
624 -value_position => 3,
625 5 ▶ -code => sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
626
627 5 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
50
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
628 5 local $_ = shift;
629 5 my $message_ref = shift;
630
631 5 my $content_ref = _ref_empty_check($subject_info, $_, $message_ref);
632
633 2 ▶ return undef unless defined $content_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content_ref 0 2 2
634
635 2 my $count_results = scalar @$content_ref;
636 2 ▶ return 0 unless $count_results;
Branch: 1 branch
BranchTrueFalseTotal
unless $count_results 0 2 2
637
638 2 ▶ _do_fail($subject_info, sub { "Should be empty, but contains $count_results entries: [ ".
Subroutine __ANON__: called CC=1 SCAR=0.0
639 2 join (", ", @$content_ref)." ];" });
640
641 0 return $count_results;
642 },
643 0 ▶ -print_args => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
644 0 return "EmptyReference";
645 }
646 },
647 1 };
648  
649 # ------------------------------------------------------------------------------
650 #
651 # internal Methods
652 #
653 # ------------------------------------------------------------------------------
654  
655 2 ▶sub _handle_enum_explained {
Subroutine _handle_enum_explained: called CC=7 SCAR=19.7
656 2 my $transform_key_ref = shift;
657 2 my $transformed_text = shift;
658 2 my $rule_name = shift;
659 2 my @enum_args;
660 my @enums_list;
661 2 my %enums;
662
663 2 foreach my $arg (@_) {
664 24 ▶ if ($arg eq 1 or $arg eq 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 1 or $arg eq 0) { } 6 18 24
Condition66%2/3
$arg eq 1 or $arg eq 0
l!l&&r!l&&!r
6018
MC/DC50%1/2
$arg eq 1 or $arg eq 0
$arg eq 1 $arg eq 0
Truth table66%2/3
A or B
A= $arg eq 1B= $arg eq 0
ABresult
1X1
011
000
665 # arg is complete
666 6 my $last_idx = $#enum_args;
667  
668 6 ▶ if ($last_idx < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_idx < 1) 0 6 6
669 0 $trouble_level++;
670 0 $fail_action->("Rules: not enough configuration values for enum '$enum_args[0]'");
671 }
672
673 6 my $explanation = $enum_args[$last_idx];
674 6 ▶ map { my $key = $transform_key_ref ? $transform_key_ref->($_): $_;
Branch: 1 branch
BranchTrueFalseTotal
$transform_key_ref ? : 6 6 12
675 12 $enums{$key} = $explanation;
676 12 push (@enums_list, $key);
677 } @enum_args[0..--$last_idx];
678 6 @enum_args = ();
679 }
680 else {
681 18 push (@enum_args, $arg);
682 }
683 }
684  
685 my $validation_sub_ref = $transform_key_ref
686 3 ▶ ? sub { defined $_ && defined $enums{$transform_key_ref->($_)} }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and defined $enums{&$transform_key_ref($_)} 3 0 3
687 2 ▶ : sub { defined $_ && defined $enums{$_} };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
$transform_key_ref ? : 1 1 2
defined $_ and defined $enums{$_} 3 0 3
688
689 return ($rule_name,
690 -where => $validation_sub_ref,
691 -enum => \%enums,
692 2 ▶ -message => sub { "$rule_name: value $_ unknown, allowed values$transformed_text ".
Subroutine __ANON__: called CC=1 SCAR=0.0
693 "are: [ ".join (", ", @enums_list)." ]" }
694 2 );
695 }
696  
697 226 ▶sub _check_parent_rules {
Subroutine _check_parent_rules: called CC=4 SCAR=13.9
698 226 my $rule_name = shift;
699 226 local $_ = shift;
700  
701 226 my $orig_value = $_;
702  
703 226 my $rule_ref = $rule_store->{$rule_name};
704  
705 226 ▶ unless ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_ref) 1 225 226
706 1 $trouble_level++;
707 1 $fail_action->("Rules: unknown rule '$rule_name' for validation");
708 0 return 0; # in case of fail action doesn't die
709 }
710
711 225 ▶ if (defined $rule_ref->{-as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule_ref->{"-as"}) 64 161 225
712 64 ▶ return 0 unless _check_parent_rules($rule_ref->{-as}, $_);
Branch: 1 branch
BranchTrueFalseTotal
unless _check_parent_rules($rule_ref->{"-as"}, $_) 0 64 64
713 }
714
715 225 return $rule_ref->{-where}->();
716 }
717  
718 5 ▶sub _ref_empty_check {
Subroutine _ref_empty_check: called CC=4 SCAR=14.0
719 5 ▶ my $subject_info = shift || '';
Condition50%1/2
shift() || ''
l!l
50
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
720 5 local $_ = shift;
721 5 my $message_ref = shift;
722  
723 5 my $ref_type = ref($_);
724  
725 5 ▶ unless ($ref_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref_type) 2 3 5
726 2 ▶ _do_fail($subject_info, sub { "Not a reference: $_" });
Subroutine __ANON__: called CC=1 SCAR=0.0
727 0 return undef;
728 }
729
730 3 my $get_contents_ref = $get_content_subs->{$ref_type};
731  
732 3 ▶ unless ($get_contents_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($get_contents_ref) 1 2 3
733 1 ▶ _do_fail($subject_info, sub { "could not check, if $ref_type is empty" });
Subroutine __ANON__: called CC=1 SCAR=0.0
734 0 return undef;
735 }
736
737 2 return $get_contents_ref->();
738 }
739  
740 24 ▶sub _do_fail {
Subroutine _do_fail: called CC=3 SCAR=11.0
741 24 my $subject_info = shift;
742 24 my $message_ref = shift;
743  
744 24 $trouble_level++;
745  
746 24 ▶ $_ = defined ($_) ? "'$_'" : '<undef>';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 21 3 24
747  
748 24 my $message = $get_caller_info->()."($call_info$subject_info): ".$message_ref->();
749 24 ▶ push (@$message_store, $message) if $message_store;
Branch: 1 branch
BranchTrueFalseTotal
if $Scalar::Validation::message_store 0 24 24
750  
751 24 return $fail_action->($message);
752 }
753  
754 # ------------------------------------------------------------------------------
755 #
756 # API Methods
757 #
758 # ------------------------------------------------------------------------------
759  
760 1 ▶sub _init_run_API {
Subroutine _init_run_API: called CC=1 SCAR=0.0
761 1 *p_start = *_do_parameters_start;
762 1 *parameters_start = *_do_parameters_start;
763  
764 1 *p_end = *_do_parameters_end;
765 1 *parameters_end = *_do_parameters_end;
766  
767 1 *npar = *_do_named_parameter;
768 1 *named_parameter = *_do_named_parameter;
769  
770 1 *parameter = *_do_validate_parameter;
771 1 *par = *_do_validate_parameter;
772  
773 1 *validate = *_do_validate_call;
774 1 *is_valid = *_do_is_valid;
775  
776 1 *get_api_doc = *get_api_documentation;
777  
778 1 *g_t = *greater_than;
779 1 *g_e = *greater_equal;
780 1 *l_t = *less_than;
781 1 *l_e = *less_equal;
782  
783 }
784  
785 1 ▶sub _init_private_calls {
Subroutine _init_private_calls: called CC=1 SCAR=0.0
786 1 *_p_npar = *_do_named_parameter;
787 1 *_p_named_parameter = *_do_named_parameter;
788
789 1 *_p_parameter = *_do_validate_parameter;
790 1 *_p_par = *_do_validate_parameter;
791
792 1 *_p_validate = *_do_validate_call;
793 1 *_p_is_valid = *_do_is_valid;
794 }
795  
796 0 ▶sub _get_meta_extraction_code {
Subroutine _get_meta_extraction_code: not called CC=1 SCAR=6.9
797 0 my $module_name = shift;
798 0 return "
799 use $module_name;
800 *".$module_name."::p_start = *".__PACKAGE__."::_start_sub_meta_extraction;
801 *".$module_name."::parameters_start = *".__PACKAGE__."::_start_sub_meta_extraction;
802
803 *".$module_name."::p_end = *".__PACKAGE__."::_end_sub_meta_extraction;
804 *".$module_name."::parameters_end = *".__PACKAGE__."::_end_sub_meta_extraction;
805
806 *".$module_name."::npar = *".__PACKAGE__."::_sub_meta_extract_named_parameter;
807 *".$module_name."::named_parameter = *".__PACKAGE__."::_sub_meta_extract_named_parameter;
808
809 *".$module_name."::parameter = *".__PACKAGE__."::_sub_meta_extract_positional_parameter;
810 *".$module_name."::par = *".__PACKAGE__."::_sub_meta_extract_positional_parameter;
811
812 *".$module_name."::validate = *".__PACKAGE__."::_sub_meta_no_extraction;
813 ";
814 }
815  
816 0 ▶sub _reset_validation_code {
Subroutine _reset_validation_code: not called CC=1 SCAR=6.9
817 0 my $module_name = shift;
818 0 return "
819 use $module_name;
820 *".$module_name."::p_start = *".__PACKAGE__."::_do_parameters_start;
821 *".$module_name."::parameters_start = *".__PACKAGE__."::_do_parameters_start;
822
823 *".$module_name."::p_end = *".__PACKAGE__."::_do_parameters_end;
824 *".$module_name."::parameters_end = *".__PACKAGE__."::_do_parameters_end;
825
826 *".$module_name."::npar = *".__PACKAGE__."::_do_named_parameter;
827 *".$module_name."::named_parameter = *".__PACKAGE__."::_do_named_parameter;
828
829 *".$module_name."::parameter = *".__PACKAGE__."::_do_validate_parameter;
830 *".$module_name."::par = *".__PACKAGE__."::_do_validate_parameter;
831
832 *".$module_name."::validate = *".__PACKAGE__."::_do_validate_call;
833 ";
834 }
835  
836 0 ▶sub _do_parameters_start {
Subroutine _do_parameters_start: not called CC=1 SCAR=6.9
837 0 return $trouble_level;
838 }
839  
840 17 ▶sub convert_to_named_params {
Subroutine convert_to_named_params: called CC=1 SCAR=0.0
Pod undocumented
841 17 my $array_ref = _p_validate (args => ArrayRef => shift);
842  
843 _p_validate (arg_count => Even => scalar @$array_ref =>
844 16 ▶ sub { "Even number of args needed to build a hash, but arg-count = $_" });
Subroutine __ANON__: called CC=1 SCAR=0.0
845 15 return @$array_ref;
846 }
847  
848 12 ▶sub _do_parameters_end {
Subroutine _do_parameters_end: called CC=4 SCAR=17.9
849  
850 12 my $container_ref = _p_par (container_ref => -Or => [HashRef => 'ArrayRef'] => shift);
851 12 ▶ my $message_text = _p_par (message_text => Scalar => shift) || "extra parameters found";
Condition50%1/2
_p_par('message_text', 'Scalar', shift()) || 'extra parameters found'
l!l
012
MC/DC0%0/1
_p_par('message_text', 'Scalar', shift()) || 'extra parameters found'
_p_par('message_text', 'Scalar', shift())
Truth table50%1/2
A
A= _p_par('message_text', 'Scalar', shift())
Aresult
11
00
852  
853 12 my $container_type = ref ($container_ref);
854 12 ▶ if ($container_type eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($container_type eq 'ARRAY') { } 0 12 12
elsif ($container_type eq 'HASH') { } 12 0 12
855 0 ▶ _p_validate (parameters => sub { scalar @$container_ref == 0 } => $container_ref => sub { "$message_text: [ '".join ("', '", @$container_ref)."' ]"; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
856 0 return scalar @$container_ref;
857 }
858 elsif ($container_type eq 'HASH') {
859 12 my @arg_names = keys %$container_ref;
860 0 ▶ _p_validate (parameters => sub { scalar @arg_names == 0 } => $container_ref => sub { "$message_text: [ '".join ("', '", @arg_names)."' ]"; });
Subroutine __ANON__: called CC=1 SCAR=0.4
Subroutine __ANON__: not called CC=1 SCAR=0.4
861 12 return scalar @arg_names;
862 }
863  
864 0 ▶ _do_fail("parameters_end()", sub { "unknown reference type $container_ref" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
865 0 return -1;
866 }
867  
868 # ------------------------------------------------------------------------------
869 #
870 # Meta Information Mode
871 #
872 # ------------------------------------------------------------------------------
873  
874 0 ▶sub meta_info_clear {
Subroutine meta_info_clear: not called CC=1 SCAR=6.9
Pod undocumented
875 0 print "# Start API documentation ==============================\n";
876 0 $class_meta_model = {};
877 }
878  
879 0 ▶sub build_meta_info_for_module {
Subroutine build_meta_info_for_module: not called CC=3 SCAR=24.8
Pod undocumented
880 0 end_meta_info_gen();
881 0 $doc_class_name = _p_par(module_name => ModuleName => shift);
882 0 my $instance_creator = _p_par(instance_creation => -Optional => CodeRef => shift);
883  
884 # --- run sub -------------------------------------------------
885  
886 # print "# Module: $doc_class_name; # ==============================\n";
887 0 $class_meta_model->{$doc_class_name} = {};
888 0 $class_meta_model->{$doc_class_name}->{subs} = {};
889 0 ▶ eval (_get_meta_extraction_code($doc_class_name)); print $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
890
891 0 ▶ $instance_creator = eval ('sub { return '.$doc_class_name.'->new(); }')
Branch: 1 branch
BranchTrueFalseTotal
unless $instance_creator 0 0 0
892 unless $instance_creator;
893  
894 0 return $instance_creator->();
895 }
896  
897 0 ▶sub end_meta_info_gen {
Subroutine end_meta_info_gen: not called CC=3 SCAR=24.8
Pod undocumented
898 0 ▶ eval (_reset_validation_code($doc_class_name)) if $doc_class_name; print $@ if $@;
Branch: 2 branches
BranchTrueFalseTotal
if $doc_class_name 0 0 0
if $@ 0 0 0
899 }
900  
901 0 ▶sub get_meta_info {
Subroutine get_meta_info: not called CC=1 SCAR=6.9
Pod undocumented
902 0 return $class_meta_model;
903 }
904  
905 0 ▶sub list_meta_info {
Subroutine list_meta_info: not called CC=5 SCAR=34.0
Pod undocumented
906 0 my @meta_info_list;
907  
908 0 foreach my $pm (sort (keys (%$class_meta_model))) {
909 0 my $subs = $class_meta_model->{$pm}->{subs};
910  
911 0 foreach my $sub_name (sort (keys (%$subs))) {
912 0 my $parameters = $subs->{$sub_name}->{params};
913 0 my %sub_info = (module => $pm,
914 sub => $sub_name);
915  
916 0 ▶ if (scalar (@$parameters)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$parameters) { } 0 0 0
917 0 foreach my $parameter (@$parameters) {
918 0 my %par_info = (%$parameter, %sub_info);
919 0 push (@meta_info_list, \%par_info);
920 }
921 }
922 else { # sub has no parameters
923 0 $sub_info{rule} = $sub_info{kind} = $sub_info{name} = '';
924 0 push (@meta_info_list, \%sub_info);
925 }
926 }
927 }
928  
929 0 return \@meta_info_list;
930 }
931  
932 0 ▶sub _start_sub_meta_extraction { # parameters_start
Subroutine _start_sub_meta_extraction: not called CC=1 SCAR=6.9
933 0 $doc_class_method = $get_caller_info->();
934 0 $doc_class_method =~ s/.*\:\://og;
935 0 $class_meta_model->{$doc_class_name}->{subs}->{$doc_class_method} = {};
936 0 $class_meta_model->{$doc_class_name}->{subs}->{$doc_class_method}->{params} = [];
937 # print "\tMethod: $doc_class_method # ---------------------------------------\n";
938 0 return 0;
939 }
940  
941 0 ▶sub _end_sub_meta_extraction { # parameters_end
Subroutine _end_sub_meta_extraction: not called CC=1 SCAR=6.9
942 # print "\t# --- end method documentation ---\n";
943 0 $trouble_level++;
944 0 return 0;
945 }
946  
947 0 ▶sub _sub_meta_extract_named_parameter { # named_parameter
Subroutine _sub_meta_extract_named_parameter: not called CC=1 SCAR=6.9
948 0 my $argument_infos = _api_doc_get_argument_info(@_);
949 0 push (@{$class_meta_model->{$doc_class_name}->{subs}->{$doc_class_method}->{params}},
950 {
951 kind => Named =>
952 name => $argument_infos->[0],
953 rule => $argument_infos->[1],
954 }
955 );
956  
957 0 local ($Scalar::Validation::off) = 1;
958 0 $trouble_level++;
959 0 return _do_named_parameter(@_);
960 }
961  
962 0 ▶sub _sub_meta_extract_positional_parameter { # parameter
Subroutine _sub_meta_extract_positional_parameter: not called CC=1 SCAR=6.9
963 0 my $argument_infos = _api_doc_get_argument_info(@_);
964 # print "\t\tPositional Parameter: $argument_info_str\n";
965 0 push (@{$class_meta_model->{$doc_class_name}->{subs}->{$doc_class_method}->{params}},
966 {
967 kind => Positional =>
968 name => $argument_infos->[0],
969 rule => $argument_infos->[1],
970 }
971 );
972  
973 0 local ($Scalar::Validation::off) = 1;
974 0 $trouble_level++;
975 0 return _do_validate_parameter(@_);
976 }
977  
978 0 ▶sub _sub_meta_no_extraction { # validate
Subroutine _sub_meta_no_extraction: not called CC=1 SCAR=6.9
979 # print "_sub_meta_no_extraction ".join (', ', @_)."\n";
980 0 local ($Scalar::Validation::off) = 1;
981 0 $trouble_level++;
982 0 return _do_validate_call(@_);
983 }
984  
985 0 ▶sub _api_doc_get_argument_info {
Subroutine _api_doc_get_argument_info: not called CC=10 SCAR=47.0
986 0 my $name = shift;
987 0 my $rule_info = shift;
988  
989 0 ▶ unless ($rule_info) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_info) 0 0 0
990 0 $trouble_level++;
991 0 $fail_action->("rule for validation not set");
992 0 return [$_]; # in case of fail action doesn't die
993 }
994  
995 0 my $rule_ref = $rule_store->{$rule_info};
996  
997 0 my $rule_info_string = 'No info for rule';
998  
999 0 ▶ if ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_ref) { } 0 0 0
1000 0 $rule_info_string = $rule_ref->{-name};
1001 }
1002 else {
1003 0 my $special_rule_name = $rule_info;
1004 # print "$rule_info, ".join(', ', @_)."\n";
1005 0 $rule_ref = $special_rules->{$special_rule_name};
1006 0 ▶ if ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_ref) 0 0 0
1007 0 my $last_idx = scalar(@_);
1008 0 ▶ if ($last_idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_idx >= 0) 0 0 0
1009 0 my $print_args = $rule_ref->{-print_args};
1010 0 ▶ if ($print_args) {
Branch: 1 branch
BranchTrueFalseTotal
if ($print_args) { } 0 0 0
1011 0 $rule_info_string = $print_args->(@_);
1012 }
1013 else {
1014 0 $fail_action->("print_args missing for rule '$special_rule_name'");
1015 }
1016 }
1017 }
1018 }
1019  
1020 0 ▶ unless ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_ref) 0 0 0
1021 0 my $ref_type = ref ($rule_info);
1022
1023 0 ▶ unless ($ref_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_type) { } 0 0 0
if (do { $ref_type eq 'HASH' }) { } 0 0 0
elsif ($ref_type eq 'CODE') { } 0 0 0
1024 0 $trouble_level++;
1025 0 my $error_message = "unknown rule '$rule_info' for validation";
1026 0 $fail_action->($error_message);
1027 0 return [$error_message];
1028 }
1029 elsif ($ref_type eq 'HASH') { # given rule
1030 0 $rule_ref = $rule_info;
1031 # TODO: _p_validate rule ...
1032 }
1033 elsif ($ref_type eq 'CODE') { # where condition for rule
1034 0 $rule_ref = {
1035 -name => 'anonymous private rule'
1036 };
1037 }
1038 else {
1039 0 $trouble_level++;
1040 0 my $error_message = "Rules: cannot handle ref type '$ref_type' of rule '$rule_info' for validation";
1041 0 $fail_action->$error_message();
1042 0 return [$error_message]; # in case of fail action doesn't die
1043 }
1044 0 $rule_info_string = $rule_ref->{-name};
1045 }
1046  
1047 0 return [$name, $rule_info_string];
1048  
1049 }
1050  
1051 # ------------------------------------------------------------------------------
1052 #
1053 # Messages and Validation Mode
1054 #
1055 # ------------------------------------------------------------------------------
1056  
1057 0 ▶sub add_validation_trouble {
Subroutine add_validation_trouble: not called CC=2 SCAR=17.9
Pod undocumented
1058 0 my ($value
1059 ) = @_;
1060  
1061 0 ▶ $trouble_level += ($value || 1);
Condition0%0/2
$value || 1
l!l
00
MC/DC0%0/1
$value || 1
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
1062 }
1063  
1064 0 ▶sub get_validation_trouble_level {
Subroutine get_validation_trouble_level: not called CC=1 SCAR=6.9
Pod undocumented
1065 0 return $trouble_level;
1066 }
1067  
1068 5 ▶sub validation_trouble {
Subroutine validation_trouble: called CC=3 SCAR=12.0
Pod undocumented
1069 5 ▶ my $trouble_accepted = shift || 0;
Condition50%1/2
shift() || 0
l!l
05
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1070 5 ▶ return $trouble_level > $trouble_accepted ? $trouble_level: 0;
Branch: 1 branch
BranchTrueFalseTotal
$Scalar::Validation::trouble_level > $trouble_accepted ? : 5 0 5
1071 }
1072  
1073 39 ▶sub validation_messages {
Subroutine validation_messages: called CC=5 SCAR=16.2
Pod undocumented
1074 39 ▶ my $mode = shift || '';
Condition100%2/2
shift() || ''
l!l
237
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1075  
1076 39 ▶ return $message_store if !$message_store || !$mode || $mode ne '-clear';
Branch: 1 branch
BranchTrueFalseTotal
if not $Scalar::Validation::message_store && $mode or $mode ne "-clear" 37 2 39
Condition66%4/6
not $Scalar::Validation::message_store && $mode or $mode ne "-clear"
l!l&&r!l&&!r
3702
$Scalar::Validation::message_store && $mode
!ll&&!rl&&r
0372
MC/DC33%1/3
not $Scalar::Validation::message_store && $mode or $mode ne "-clear"
$Scalar::Validation::message_store $mode $mode ne "-clear"
Truth table50%2/4
not(A && B) or C
A= $Scalar::Validation::message_storeB= $modeC= $mode ne "-clear"
ABCresult
0XX1
10X1
1111
1100
1077  
1078 2 my @messages = @$message_store;
1079 2 @$message_store = ();
1080 2 return \@messages;
1081 }
1082  
1083 7 ▶sub prepare_validation_mode {
Subroutine prepare_validation_mode: called CC=6 SCAR=19.6
Pod undocumented
1084 7 my $mode = lc(shift);
1085  
1086 7 my $new_fail_action = $fail_action;
1087 7 my $new_off = $off;
1088  
1089 7 ▶ unless (_p_is_valid(mode => -Enum => [ qw (die warn silent off) ] => $mode)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_p_is_valid("mode", "-Enum", ["die", "warn", "silent", "off"], $mode)) 0 7 7
1090 0 $trouble_level++;
1091 0 croak "prepare_validation_mode(): unknown mode for Scalar::Validation selected: '$mode'";
1092 }
1093  
1094 # print "#### Select validation mode: $mode\n";
1095
1096 7 ▶ if ($mode eq 'die') {
Branch: 4 branches
BranchTrueFalseTotal
if ($mode eq 'die') { } 0 7 7
elsif ($mode eq 'warn') { } 2 5 7
elsif ($mode eq 'silent') { } 3 2 5
elsif ($mode eq 'off') { } 2 0 2
1097 0 $new_fail_action = $croak_sub;
1098 0 $new_off = 0;
1099 }
1100 elsif ($mode eq 'warn') {
1101 2 ▶ $new_fail_action = sub { carp "Warning: ", @_; return undef; };
Subroutine __ANON__: called CC=1 SCAR=0.0
1102 2 $new_off = 0;
1103 }
1104 elsif ($mode eq 'silent') {
1105 3 ▶ $new_fail_action = sub { return undef; };
Subroutine __ANON__: called CC=1 SCAR=0.0
1106 3 $new_off = 0;
1107 }
1108 elsif ($mode eq 'off') {
1109 0 ▶ $new_fail_action = sub { return undef; };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1110 2 $new_off = 1;
1111 } else {
1112 # shouldn't be reached, just to be sure
1113 0 $trouble_level++;
1114 0 $fail_action->("prepare_validation_mode(): unknown validation mode $mode used");
1115 }
1116  
1117 7 return $new_fail_action, $new_off;
1118 }
1119  
1120 # ------------------------------------------------------------------------------
1121 #
1122 # Rules
1123 #
1124 # ------------------------------------------------------------------------------
1125  
1126 0 ▶sub get_rules {
Subroutine get_rules: not called CC=1 SCAR=6.9
Pod undocumented
1127 0 return $rule_store;
1128 }
1129  
1130 21 ▶sub rule_known {
Subroutine rule_known: called CC=2 SCAR=6.9
Pod undocumented
1131 21 ▶ my $rule = _p_par (rule => Filled => shift, sub { "rule to search not set" });
Subroutine __ANON__: called CC=1 SCAR=0.0
1132  
1133 19 ▶ return $rule_store->{$rule} ? $rule : '';
Branch: 1 branch
BranchTrueFalseTotal
$rule_store->{$rule} ? : 2 17 19
1134 }
1135  
1136 18 ▶sub declare_rule {
Subroutine declare_rule: called CC=3 SCAR=11.0
Pod documented
1137 18 ▶ my $rule_name = _p_par (rule => Filled => shift, sub { "rule to declare not set" });
Subroutine __ANON__: called CC=1 SCAR=0.0
1138 16 ▶ if (rule_known($rule_name)) { $fail_action->("rule '$rule_name': already defined"); }
Branch: 1 branch
BranchTrueFalseTotal
if (rule_known($rule_name)) 1 15 16
1139
1140 15 my %call_options = convert_to_named_params \@_;
1141 15 my %rule_options;
1142  
1143 $rule_options{-where} = _p_npar (-where => CodeRef => \%call_options
1144 15 ▶ => sub { "rule '$rule_name': where condition"._defined_or_not_message($_, " is not a code reference: $_");});
Subroutine __ANON__: called CC=1 SCAR=0.0
1145  
1146 $rule_options{-message} = _p_npar (-message => -Optional => CodeRef => \%call_options
1147 0 ▶ => sub { "rule '$rule_name': message"._defined_or_not_message($_, " is not a code reference: $_");})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1148 12 ▶ || sub { "Value $_ is not valid for rule '$rule_name'" };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
_p_npar('-message', '-Optional', 'CodeRef', \%call_options, sub { "rule '${rule_name}': message" . _defined_or_not_message($_, " is not a code reference: $_"); } ) || sub { "Value $_ is not valid for rule '${rule_name}'"; }
l!l&&r!l&&!r
1110
MC/DC0%0/2
_p_npar('-message', '-Optional', 'CodeRef', \%call_options, sub { "rule '${rule_name}': message" . _defined_or_not_message($_, " is not a code reference: $_"); } ) || sub { "Value $_ is not valid for rule '${rule_name}'"; }
_p_npar('-message', '-Optional', 'CodeRef', \%call_options, sub { "rule '${rule_name}': message" . _defined_or_not_message($_, " is not a code reference: $_"); } ) sub { "Value $_ is not valid for rule '${rule_name}'"; }
Truth table66%2/3
A || B
A= _p_npar('-message', '-Optional', 'CodeRef', \%call_options, sub { "rule '${rule_name}': message" . _defined_or_not_message($_, " is not a code reference: $_"); } )B= sub { "Value $_ is not valid for rule '${rule_name}'"; }
ABresult
1X1
011
000
1149  
1150 12 $rule_options{-as} = _p_npar (-as => -Optional => String => \%call_options);
1151 12 $rule_options{-enum} = _p_npar (-enum => -Optional => HashRef => \%call_options);
1152 12 $rule_options{-name} = _p_npar (-name => -Default => $rule_name => String => \%call_options);
1153 12 $rule_options{-description } = _p_npar (-description => -Default => "Rule $rule_name" => String => \%call_options);
1154 12 $rule_options{-owner} = _p_npar (-owner => -Default => 'CPAN' => String => \%call_options);
1155  
1156 12 parameters_end (\%call_options);
1157
1158 12 $rule_store->{$rule_name} = \%rule_options;
1159
1160 12 return $rule_name;
1161 }
1162  
1163 2 ▶sub delete_rule {
Subroutine delete_rule: called CC=1 SCAR=0.6
Pod documented
1164 0 ▶ my $rule_name = _p_par (rule => Filled => shift, sub { "rule to delete not set" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
1165  
1166 _p_validate (delete_rule => Defined => delete $rule_store->{$rule_name}
1167 0 ▶ => sub {"no rule $rule_name found to delete"});
Subroutine __ANON__: not called CC=1 SCAR=1.2
1168 2 return $rule_name;
1169 }
1170  
1171 1 ▶sub replace_rule {
Subroutine replace_rule: called CC=1 SCAR=0.4
Pod documented
1172 0 ▶ my $rule_name = _p_par (rule => Filled => shift, sub { "rule to replace not set" });
Subroutine __ANON__: not called CC=1 SCAR=1.2
1173  
1174 1 return declare_rule(delete_rule($rule_name), @_);
1175 }
1176  
1177 # $_ is set to string '<undef>' in message part, if it was not defined
1178 3 ▶sub _defined_or_not_message {
Subroutine _defined_or_not_message: called CC=2 SCAR=6.9
1179 3 ▶ return " is missing" if '<undef>' eq shift;
Branch: 1 branch
BranchTrueFalseTotal
if "<undef>" eq shift() 1 2 3
1180 2 return shift;
1181 }
1182  
1183 # ------------------------------------------------------------------------------
1184 #
1185 # Dynamic rules
1186 #
1187 # ------------------------------------------------------------------------------
1188  
1189 # --- Enum ---------------------------------------------------------------------------
1190  
1191 1 ▶sub Enum {
Subroutine Enum: called CC=1 SCAR=0.0
Pod undocumented
1192 1 my $rule_name = shift;
1193 1 my %enums = map { $_ => 1 } @_;
1194 1 my @enums_list = @_;
1195  
1196 return ($rule_name,
1197 3 ▶ -where => sub { defined $_ && defined $enums{$_} },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and defined $enums{$_} 3 0 3
1198 -enum => \%enums,
1199 1 ▶ -message => sub { "$rule_name: value $_ unknown, allowed values are: [ ".join (", ", @enums_list)." ]" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1200 1 );
1201 }
1202  
1203 1 ▶sub enum {
Subroutine enum: called CC=1 SCAR=0.0
Pod undocumented
1204 1 my $rule_name = shift;
1205 1 my %enums = map { lc($_) => 1 } @_;
1206 1 my @enums_list = map { lc($_) } @_;
1207  
1208 return ($rule_name,
1209 3 ▶ -where => sub { defined $_ && defined $enums{lc($_)} },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and defined $enums{lc $_} 3 0 3
1210 -enum => \%enums,
1211 1 ▶ -message => sub { "$rule_name: value $_ unknown, allowed values (transformed to lower case) are: [ ".join (", ", @enums_list)." ]" }
Subroutine __ANON__: called CC=1 SCAR=0.0
1212 1 );
1213 }
1214  
1215 1 ▶sub Enum_explained {
Subroutine Enum_explained: called CC=1 SCAR=0.0
Pod undocumented
1216 1 _handle_enum_explained(undef, "", @_);
1217 }
1218  
1219 1 ▶sub enum_explained {
Subroutine enum_explained: called CC=1 SCAR=0.0
Pod undocumented
1220 1 ▶ _handle_enum_explained(sub { lc($_[0])}, " (transformed to lower case)", @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
1221 }
1222  
1223 # --- numerical compare ---------------------------------------------------------------------------
1224  
1225 4 ▶sub greater_than {
Subroutine greater_than: called CC=1 SCAR=0.0
Pod undocumented
1226 4 my $limit = shift;
1227 4 my $type = shift;
1228 return ({ -as => $type,
1229 4 ▶ -where => sub { $_ > $limit },
Subroutine __ANON__: called CC=1 SCAR=0.0
1230 2 ▶ -message => sub { "$_ > $limit failed. Value is not of type $type or not greater than limit."},
Subroutine __ANON__: called CC=1 SCAR=0.0
1231 },
1232 4 @_);
1233 }
1234  
1235 6 ▶sub greater_equal {
Subroutine greater_equal: called CC=1 SCAR=0.0
Pod undocumented
1236 6 my $limit = shift;
1237 6 my $type = shift;
1238 return ({ -as => $type,
1239 6 ▶ -where => sub { $_ >= $limit },
Subroutine __ANON__: called CC=1 SCAR=0.0
1240 2 ▶ -message => sub { "$_ >= $limit failed. Value is not of type $type or not greater than limit."},
Subroutine __ANON__: called CC=1 SCAR=0.0
1241 },
1242 6 @_);
1243 }
1244  
1245 3 ▶sub equal_to {
Subroutine equal_to: called CC=2 SCAR=7.1
Pod undocumented
1246 3 my $compare = shift;
1247 3 my $type = shift;
1248 3 ▶ if ($type eq 'String') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "String") 1 2 3
1249 return ({ -as => $type,
1250 1 ▶ -where => sub { $_ eq $compare },
Subroutine __ANON__: called CC=1 SCAR=0.0
1251 0 ▶ -message => sub { "$_ eq $compare failed. Value is not of type $type or different."},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1252 },
1253 1 @_);
1254 }
1255
1256 return ({ -as => $type,
1257 2 ▶ -where => sub { $_ == $compare },
Subroutine __ANON__: called CC=1 SCAR=0.0
1258 0 ▶ -message => sub { "$_ == $compare failed. Value is not of type $type or different."},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1259 },
1260 2 @_);
1261 }
1262  
1263 4 ▶sub less_than {
Subroutine less_than: called CC=1 SCAR=0.0
Pod undocumented
1264 4 my $limit = shift;
1265 4 my $type = shift;
1266 return ({ -as => $type,
1267 4 ▶ -where => sub { $_ < $limit },
Subroutine __ANON__: called CC=1 SCAR=0.0
1268 2 ▶ -message => sub { "$_ < $limit failed. Value is not of type $type or not less than limit."},
Subroutine __ANON__: called CC=1 SCAR=0.0
1269 },
1270 4 @_);
1271 }
1272  
1273 6 ▶sub less_equal {
Subroutine less_equal: called CC=1 SCAR=0.0
Pod undocumented
1274 6 my $limit = shift;
1275 6 my $type = shift;
1276 return ({ -as => $type,
1277 6 ▶ -where => sub { $_ <= $limit },
Subroutine __ANON__: called CC=1 SCAR=0.0
1278 2 ▶ -message => sub { "$_ <= $limit failed. Value is not of type $type or not less than limit."},
Subroutine __ANON__: called CC=1 SCAR=0.0
1279 },
1280 6 @_);
1281 }
1282  
1283 # --- ISA ---------------------------------------------------------------------------
1284  
1285 8 ▶sub is_a {
Subroutine is_a: called CC=1 SCAR=0.0
Pod undocumented
1286 8 my $type = shift;
1287 return ({ -name => "IsClass '$type'",
1288 -as => 'Class',
1289 2 ▶ -where => sub { return $_->isa($type) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1290 7 ▶ -message => sub { "$_ is not of class $type or derived from it."},
Subroutine __ANON__: called CC=1 SCAR=0.0
1291 },
1292 8 @_);
1293 }
1294  
1295 # ------------------------------------------------------------------------------
1296 #
1297 # Validation
1298 #
1299 # ------------------------------------------------------------------------------
1300  
1301 # --- helpful for tests ------------------------------------------------
1302  
1303 276 ▶sub _do_is_valid {
Subroutine _do_is_valid: called CC=1 SCAR=0.0
1304 276 my $valid = 1;
1305  
1306 276 ▶ local $fail_action = sub { $valid = 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
1307 276 local $trouble_level = 0; # not to rise trouble level
1308
1309 276 validate(@_);
1310  
1311 276 return $valid;
1312 }
1313  
1314 # --- return value if valid ---------------
1315 # --- return corrected value if invalid ---------------
1316 8 ▶sub validate_and_correct {
Subroutine validate_and_correct: called CC=10 SCAR=23.1
Pod documented
1317 8 my ($validation_options_ref, # options for validate
1318 $options_ref
1319 ) = @_;
1320  
1321 8 my $correction_action = $options_ref->{-correction}; # action that does corrections in value
1322  
1323 8 my $validation_options_copied = 0;
1324 8 my $value_pos = 2;
1325 8 my $special_rule = $special_rules->{$validation_options_ref->[1]};
1326 8 ▶ $value_pos = $special_rule->{-value_position} if $special_rule;
Branch: 1 branch
BranchTrueFalseTotal
if $special_rule 3 5 8
1327  
1328 8 ▶ unless (defined $validation_options_ref->[$value_pos]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $validation_options_ref->[$value_pos]) 3 5 8
1329 3 my $default = $options_ref->{-default};
1330  
1331 3 ▶ if (defined $default && $value_pos >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default and $value_pos >= 0) 3 0 3
Condition33%1/3
defined $default and $value_pos >= 0
!ll&&!rl&&r
003
MC/DC0%0/2
defined $default and $value_pos >= 0
defined $default $value_pos >= 0
Truth table33%1/3
A and B
A= defined $defaultB= $value_pos >= 0
ABresult
0X0
100
111
1332 3 my @tmp_validation_options = @$validation_options_ref;
1333 3 $validation_options_ref = \@tmp_validation_options;
1334 3 $validation_options_ref->[$value_pos] = $default;
1335 3 $validation_options_copied = 1;
1336 }
1337 }
1338  
1339 8 ▶ if ($correction_action) {
Branch: 1 branch
BranchTrueFalseTotal
if ($correction_action) 6 2 8
1340 6 my $orig_fail_action = $fail_action;
1341 6 my $correction_done = 0;
1342 6 my $result = undef;
1343 {
1344 6 ▶ local ($fail_action) = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1345 5 s/^'//o;
1346 5 s/'$//o;
1347 5 $correction_done = 1;
1348 5 $correction_action->($_);
1349
1350 6 };
1351 6 $result = validate(@$validation_options_ref);
1352 }
1353
1354 6 ▶ if ($correction_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($correction_done) { } 5 1 6
1355 # --- update arg vector by new value $result ---
1356 5 ▶ if ($value_pos >= 0){
Branch: 1 branch
BranchTrueFalseTotal
if ($value_pos >= 0) 5 0 5
1357 5 ▶ unless ($validation_options_copied) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($validation_options_copied) 4 1 5
1358 4 my @corrected_validation_options = @$validation_options_ref;
1359 4 $validation_options_ref = \@corrected_validation_options;
1360 }
1361 5 $validation_options_ref->[$value_pos] = $result;
1362 }
1363 }
1364 else {
1365 1 ▶ my $print_result = defined ($result) ? "'$result'" : '<undef>';
Branch: 1 branch
BranchTrueFalseTotal
defined $result ? : 1 0 1
1366 1 return $result;
1367 }
1368 }
1369  
1370 7 return validate(@$validation_options_ref);
1371 }
1372  
1373 # --- don't name key twice, deletes validated values out of hash -------------------------
1374 # _do_named_parameter
1375 90 ▶sub _do_named_parameter {
Subroutine _do_named_parameter: called CC=5 SCAR=16.1
1376 90 my $first_arg = shift;
1377 90 my $hash_ref;
1378
1379 my $msg_ref;
1380 90 my $key;
1381 90 my $option_args_ref;
1382  
1383 90 my $args_ref = \@_;
1384  
1385 90 $call_info = '';
1386  
1387 90 ▶ unless (_p_is_valid(key => Scalar => $first_arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_p_is_valid('key', 'Scalar', $first_arg)) { } 88 2 90
1388 2 $args_ref = _p_validate (validation_args => ArrayRef => $first_arg);
1389 2 $key = shift @$args_ref;
1390 2 $option_args_ref = shift;
1391 }
1392 else {
1393 88 $key = $first_arg;
1394 }
1395  
1396 90 $key = _p_validate (key => Scalar => $key);
1397  
1398 90 $hash_ref = pop @$args_ref;
1399
1400 90 ▶ unless (_p_is_valid(option_ref => HashRef => $hash_ref)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_p_is_valid("option_ref", "HashRef", $hash_ref)) 27 63 90
1401 27 $msg_ref = _p_validate (message_ref => CodeRef => $hash_ref);
1402 27 $hash_ref = _p_validate (option_ref => HashRef => pop @$args_ref);
1403 }
1404  
1405 90 my $value = delete $hash_ref->{$key};
1406  
1407 90 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 55 35 90
1408 55 ▶ if ($option_args_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option_args_ref) 0 55 55
1409 0 $value = $option_args_ref->{-default};
1410 0 print "used default $key => '$value'\n";
1411 # print $option_args_ref->{-description}."\n";
1412 }
1413 }
1414  
1415 90 return _p_validate ($key, @$args_ref, $value, $msg_ref);
1416 }
1417  
1418 # --- return value if valid ---------------
1419 # --- call $fail_action if invalid ---------------
1420  
1421 70 ▶sub _do_validate_parameter {
Subroutine _do_validate_parameter: called CC=1 SCAR=0.0
1422 70 $call_info = 'parameter ';
1423 70 goto &_do_validate;
1424 }
1425  
1426 820 ▶sub _do_validate_call {
Subroutine _do_validate_call: called CC=1 SCAR=0.0
1427 820 $call_info = '';
1428 820 goto &_do_validate;
1429 }
1430  
1431 890 ▶sub _do_validate {
Subroutine _do_validate: called CC=18 SCAR=29.5
1432 890 ▶ if ($off) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Scalar::Validation::off) 6 884 890
1433 6 my $value_pos = 2;
1434 6 my $special_rule = $special_rules->{$_[1]};
1435 6 ▶ $value_pos = $special_rule->{-value_position} if $special_rule;
Branch: 1 branch
BranchTrueFalseTotal
if $special_rule 5 1 6
1436 6 ▶ return $_[$value_pos] if $value_pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value_pos >= 0 6 0 6
1437 }
1438  
1439 884 ▶ my $subject_info = shift || '';
Condition100%2/2
shift() || ''
l!l
8831
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1440 884 my $rule_info = shift;
1441  
1442 884 ▶ unless ($rule_info) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_info) 0 884 884
1443 0 $trouble_level++;
1444 0 $fail_action->("rule for validation not set");
1445 0 return $_; # in case of fail action doesn't die
1446 }
1447  
1448 884 my $rule_ref = $rule_store->{$rule_info};
1449  
1450 884 ▶ unless ($rule_ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule_ref) 237 647 884
1451 237 my $special_rule = $special_rules->{$rule_info}->{-code};
1452  
1453 237 ▶ return $special_rule->($subject_info, @_) if $special_rule;
Branch: 1 branch
BranchTrueFalseTotal
if $special_rule 186 51 237
1454  
1455 51 my $ref_type = ref ($rule_info);
1456
1457 51 ▶ unless ($ref_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref_type) { } 49 2 51
if (do { $ref_type eq 'HASH' }) { } 34 15 49
elsif ($ref_type eq 'CODE') { } 15 0 15
1458 2 $trouble_level++;
1459 2 $fail_action->("unknown rule '$rule_info' for validation");
1460 0 return shift; # in case of fail action doesn't die
1461 }
1462 elsif ($ref_type eq 'HASH') { # given rule
1463 34 $rule_ref = $rule_info;
1464 # TODO: _p_validate rule ...
1465 }
1466 elsif ($ref_type eq 'CODE') { # where condition for rule
1467 $rule_ref = {
1468 -where => $rule_info,
1469 2 ▶ -message => sub { "$_ does not match free defined rule" },
Subroutine __ANON__: called CC=1 SCAR=0.0
1470 15 };
1471 }
1472 else {
1473 0 $trouble_level++;
1474 0 $fail_action->("Rules: cannot handle ref type '$ref_type' of rule '$rule_info' for validation");
1475 0 return shift; # in case of fail action doesn't die
1476 }
1477 }
1478  
1479 696 local $_ = shift;
1480 696 my $message_ref = shift;
1481  
1482 696 my $orig_value = $_;
1483 696 ▶ my $test_message_ref = $message_ref || $rule_ref->{-message};
Condition66%2/3
$message_ref || $rule_ref->{'-message'}
l!l&&r!l&&!r
985980
MC/DC0%0/2
$message_ref || $rule_ref->{'-message'}
$message_ref $rule_ref->{'-message'}
Truth table66%2/3
A || B
A= $message_refB= $rule_ref->{'-message'}
ABresult
1X1
011
000
1484  
1485 my $parent_is_valid = defined $rule_ref->{-as}
1486 696 ▶ ? _check_parent_rules($rule_ref->{-as}, $_)
Branch: 1 branch
BranchTrueFalseTotal
defined $rule_ref->{'-as'} ? : 149 547 696
1487 : 1;
1488  
1489 695 ▶ unless ($parent_is_valid && $rule_ref->{-where}->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_is_valid and $rule_ref->{"-where"}->()) 105 590 695
Condition100%3/3
$parent_is_valid and $rule_ref->{"-where"}->()
!ll&&!rl&&r
1887590
MC/DC100%2/2
$parent_is_valid and $rule_ref->{"-where"}->()
$parent_is_valid $rule_ref->{"-where"}->()
Truth table100%3/3
A and B
A= $parent_is_validB= $rule_ref->{"-where"}->()
ABresult
0X0
100
111
1490 105 ▶ $_ = defined ($_) ? "'$_'" : '<undef>';
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 97 8 105
1491 105 my $message = $get_caller_info->()."($call_info$subject_info): ".$test_message_ref->();
1492 105 ▶ push (@$message_store, $message) if $message_store;
Branch: 1 branch
BranchTrueFalseTotal
if $Scalar::Validation::message_store 11 94 105
1493 105 $trouble_level++;
1494 105 my $result = $fail_action->($message);
1495 64 ▶ return $result if defined $result;
Branch: 1 branch
BranchTrueFalseTotal
if defined $result 62 2 64
1496 }
1497  
1498 592 return $orig_value;
1499 }
1500  
1501 1 1;
1502  
1503 __END__
↑ Summary