| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$params{"show_source"} or $params{"inject_name"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 10 | ▶ | $hub->listen(\&listener, inherit => 1) if $params{show_source} || $params{show_args}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$params{"show_source"} or $params{"show_args"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | ▶ | $hub->add_context_init(\&context_init) if $params{show_args}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$args{'level'} || 1
$args{'level'}
Truth table50%1/2
A
A= $args{'level'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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')
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')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 7 | ▶ | my $trace = $event->trace or return $event; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | my $code = get_assert_code($trace) or return $event; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 7 | my $name_set = 0; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | if ($event->can('name') && !$event->name && $event->can('set_name')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$event->can("name") and not $event->name and $event->can("set_name")
$event->can("name") and not $event->name
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | ▶ | my $args = $trace ? $trace->{args} : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | ▶ | return unless $code || $args; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$code or $args
MC/DC0%0/2
$code or $args
$code
$args
Truth table66%2/3
A or B
A= $codeB= $args
| |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | my $msg = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | $msg .= "Failure source code:\n------------\n$code\n------------\n" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | if $code; | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 6 | ▶ | $msg .= "Failure Arguments: (" . join(', ', map { defined($_) ? "'$_'" : 'undef' } @$args) . ")" | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | ▶ | my $line = $trace->line or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 7 | ▶ | my $sub = $trace->subname or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | my $short_sub = $sub; | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 7 | $short_sub =~ s/^.*:://; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 7 | ▶ | return if $short_sub eq '__ANON__'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 7 | ▶ | my $found = $it->[0] or return; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | my $thing = $found; | |||||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | ▶ | while ($thing) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 10 | ▶ | if (($thing->can('children') && $subs{($thing->children)[0]->content}) || $subs{$thing->content}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$thing->can("children") and $subs{($thing->children)[0]->content} or $subs{$thing->content}
$thing->can("children") and $subs{($thing->children)[0]->content}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
$prefix and $prefix->isa("PPI::Token::Whitespace") and $prefix->content ne "\n"
$prefix and $prefix->isa("PPI::Token::Whitespace")
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"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||