blib/lib/Zoidberg/Utils/Error.pm

filter: Statementstmt 77.7% 63 / 81 Branchbran 47.6% 20 / 42 Conditioncond 54.1% 13 / 24 MC/DCmcdc 33.3% 5 / 15 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 65.3% 117 / 179 CC 34 SCAR 34.3
CC
34
Cov
65%
error
57.0
stringify
19.8
PROPAGATE
5.0
CRAP 30.9
« blib/lib/Zoidberg/StringParser.pm ↑ Summary blib/script/zoid »
1  
2 package Zoidberg::Utils::Error;
3  
4 69 our $VERSION = '0.981';
5  
6 69 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 69 ▶use Exporter::Tidy default => [qw/error bug todo/];
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use overload
9 69 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ 'eq' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 69 fallback => 'TRUE';
12  
13 69 ▶use Scalar::Util qw/reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 69 our $Scope = $0;
16 69 $Scope =~ s#.*/##;
17  
18 # ################ #
19 # Exported methods #
20 # ################ #
21  
22 1363 ▶sub error {
Subroutine error: called CC=20 SCAR=40.4
Pod documented
23 1363 my @caller = caller;
24
25 1363 ▶ if ($@ && !@_) { # make it work more like die
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not @_) 0 1363 1363
Condition33%1/3
$@ and not @_
!ll&&!rl&&r
136300
MC/DC0%0/2
$@ and not @_
$@ not @_
Truth table33%1/3
A and not(B)
A= $@B= not @_
ABresult
0X0
100
111
26 0 my $error = $@;
27 0 my $can_propagate = do {
28 0 local $@;
29 0 eval{ $error->can( 'PROPAGATE') };
30 };
31 0 ▶ die $@->PROPAGATE(@caller[1,2]) if $can_propagate;
Branch: 1 branch
BranchTrueFalseTotal
if $can_propagate 0 0 0
32 0 unshift @_, PROPAGATE({}, @caller[1,2]), $@;
33 }
34  
35 1363 my $error = bless {};
36  
37 1363 for (@_) { # compiling the error here
38 1375 ▶ if ( (reftype $_ || '') eq 'HASH') { %$error = (%$error, %$_) }
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($_) || '') eq 'HASH') { } 14 1361 1375
Condition100%2/2
builtin::reftype($_) || ''
l!l
141361
MC/DC100%1/1
builtin::reftype($_) || ''
builtin::reftype($_)
Truth table100%2/2
A
A= builtin::reftype($_)
Aresult
11
00
39 1361 else { $$error{string} .= $_ }
40 }
41  
42 1363 ▶ unless ($$error{string}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($error->{'string'}) { } 1363 0 1363
if (do { $error->{"string"} =~ s/\(\@INC contains\: (.*?)\)\s*//g }) 0 1363 1363
43 $$error{string} =
44 $$error{is_bug} ? 'This is a bug'
45 0 ▶ : $$error{is_todo} ? 'Something TODO here' : 'Error' ;
Branch: 2 branches
BranchTrueFalseTotal
$error->{'is_bug'} ? : 0 0 0
$error->{'is_todo'} ? : 0 0 0
46 }
47 0 elsif ($$error{string} =~ s/\(\@INC contains\: (.*?)\)\s*//g) { # make it less verbose
48 0 $$error{INC} = $1;
49 }
50  
51 # trace stack
52 1363 ▶ $$error{stack} ||= [];
Condition100%2/2
$error->{"stack"} ||= []
l!l
21361
MC/DC100%1/1
$error->{"stack"} ||= []
$error->{"stack"}
Truth table100%2/2
A
A= $error->{"stack"}
Aresult
11
00
53 {
54 69 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 3 @caller = caller(${$caller[0].'::ERROR_CALLER'})
56 1363 ▶ if ${$caller[0].'::ERROR_CALLER'};
Branch: 1 branch
BranchTrueFalseTotal
if ${$caller[0] . '::ERROR_CALLER';} 3 1360 1363
57 1363 push @{$$error{stack}}, \@caller;
58  
59 1363 ▶ if ( # debug code
Branch: 1 branch
BranchTrueFalseTotal
if ($error->{"debug"} = ${$caller[0] . '::DEBUG';} || $Zoidberg::CURRENT->{"settings"}{"debug"}) 0 1363 1363
Condition33%1/3
${$caller[0] . '::DEBUG';} || $Zoidberg::CURRENT->{'settings'}{'debug'}
l!l&&r!l&&!r
001363
MC/DC0%0/2
${$caller[0] . '::DEBUG';} || $Zoidberg::CURRENT->{'settings'}{'debug'}
${$caller[0] . '::DEBUG';} $Zoidberg::CURRENT->{'settings'}{'debug'}
Truth table33%1/3
A || B
A= ${$caller[0] . '::DEBUG';}B= $Zoidberg::CURRENT->{'settings'}{'debug'}
ABresult
1X1
011
000
60 1363 $$error{debug} = ${$caller[0].'::DEBUG'}
61 || $Zoidberg::CURRENT->{settings}{debug}
62 ) {
63 0 push @{$$error{stack}}, [ (caller $_)[0..2] ]
64 0 for (1..$$error{debug});
65 }
66 }
67
68 1363 ▶ if (defined $Scope) { # set fake caller
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Zoidberg::Utils::Error::Scope) 1363 0 1363
69 1363 ▶ $$error{scope} ||= ref($Scope)
Branch: 1 branch
BranchTrueFalseTotal
ref $Zoidberg::Utils::Error::Scope ? : 0 1363 1363
Condition16%1/6
$error->{"scope"} ||= ref $Zoidberg::Utils::Error::Scope ? [$Zoidberg::Utils::Error::Scope->[0], $Zoidberg::Utils::Error::Scope->[1] || $caller[2]] : [$Zoidberg::Utils::Error::Scope]
l!l&&r!l&&!r
013630
$Zoidberg::Utils::Error::Scope->[1] || $caller[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$error->{"scope"} ||= ref $Zoidberg::Utils::Error::Scope ? [$Zoidberg::Utils::Error::Scope->[0], $Zoidberg::Utils::Error::Scope->[1] || $caller[2]] : [$Zoidberg::Utils::Error::Scope]
$error->{"scope"} ref $Zoidberg::Utils::Error::Scope ? [$Zoidberg::Utils::Error::Scope->[0], $Zoidberg::Utils::Error::Scope->[1] || $caller[2]] : [$Zoidberg::Utils::Error::Scope]
MC/DC0%0/2
$Zoidberg::Utils::Error::Scope->[1] || $caller[2]
$Zoidberg::Utils::Error::Scope->[1] $caller[2]
Truth table33%1/3
A ||= B
A= $error->{"scope"}B= ref $Zoidberg::Utils::Error::Scope ? [$Zoidberg::Utils::Error::Scope->[0], $Zoidberg::Utils::Error::Scope->[1] || $caller[2]] : [$Zoidberg::Utils::Error::Scope]
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $Zoidberg::Utils::Error::Scope->[1]B= $caller[2]
ABresult
1X1
011
000
70 ? [ $$Scope[0], $$Scope[1] || $caller[2] ]
71 : [ $Scope ];
72 }
73  
74 1363 die $error;
75 }
76  
77 1 ▶sub bug {
Subroutine bug: called CC=1 SCAR=0.0
Pod documented
78 1 unshift @_, { is_bug => 1 };
79 1 goto \&error;
80 }
81  
82 0 ▶sub todo {
Subroutine todo: not called CC=1 SCAR=6.9
Pod documented
83 0 unshift @_, { is_todo => 1 };
84 0 goto \&error;
85 }
86  
87 # ############## #
88 # Object methods #
89 # ############## #
90  
91 585 ▶sub stringify {
Subroutine stringify: called CC=11 SCAR=29.8
Pod documented
92 # TODO verbosity optie
93 69 ▶ no warnings; # lots of stupid warnings here (due to 'overload' ?)
Subroutine BEGIN: called CC=1 SCAR=0.0
94 585 my $self = shift;
95 585 my %opt = @_;
96 585 my $string;
97 585 ▶ if ($opt{format} eq 'gnu') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'format'} eq 'gnu') { } 0 585 585
98 0 ▶ $string = join( ': ', grep {defined $_}
Branch: 1 branch
BranchTrueFalseTotal
$self->{'scope'} ? : 0 0 0
99 0 ▶ ( $$self{scope} ? (@{$$self{scope}}) : (@{$$self{stack}[0]}) ),
Branch: 2 branches
BranchTrueFalseTotal
$self->{'is_bug'} ? : 0 0 0
$self->{'is_todo'} ? : 0 0 0
100 ( $$self{is_bug} ? 'BUG' : $$self{is_todo} ? 'TODO' : undef ),
101 0 $$self{string} ) . "\n" ;
102 }
103 else {
104 $string = ($$self{is_bug} ? 'BUG: ' : $$self{is_todo} ? 'TODO: ' : '')
105 585 ▶ . $self->{string};
Branch: 2 branches
BranchTrueFalseTotal
$self->{'is_bug'} ? : 0 585 585
$self->{'is_todo'} ? : 0 585 585
106 585 ▶ $string .= qq# at $$self{stack}[0][1] line $$self{stack}[0][2]\n#
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\n$/ 585 0 585
107 unless $string =~ /\n$/;
108 585 ▶ if (exists $$self{propagated} and ref $$self{propagated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"propagated"} and ref $self->{"propagated"}) 62 523 585
Condition66%2/3
exists $self->{"propagated"} and ref $self->{"propagated"}
!ll&&!rl&&r
523062
MC/DC50%1/2
exists $self->{"propagated"} and ref $self->{"propagated"}
exists $self->{"propagated"} ref $self->{"propagated"}
Truth table66%2/3
A and B
A= exists $self->{"propagated"}B= ref $self->{"propagated"}
ABresult
0X0
100
111
109 62 $string = PROPAGATE($string, @$_) for @{$self->{propagated}};
110 }
111 }
112 585 return $string;
113 }
114  
115 114 ▶sub PROPAGATE { # see perldoc -f die
Subroutine PROPAGATE: called CC=5 SCAR=16.1
Pod documented
116 114 my ($self, $file, $line) = @_;
117 114 ▶ ($file, $line) = ( caller() )[1,2] unless $file or $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $file or $line 10 104 114
Condition66%2/3
$file or $line
l!l&&r!l&&!r
104010
MC/DC50%1/2
$file or $line
$file $line
Truth table66%2/3
A or B
A= $fileB= $line
ABresult
1X1
011
000
118 114 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 20 94 114
119 20 ▶ $self->{propagated} ||= [];
Condition100%2/2
$self->{"propagated"} ||= []
l!l
1010
MC/DC100%1/1
$self->{"propagated"} ||= []
$self->{"propagated"}
Truth table100%2/2
A
A= $self->{"propagated"}
Aresult
11
00
120 20 push @{$self->{propagated}}, [$file, $line];
121 }
122 94 else { $self .= "\t...propagated at $file line $line\n" }
123 114 return $self;
124 }
125  
126  
127 69 1;
128  
129 __END__
« blib/lib/Zoidberg/StringParser.pm ↑ Summary blib/script/zoid »