| 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
MC/DC0%0/2
$args{'into'} || caller
$args{'into'}
caller
Truth table66%2/3
A || B
A= $args{'into'}B= caller
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$name =~ s/^::// or $mod eq "main"
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"
| |||||||||||||||||||||||||||||
| 45 | 15 | ($file = $name) =~ s/::/\//g; | |||||||||||||||||||||||||||
| 46 | die "Cannot find module '$name' to reuse..." | ||||||||||||||||||||||||||||
| 47 | 15 | ▶ | unless exists $INC{"$file.pm"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 56 | 37 | $file = join('/', @pkg_parts[1..@pkg_parts-1], @name_parts); | |||||||||||||||||||||||||||
| 57 | 37 | ▶ | last if exists $INC{"$file.pm"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 25 | pop(@pkg_parts); | |||||||||||||||||||||||||||
| 59 | 25 | $file = undef; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 12 | ▶ | die <<ERROR unless $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||