blib/lib/SmartMatch/Sugar.pm

filter: Statementstmt 90.0% 99 / 110 Branchbran 47.0% 16 / 34 Conditioncond 26.6% 12 / 45 MC/DCmcdc 7.6% 2 / 26 Subroutinesub 86.0% 43 / 50 Podpod 100% 7 / 7 totaltotal 65.8% 179 / 272 CC 33 SCAR 15.3
CC
33
Cov
67%
__ANON__
20.0
__ANON__
12.0
__ANON__
12.0
CRAP 4.6
↑ Summary
1 #!/usr/bin/perl
2  
3 package SmartMatch::Sugar;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Class::Inspector ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = "0.04";
13  
14 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 exports => [qw(
16 any none
17  
18 object class inv_isa inv_can inv_does
19  
20 overloaded stringifies
21  
22 array array_length_is non_empty_array even_sized_array
23  
24 hash hash_size_is non_empty_hash
25  
26 non_ref string_length_is non_empty_string
27  
28 match
29 )],
30 groups => {
31 default => [ -all ],
32 base => [ qw/any none/ ],
33 object => [ qw/object class inv_isa inv_can inv_does/ ],
34 overload => [ qw/overloaded stringifies/ ],
35 array => [ qw/array array_length_is non_empty_array even_sized_array/ ],
36 hash => [ qw/hash hash_size_is non_empty_hash/ ],
37 string => [ qw/non_ref string_length_is non_empty_string/ ],
38 match => [ qw/match/ ],
39 },
40 1 };
41  
42 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 {
45 package SmartMatch::Sugar::Overloaded;
46 1 ▶ use overload '~~' => sub { $_[0]->(@_) };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
47 }
48  
49 36 ▶sub match (&) { bless $_[0], "SmartMatch::Sugar::Overloaded" }
Subroutine match: called CC=1 SCAR=0.0
Pod documented
50  
51 1 ▶use constant any => match { not(not(1)) };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
52 1 ▶use constant none => match { not(not(0)) };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 1 ▶use constant non_empty_string => match {
Subroutine BEGIN: called CC=1 SCAR=2.6
Subroutine __ANON__: not called CC=3 SCAR=24.8
55 0 ▶ defined($_[1])
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and not ref $_[1] and length $_[1] 0 0 0
Condition0%0/3
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] and not ref $_[1]
defined $_[1] not ref $_[1]
Truth table0%0/3
A and not(B)
A= defined $_[1]B= not ref $_[1]
ABresult
0X0
100
111
56 and
57 not ref($_[1])
58 and
59 length($_[1])
60 1 };
61  
62 0 ▶sub string_length_is ($) {
Subroutine string_length_is: not called CC=1 SCAR=6.9
Pod documented
63 0 my $length = _length(shift);
64  
65 0 ▶ return match {
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 ▶ defined($_[1])
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and not ref $_[1] and length $_[1] == $length 0 0 0
Condition0%0/3
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[1] and not ref $_[1]
defined $_[1] not ref $_[1]
Truth table0%0/3
A and not(B)
A= defined $_[1]B= not ref $_[1]
ABresult
0X0
100
111
67 and
68 not ref($_[1])
69 and
70 length($_[1]) == $length
71 }
72 0 }
73  
74 1 ▶use constant non_ref => match {
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=2 SCAR=17.9
75 0 ▶ defined($_[1])
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and not ref $_[1] 0 0 0
76 and
77 not ref($_[1])
78 1 };
79  
80 1 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 ▶use constant overloaded => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
82 3 ▶ blessed($_[1])
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) and overload::Overloaded($_[1]) 2 1 3
83 and
84 overload::Overloaded($_[1]);
85 1 };
86  
87 1 ▶use constant stringifies => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.6
88 1 ▶ blessed($_[1])
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) and overload::OverloadedStringify($_[1]) 1 0 1
89 and
90 overload::OverloadedStringify($_[1]);
91 1 };
92  
93 1 ▶use constant object => match { blessed($_[1]) };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
94  
95 1 ▶use constant class => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=6.9
96 4 ▶ not ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
not ref $_[1] and "Class::Inspector"->loaded($_[1]) 2 2 4
97 and
98 Class::Inspector->loaded($_[1])
99 1 };
100  
101 0 ▶sub inv_does ($) {
Subroutine inv_does: not called CC=1 SCAR=6.9
Pod documented
102 0 my $role = shift;
103  
104 0 ▶ return match {
Subroutine __ANON__: not called CC=4 SCAR=30.0
105 0 ▶ blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) || defined $_[1] && !ref($_[1]) and $_[1]->DOES($role) 0 0 0
Condition0%0/6
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
l!l&&r!l&&!r
000
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
builtin::blessed($_[1]) defined $_[1] not ref $_[1]
Truth table0%0/4
A or B and not(C)
A= builtin::blessed($_[1])B= defined $_[1]C= not ref $_[1]
ABCresult
1XX1
0111
00X0
0100
106 and
107 $_[1]->DOES($role);
108 }
109 0 }
110  
111 6 ▶sub inv_isa ($) {
Subroutine inv_isa: called CC=1 SCAR=1.5
Pod documented
112 6 my $class = shift;
113 6 ▶ return match {
Subroutine __ANON__: called CC=4 SCAR=21.7
114 6 ▶ blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) || defined $_[1] && !ref($_[1]) and $_[1]->isa($class) 6 0 6
Condition16%1/6
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
l!l&&r!l&&!r
600
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
builtin::blessed($_[1]) defined $_[1] not ref $_[1]
Truth table25%1/4
A or B and not(C)
A= builtin::blessed($_[1])B= defined $_[1]C= not ref $_[1]
ABCresult
1XX1
0111
00X0
0100
115 and
116 $_[1]->isa($class);
117 }
118 6 }
119  
120 7 ▶sub inv_can ($) {
Subroutine inv_can: called CC=1 SCAR=0.5
Pod documented
121 7 my $method = shift;
122 7 ▶ return match {
Subroutine __ANON__: called CC=4 SCAR=16.9
123 7 ▶ blessed($_[1]) || ( defined($_[1]) && not(ref($_[1])) )
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[1]) || defined $_[1] && !ref($_[1]) and $_[1]->can($method) 7 0 7
Condition50%3/6
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
l!l&&r!l&&!r
610
defined $_[1] and not ref $_[1]
!ll&&!rl&&r
001
MC/DC0%0/3
builtin::blessed($_[1]) or defined $_[1] and not ref $_[1]
builtin::blessed($_[1]) defined $_[1] not ref $_[1]
Truth table50%2/4
A or B and not(C)
A= builtin::blessed($_[1])B= defined $_[1]C= not ref $_[1]
ABCresult
1XX1
0111
00X0
0100
124 and
125 $_[1]->can($method);
126 }
127 7 }
128 1 ▶use constant array => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.6
129 5 ▶ ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "ARRAY" 5 0 5
130 and
131 ref($_[1]) eq 'ARRAY'
132 1 };
133  
134 1 ▶use constant hash => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=2 SCAR=7.6
135 5 ▶ ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "HASH" 5 0 5
136 and
137 ref($_[1]) eq 'HASH'
138 1 };
139  
140 1 ▶use constant non_empty_array => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.1
141 ref($_[1])
142 and
143 ref($_[1]) eq 'ARRAY'
144 and
145 4 ▶ scalar(@{ $_[1] })
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "ARRAY" and scalar @{$_[1];} 2 2 4
Condition66%2/3
ref $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
022
MC/DC50%1/2
ref $_[1] and ref $_[1] eq "ARRAY"
ref $_[1] ref $_[1] eq "ARRAY"
Truth table66%2/3
A and B
A= ref $_[1]B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
146 1 };
147  
148 1 ▶use constant non_empty_hash => match {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.1
149 ref($_[1])
150 and
151 ref($_[1]) eq 'HASH'
152 and
153 4 ▶ scalar(keys %{ $_[1] });
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "HASH" and scalar keys %{$_[1];} 1 3 4
Condition66%2/3
ref $_[1] and ref $_[1] eq "HASH"
!ll&&!rl&&r
031
MC/DC50%1/2
ref $_[1] and ref $_[1] eq "HASH"
ref $_[1] ref $_[1] eq "HASH"
Truth table66%2/3
A and B
A= ref $_[1]B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
154 1 };
155  
156 1 ▶use constant even_sized_array => match {
Subroutine BEGIN: called CC=1 SCAR=2.9
Subroutine __ANON__: not called CC=3 SCAR=24.8
157 ref($_[1])
158 and
159 ref($_[1]) eq 'ARRAY'
160 and
161 0 ▶ scalar(@{$_[1]}) % 2 == 0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "ARRAY" and scalar(@{$_[1];}) % 2 == 0 0 0 0
Condition0%0/3
ref $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[1] and ref $_[1] eq "ARRAY"
ref $_[1] ref $_[1] eq "ARRAY"
Truth table0%0/3
A and B
A= ref $_[1]B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
162 1 };
163  
164 6 ▶sub array_length_is ($) {
Subroutine array_length_is: called CC=1 SCAR=0.4
Pod documented
165 6 my $length = _length(shift);
166  
167 6 ▶ return match {
Subroutine __ANON__: called CC=3 SCAR=13.1
168 ref($_[1])
169 and
170 ref($_[1]) eq 'ARRAY'
171 and
172 6 ▶ scalar(@{$_[1]}) == $length
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "ARRAY" and scalar @{$_[1];} == $length 6 0 6
Condition33%1/3
ref $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
006
MC/DC0%0/2
ref $_[1] and ref $_[1] eq "ARRAY"
ref $_[1] ref $_[1] eq "ARRAY"
Truth table33%1/3
A and B
A= ref $_[1]B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
173 6 };
174 }
175  
176 4 ▶sub hash_size_is ($) {
Subroutine hash_size_is: called CC=1 SCAR=0.4
Pod documented
177 4 my $length = _length(shift);
178  
179 4 ▶ return match {
Subroutine __ANON__: called CC=3 SCAR=13.1
180 ref($_[1])
181 and
182 ref($_[1]) eq 'HASH'
183 and
184 4 ▶ scalar(keys %{$_[1]}) == $length
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] and ref $_[1] eq "HASH" and scalar keys %{$_[1];} == $length 4 0 4
Condition33%1/3
ref $_[1] and ref $_[1] eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $_[1] and ref $_[1] eq "HASH"
ref $_[1] ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= ref $_[1]B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
185 4 };
186 }
187  
188 10 ▶sub _length ($) {
Subroutine _length: called CC=4 SCAR=17.9
189 10 my $length = shift;
190  
191 10 ▶ unless ( looks_like_number($length) and $length >= 0 and int($length) == $length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&looks_like_number($length) and $length >= 0 and int $length == $length) 0 10 10
Condition33%2/6
&looks_like_number($length) and $length >= 0 and int $length == $length
!ll&&!rl&&r
0010
&looks_like_number($length) and $length >= 0
!ll&&!rl&&r
0010
MC/DC0%0/3
&looks_like_number($length) and $length >= 0 and int $length == $length
&looks_like_number($length) $length >= 0 int $length == $length
Truth table25%1/4
A and B and C
A= &looks_like_number($length)B= $length >= 0C= int $length == $length
ABCresult
0XX0
10X0
1100
1111
192 0 croak "Length is not a positive integer";
193 }
194  
195 10 return int $length;
196 }
197  
198 1 __PACKAGE__
199  
200 __END__
↑ Summary