Pulp/Pulper.pm

filter: Statementstmt 84.0% 105 / 125 Branchbran 73.3% 44 / 60 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 6 / 6 Subroutinesub 80.0% 20 / 25 Podpod 0.0% 0 / 14 totaltotal 77.2% 187 / 242 CC 36 SCAR 19.4
CC
36
Cov
82%
parse
12.6
match
11.0
pulp
10.2
CRAP 7.0
↑ Summary Pulp.pm »
1 package Data::Pulp::Pulper;
2  
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use MooseX::AttributeHelpers;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Data::Pulp::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶sub parse {
Subroutine parse: called CC=12 SCAR=25.3
Pod undocumented
8 5 my $class = shift;
9  
10 5 my ( @rule, @case, $in_case, $empty_then, $nil_then, $default_then );
11 5 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 48 5 53
12 48 my $token = shift;
13 48 ▶ if ( $token eq 'case' || $token eq 'if_type' || $token eq 'if_value' || $token eq 'if_object' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($token eq 'case' or $token eq 'if_type' or $token eq 'if_value' or $token eq 'if_object') { } 20 28 48
elsif ($in_case) { } 16 12 28
elsif ($token eq 'empty') { } 4 8 12
elsif ($token eq 'nil') { } 4 4 8
elsif ($token eq 'default') { } 4 0 4
elsif ($token eq 'then') { } 0 0 0
Condition100%9/9
$token eq "case" or $token eq "if_type" or $token eq "if_value" or $token eq "if_object"
l!l&&r!l&&!r
19128
$token eq "case" or $token eq "if_type" or $token eq "if_value"
l!l&&r!l&&!r
18129
$token eq "case" or $token eq "if_type"
l!l&&r!l&&!r
17130
MC/DC100%4/4
$token eq "case" or $token eq "if_type" or $token eq "if_value" or $token eq "if_object"
$token eq "case" $token eq "if_type" $token eq "if_value" $token eq "if_object"
Truth table100%5/5
A or B or C or D
A= $token eq "case"B= $token eq "if_type"C= $token eq "if_value"D= $token eq "if_object"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
14 20 $in_case = 1;
15 20 push @case, [ $token, shift ];
16 }
17 elsif ( $in_case ) {
18 16 ▶ if ( $token eq 'then' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq 'then') { } 16 0 16
19 16 my $then = shift;
20 16 push @rule, map { Data::Pulp::Rule->new( kind => $_->[0], matcher => $_->[1], then => $then ) } @case;
21 16 $in_case = 0;
22 }
23 else {
24 0 croak "Unrecognized token in case: $token";
25 }
26 }
27 elsif ( $token eq 'empty' ) {
28 4 $empty_then = shift;
29 }
30 elsif ( $token eq 'nil' ) {
31 4 $nil_then = shift;
32 }
33 elsif ( $token eq 'default' ) {
34 4 $default_then = shift;
35 }
36 elsif ( $token eq 'then' ) {
37 0 croak "Then without opening case ($token)";
38 }
39 else {
40 0 croak "Unrecognized token in case: $token";
41 }
42 }
43  
44 5 return __PACKAGE__->new( rule_list => \@rule, empty_then => $empty_then, nil_then => $nil_then, default_then => $default_then );
45 }
46  
47 6 has [qw/ empty_then nil_then default_then /] => qw/is ro isa Maybe[CodeRef]/;
48 0 ▶has rule_list => qw/metaclass Collection::Array reader _rule_list isa ArrayRef/, default => sub { [] }, provides => {qw/
Subroutine __ANON__: not called CC=1 SCAR=1.2
49 elements rule_list
50 /};
51  
52 47 ▶sub pulp {
Subroutine pulp: called CC=10 SCAR=23.2
Pod undocumented
53 47 my $self = shift;
54 47 my $value = shift;
55  
56 47 my $then;
57 47 ▶ if ( defined $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 42 5 47
elsif ($then = $self->nil_then) { } 5 0 5
58 42 ▶ if ( ref $value || length $value ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value or length $value) { } 37 5 42
elsif ($then = $self->empty_then) { } 5 0 5
Condition100%3/3
ref $value or length $value
l!l&&r!l&&!r
4335
MC/DC100%2/2
ref $value or length $value
ref $value length $value
Truth table100%3/3
A or B
A= ref $valueB= length $value
ABresult
1X1
011
000
59 37 for my $rule ( $self->rule_list ) {
60 225 ▶ if ( $rule->match( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->match($value)) 31 194 225
61 31 $then = $rule->then;
62 31 last;
63 }
64 }
65 }
66 elsif ( $then = $self->empty_then ) {
67 }
68 }
69 elsif ( $then = $self->nil_then ) {
70 }
71  
72 47 ▶ $then = $self->default_then unless $then;
Branch: 1 branch
BranchTrueFalseTotal
unless $then 6 41 47
73  
74 47 ▶ if ( $then ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($then) 47 0 47
75 47 local $_ = $value;
76 47 return $then->( $_ );
77 }
78  
79 0 return $value; # Unmolested
80 }
81  
82 2 ▶sub prepare {
Subroutine prepare: called CC=1 SCAR=0.0
Pod undocumented
83 2 my $self = shift;
84 2 return Data::Pulp::Set->new( pulper => $self, source => shift );
85 }
86  
87 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod undocumented
88 0 return shift->prepare( @_ );
89 }
90  
91 package Data::Pulp::Set;
92  
93 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 6 ▶use Data::Pulp::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 6 ▶use List::Enumerator qw/E/;
Subroutine BEGIN: called CC=1 SCAR=0.0
97  
98 6 has pulper => qw/is ro required 1 isa Data::Pulp::Pulper/;
99 6 has source => qw/is ro/;
100 6 has _list => qw/is ro lazy_build 1/, handles => [qw/ is_empty /];
101 3 ▶sub _build__list {
Subroutine _build__list: called CC=3 SCAR=11.2
102 3 my $self = shift;
103  
104 3 my $source = $self->source;
105 3 my @list;
106 3 ▶ if ( ref $source eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $source eq 'ARRAY') { } 2 1 3
elsif (ref $source eq 'HASH') { } 0 1 1
107 2 my $count = 0;
108 2 @list = map { [ $count++, $_ ] } @$source;
109 }
110 elsif ( ref $source eq 'HASH' ) {
111 0 @list = map { [ $_, $source->{$_} ] } keys %$source;
112 }
113 else {
114 1 @list = ( [ undef, $source ] );
115 }
116  
117 3 return E \@list;
118 }
119  
120 35 ▶sub pulp_value {
Subroutine pulp_value: called CC=1 SCAR=0.0
Pod undocumented
121 35 my $self = shift;
122 35 my $value = shift;
123 35 return $self->pulper->pulp( $value );
124 }
125  
126 35 ▶sub pulp_pair {
Subroutine pulp_pair: called CC=1 SCAR=0.0
Pod undocumented
127 35 my $self = shift;
128 35 my $pair = shift;
129 35 return $self->pulp_value( $pair->[1] );
130 }
131  
132 2 ▶sub all {
Subroutine all: called CC=1 SCAR=0.0
Pod undocumented
133 2 my $self = shift;
134 2 ▶ return $self->_list->map( sub { $self->pulp_pair( $_ ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
135 }
136  
137 0 ▶sub pulp {
Subroutine pulp: not called CC=1 SCAR=6.9
Pod undocumented
138 0 return shift->first( @_ );
139 }
140  
141 1 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
142 1 return shift->first( @_ );
143 }
144  
145 1 ▶sub first {
Subroutine first: called CC=2 SCAR=7.1
Pod undocumented
146 1 my $self = shift;
147 1 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 1 1
148 1 return $self->pulp_pair( $self->_list->first );
149 }
150  
151 0 ▶sub last {
Subroutine last: not called CC=2 SCAR=17.9
Pod undocumented
152 0 my $self = shift;
153 0 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 0 0
154 0 return $self->pulp_pair( $self->_list->last );
155 }
156  
157 19 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod undocumented
158 19 my $self = shift;
159 19 ▶ return if $self->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_empty 0 19 19
160 19 my $pair;
161 19 eval {
162 19 $pair = $self->_list->next;
163 };
164 19 ▶ return unless $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless $pair 2 17 19
165 17 return $self->pulp_pair( $pair );
166 }
167  
168 package Data::Pulp::Rule;
169  
170 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 6 ▶use Data::Pulp::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
172  
173 6 has kind => qw/is ro required 1 isa Str/;
174 6 has matcher => qw/is ro required 1 isa Str|CodeRef|RegexpRef/;
175 6 has then => qw/is ro required 1 isa CodeRef/;
176  
177 225 ▶sub match {
Subroutine match: called CC=10 SCAR=23.9
Pod undocumented
178 225 my $self = shift;
179 225 my $value = shift;
180  
181 225 my $matcher = $self->matcher;
182 225 my $kind = $self->kind;
183  
184 225 ▶ if ($kind eq 'case') {
Branch: 4 branches
BranchTrueFalseTotal
if ($kind eq 'case') { } 215 10 225
elsif ($kind eq 'if_value') { } 1 9 10
elsif ($kind eq 'if_type') { } 3 6 9
elsif ($kind eq 'if_object') { } 6 0 6
185 }
186 elsif ($kind eq 'if_value') {
187 1 ▶ return unless ! ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 0 1 1
188 }
189 elsif ($kind eq 'if_type') {
190 3 $value = ref $value;
191 }
192 elsif ($kind eq 'if_object') {
193 6 ▶ return unless blessed $value;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 5 1 6
194 }
195 else {
196 0 croak "Don't know how to match kind \"$kind\"";
197 }
198  
199 220 ▶ if ( ref $matcher eq 'CODE' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $matcher eq 'CODE') { } 133 87 220
elsif (ref $matcher eq 'Regexp') { } 0 87 87
elsif (not ref $matcher) { } 87 0 87
200 133 local $_ = $value;
201 133 return $matcher->( $value );
202 }
203 elsif ( ref $matcher eq 'Regexp' ) { # Meh, not really used
204 0 return $value =~ $matcher;
205 }
206 elsif ( ! ref $matcher ) { # Meh, not really used
207 87 return $value eq $matcher;
208 }
209 else {
210 0 croak "Don't understand matcher \"$matcher\"";
211 }
212 }
213  
214 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my $self = shift;
216 0 my $value = shift;
217
218 {
219 0 local $_ = $value;
220 0 return $self->then->( $value );
221 }
222 }
223  
224 6 1;
↑ Summary Pulp.pm »