blib/lib/IO/Iron/PolicyBase.pm

filter: Statementstmt 87.2% 171 / 196 Branchbran 67.6% 23 / 34 Conditioncond 66.6% 10 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 96.0% 24 / 25 Podpod 100% 5 / 5 totaltotal 83.8% 238 / 284 CC 30 SCAR 19.0
CC
30
Cov
83%
is_valid_policy
30.0
_do_alt
10.0
_get_character_group_alternatives
5.1
CRAP 6.7
« blib/lib/IO/Iron/IronMQ/Message.pm ↑ Summary blib/lib/IO/Iron/IronCache/Cache.pm »
1 package IO::Iron::PolicyBase;
2  
3 ## no critic (Documentation::RequirePodAtEnd)
4 ## no critic (Documentation::RequirePodSections)
5 ## no critic (Subroutines::RequireArgUnpacking)
6  
7 6 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Global creator
12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # Inherit nothing
14 }
15  
16 # Global destructor
17 ▶END {
Subroutine END: called CC=1 SCAR=0.0
18 }
19  
20  
21 # ABSTRACT: Base package (inherited) for IO::Iron::IronMQ/Cache/Worker::Policy packages.
22  
23 6 our $VERSION = '0.12_01'; # TRIAL VERSION: generated by DZP::OurPkgVersion
24  
25  
26 6 ▶use Log::Any qw{$log};
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶use Hash::Util 0.06 qw{lock_keys unlock_keys};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 6 ▶use Carp::Assert::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 6 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 6 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 6 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 use Exception::Class (
34 6 'IronPolicyException' => {
35 fields => ['policy', 'candidate'],
36 },
37 'NoIronPolicyException' => {
38 fields => [],
39 },
40 'CharacterGroupNotDefinedIronPolicyException' => {
41 fields => [],
42 }
43 6 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 6 ▶use IO::Iron::Common ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 6 ▶use IO::Iron::PolicyBase::CharacterGroup ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48  
49 # INTERNAL METHODS
50 # For use in the inheriting subclass
51  
52  
53 # TODO policy character set, list possible alternatives:
54 sub IRON_CLIENT_DEFAULT_POLICIES {
55 2 ▶ my %default_policies =
Subroutine IRON_CLIENT_DEFAULT_POLICIES: called CC=1 SCAR=0.0
Pod documented
56 (
57 'definition' => {
58 'character_set' => 'ascii', # The only supported character set!
59 'character_group' => {
60 },
61 'no_limitation' => 1, # There is an unlimited number of alternatives.
62 },
63 'queue' => { 'name' => [ '[:word:]{1,}' ], },
64 'cache' => { 'name' => [ '[:word:]{1,}' ], 'item_key' => [ '[:word:]{1,}' ]},
65 'worker' => { 'name' => [ '[:word:]{1,}' ], },
66 );
67 2 return %default_policies;
68 }
69  
70 sub _do_alt {
71 618 ▶ my $self = shift;
Subroutine _do_alt: called CC=10 SCAR=23.0
72 618 my %params = validate(
73 @_, {
74 'str' => { type => SCALAR, }, # name/key name.
75 }
76 );
77 618 my $str = $params{'str'};
78 618 $log->tracef('Entering _do_alt(%s)', $str);
79 618 assert(length $str > 0, 'String length > 0.');
80 618 my @processed_alts;
81 618 ▶ if( $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sx
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sx or $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx and $3 > 1) { } 12 606 618
Condition100%6/6
$str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sx or $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx and $3 > 1
l!l&&r!l&&!r
111606
$str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx and $3 > 1
!ll&&!rl&&r
36823811
MC/DC100%3/3
$str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sx or $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx and $3 > 1
$str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sx $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx $3 > 1
Truth table100%4/4
A or B and C
A= $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{[[:digit:]]+\,[[:digit:]]+\})([[:graph:]]*)$/sxB= $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sxC= $3 > 1
ABCresult
1XX1
0111
00X0
0100
82 || ($str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{([[:digit:]]+)\})([[:graph:]]*)$/sx && $3 > 1)
83 ) {
84 12 $log->tracef('We need to do recursion.', $str);
85 12 my $preceeding_part = $1;
86 12 my $group_part = $2;
87 12 ▶ my $succeeding_part = defined $4 ? $4 : $3;
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 11 1 12
88 12 $log->tracef('$preceeding_part=%s;$group_part=%s;$succeeding_part=%s;',
89 $preceeding_part, $group_part, $succeeding_part);
90 12 my @alternatives = _make_ones($preceeding_part, $group_part, $succeeding_part);
91 12 foreach (@alternatives) {
92 13 push @processed_alts, $self->_do_alt('str' => $_);
93 }
94 }
95 else {
96 606 $log->tracef('We need to create the alternatives.', $str);
97 606 ▶ if( $str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{1\})([[:graph:]]*)$/sx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^([[:graph:]]*)(\[:[[:graph:]]+:\]\{1\})([[:graph:]]*)$/sx) { } 238 368 606
98 238 my @alts;
99 238 my $preceeding_part = $1;
100 238 my $group_part = $2;
101 238 my $succeeding_part = $3;
102 238 $log->tracef('$preceeding_part=%s;$group_part=%s;$succeeding_part=%s;',
103 $preceeding_part, $group_part, $succeeding_part);
104 238 ▶ if($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\}$/sx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\}$/sx) 238 0 238
105 238 my $group = $1;
106 238 my $lowest_amount = $2;
107 238 my $highest_amount = $3;
108 238 $log->tracef('$group=%s;$lowest_amount=%s;$highest_amount=%s;',
109 $group, $lowest_amount, $highest_amount);
110 238 foreach ($self->_get_character_group_alternatives('character_group' => $group)) {
111 602 push @alts, $preceeding_part . $_ . $succeeding_part;
112 }
113 }
114 238 $log->tracef('@alts=%s;', \@alts);
115 238 foreach (@alts) {
116 602 push @processed_alts, $self->_do_alt('str' => $_);
117 }
118 }
119 else {
120 368 push @processed_alts, $str;
121 }
122 }
123 618 $log->tracef('Exiting _do_alt():%s', \@processed_alts);
124 618 return @processed_alts;
125 }
126  
127 sub _make_ones {
128 12 ▶ my $preceeding_part = $_[0];
Subroutine _make_ones: called CC=5 SCAR=16.1
129 12 my $group_part = $_[1];
130 12 my $succeeding_part = $_[2];
131 12 $log->tracef('_make_ones():$preceeding_part=%s;$group_part=%s;$succeeding_part=%s;',
132 $preceeding_part, $group_part, $succeeding_part);
133 12 $log->tracef('$group_part=%s;', $group_part);
134 12 my @alternatives;
135 12 ▶ if($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\,([[:digit:]]+)\}$/msx) {
Branch: 2 branches
BranchTrueFalseTotal
if ($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\,([[:digit:]]+)\}$/msx) { } 1 11 12
elsif ($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\}$/msx) { } 11 0 11
136 1 my $group = $1;
137 1 my $lowest_amount = $2;
138 1 my $highest_amount = $3;
139 1 $log->tracef('$group=%s;$lowest_amount=%s;$highest_amount=%s;',
140 $group, $lowest_amount, $highest_amount);
141 1 for($lowest_amount..$highest_amount) {
142 2 my $group_str = $group . '{1}';
143 2 push @alternatives, $preceeding_part . $group_str x $_ . $succeeding_part;
144 }
145 }
146 elsif($group_part =~ /^(\[:[[:graph:]]+:\])\{([[:digit:]]+)\}$/msx) {
147 11 my $group = $1;
148 11 my $lowest_amount = $2;
149 11 my $highest_amount = $2;
150 11 $log->tracef('$group=%s;$lowest_amount=%s;$highest_amount=%s;',
151 $group, $lowest_amount, $highest_amount);
152 11 ▶ for(my $i = $lowest_amount; $i < $highest_amount + 1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $highest_amount + 1) 11 11 22
153 11 my $group_str = $group . '{1}';
154 11 push @alternatives, $preceeding_part . $group_str x $i . $succeeding_part;
155 }
156 }
157 else {
158 0 $log->fatalf('Illegal string \'%s\'.', $group_part);
159 }
160 12 $log->tracef('@alternatives=%s;', \@alternatives);
161 12 return @alternatives;
162 }
163  
164 sub _get_character_group_alternatives {
165 254 ▶ my $self = shift;
Subroutine _get_character_group_alternatives: called CC=5 SCAR=16.2
166 254 my %params = validate(
167 @_, {
168 'character_group' => { type => SCALAR, regex => qr/^[[:graph:]]+$/msx, }, # name/key name.
169 'keep_posix_group' => { type => BOOLEAN, optional => 1, }, # Keep POSIX (subset) group name and return it.
170 },
171 );
172 254 my $chars;
173  
174 # Predefined groups (subset of POSIX) first!
175 $chars = IO::Iron::PolicyBase::CharacterGroup::group(
176 254 'character_group' => $params{'character_group'});
177 254 ▶ if($chars && $params{'keep_posix_group'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars and $params{"keep_posix_group"}) 16 238 254
Condition100%3/3
$chars and $params{"keep_posix_group"}
!ll&&!rl&&r
237116
MC/DC100%2/2
$chars and $params{"keep_posix_group"}
$chars $params{"keep_posix_group"}
Truth table100%3/3
A and B
A= $charsB= $params{"keep_posix_group"}
ABresult
0X0
100
111
178 16 $chars = $params{'character_group'}; # Put the group name back.
179 }
180 254 ▶ if(!$chars) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chars) 237 17 254
181 $chars = $self->{'policy'}->{'definition'}->{'character_group'}
182 237 ->{$params{'character_group'}};
183 }
184 254 ▶ if($chars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars) { } 254 0 254
185 254 $log->tracef('$chars=%s;', $chars);
186 }
187 else {
188 0 $log->fatalf('Character group \'%s\' not defined.', $params{'character_group'});
189 CharacterGroupNotDefinedIronPolicyException->throw(
190 0 error => 'CharacterGroupNotDefinedIronPolicyException: Character group \'' . $params{'character_group'} . '\' not defined!',
191 );
192 }
193 254 return split //msx, $chars;
194 }
195  
196  
197 sub alternatives {
198 3 ▶ my $self = shift;
Subroutine alternatives: called CC=4 SCAR=14.2
Pod documented
199 3 my %params = validate(
200 @_, {
201 'required_policy' => { type => SCALAR, }, # name/key name.
202 }
203 );
204 3 assert_hashref( $self->{'policy'}, 'self->{required_policy} is a reference to a list.');
205 3 $log->tracef('Entering alternatives(%s)', \%params);
206  
207 3 ▶ if(defined $self->{'policy'}->{'definition'}->{'no_limitation'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"policy"}{"definition"}{"no_limitation"} and $self->{"policy"}{"definition"}{"no_limitation"} == 1) 0 3 3
Condition33%1/3
defined $self->{"policy"}{"definition"}{"no_limitation"} and $self->{"policy"}{"definition"}{"no_limitation"} == 1
!ll&&!rl&&r
300
MC/DC0%0/2
defined $self->{"policy"}{"definition"}{"no_limitation"} and $self->{"policy"}{"definition"}{"no_limitation"} == 1
defined $self->{"policy"}{"definition"}{"no_limitation"} $self->{"policy"}{"definition"}{"no_limitation"} == 1
Truth table33%1/3
A and B
A= defined $self->{"policy"}{"definition"}{"no_limitation"}B= $self->{"policy"}{"definition"}{"no_limitation"} == 1
ABresult
0X0
100
111
208 $self->{'policy'}->{'definition'}->{'no_limitation'} == 1) {
209 0 NoIronPolicyException->throw(
210 error => 'NoIronPolicyException: Cannot list alternatives, unlimited number!',
211 );
212 }
213 3 my $templates = $self->{'policy'}->{$params{'required_policy'}};
214 3 assert_listref($templates, 'templates is a reference to a list');
215 3 my @template_alternatives;
216 3 foreach (@{$templates}) {
217 3 $log->tracef('alternatives(): Template:\"%s\".)', $_);
218 3 push @template_alternatives, $self->_do_alt('str' => $_);
219 }
220 3 $log->tracef('Exiting alternatives():%s', \@template_alternatives);
221 3 return @template_alternatives;
222 }
223  
224 sub _get_chars_or_remain_posix_group {
225 16 ▶ my $self = shift;
Subroutine _get_chars_or_remain_posix_group: called CC=1 SCAR=0.0
226 16 $log->tracef('Entering _get_chars_or_remain_posix_group(%s)', \@_);
227 16 my $group = $_[0];
228 16 $log->tracef('_get_chars_or_remain_posix_group(): Ask for Group alternatives for :%s', $group);
229 16 my @chars = $self->_get_character_group_alternatives('character_group' => $group, 'keep_posix_group' => 1);
230 16 $log->tracef('_get_chars_or_remain_posix_group(): Group alternatives:%s', \@chars);
231 16 my $group_chars = join q{}, @chars;
232 16 $log->tracef('Exiting _get_chars_or_remain_posix_group():%s', ('[' . $group_chars . ']') );
233 16 return '[' . $group_chars . ']';
234 }
235  
236 sub _convert_policy_to_normal_regexp {
237 22 ▶ my $self = shift;
Subroutine _convert_policy_to_normal_regexp: called CC=1 SCAR=0.0
238 22 $log->tracef('Entering _convert_policy_to_normal_regexp(%s)', \@_);
239 22 my $policy_regexp = $_[0];
240 22 $policy_regexp =~ s/(\[:[[:graph:]]+?:\])/$self->_get_chars_or_remain_posix_group($1)/egsx;
241 22 $log->tracef('Exiting _convert_policy_to_normal_regexp():%s', $policy_regexp);
242 22 return $policy_regexp;
243 }
244  
245  
246 sub is_valid_policy {
247 0 ▶ my $self = shift;
Subroutine is_valid_policy: not called CC=5 SCAR=34.0
Pod documented
248 0 my %params = validate(
249 @_, {
250 'policy' => { type => SCALAR, }, # name/key name.
251 'candidate' => { type => SCALAR, }, # string to check.
252 }
253 );
254 0 assert_listref( $self->{'policy'}, 'self->{policy} is a reference to a list.');
255 0 $log->tracef('Entering is_valid_policy(%s)', \%params);
256  
257 0 my $validity = 0;
258 0 ▶ if(defined $self->{'policy'}->{'definition'}->{'no_limitation'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'policy'}{'definition'}{'no_limitation'} and $self->{'policy'}{'definition'}{'no_limitation'} == 1) { } 0 0 0
Condition0%0/3
defined $self->{"policy"}{"definition"}{"no_limitation"} and $self->{"policy"}{"definition"}{"no_limitation"} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"policy"}{"definition"}{"no_limitation"} and $self->{"policy"}{"definition"}{"no_limitation"} == 1
defined $self->{"policy"}{"definition"}{"no_limitation"} $self->{"policy"}{"definition"}{"no_limitation"} == 1
Truth table0%0/3
A and B
A= defined $self->{"policy"}{"definition"}{"no_limitation"}B= $self->{"policy"}{"definition"}{"no_limitation"} == 1
ABresult
0X0
100
111
259 && $self->{'policy'}->{'definition'}->{'no_limitation'} == 1) {
260 0 $log->trace('is_valid_policy', 'no_limitation: no policy check!');
261 0 $validity = 1;
262 }
263 else {
264 0 my $templates = $self->{'policy'}->{$params{'policy'}};
265 0 assert_listref($templates, "templates is a reference to a list");
266 0 foreach (@{$templates}) {
267 0 $log->tracef('is_valid_policy(): Going to comparing with raw template:\"%s\".)', $_);
268 0 my $template = $self->_convert_policy_to_normal_regexp($_);
269 0 $log->tracef('is_valid_policy(): Comparing with template:\"%s\".)', $template);
270 0 ▶ if($params{'candidate'} =~ /^$template$/xgsm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"candidate"} =~ /^$template$/gmsx) 0 0 0
271 0 $validity = 1;
272 0 last;
273 }
274 }
275 }
276 0 $log->tracef('Exiting is_valid_policy():%d', $validity);
277 0 return $validity;
278 }
279  
280 # This method throws an exception of type IronPolicyException.
281  
282  
283 sub validate_with_policy {
284 9 ▶ my $self = shift;
Subroutine validate_with_policy: called CC=4 SCAR=13.9
Pod documented
285 9 my %params = validate(
286 @_, {
287 'policy' => { type => SCALAR, }, # name/key name.
288 'candidate' => { type => SCALAR, }, # string to check.
289 }
290 );
291 9 assert_hashref( $self->{'policy'}, 'self->{policy} is a reference to a hash.');
292 9 $log->tracef('Entering validate_with_policy(%s)', \%params);
293 9 my $validity = 0;
294 9 my $templates = $self->{'policy'}->{$params{'policy'}};
295 9 assert_listref($templates, 'templates is a reference to a list');
296 9 foreach (@{$templates}) {
297 22 my $template = $self->_convert_policy_to_normal_regexp($_);
298 22 $log->tracef('validate_with_policy(): Comparing with template:\"%s\".)', $template);
299 22 ▶ if($params{'candidate'} =~ /^$template$/xgsm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"candidate"} =~ /^$template$/gmsx) 4 18 22
300 4 $validity = 1;
301 4 last;
302 }
303 }
304 9 ▶ if($validity == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($validity == 0) 5 4 9
305 5 $log->tracef('Throwing exception in validate_with_policy(): policy=%s, candidate=%s', $params{'policy'}, $params{'candidate'});
306 IronPolicyException->throw(
307 policy => $params{'policy'},
308 candidate => $params{'candidate'},
309 error => 'IronPolicyException: policy=' . $params{'policy'}
310 5 . ' candidate=' . $params{'candidate'},
311 );
312 }
313 4 $log->tracef('Exiting validate_with_policy():%d', $validity);
314 4 return $validity;
315 }
316  
317  
318 sub get_policies { ## no critic (Subroutines::RequireArgUnpacking)
319 2 ▶ my $self = shift;
Subroutine get_policies: called CC=3 SCAR=11.4
Pod documented
320 2 my %params = validate(
321 @_, {
322 'policies' => { type => SCALAR|UNDEF, optional => 0, },
323 }
324 );
325 2 $log->tracef('Entering get_policies(%s)', \%params);
326 2 my %all_policies = IRON_CLIENT_DEFAULT_POLICIES(); ## Preset default policies.
327 2 $log->tracef('Default policies: %s', \%all_policies);
328 2 ▶ if(defined $params{'policies'}) { # policies file specified when creating the object, if given.
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"policies"}) 0 2 2
329 IO::Iron::Common::_read_iron_config_file(\%all_policies,
330 File::Spec->file_name_is_absolute($params{'policies'})
331 0 ▶ ? $params{'policies'} : File::Spec->catfile(File::Spec->curdir(), $params{'policies'})
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($params{'policies'}) ? : 0 0 0
332 );
333 }
334 2 my %policies = %{$all_policies{$self->_THIS_POLICY()}};
335 2 $policies{'definition'} = $all_policies{'definition'};
336 2 $log->tracef('Exiting get_policies: %s', \%policies);
337 2 return \%policies;
338 }
339  
340 6 1;
341  
342 __END__
« blib/lib/IO/Iron/IronMQ/Message.pm ↑ Summary blib/lib/IO/Iron/IronCache/Cache.pm »