Unit/Sugar/Reuse.pm

filter: Statementstmt 100% 58 / 58 Branchbran 70.0% 7 / 10 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 3 totaltotal 90.3% 84 / 93 CC 8 SCAR 12.0
CC
8
Cov
95%
qualify_name
7.0
import
2.0
BEGIN
1.0
CRAP 3.3
« Unit/Sugar/Shared.pm ↑ Summary Unit/Sugar/Advice.pm »
1 # Defines the behavior of the +reuse+ keyword.
2 # @api private
3 package Test::Mini::Unit::Sugar::Reuse;
4 13 ▶use base 'Devel::Declare::Context::Simple';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Devel::Declare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 32 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
12 32 my ($class, %args) = @_;
13 32 ▶ my $caller = $args{into} || caller;
Condition66%2/3
$args{'into'} || caller
l!l&&r!l&&!r
1310
MC/DC0%0/2
$args{'into'} || caller
$args{'into'} caller
Truth table66%2/3
A || B
A= $args{'into'}B= caller
ABresult
1X1
011
000
14  
15 {
16 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 32 ▶ *{"$caller\::reuse"} = sub ($) {
Subroutine __ANON__: called CC=1 SCAR=0.0
18 27 my $caller = caller;
19 27 my $pkg = __PACKAGE__->qualify_name(shift, $caller);
20 27 unshift(@_, $pkg);
21 27 goto &{$pkg->can('import')};
22 32 };
23 }
24  
25 Devel::Declare->setup_for(
26 27 ▶ $caller => { reuse => { const => sub { $class->new()->parser(@_) } } }
Subroutine __ANON__: called CC=1 SCAR=0.0
27 32 );
28 }
29  
30 27 ▶sub parser {
Subroutine parser: called CC=1 SCAR=0.0
Pod undocumented
31 27 my $self = shift;
32 27 $self->init(@_);
33  
34 27 $self->skip_declarator();
35  
36 27 my $name = $self->strip_name();
37 27 $self->inject("'${name}'");
38 }
39  
40 27 ▶sub qualify_name {
Subroutine qualify_name: called CC=7 SCAR=19.5
Pod undocumented
41 27 my ($self, $name, $mod) = @_;
42 27 my $file;
43  
44 27 ▶ if ($name =~ s/^::// || $mod eq 'main') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ s/^::// or $mod eq 'main') { } 15 12 27
Condition100%3/3
$name =~ s/^::// or $mod eq "main"
l!l&&r!l&&!r
10512
MC/DC100%2/2
$name =~ s/^::// or $mod eq "main"
$name =~ s/^::// $mod eq "main"
Truth table100%3/3
A or B
A= $name =~ s/^:://B= $mod eq "main"
ABresult
1X1
011
000
45 15 ($file = $name) =~ s/::/\//g;
46 die "Cannot find module '$name' to reuse..."
47 15 ▶ unless exists $INC{"$file.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $INC{"$file.pm"} 0 15 15
48 } else {
49 12 my $pkg = $mod;
50 12 my @pkg_parts = split('::', $pkg);
51 12 my @name_parts = split('::', $name);
52  
53 12 unshift @pkg_parts, '';
54  
55 12 ▶ while (@pkg_parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pkg_parts) 37 0 37
56 37 $file = join('/', @pkg_parts[1..@pkg_parts-1], @name_parts);
57 37 ▶ last if exists $INC{"$file.pm"};
Branch: 1 branch
BranchTrueFalseTotal
if exists $INC{"$file.pm"} 12 25 37
58 25 pop(@pkg_parts);
59 25 $file = undef;
60 }
61  
62 12 ▶ die <<ERROR unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 12 12
63  
64 Cannot resolve module '$name' relative to '$pkg'...
65 Remember that shared blocks must be declared before the call to `reuse`.
66 ERROR
67  
68 12 ($name = $file) =~ s/\//::/g;
69 }
70  
71 27 return $name;
72 }
73  
74 27 ▶sub inject {
Subroutine inject: called CC=1 SCAR=0.0
Pod undocumented
75 27 my ($self, $inject) = @_;
76  
77 27 my $linestr = $self->get_linestr();
78 27 substr($linestr, $self->offset, 0) = $inject;
79 27 $self->set_linestr($linestr);
80 27 $self->inc_offset(length($inject));
81 }
82  
83 13 1;
« Unit/Sugar/Shared.pm ↑ Summary Unit/Sugar/Advice.pm »