Predicate/Predicates.pm

filter: Statementstmt 100% 104 / 104 Branchbran 100% 46 / 46 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 39 / 39 Podpod 100% 16 / 16 totaltotal 100% 230 / 230 CC 31 SCAR 7.8
CC
31
Cov
100%
__ANON__
3.0
__ANON__
3.0
__ANON__
3.0
CRAP 2.2
↑ Summary Predicate/ClosurePredicate.pm »
1 package Data::Predicate::Predicates;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Data::Predicate::ClosurePredicate;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 our @EXPORT_OK = qw(
12 p_and
13 p_or
14 p_not
15 p_always_true
16 p_always_false
17 p_undef
18 p_defined
19 p_blessed
20 p_is_number
21 p_ref_type
22 p_isa
23 p_can
24 p_numeric_equals
25 p_string_equals
26 p_regex
27 p_substring
28 );
29 7 our %EXPORT_TAGS = (
30 all => [@EXPORT_OK],
31 logic => [qw(p_and p_or p_not)],
32 defaults => [qw(p_always_true p_always_false)],
33 tests => [qw(p_defined p_undef p_blessed p_is_number p_ref_type
34 p_isa p_can p_numeric_equals p_string_equals
35 p_regex p_substring)],
36 );
37  
38 #Set of predicates which never change so we build once & cache; others may & do
39 Readonly::Hash my %STATIC_PREDICATES => (
40 'true' => Data::Predicate::ClosurePredicate->new(
41 2 ▶ closure => sub { 1 } ,
Subroutine __ANON__: called CC=1 SCAR=0.0
42 description => 'true'
43 ),
44 'false' => Data::Predicate::ClosurePredicate->new(
45 2 ▶ closure => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 description => 'false'
47 ),
48 'defined' => Data::Predicate::ClosurePredicate->new(
49 70 ▶ closure => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
50 70 my ($object) = @_;
51 70 ▶ return ( defined $object ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $object ? : 61 9 70
52 },
53 description => 'defined'
54 ),
55 'undef' => Data::Predicate::ClosurePredicate->new(
56 2 ▶ closure => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
57 2 my ($object) = @_;
58 2 ▶ return ( defined $object ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $object ? : 1 1 2
59 },
60 description => 'undef'
61 ),
62 'blessed' => Data::Predicate::ClosurePredicate->new(
63 31 ▶ closure => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
64 31 my ($object) = @_;
65 31 ▶ return ( blessed($object) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($object) ? : 19 12 31
66 },
67 description => 'blessed'
68 ),
69 'number' => Data::Predicate::ClosurePredicate->new(
70 48 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
71 48 my ($object) = @_;
72 48 ▶ return ( defined $object && looks_like_number($object) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $object && &looks_like_number($object) ? : 24 24 48
Condition100%3/3
defined $object and &looks_like_number($object)
!ll&&!rl&&r
22224
MC/DC100%2/2
defined $object and &looks_like_number($object)
defined $object &looks_like_number($object)
Truth table100%3/3
A and B
A= defined $objectB= &looks_like_number($object)
ABresult
0X0
100
111
73 },
74 7 description => 'number'
75 )
76 );
77  
78 2 ▶sub p_and {
Subroutine p_and: called CC=1 SCAR=0.0
Pod documented
79 2 my ( @predicates ) = @_;
80 return Data::Predicate::ClosurePredicate->new(
81 41 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
82 41 my ($object) = @_;
83 41 foreach my $pred (@predicates) {
84 76 ▶ if(!$pred->apply($object)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pred->apply($object)) 23 53 76
85 23 return 0;
86 }
87 }
88 18 return 1;
89 },
90 2 description => 'and'
91 );
92 }
93  
94 1 ▶sub p_or {
Subroutine p_or: called CC=1 SCAR=0.0
Pod documented
95 1 my ( @predicates ) = @_;
96 return Data::Predicate::ClosurePredicate->new(
97 5 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
98 5 my ($object) = @_;
99 5 foreach my $pred (@predicates) {
100 9 ▶ if($pred->apply($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->apply($object)) 2 7 9
101 2 return 1;
102 }
103 }
104 3 return 0;
105 },
106 1 description => 'or'
107 );
108 }
109  
110 2 ▶sub p_not {
Subroutine p_not: called CC=1 SCAR=0.0
Pod documented
111 2 my ($predicate) = @_;
112 return Data::Predicate::ClosurePredicate->new(
113 2 ▶ closure => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
114 2 my ($object) = @_;
115 2 ▶ return ($predicate->apply($object)) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$predicate->apply($object) ? : 1 1 2
116 },
117 2 description => 'not'
118 );
119 }
120  
121 2 ▶sub p_always_true {
Subroutine p_always_true: called CC=1 SCAR=0.0
Pod documented
122 2 return $STATIC_PREDICATES{'true'};
123 }
124  
125 2 ▶sub p_always_false {
Subroutine p_always_false: called CC=1 SCAR=0.0
Pod documented
126 2 return $STATIC_PREDICATES{'false'};
127 }
128  
129 31 ▶sub p_defined {
Subroutine p_defined: called CC=1 SCAR=0.0
Pod documented
130 31 return $STATIC_PREDICATES{'defined'};
131 }
132  
133 2 ▶sub p_undef {
Subroutine p_undef: called CC=1 SCAR=0.0
Pod documented
134 2 return $STATIC_PREDICATES{'undef'};
135 }
136  
137 31 ▶sub p_blessed {
Subroutine p_blessed: called CC=1 SCAR=0.0
Pod documented
138 31 return $STATIC_PREDICATES{'blessed'};
139 }
140  
141 11 ▶sub p_is_number {
Subroutine p_is_number: called CC=1 SCAR=0.0
Pod documented
142 11 return $STATIC_PREDICATES{'number'};
143 }
144  
145 3 ▶sub p_ref_type {
Subroutine p_ref_type: called CC=1 SCAR=0.0
Pod documented
146 3 my ($ref_type) = @_;
147 return Data::Predicate::ClosurePredicate->new(
148 10 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
149 10 my ($object) = @_;
150 10 my $ref = ref($object);
151 10 ▶ return 0 unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 4 6 10
152 6 ▶ return ( $ref eq $ref_type ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$ref eq $ref_type ? : 3 3 6
153 },
154 3 description => 'ref_type predicate with type '.$ref_type
155 );
156 }
157  
158 4 ▶sub p_isa {
Subroutine p_isa: called CC=1 SCAR=0.0
Pod documented
159 4 my ($isa) = @_;
160 return Data::Predicate::ClosurePredicate->new(
161 11 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
162 11 my ($object) = @_;
163 11 ▶ return 0 unless p_blessed()->apply($object);
Branch: 1 branch
BranchTrueFalseTotal
unless p_blessed()->apply($object) 7 4 11
164 4 ▶ return ( $object->isa($isa) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$object->isa($isa) ? : 3 1 4
165 },
166 4 description => 'isa predicate with type '.$isa
167 );
168 }
169  
170 14 ▶sub p_can {
Subroutine p_can: called CC=1 SCAR=0.0
Pod documented
171 14 my ($method) = @_;
172 return Data::Predicate::ClosurePredicate->new(
173 20 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
174 20 my ($object) = @_;
175 20 ▶ return 0 unless p_blessed()->apply($object);
Branch: 1 branch
BranchTrueFalseTotal
unless p_blessed()->apply($object) 5 15 20
176 15 ▶ return ( $object->can($method) ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$object->can($method) ? : 10 5 15
177 },
178 14 description => 'Can predicate with method '.$method
179 );
180 }
181  
182 2 ▶sub p_string_equals {
Subroutine p_string_equals: called CC=2 SCAR=6.9
Pod documented
183 2 my ($str, $method) = @_;
184 2 ▶ my $target = (defined $method) ? "against method ${method}" : 'against values';
Branch: 1 branch
BranchTrueFalseTotal
defined $method ? : 1 1 2
185 return Data::Predicate::ClosurePredicate->new(
186 8 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
187 8 my ($object) = @_;
188 8 my $val = _invoke($object, $method);
189 7 ▶ return ( p_defined->apply($val) && $val eq $str ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
p_defined()->apply($val) && $val eq $str ? : 2 5 7
Condition100%3/3
p_defined()->apply($val) and $val eq $str
!ll&&!rl&&r
142
MC/DC100%2/2
p_defined()->apply($val) and $val eq $str
p_defined()->apply($val) $val eq $str
Truth table100%3/3
A and B
A= p_defined()->apply($val)B= $val eq $str
ABresult
0X0
100
111
190 },
191 2 description => "String equals predicate $target with number $str"
192 );
193 }
194  
195 2 ▶sub p_numeric_equals {
Subroutine p_numeric_equals: called CC=2 SCAR=6.9
Pod documented
196 2 my ($number, $method) = @_;
197 2 ▶ my $target = (defined $method) ? "against method ${method}" : 'against values';
Branch: 1 branch
BranchTrueFalseTotal
defined $method ? : 1 1 2
198 return Data::Predicate::ClosurePredicate->new(
199 9 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
200 9 my ($object) = @_;
201 9 my $val = _invoke($object, $method);
202 8 ▶ return ( p_is_number->apply($val) && $val == $number ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
p_is_number()->apply($val) && $val == $number ? : 3 5 8
Condition100%3/3
p_is_number()->apply($val) and $val == $number
!ll&&!rl&&r
323
MC/DC100%2/2
p_is_number()->apply($val) and $val == $number
p_is_number()->apply($val) $val == $number
Truth table100%3/3
A and B
A= p_is_number()->apply($val)B= $val == $number
ABresult
0X0
100
111
203 },
204 2 description => "Numeric equals predicate $target with number $number"
205 );
206 }
207  
208 3 ▶sub p_regex {
Subroutine p_regex: called CC=2 SCAR=6.9
Pod documented
209 3 my ($regex, $method) = @_;
210 3 ▶ my $target = (defined $method) ? "against method ${method}" : 'against values';
Branch: 1 branch
BranchTrueFalseTotal
defined $method ? : 1 2 3
211 return Data::Predicate::ClosurePredicate->new(
212 14 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
213 14 my ($object) = @_;
214 14 my $val = _invoke($object, $method);
215 13 ▶ return ( p_defined->apply($val) && $val =~ $regex) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
p_defined()->apply($val) && $val =~ /$regex/ ? : 7 6 13
Condition100%3/3
p_defined()->apply($val) and $val =~ /$regex/
!ll&&!rl&&r
157
MC/DC100%2/2
p_defined()->apply($val) and $val =~ /$regex/
p_defined()->apply($val) $val =~ /$regex/
Truth table100%3/3
A and B
A= p_defined()->apply($val)B= $val =~ /$regex/
ABresult
0X0
100
111
216 },
217 3 description => "Regular expression predicate $target with regex $regex"
218 );
219 }
220  
221 2 ▶sub p_substring {
Subroutine p_substring: called CC=2 SCAR=6.9
Pod documented
222 2 my ($substring, $method) = @_;
223 2 ▶ my $target = (defined $method) ? "against method ${method}" : 'against values';
Branch: 1 branch
BranchTrueFalseTotal
defined $method ? : 1 1 2
224 return Data::Predicate::ClosurePredicate->new(
225 10 ▶ closure => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
226 10 my ($object) = @_;
227 10 my $val = _invoke($object, $method);
228 9 ▶ return ( p_defined->apply($val) && index($val, $substring) > -1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
p_defined()->apply($val) && index($val, $substring) != -1 ? : 4 5 9
Condition100%3/3
p_defined()->apply($val) and index($val, $substring) != -1
!ll&&!rl&&r
144
MC/DC100%2/2
p_defined()->apply($val) and index($val, $substring) != -1
p_defined()->apply($val) index($val, $substring) != -1
Truth table100%3/3
A and B
A= p_defined()->apply($val)B= index($val, $substring) != -1
ABresult
0X0
100
111
229 },
230 2 description => "Substring predicate $target with substring $substring"
231 );
232 }
233  
234 41 ▶sub _invoke {
Subroutine _invoke: called CC=3 SCAR=11.0
235 41 my ($object, $method) = @_;
236 41 my $val;
237 41 ▶ if($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 12 29 41
238 12 ▶ if(p_can($method)->apply($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (p_can($method)->apply($object)) { } 8 4 12
239 8 $val = $object->can($method)->($object);
240 }
241 else {
242 4 confess("Cannot call the method '${method}' on the given object ${object}");
243 }
244 }
245 else {
246 29 $val = $object;
247 }
248 37 return $val;
249 }
250  
251 7 1;
252 __END__
↑ Summary Predicate/ClosurePredicate.pm »