Unexpected/Functions.pm

filter: Statementstmt 99.1% 118 / 119 Branchbran 97.2% 70 / 72 Conditioncond 86.0% 36 / 43 (1 uncoverable) MC/DCmcdc 73.0% 17 / 26 (2 uncoverable) Subroutinesub 100% 31 / 31 Podpod 100% 11 / 11 totaltotal 94.7% 283 / 302 (3 uncoverable) CC 53 SCAR 17.2
CC
53
Cov
96%
parse_arg_list
10.0
is_class_loaded
10.0
__ANON__
9.6
CRAP 5.6
« Unexpected/TraitFor/TracingStacks.pm ↑ Summary Unexpected/TraitFor/StringifyingError.pm »
1 package Unexpected::Functions;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use parent 'Exporter::Tiny';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Sub::Install qw( install_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our @EXPORT_OK = qw( catch_class exception has_exception
13 inflate_message inflate_placeholders is_class_loaded
14 is_one_of_us parse_arg_list throw throw_on_error );
15  
16 4 my $Exception_Class = 'Unexpected'; my $Should_Quote = 1;
17  
18 # Private functions
19 10 ▶my $_catch = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 10 my $block = shift; return ((bless \$block, 'Try::Tiny::Catch'), @_);
21 4 };
22  
23 2 ▶my $_clone_one_of_us = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
24 2 ▶ return $_[ 1 ] ? { %{ $_[ 0 ] }, %{ $_[ 1 ] } } : { error => $_[ 0 ] };
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 1 2
25 4 };
26  
27 3 ▶my $_dereference_code = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 3 my ($code, @args) = @_;
29  
30 3 ▶ $args[ 0 ] and ref $args[ 0 ] eq 'ARRAY' and unshift @args, 'args';
Branch: 1 branch
BranchTrueFalseTotal
$args[0] and ref $args[0] eq "ARRAY" and unshift @args, "args" 1 2 3
Condition100%3/3
$args[0] and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
111
MC/DC100%2/2
$args[0] and ref $args[0] eq "ARRAY"
$args[0] ref $args[0] eq "ARRAY"
Truth table100%3/3
A and B
A= $args[0]B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
31  
32 3 return { class => $code->(), @args };
33 4 };
34  
35 4 my $_exception_class_cache = {};
36  
37 28 ▶my $_exception_class = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
38 28 my $caller = shift;
39  
40 exists $_exception_class_cache->{ $caller }
41 and defined $_exception_class_cache->{ $caller }
42 28 ▶ and return $_exception_class_cache->{ $caller };
Branch: 1 branch
BranchTrueFalseTotal
exists $_exception_class_cache->{$caller} and defined $_exception_class_cache->{$caller} and return $_exception_class_cache->{$caller} 10 18 28
Condition66%2/3
exists $_exception_class_cache->{$caller} and defined $_exception_class_cache->{$caller}
!ll&&!rl&&r
18010
MC/DC50%1/2
exists $_exception_class_cache->{$caller} and defined $_exception_class_cache->{$caller}
exists $_exception_class_cache->{$caller} defined $_exception_class_cache->{$caller}
Truth table66%2/3
A and B
A= exists $_exception_class_cache->{$caller}B= defined $_exception_class_cache->{$caller}
ABresult
0X0
100
111
43  
44 18 my $code = $caller->can( 'EXCEPTION_CLASS' );
45 18 ▶ my $class = $code ? $code->() : $Exception_Class;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 2 16 18
46  
47 18 return $_exception_class_cache->{ $caller } = $class;
48 4 };
49  
50 13 ▶my $_match_class = sub {
Subroutine __ANON__: called CC=9 SCAR=22.7
51 13 my ($e, $ref, $blessed, $does, $key) = @_;
52  
53 13 ▶ return !defined $key ? !defined $e
Branch: 6 branches
BranchTrueFalseTotal
!defined($key) ? : 1 12 13
$key eq '*' ? : 3 9 12
$key eq ':str' ? : 1 8 9
$key eq $ref ? : 1 7 8
$blessed && $e->can('instance_of') ? : 6 1 7
$blessed && $e->$does($key) ? : 0 1 1
Condition50%3/6
$blessed and $e->can("instance_of")
!ll&&!rl&&r
106
$blessed and $e->$does($key)
!ll&&!rl&&r
100
MC/DC50%1/2
$blessed and $e->can("instance_of")
$blessed $e->can("instance_of")
MC/DC0%0/2
$blessed and $e->$does($key)
$blessed $e->$does($key)
Truth table66%2/3
A and B
A= $blessedB= $e->can("instance_of")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $blessedB= $e->$does($key)
ABresult
0X0
100
111
54 : $key eq '*' ? 1
55 : $key eq ':str' ? !$ref
56 : $key eq $ref ? 1
57 : $blessed && $e->can( 'instance_of' ) ? $e->instance_of( $key )
58 : $blessed && $e->$does( $key ) ? 1
59 : 0;
60 4 };
61  
62 495 ▶my $_quote_maybe = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
63 495 ▶ return $Should_Quote ? "'".$_[ 0 ]."'" : $_[ 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$Should_Quote ? : 463 32 495
64 4 };
65  
66 11 ▶my $_gen_checker = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 11 my @prototable = @_;
68  
69 10 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
70 10 my $e = shift;
71 10 my $ref = ref $e;
72 10 my $blessed = blessed $e;
73 10 ▶ my $does = ($blessed && $e->can( 'DOES' )) || 'isa';
Condition80%4/5
$blessed && $e->can('DOES') || 'isa'
l!l
64
$blessed and $e->can("DOES")
!ll&&!rl&&r
406
MC/DC50%1/2
$blessed && $e->can('DOES') || 'isa'
$blessed $e->can("DOES")
Truth table66%2/3
A and B
A= $blessedB= $e->can("DOES")
ABresult
111
0X0
100
74 10 my @table = @prototable;
75  
76 10 ▶ while (my ($key, $value) = splice @table, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@table, 0, 2)) 13 0 13
77 13 ▶ $_match_class->( $e, $ref, $blessed, $does, $key ) and return $value
Branch: 1 branch
BranchTrueFalseTotal
&$_match_class($e, $ref, $blessed, $does, $key) and return $value 10 3 13
78 }
79  
80 0 return;
81 }
82 11 };
83  
84 # Package methods
85 23 ▶sub import {
Subroutine import: called CC=8 SCAR=20.8
86 23 my $class = shift;
87 23 ▶ my $global_opts = { $_[ 0 ] && ref $_[ 0 ] eq 'HASH' ? %{+ shift } : () };
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && ref $_[0] eq 'HASH' ? : 3 20 23
Condition100%3/3
$_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
2183
MC/DC100%2/2
$_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= $_[0]B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
88 23 my $ex_class = delete $global_opts->{exception_class};
89 # uncoverable condition false
90 23 ▶ my $target = $global_opts->{into} //= caller;
Condition100%2/3
$global_opts->{"into"} //= caller
l!l&&r!l&&!r
2210
MC/DC100%0/2
$global_opts->{"into"} //= caller
$global_opts->{"into"} caller
Truth table100%2/3
A //= B
A= $global_opts->{"into"}B= caller
ABresult
1X1
011
000
91 23 my @want = @_;
92 23 my @args = ();
93  
94 23 ▶ $ex_class or $ex_class = $_exception_class->( $target );
Branch: 1 branch
BranchTrueFalseTotal
$ex_class or $ex_class = &$_exception_class($target) 1 22 23
95  
96 23 for my $sym (@want) {
97 29 ▶ if ($ex_class->can( 'is_exception' ) and $ex_class->is_exception( $sym )){
Branch: 1 branch
BranchTrueFalseTotal
if ($ex_class->can('is_exception') and $ex_class->is_exception($sym)) { } 2 27 29
Condition100%3/3
$ex_class->can("is_exception") and $ex_class->is_exception($sym)
!ll&&!rl&&r
2432
MC/DC100%2/2
$ex_class->can("is_exception") and $ex_class->is_exception($sym)
$ex_class->can("is_exception") $ex_class->is_exception($sym)
Truth table100%3/3
A and B
A= $ex_class->can("is_exception")B= $ex_class->is_exception($sym)
ABresult
0X0
100
111
98 2 ▶ my $code = sub { sub { $sym } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
99  
100 2 install_sub { as => $sym, code => $code, into => $target, };
101 }
102 27 else { push @args, $sym }
103 }
104  
105 23 $class->SUPER::import( $global_opts, @args );
106 23 return;
107 }
108  
109 2 ▶sub quote_bind_values { # Deprecated. Use third arg in inflate_placeholders defs
Subroutine quote_bind_values: called CC=2 SCAR=6.9
Pod documented
110 2 ▶ defined $_[ 1 ] and $Should_Quote = !!$_[ 1 ]; return $Should_Quote;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and $Should_Quote = !!$_[1] 1 1 2
111 }
112  
113 # Public functions
114 43 ▶sub parse_arg_list (;@) { # Coerce a hash ref from whatever was passed
Subroutine parse_arg_list: called CC=10 SCAR=23.0
Pod documented
115 43 ▶ my $n = 0; $n++ while (defined $_[ $n ]);
Branch: 1 branch
BranchTrueFalseTotal
defined $_[$n] and do { ++$n } 75 43 118
116  
117 return ( $n == 0) ? {}
118 : (is_one_of_us( $_[ 0 ] )) ? $_clone_one_of_us->( @_ )
119 : ( ref $_[ 0 ] eq 'CODE') ? $_dereference_code->( @_ )
120 6 ▶ : ( ref $_[ 0 ] eq 'HASH') ? { %{ $_[ 0 ] } }
Branch: 3 branches
BranchTrueFalseTotal
$n == 1 ? : 8 16 24
ref $_[1] eq 'ARRAY' ? : 4 12 16
ref $_[1] eq 'HASH' ? : 1 11 12
121 : ( $n == 1) ? { error => $_[ 0 ] }
122 : ( ref $_[ 1 ] eq 'ARRAY') ? { error => (shift), args => @_ }
123 43 ▶ : ( ref $_[ 1 ] eq 'HASH') ? { error => $_[ 0 ], %{ $_[ 1 ] } }
Branch: 5 branches
BranchTrueFalseTotal
$n == 0 ? : 8 35 43
&is_one_of_us($_[0]) ? : 2 33 35
ref $_[0] eq 'CODE' ? : 3 30 33
ref $_[0] eq 'HASH' ? : 6 24 30
$n % 2 == 1 ? : 2 9 11
124 : ( $n % 2 == 1) ? { error => @_ }
125 : { @_ };
126 }
127  
128 11 ▶sub catch_class ($@) {
Subroutine catch_class: called CC=2 SCAR=6.9
Pod documented
129 11 ▶ my $check = $_gen_checker->( @{+ shift }, '*' => sub { die $_[ 0 ] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
130  
131 11 ▶ wantarray or croak 'Useless bare catch_class()';
Branch: 1 branch
BranchTrueFalseTotal
wantarray or croak("Useless bare catch_class()") 10 1 11
132  
133 10 ▶ return $_catch->( sub { ($check->( $_[ 0 ] ) || return)->( $_[ 0 ] ) }, @_ );
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
&$check($_[0]) || (return)
l!l
91
MC/DC100%1/1
&$check($_[0]) || (return)
&$check($_[0])
Truth table100%2/2
A
A= &$check($_[0])
Aresult
11
00
134 }
135  
136 1 ▶sub exception (;@) {
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
137 1 return $_exception_class->( caller )->caught( @_ );
138 }
139  
140 5 ▶sub has_exception ($;@) {
Subroutine has_exception: called CC=1 SCAR=0.0
Pod documented
141 5 my ($name, %args) = @_; my $exception_class = caller;
142  
143 5 return $exception_class->add_exception( $name, \%args );
144 }
145  
146 31 ▶sub inflate_message ($;@) { # Expand positional parameters of the form [_<n>]
Subroutine inflate_message: called CC=1 SCAR=0.0
Pod documented
147 31 return inflate_placeholders( [ '[?]', '[]' ], @_ );
148 }
149  
150 46 ▶sub inflate_placeholders ($;@) { # Sub visible strings for null and undef
Subroutine inflate_placeholders: called CC=4 SCAR=13.9
Pod documented
151 46 my $defaults = shift;
152 46 my $msg = shift;
153 507 ▶ my @vals = map { $defaults->[ 2 ] ? $_ : $_quote_maybe->( $_ ) }
Branch: 1 branch
BranchTrueFalseTotal
$defaults->[2] ? : 12 495 507
154 # uncoverable condition false
155 507 ▶ map { (length) ? $_ : $defaults->[ 1 ] }
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 500 7 507
156 507 ▶ map { $_ // $defaults->[ 0 ] } @_,
Condition66%2/3
$_ // $defaults->[0]
l!l&&r!l&&!r
50250
MC/DC0%0/2
$_ // $defaults->[0]
$_ $defaults->[0]
Truth table66%2/3
A // B
A= $_B= $defaults->[0]
ABresult
1X1
011
000
157 46 map { $defaults->[ 0 ] } 0 .. 9;
158  
159 46 $msg =~ s{ \[ _ (\d+) \] }{$vals[ $1 - 1 ]}gmx;
160 46 return $msg;
161 }
162  
163 17 ▶sub is_class_loaded ($) { # Lifted from Class::Load
Subroutine is_class_loaded: called CC=10 SCAR=23.0
Pod documented
164 17 my $class = shift; my $stash = Package::Stash->new( $class );
165  
166 17 ▶ if ($stash->has_symbol( '$VERSION' )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->has_symbol("\$VERSION")) 5 12 17
167 5 my $version = ${ $stash->get_symbol( '$VERSION' ) };
168  
169 5 ▶ if (defined $version) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $version) 4 1 5
170 4 ▶ not ref $version and return 1;
Branch: 1 branch
BranchTrueFalseTotal
ref $version or return 1 3 1 4
171 # Sometimes $VERSION ends up as a reference to undef (weird)
172 3 ▶ reftype $version eq 'SCALAR' and defined ${ $version } and return 1;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($version) eq "SCALAR" and defined ${$version;} and return 1 1 2 3
Condition100%3/3
builtin::reftype($version) eq "SCALAR" and defined ${$version;}
!ll&&!rl&&r
111
MC/DC100%2/2
builtin::reftype($version) eq "SCALAR" and defined ${$version;}
builtin::reftype($version) eq "SCALAR" defined ${$version;}
Truth table100%3/3
A and B
A= builtin::reftype($version) eq "SCALAR"B= defined ${$version;}
ABresult
0X0
100
111
173 2 ▶ blessed $version and return 1; # A version object
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($version) and return 1 1 1 2
174 }
175 }
176  
177 14 ▶ $stash->has_symbol( '@ISA' ) and @{ $stash->get_symbol( '@ISA' ) }
Branch: 1 branch
BranchTrueFalseTotal
$stash->has_symbol("\@ISA") and @{$stash->get_symbol("\@ISA");} and return 1 2 12 14
Condition100%3/3
$stash->has_symbol("\@ISA") and @{$stash->get_symbol("\@ISA");}
!ll&&!rl&&r
1112
MC/DC100%2/2
$stash->has_symbol("\@ISA") and @{$stash->get_symbol("\@ISA");}
$stash->has_symbol("\@ISA") @{$stash->get_symbol("\@ISA");}
Truth table100%3/3
A and B
A= $stash->has_symbol("\@ISA")B= @{$stash->get_symbol("\@ISA");}
ABresult
0X0
100
111
178 and return 1;
179 # Check for any method
180 12 ▶ return $stash->list_all_symbols( 'CODE' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$stash->list_all_symbols('CODE') ? : 5 7 12
181 }
182  
183 71 ▶sub is_one_of_us ($) {
Subroutine is_one_of_us: called CC=3 SCAR=11.0
Pod documented
184 71 ▶ return $_[ 0 ] && (blessed $_[ 0 ]) && $_[ 0 ]->isa( $Exception_Class );
Condition100%6/6
$_[0] && builtin::blessed($_[0]) && $_[0]->isa($Exception_Class)
!ll&&!rl&&r
6236
$_[0] and builtin::blessed($_[0])
!ll&&!rl&&r
1619
MC/DC100%3/3
$_[0] && builtin::blessed($_[0]) && $_[0]->isa($Exception_Class)
$_[0] builtin::blessed($_[0]) $_[0]->isa($Exception_Class)
Truth table100%4/4
A and B && C
A= $_[0]B= builtin::blessed($_[0])C= $_[0]->isa($Exception_Class)
ABCresult
0XX0
10X0
1100
1111
185 }
186  
187 3 ▶sub throw (;@) {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
188 3 $_exception_class->( caller )->throw( @_ );
189 }
190  
191 2 ▶sub throw_on_error (;@) {
Subroutine throw_on_error: called CC=1 SCAR=0.0
Pod documented
192 2 return $_exception_class->( caller )->throw_on_error( @_ );
193 }
194  
195 4 1;
196  
197 __END__
« Unexpected/TraitFor/TracingStacks.pm ↑ Summary Unexpected/TraitFor/StringifyingError.pm »