blib/lib/YAML/Logic.pm

filter: Statementstmt 93.5% 102 / 109 Branchbran 82.6% 43 / 52 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 8 / 8 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 6 totaltotal 88.6% 180 / 203 CC 33 SCAR 23.4
CC
33
Cov
91%
evaluate
16.3
evaluate_single
13.1
interpolate
4.0
CRAP 10.3
↑ Summary
1 ###########################################
2 package YAML::Logic;
3 ###########################################
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Safe;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = "0.07";
13 3 our %OPS = map { $_ => 1 }
14 qw(eq ne lt gt < > <= >= == =~ like);
15  
16 ###########################################
17 67 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 ###########################################
19 67 my($class, %options) = @_;
20  
21 67 my $self = {
22 safe => Safe->new(),
23 template => Template->new(),
24 error => "",
25 %options,
26 };
27  
28 67 $self->{safe}->permit();
29  
30 67 bless $self, $class;
31 }
32  
33 ###########################################
34 259 ▶sub interpolate {
Subroutine interpolate: called CC=4 SCAR=13.9
Pod undocumented
35 ###########################################
36 259 my($self, $input, $vars) = @_;
37  
38 259 ▶ if(ref($input) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq "HASH") 34 225 259
39 # When working on the original, we got weird memory errors
40 # in perl5.8, so just copy the hash.
41 34 my %dupe = %$input;
42 34 my @keyvals = ();
43 34 for my $entry (each %dupe) {
44 68 push @keyvals, $self->interpolate( $entry, $vars );
45 }
46 34 return { @keyvals };
47 }
48  
49 225 my $out;
50 225 $input =~ s/(?:\$\{([\w.]+)})/[%- $1 %]/gx;
51 225 $input =~ s/(?:\$([\w.]+)) /[%- $1 %]/gx;
52  
53 $self->{template}->process( \$input, $vars, \$out ) or
54 225 ▶ LOGDIE $self->{template}->error();
Branch: 1 branch
BranchTrueFalseTotal
$self->{"template"}->process(\$input, $vars, \$out) or LOGDIE($self->{"template"}->error) 225 0 225
55  
56 225 return $out;
57 }
58  
59 ###########################################
60 91 ▶sub evaluate {
Subroutine evaluate: called CC=16 SCAR=27.9
Pod undocumented
61 ###########################################
62 91 my($self, $data, $vars, $not_glob, $boolean_or) = @_;
63  
64 91 ▶ $not_glob = 0 unless defined $not_glob;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $not_glob 91 0 91
65 91 ▶ $boolean_or = 0 unless defined $boolean_or;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boolean_or 73 18 91
66  
67 0 ▶ DEBUG sub { "evaluate: " .
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 Dumper( $data ) . "\n" .
69 Dumper( $vars ) . "\n" .
70 "not_glob=$not_glob " .
71 "boolean_or=$boolean_or " .
72 ""
73 91 };
74  
75 91 ▶ if( ref($data) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq 'ARRAY') { } 91 0 91
76 91 my @data = @$data; # make a copy, so splice() doesn't destroy
77 # the original.
78 91 ▶ while( my($field, $value) = splice @data, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@data, 0, 2)) 115 54 169
79 115 my $res;
80  
81 my $not;
82  
83 115 ▶ if(! defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 0 115 115
84 0 LOGDIE "Rule variable not defined (value=$value)",
85 "(maybe YAML rule: !\$var without quotes?";
86 }
87  
88 115 ▶ if($field =~ s/^!//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field =~ s/^!//) 9 106 115
89 9 $not = !$not_glob;
90 }
91  
92 115 ▶ if($field eq "or") {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'or') { } 18 97 115
elsif ($field eq 'and') { } 8 89 97
93 18 $res = $self->evaluate($value, $vars, $not, 1);
94 } elsif( $field eq "and") {
95 8 $res = $self->evaluate($value, $vars, $not);
96 } else {
97 89 $field = $self->interpolate($field, $vars);
98 89 $value = $self->interpolate($value, $vars);
99  
100 89 ▶ if(ref($value) eq "") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq '') { } 55 34 89
elsif (ref $value eq 'HASH') { } 34 0 34
101 55 $res = $self->evaluate_single( $field, $value, "eq", $not );
102 } elsif(ref($value) eq "HASH") {
103 34 my($op) = keys %$value;
104 34 ($value) = values %$value;
105 34 $res = $self->evaluate_single( $field, $value, $op, $not );
106 }
107 }
108  
109 114 ▶ if($boolean_or and $res) {
Branch: 2 branches
BranchTrueFalseTotal
if ($boolean_or and $res) { } 12 102 114
elsif (not $boolean_or || $res) { } 24 78 102
Condition100%3/3
$boolean_or and $res
!ll&&!rl&&r
812112
MC/DC100%2/2
$boolean_or and $res
$boolean_or $res
Truth table100%3/3
A and B
A= $boolean_orB= $res
ABresult
0X0
100
111
110 # It's a boolean OR, so all it takes is one true result
111 12 my $rc = 1;
112 12 DEBUG "evaluate: rc=$rc";
113 12 return $rc;
114 ▶ } elsif(!$boolean_or and !$res) {
Condition100%3/3
$boolean_or || $res
l!l&&r!l&&!r
215724
MC/DC100%2/2
$boolean_or || $res
$boolean_or $res
Truth table100%3/3
A || B
A= $boolean_orB= $res
ABresult
1X1
011
000
115 # It's a boolean AND, so all it takes is one false result
116 24 my $rc = 0;
117 24 DEBUG "evaluate: rc=$rc";
118 24 return $rc;
119 }
120 }
121 } else {
122 0 LOGDIE "Unknown type: $data";
123 }
124  
125 # Return 1 if all ANDed conditions succeeded, and 0 if all
126 # ORed conditions failed.
127 54 my $rc = 1;
128 54 ▶ $rc = 0 if $boolean_or;
Branch: 1 branch
BranchTrueFalseTotal
if $boolean_or 6 48 54
129 54 DEBUG "evaluate: rc=$rc";
130 54 return $rc;
131 }
132  
133 ###########################################
134 89 ▶sub evaluate_single {
Subroutine evaluate_single: called CC=13 SCAR=25.8
Pod undocumented
135 ###########################################
136 89 my($self, $field, $value, $op, $not) = @_;
137  
138 89 $op = lc $op ;
139 89 ▶ $op = '=~' if $op eq "like";
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "like" 18 71 89
140  
141 89 $self->error("");
142  
143 89 ▶ if(! exists $OPS{ $op }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $YAML::Logic::OPS{$op}) 0 89 89
144 0 LOGDIE "Unknown op: $op";
145 }
146  
147 89 $field = '"' . esc($field) . '"';
148 89 my $cmd;
149  
150 89 ▶ if($op eq "=~") {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq '=~') { } 20 69 89
151 20 ▶ if($value =~ /\?\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\?\{/) 1 19 20
152 1 LOGDIE "Trapped ?{ in regex.";
153 }
154 19 $value =~ s#(\\\\|/)#\\$1#g;
155 # If we ever get something like \\/, slap another backslash
156 # onto the "/" to mask it.
157 19 ▶ $value =~ s#(\\+)/# (length($1) % 2) ? "$1/" : "$1\\/"#ge;
Branch: 1 branch
BranchTrueFalseTotal
length($1) % 2 ? : 10 2 12
158 19 $value = qr($value);
159 19 $cmd = "$field =~ /$value/";
160 } else {
161 69 $value = '"' . esc($value) . '"';
162 69 $cmd = "$field $op $value";
163 }
164  
165 88 INFO "Test: $cmd";
166 88 my $res = $self->{safe}->reval($cmd);
167  
168 88 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 88 88
169 0 LOGDIE "Evaling [$cmd] failed: $@";
170 }
171  
172 88 ▶ if(!$res and !$not or
Branch: 1 branch
BranchTrueFalseTotal
if (not $res || $not or $res and $not) 35 53 88
Condition100%9/9
not $res || $not or $res and $not
l!l&&r!l&&!r
31453
$res || $not
l!l&&r!l&&!r
52531
$res and $not
!ll&&!rl&&r
5484
MC/DC100%4/4
not $res || $not or $res and $not
$res $not $res $not
Truth table57%4/7
not(A || B) or C and D
A= $resB= $notC= $resD= $not
ABCDresult
00XX1
1X111
01111
1X0X0
1X100
010X0
01100
173 $res and $not) {
174 35 ▶ $res = "" if !defined $res;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $res 0 35 35
175 35 $self->error("Test [$cmd] returned [$res]");
176 }
177  
178 88 ▶ return ($not ? (!$res) : $res);
Branch: 1 branch
BranchTrueFalseTotal
$not ? : 9 79 88
179 }
180  
181 ###########################################
182 127 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod undocumented
183 ###########################################
184 127 my($self, $error) = @_;
185  
186 127 ▶ if(defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 124 3 127
187 124 $self->{error} = $error;
188 }
189  
190 127 return $self->{error};
191 }
192  
193 ###############################################
194 158 ▶sub esc {
Subroutine esc: called CC=2 SCAR=7.9
Pod undocumented
195 ###############################################
196 158 my($str, $metas) = @_;
197  
198 158 $str =~ s/([\\"])/\\$1/g;
199  
200 158 ▶ if(defined $metas) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $metas) 0 158 158
201 0 $metas =~ s/\]/\\]/g;
202 0 $str =~ s/([$metas])/\\$1/g;
203 }
204  
205 158 return $str;
206 }
207  
208 3 1;
209  
210 __END__
↑ Summary