Sub/Meta/Returns.pm

filter: Statementstmt 100% 140 / 140 Branchbran 100% 108 / 108 Conditioncond 100% 55 / 55 MC/DCmcdc 100% 32 / 32 Subroutinesub 100% 29 / 29 Podpod 100% 20 / 20 totaltotal 100% 384 / 384 CC 77 SCAR 21.2
CC
77
Cov
100%
error_message
16.0
relaxed_error_message
13.0
is_same_interface
12.0
CRAP 8.4
↑ Summary Sub/Meta/Test.pm »
1 package Sub::Meta::Returns;
2 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 32 our $VERSION = "0.15";
7  
8 32 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use overload
11 32 ▶ fallback => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
12 eq => \&is_same_interface
13 32 ;
14  
15 248 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
16 248 my ($class, @args) = @_;
17 248 my $v = $args[0];
18 248 ▶ my %args = @args == 1 ? ref $v && ref $v eq 'HASH' ? %{$v}
Branch: 2 branches
BranchTrueFalseTotal
@args == 1 ? : 241 7 248
ref $v && ref $v eq 'HASH' ? : 58 183 241
Condition100%3/3
ref $v and ref $v eq "HASH"
!ll&&!rl&&r
1691458
MC/DC100%2/2
ref $v and ref $v eq "HASH"
ref $v ref $v eq "HASH"
Truth table100%3/3
A and B
A= ref $vB= ref $v eq "HASH"
ABresult
0X0
100
111
19 : ( scalar => $v, list => $v, void => $v )
20 : @args;
21  
22 248 return bless \%args => $class;
23 }
24  
25 377 ▶sub scalar() :method { my $self = shift; return $self->{scalar} } ## no critic (ProhibitBuiltinHomonyms)
Subroutine scalar: called CC=1 SCAR=0.0
Pod documented
26 198 ▶sub list() { my $self = shift; return $self->{list} }
Subroutine list: called CC=1 SCAR=0.0
Pod documented
27 189 ▶sub void() { my $self = shift; return $self->{void} }
Subroutine void: called CC=1 SCAR=0.0
Pod documented
28 40 ▶sub coerce() { my $self = shift; return $self->{coerce} }
Subroutine coerce: called CC=1 SCAR=0.0
Pod documented
29  
30 340 ▶sub has_scalar() { my $self = shift; return defined $self->{scalar} }
Subroutine has_scalar: called CC=1 SCAR=0.0
Pod documented
31 261 ▶sub has_list() { my $self = shift; return defined $self->{list} }
Subroutine has_list: called CC=1 SCAR=0.0
Pod documented
32 243 ▶sub has_void() { my $self = shift; return defined $self->{void} }
Subroutine has_void: called CC=1 SCAR=0.0
Pod documented
33 38 ▶sub has_coerce() { my $self = shift; return defined $self->{coerce} }
Subroutine has_coerce: called CC=1 SCAR=0.0
Pod documented
34  
35 1 ▶sub set_scalar { my ($self, $v) = @_; $self->{scalar} = $v; return $self }
Subroutine set_scalar: called CC=1 SCAR=0.0
Pod documented
36 1 ▶sub set_list { my ($self, $v) = @_; $self->{list} = $v; return $self }
Subroutine set_list: called CC=1 SCAR=0.0
Pod documented
37 1 ▶sub set_void { my ($self, $v) = @_; $self->{void} = $v; return $self }
Subroutine set_void: called CC=1 SCAR=0.0
Pod documented
38 1 ▶sub set_coerce { my ($self, $v) = @_; $self->{coerce} = $v; return $self }
Subroutine set_coerce: called CC=1 SCAR=0.0
Pod documented
39  
40 53 ▶sub is_same_interface {
Subroutine is_same_interface: called CC=12 SCAR=24.8
Pod documented
41 53 my ($self, $other) = @_;
42  
43 53 ▶ return unless Scalar::Util::blessed($other) && $other->isa('Sub::Meta::Returns');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa("Sub::Meta::Returns") 2 51 53
Condition100%3/3
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
!ll&&!rl&&r
1151
MC/DC100%2/2
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
builtin::blessed($other) $other->isa("Sub::Meta::Returns")
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa("Sub::Meta::Returns")
ABresult
0X0
100
111
44  
45 51 ▶ if ($self->has_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_scalar) { } 25 26 51
46 25 ▶ return unless _eq($self->scalar, $other->scalar)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->scalar, $other->scalar) 12 13 25
47 }
48 else {
49 26 ▶ return if $other->has_scalar
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_scalar 5 21 26
50 }
51  
52 34 ▶ if ($self->has_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_list) { } 9 25 34
53 9 ▶ return unless _eq($self->list, $other->list)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->list, $other->list) 1 8 9
54 }
55 else {
56 25 ▶ return if $other->has_list
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_list 2 23 25
57 }
58  
59 31 ▶ if ($self->has_void) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_void) { } 8 23 31
60 8 ▶ return unless _eq($self->void, $other->void)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->void, $other->void) 1 7 8
61 }
62 else {
63 23 ▶ return if $other->has_void
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_void 2 21 23
64 }
65  
66 28 return !!1;
67 }
68  
69 60 ▶sub is_relaxed_same_interface {
Subroutine is_relaxed_same_interface: called CC=9 SCAR=22.0
Pod documented
70 60 my ($self, $other) = @_;
71  
72 60 ▶ return unless Scalar::Util::blessed($other) && $other->isa('Sub::Meta::Returns');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa("Sub::Meta::Returns") 2 58 60
Condition100%3/3
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
!ll&&!rl&&r
1158
MC/DC100%2/2
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
builtin::blessed($other) $other->isa("Sub::Meta::Returns")
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa("Sub::Meta::Returns")
ABresult
0X0
100
111
73  
74 58 ▶ if ($self->has_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_scalar) 27 31 58
75 27 ▶ return unless _eq($self->scalar, $other->scalar)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->scalar, $other->scalar) 12 15 27
76 }
77  
78 46 ▶ if ($self->has_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_list) 12 34 46
79 12 ▶ return unless _eq($self->list, $other->list)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->list, $other->list) 1 11 12
80 }
81  
82 45 ▶ if ($self->has_void) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_void) 12 33 45
83 12 ▶ return unless _eq($self->void, $other->void)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->void, $other->void) 1 11 12
84 }
85  
86 44 return !!1;
87 }
88  
89 16 ▶sub is_same_interface_inlined {
Subroutine is_same_interface_inlined: called CC=4 SCAR=13.9
Pod documented
90 16 my ($self, $v) = @_;
91  
92 16 my @src;
93  
94 16 push @src => sprintf("Scalar::Util::blessed(%s) && %s->isa('Sub::Meta::Returns')", $v, $v);
95  
96 16 ▶ push @src => $self->has_scalar ? _eq_inlined($self->scalar, sprintf('%s->scalar', $v))
Branch: 1 branch
BranchTrueFalseTotal
$self->has_scalar ? : 7 9 16
97 : sprintf('!%s->has_scalar', $v);
98  
99 16 ▶ push @src => $self->has_list ? _eq_inlined($self->list, sprintf('%s->list', $v))
Branch: 1 branch
BranchTrueFalseTotal
$self->has_list ? : 5 11 16
100 : sprintf('!%s->has_list', $v);
101  
102 16 ▶ push @src => $self->has_void ? _eq_inlined($self->void, sprintf('%s->void', $v))
Branch: 1 branch
BranchTrueFalseTotal
$self->has_void ? : 5 11 16
103 : sprintf('!%s->has_void', $v);
104  
105 16 return join "\n && ", @src;
106 }
107  
108 20 ▶sub is_relaxed_same_interface_inlined {
Subroutine is_relaxed_same_interface_inlined: called CC=4 SCAR=13.9
Pod documented
109 20 my ($self, $v) = @_;
110  
111 20 my @src;
112  
113 20 push @src => sprintf("Scalar::Util::blessed(%s) && %s->isa('Sub::Meta::Returns')", $v, $v);
114  
115 20 ▶ push @src => _eq_inlined($self->scalar, sprintf('%s->scalar', $v)) if $self->has_scalar;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_scalar 9 11 20
116  
117 20 ▶ push @src => _eq_inlined($self->list, sprintf('%s->list', $v)) if $self->has_list;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_list 7 13 20
118  
119 20 ▶ push @src => _eq_inlined($self->void, sprintf('%s->void', $v)) if $self->has_void;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_void 7 13 20
120  
121 20 return join "\n && ", @src;
122 }
123  
124 160 ▶sub _eq {
Subroutine _eq: called CC=9 SCAR=22.0
125 160 my ($type, $other) = @_;
126  
127 160 ▶ if (ref $type && ref $type eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and ref $type eq 'ARRAY') { } 11 149 160
Condition100%3/3
ref $type and ref $type eq "ARRAY"
!ll&&!rl&&r
1252411
MC/DC100%2/2
ref $type and ref $type eq "ARRAY"
ref $type ref $type eq "ARRAY"
Truth table100%3/3
A and B
A= ref $typeB= ref $type eq "ARRAY"
ABresult
0X0
100
111
128 11 ▶ return unless ref $other eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other eq "ARRAY" 3 8 11
129 8 ▶ return unless @$type == @$other;
Branch: 1 branch
BranchTrueFalseTotal
unless @$type == @$other 4 4 8
130 4 ▶ for (my $i = 0; $i < @$type; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$type) 8 2 10
131 8 ▶ return unless $type->[$i] eq $other->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $type->[$i] eq $other->[$i] 2 6 8
132 }
133 }
134 else {
135 149 ▶ return unless defined $other && $type eq $other;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $other and $type eq $other 36 113 149
Condition100%3/3
defined $other and $type eq $other
!ll&&!rl&&r
1026113
MC/DC100%2/2
defined $other and $type eq $other
defined $other $type eq $other
Truth table100%3/3
A and B
A= defined $otherB= $type eq $other
ABresult
0X0
100
111
136 }
137 115 return 1;
138 }
139  
140 40 ▶sub _eq_inlined {
Subroutine _eq_inlined: called CC=4 SCAR=13.9
141 40 my ($type, $v) = @_;
142  
143 40 my @src;
144 40 ▶ if (ref $type && ref $type eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and ref $type eq 'ARRAY') { } 2 38 40
Condition100%3/3
ref $type and ref $type eq "ARRAY"
!ll&&!rl&&r
18202
MC/DC100%2/2
ref $type and ref $type eq "ARRAY"
ref $type ref $type eq "ARRAY"
Truth table100%3/3
A and B
A= ref $typeB= ref $type eq "ARRAY"
ABresult
0X0
100
111
145 2 push @src => sprintf('ref %s eq "ARRAY"', $v);
146 2 push @src => sprintf('%d == @{%s}', scalar @$type, $v);
147 2 ▶ for (my $i = 0; $i < @$type; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$type) 4 2 6
148 4 push @src => sprintf('"%s" eq %s->[%d]', $type->[$i], $v, $i);
149 }
150 }
151 else {
152 38 push @src => sprintf('defined %s && "%s" eq %s', $v, $type, $v);
153 }
154  
155 40 return join "\n && ", @src;
156 }
157  
158 22 ▶sub error_message {
Subroutine error_message: called CC=16 SCAR=27.7
Pod documented
159 22 my ($self, $other) = @_;
160  
161 22 ▶ return sprintf('other returns must be Sub::Meta::Returns. got: %s', $other // 'Undef')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa("Sub::Meta::Returns") 2 20 22
Condition100%5/5
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
!ll&&!rl&&r
1120
$other // "Undef"
l!l
11
MC/DC100%2/2
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
builtin::blessed($other) $other->isa("Sub::Meta::Returns")
MC/DC100%1/1
$other // "Undef"
$other
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa("Sub::Meta::Returns")
ABresult
0X0
100
111
Truth table100%2/2
A
A= $other
Aresult
11
00
162 unless Scalar::Util::blessed($other) && $other->isa('Sub::Meta::Returns');
163  
164 20 ▶ if ($self->has_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_scalar) { } 7 13 20
165 7 ▶ return sprintf('invalid scalar return. got: %s, expected: %s', $other->scalar // '', $self->scalar)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->scalar, $other->scalar) 3 4 7
Condition100%2/2
$other->scalar // ""
l!l
21
MC/DC100%1/1
$other->scalar // ""
$other->scalar
Truth table100%2/2
A
A= $other->scalar
Aresult
11
00
166 unless _eq($self->scalar, $other->scalar);
167 }
168 else {
169 13 ▶ return 'should not have scalar return' if $other->has_scalar;
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_scalar 3 10 13
170 }
171  
172 14 ▶ if ($self->has_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_list) { } 5 9 14
173 5 ▶ return sprintf('invalid list return. got: %s, expected: %s', $other->list // '', $self->list)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->list, $other->list) 2 3 5
Condition100%2/2
$other->list // ""
l!l
11
MC/DC100%1/1
$other->list // ""
$other->list
Truth table100%2/2
A
A= $other->list
Aresult
11
00
174 unless _eq($self->list, $other->list);
175 }
176 else {
177 9 ▶ return 'should not have list return' if $other->has_list;
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_list 2 7 9
178 }
179  
180 10 ▶ if ($self->has_void) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_void) { } 4 6 10
181 4 ▶ return sprintf('invalid void return. got: %s, expected: %s', $other->void // '', $self->void)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->void, $other->void) 2 2 4
Condition100%2/2
$other->void // ""
l!l
11
MC/DC100%1/1
$other->void // ""
$other->void
Truth table100%2/2
A
A= $other->void
Aresult
11
00
182 unless _eq($self->void, $other->void);
183 }
184 else {
185 6 ▶ return 'should not have void return' if $other->has_void;
Branch: 1 branch
BranchTrueFalseTotal
if $other->has_void 2 4 6
186 }
187 6 return '';
188 }
189  
190 21 ▶sub relaxed_error_message {
Subroutine relaxed_error_message: called CC=13 SCAR=25.6
Pod documented
191 21 my ($self, $other) = @_;
192  
193 21 ▶ return sprintf('other returns must be Sub::Meta::Returns. got: %s', $other // 'Undef')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa("Sub::Meta::Returns") 2 19 21
Condition100%5/5
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
!ll&&!rl&&r
1119
$other // "Undef"
l!l
11
MC/DC100%2/2
builtin::blessed($other) and $other->isa("Sub::Meta::Returns")
builtin::blessed($other) $other->isa("Sub::Meta::Returns")
MC/DC100%1/1
$other // "Undef"
$other
Truth table100%3/3
A and B
A= builtin::blessed($other)B= $other->isa("Sub::Meta::Returns")
ABresult
0X0
100
111
Truth table100%2/2
A
A= $other
Aresult
11
00
194 unless Scalar::Util::blessed($other) && $other->isa('Sub::Meta::Returns');
195  
196 19 ▶ if ($self->has_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_scalar) 7 12 19
197 7 ▶ return sprintf('invalid scalar return. got: %s, expected: %s', $other->scalar // '', $self->scalar)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->scalar, $other->scalar) 3 4 7
Condition100%2/2
$other->scalar // ""
l!l
21
MC/DC100%1/1
$other->scalar // ""
$other->scalar
Truth table100%2/2
A
A= $other->scalar
Aresult
11
00
198 unless _eq($self->scalar, $other->scalar);
199 }
200  
201 16 ▶ if ($self->has_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_list) 6 10 16
202 6 ▶ return sprintf('invalid list return. got: %s, expected: %s', $other->list // '', $self->list)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->list, $other->list) 2 4 6
Condition100%2/2
$other->list // ""
l!l
11
MC/DC100%1/1
$other->list // ""
$other->list
Truth table100%2/2
A
A= $other->list
Aresult
11
00
203 unless _eq($self->list, $other->list);
204 }
205  
206 14 ▶ if ($self->has_void) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_void) 6 8 14
207 6 ▶ return sprintf('invalid void return. got: %s, expected: %s', $other->void // '', $self->void)
Branch: 1 branch
BranchTrueFalseTotal
unless _eq($self->void, $other->void) 2 4 6
Condition100%2/2
$other->void // ""
l!l
11
MC/DC100%1/1
$other->void // ""
$other->void
Truth table100%2/2
A
A= $other->void
Aresult
11
00
208 unless _eq($self->void, $other->void);
209 }
210  
211 12 return '';
212 }
213  
214 19 ▶sub _all_eq {
Subroutine _all_eq: called CC=3 SCAR=11.0
215 19 my $self = shift;
216 19 ▶ return $self->has_scalar
Condition100%6/6
$self->has_scalar && _eq($self->scalar, $self->list) && _eq($self->scalar, $self->void)
!ll&&!rl&&r
4114
$self->has_scalar and _eq($self->scalar, $self->list)
!ll&&!rl&&r
2215
MC/DC100%3/3
$self->has_scalar && _eq($self->scalar, $self->list) && _eq($self->scalar, $self->void)
$self->has_scalar _eq($self->scalar, $self->list) _eq($self->scalar, $self->void)
Truth table100%4/4
A and B && C
A= $self->has_scalarB= _eq($self->scalar, $self->list)C= _eq($self->scalar, $self->void)
ABCresult
0XX0
10X0
1100
1111
217 && _eq($self->scalar, $self->list)
218 && _eq($self->scalar, $self->void);
219 }
220  
221 22 ▶sub _display {
Subroutine _display: called CC=3 SCAR=11.0
222 22 my $type = shift;
223  
224 22 ▶ if (ref $type && ref $type eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type and ref $type eq 'ARRAY') { } 1 21 22
Condition100%3/3
ref $type and ref $type eq "ARRAY"
!ll&&!rl&&r
1471
MC/DC100%2/2
ref $type and ref $type eq "ARRAY"
ref $type ref $type eq "ARRAY"
Truth table100%3/3
A and B
A= ref $typeB= ref $type eq "ARRAY"
ABresult
0X0
100
111
225 1 return sprintf('[%s]', join ",", map { $_ . '' } @$type);
226 }
227 else {
228 21 return $type . '';
229 }
230 }
231  
232 36 ▶sub display {
Subroutine display: called CC=7 SCAR=19.5
Pod documented
233 36 my $self = shift;
234  
235 36 ▶ if (!$self->has_scalar && !$self->has_list && !$self->has_void) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->has_scalar || $self->has_list || $self->has_void) { } 17 19 36
elsif (_all_eq($self)) { } 14 5 19
Condition100%6/6
$self->has_scalar || $self->has_list || $self->has_void
l!l&&r!l&&!r
18117
$self->has_scalar or $self->has_list
l!l&&r!l&&!r
17118
MC/DC100%3/3
$self->has_scalar || $self->has_list || $self->has_void
$self->has_scalar $self->has_list $self->has_void
Truth table100%4/4
A or B || C
A= $self->has_scalarB= $self->has_listC= $self->has_void
ABCresult
1XX1
01X1
0011
0000
236 17 return '*';
237 }
238 elsif (_all_eq($self)) {
239 14 return _display($self->scalar);
240 }
241 else {
242 5 my @r;
243 5 for my $key (qw(scalar list void)) {
244 15 my $has = "has_$key";
245 15 ▶ push @r => "$key => @{[_display($self->$key)]}" if $self->$has;
Branch: 1 branch
BranchTrueFalseTotal
if $self->$has 8 7 15
246 }
247 5 return "(@{[join ', ', @r]})";
248 }
249 }
250  
251 32 1;
252 __END__
↑ Summary Sub/Meta/Test.pm »