blib/lib/Test2/Plugin/SourceDiag.pm

filter: Statementstmt 100% 95 / 95 Branchbran 74.0% 40 / 54 Conditioncond 65.7% 23 / 35 MC/DCmcdc 35.0% 7 / 20 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 4 totaltotal 80.4% 177 / 220 CC 40 SCAR 23.0
CC
40
Cov
86%
get_assert_code
19.0
listener
9.2
filter
8.1
CRAP 10.0
↑ Summary
1 package Test2::Plugin::SourceDiag;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 our $VERSION = '0.000005';
6  
7 4 ▶use Test2::Event::Diag;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Scalar::Util();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶use Test2::API qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 test2_add_callback_post_load
13 test2_stack
14 4 };
15  
16 4 my %SEEN;
17  
18 10 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
19 10 my $class = shift;
20 10 my %params = @_;
21  
22 10 ▶ $params{show_source} = 1 unless defined $params{show_source};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"show_source"} 5 5 10
23  
24 test2_add_callback_post_load(
25 10 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
26 10 my $hub = test2_stack()->top;
27 10 ▶ $hub->filter(\&filter, inherit => 1) if $params{show_source} || $params{inject_name};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"show_source"} or $params{"inject_name"} 9 1 10
Condition100%3/3
$params{"show_source"} or $params{"inject_name"}
l!l&&r!l&&!r
721
MC/DC100%2/2
$params{"show_source"} or $params{"inject_name"}
$params{"show_source"} $params{"inject_name"}
Truth table100%3/3
A or B
A= $params{"show_source"}B= $params{"inject_name"}
ABresult
1X1
011
000
28 10 ▶ $hub->listen(\&listener, inherit => 1) if $params{show_source} || $params{show_args};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"show_source"} or $params{"show_args"} 8 2 10
Condition100%3/3
$params{"show_source"} or $params{"show_args"}
l!l&&r!l&&!r
712
MC/DC100%2/2
$params{"show_source"} or $params{"show_args"}
$params{"show_source"} $params{"show_args"}
Truth table100%3/3
A or B
A= $params{"show_source"}B= $params{"show_args"}
ABresult
1X1
011
000
29 10 ▶ $hub->add_context_init(\&context_init) if $params{show_args};
Branch: 1 branch
BranchTrueFalseTotal
if $params{"show_args"} 3 7 10
30 }
31 10 );
32 }
33  
34 3 ▶sub context_init {
Subroutine context_init: called CC=3 SCAR=11.4
Pod undocumented
35 3 my $ctx = shift;
36  
37 package DB;
38  
39 3 my @caller = caller(1);
40 3 my %args = @DB::args;
41 3 ▶ my $level = $args{level} || 1;
Condition50%1/2
$args{'level'} || 1
l!l
03
MC/DC0%0/1
$args{'level'} || 1
$args{'level'}
Truth table50%1/2
A
A= $args{'level'}
Aresult
11
00
42  
43 3 @caller = caller(1 + $level);
44  
45 3 ▶ $ctx->trace->{args} = [grep { !Scalar::Util::blessed($_) || !$_->isa('Test::Builder')} @DB::args];
Condition33%1/3
builtin::blessed($_) && $_->isa('Test::Builder')
!ll&&!rl&&r
800
MC/DC0%0/2
builtin::blessed($_) && $_->isa('Test::Builder')
builtin::blessed($_) $_->isa('Test::Builder')
Truth table33%1/3
A && B
A= builtin::blessed($_)B= $_->isa('Test::Builder')
ABresult
0X0
100
111
46 }
47  
48 32 ▶sub filter {
Subroutine filter: called CC=8 SCAR=20.9
Pod undocumented
49 32 my ($hub, $event) = @_;
50 32 ▶ return $event unless $event->causes_fail;
Branch: 1 branch
BranchTrueFalseTotal
unless $event->causes_fail 25 7 32
51  
52 7 ▶ my $trace = $event->trace or return $event;
Branch: 1 branch
BranchTrueFalseTotal
my $trace = $event->trace or return $event 7 0 7
53 7 ▶ my $code = get_assert_code($trace) or return $event;
Branch: 1 branch
BranchTrueFalseTotal
my $code = get_assert_code($trace) or return $event 7 0 7
54  
55 7 my $name_set = 0;
56 7 ▶ if ($event->can('name') && !$event->name && $event->can('set_name')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->can("name") and not $event->name and $event->can("set_name")) 3 4 7
Condition66%4/6
$event->can("name") and not $event->name and $event->can("set_name")
!ll&&!rl&&r
403
$event->can("name") and not $event->name
!ll&&!rl&&r
043
MC/DC33%1/3
$event->can("name") and not $event->name and $event->can("set_name")
$event->can("name") not $event->name $event->can("set_name")
Truth table50%2/4
A and not(B) and C
A= $event->can("name")B= not $event->nameC= $event->can("set_name")
ABCresult
0XX0
10X0
1100
1111
57 3 my $text = join "\n" => @{$code->{source}};
58 3 $text =~ s/^\s*//;
59 3 $name_set = eval { $event->set_name($text); 1 };
60 }
61  
62 7 ▶ unless ($name_set) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name_set) 5 2 7
63 5 my $start = $code->{start};
64 5 my $end = $code->{end};
65 5 my $len = length("$end");
66 5 my $text = join "\n" => map { sprintf("% ${len}s: %s", $start++, $_) } @{$code->{source}};
67 5 $event->meta(__PACKAGE__, {})->{code} = $text;
68 }
69  
70 7 return $event;
71 }
72  
73 30 ▶sub listener {
Subroutine listener: called CC=9 SCAR=22.2
Pod undocumented
74 30 my ($hub, $event) = @_;
75  
76 30 ▶ return unless $event->causes_fail;
Branch: 1 branch
BranchTrueFalseTotal
unless $event->causes_fail 24 6 30
77  
78 6 my $trace = $event->trace;
79 6 my $meta = $event->get_meta(__PACKAGE__);
80 6 ▶ my $code = $meta ? $meta->{code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$meta ? : 5 1 6
81 6 ▶ my $args = $trace ? $trace->{args} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$trace ? : 6 0 6
82  
83 6 ▶ return unless $code || $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $code or $args 0 6 6
Condition66%2/3
$code or $args
l!l&&r!l&&!r
510
MC/DC0%0/2
$code or $args
$code $args
Truth table66%2/3
A or B
A= $codeB= $args
ABresult
1X1
011
000
84  
85 6 my $msg = '';
86  
87 6 ▶ $msg .= "Failure source code:\n------------\n$code\n------------\n"
Branch: 1 branch
BranchTrueFalseTotal
if $code 5 1 6
88 if $code;
89  
90 6 ▶ $msg .= "Failure Arguments: (" . join(', ', map { defined($_) ? "'$_'" : 'undef' } @$args) . ")"
Branch: 2 branches
BranchTrueFalseTotal
if $args 3 3 6
defined $_ ? : 8 0 8
91 if $args;
92  
93 6 $hub->send(
94 Test2::Event::Diag->new(
95 trace => $trace,
96 message => $msg,
97 )
98 );
99 }
100  
101 my %CACHE;
102  
103 7 ▶sub get_assert_code {
Subroutine get_assert_code: called CC=16 SCAR=29.4
Pod undocumented
104 7 my ($trace) = @_;
105  
106 7 ▶ my $file = $trace->file or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $trace->file or return 7 0 7
107 7 ▶ my $line = $trace->line or return;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $trace->line or return 7 0 7
108 7 ▶ my $sub = $trace->subname or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $trace->subname or return 7 0 7
109 7 my $short_sub = $sub;
110 7 $short_sub =~ s/^.*:://;
111 7 ▶ return if $short_sub eq '__ANON__';
Branch: 1 branch
BranchTrueFalseTotal
if $short_sub eq "__ANON__" 0 7 7
112  
113 7 my %subs = ($sub => 1, $short_sub => 1);
114  
115 7 require PPI::Document;
116 7 ▶ my $pd = $CACHE{$file} ||= PPI::Document->new($file);
Condition66%2/3
$CACHE{$file} ||= "PPI::Document"->new($file)
l!l&&r!l&&!r
430
MC/DC0%0/2
$CACHE{$file} ||= "PPI::Document"->new($file)
$CACHE{$file} "PPI::Document"->new($file)
Truth table66%2/3
A ||= B
A= $CACHE{$file}B= "PPI::Document"->new($file)
ABresult
1X1
011
000
117 7 $pd->index_locations;
118  
119 7 ▶ my $it = $pd->find(sub { !$_[1]->isa('PPI::Token::Whitespace') && $_[1]->logical_line_number == $line }) or return;
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 2 branches
BranchTrueFalseTotal
my $it = $pd->find(sub { not $_[1]->isa("PPI::Token::Whitespace") and $_[1]->logical_line_number == $line; } ) or return 7 0 7
not $_[1]->isa("PPI::Token::Whitespace") and $_[1]->logical_line_number == $line 1421 637 2058
120  
121 7 ▶ my $found = $it->[0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $found = $it->[0] or return 7 0 7
122  
123 7 my $thing = $found;
124 7 ▶ while ($thing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thing) 10 0 10
125 10 ▶ if (($thing->can('children') && $subs{($thing->children)[0]->content}) || $subs{$thing->content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thing->can("children") and $subs{($thing->children)[0]->content} or $subs{$thing->content}) 7 3 10
Condition83%5/6
$thing->can("children") and $subs{($thing->children)[0]->content} or $subs{$thing->content}
l!l&&r!l&&!r
703
$thing->can("children") and $subs{($thing->children)[0]->content}
!ll&&!rl&&r
127
MC/DC66%2/3
$thing->can("children") and $subs{($thing->children)[0]->content} or $subs{$thing->content}
$thing->can("children") $subs{($thing->children)[0]->content} $subs{$thing->content}
Truth table60%3/5
A and B or C
A= $thing->can("children")B= $subs{($thing->children)[0]->content}C= $subs{$thing->content}
ABCresult
11X1
0X11
1011
0X00
1000
126 7 $found = $thing;
127 7 last;
128 }
129  
130 3 $thing = $thing->parent;
131 }
132  
133 7 my @source;
134  
135 7 push @source => split /\r?\n/, $found->content;
136  
137 # Add in any indentation we may have cut off.
138 7 my $prefix = $thing->previous_sibling;
139 7 ▶ if ($prefix && $prefix->isa('PPI::Token::Whitespace') && $prefix->content ne "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix and $prefix->isa("PPI::Token::Whitespace") and $prefix->content ne "\n") 7 0 7
Condition33%2/6
$prefix and $prefix->isa("PPI::Token::Whitespace") and $prefix->content ne "\n"
!ll&&!rl&&r
007
$prefix and $prefix->isa("PPI::Token::Whitespace")
!ll&&!rl&&r
007
MC/DC0%0/3
$prefix and $prefix->isa("PPI::Token::Whitespace") and $prefix->content ne "\n"
$prefix $prefix->isa("PPI::Token::Whitespace") $prefix->content ne "\n"
Truth table25%1/4
A and B and C
A= $prefixB= $prefix->isa("PPI::Token::Whitespace")C= $prefix->content ne "\n"
ABCresult
0XX0
10X0
1100
1111
140 7 my $space = $prefix->content;
141 7 $space =~ s/^.*\n//s;
142 7 ▶ $source[0] = $space . $source[0] if length($space);
Branch: 1 branch
BranchTrueFalseTotal
if length $space 7 0 7
143 }
144  
145 7 my $start = $found->logical_line_number;
146 return {
147 7 start => $start,
148 end => $start + $#source,
149 source => \@source,
150 };
151 }
152  
153 4 1;
154  
155 __END__
↑ Summary