blib/lib/Sub/Stubber.pm

filter: Statementstmt 92.1% 94 / 102 Branchbran 71.7% 33 / 46 Conditioncond 66.6% 6 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 17 / 17 Podpod 75.0% 3 / 4 totaltotal 84.2% 155 / 184 CC 30 SCAR 16.5
CC
30
Cov
85%
_import_as_base
11.2
mkstubs
6.4
add_specs
5.1
CRAP 5.2
↑ Summary
1 package Sub::Stubber::Stubs;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Class::Struct;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 struct __PACKAGE__,
7 ['names' => '%',
8 'import_triggers' => '@',
9 'env_triggers' => '@',
10 'triggered' => '$',
11 'imported_into' => '@',
12 ];
13
14 3 ▶sub add_trigger {
Subroutine add_trigger: called CC=3 SCAR=11.2
15 3 my ($self,$type,$name) = @_;
16 3 ▶ if($type eq 'env') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'env') { } 1 2 3
elsif ($type eq 'import') { } 2 0 2
17 1 push @{$self->env_triggers}, $name;
18 } elsif ($type eq 'import') {
19 2 push @{$self->import_triggers}, $name;
20 } else {
21 0 die("No such trigger type '$type'");
22 }
23 }
24  
25 8 ▶sub add_specs {
Subroutine add_specs: called CC=5 SCAR=16.3
26 8 my ($self,@specs) = @_;
27 8 foreach my $spec (@specs) {
28 6 ▶ if(!ref $spec) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $spec) { } 3 3 6
elsif (ref $spec eq 'HASH') { } 3 0 3
29 3 $self->names->{$spec} = undef;
30 } elsif (ref $spec eq 'HASH') {
31 3 ▶ while ( my ($name,$val) = each %$spec ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$spec) 3 3 6
32 3 $self->names->{$name} = $val;
33 }
34 } else {
35 0 die ("Bad specifier '$spec'");
36 }
37 }
38 }
39  
40 package Sub::Stubber;
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44 1 ▶no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 1 our %PkgCache;
48 1 our $VERSION = 0.04;
49  
50  
51 8 ▶sub get_object {
Subroutine get_object: called CC=3 SCAR=11.4
Pod undocumented
52 8 my $cls = shift;
53 8 ▶ my $cpkg = $cls eq __PACKAGE__ ? caller : $cls;
Branch: 1 branch
BranchTrueFalseTotal
$cls eq __PACKAGE__ ? : 0 8 8
54
55 8 ▶ return $PkgCache{$cpkg} ||= Sub::Stubber::Stubs->new(triggered => 0);
Condition66%2/3
$Sub::Stubber::PkgCache{$cpkg} ||= "Sub::Stubber::Stubs"->new("triggered", 0)
l!l&&r!l&&!r
440
MC/DC0%0/2
$Sub::Stubber::PkgCache{$cpkg} ||= "Sub::Stubber::Stubs"->new("triggered", 0)
$Sub::Stubber::PkgCache{$cpkg} "Sub::Stubber::Stubs"->new("triggered", 0)
Truth table66%2/3
A ||= B
A= $Sub::Stubber::PkgCache{$cpkg}B= "Sub::Stubber::Stubs"->new("triggered", 0)
ABresult
1X1
011
000
56 }
57  
58  
59 5 ▶sub _mk_sub_real {
Subroutine _mk_sub_real: called CC=5 SCAR=16.1
60 5 my ($cpkg,$subname,$value) = @_;
61 5 ▶ if($subname !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($subname =~ /::/) 5 0 5
62 5 $subname = $cpkg . '::' . $subname;
63 }
64
65 5 my $old_proto = prototype $subname;
66  
67 #Hope the prototype is right!
68 5 ▶ if(defined $value && ref $value eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and ref $value eq 'CODE') { } 1 4 5
elsif (defined $old_proto) { } 1 3 4
Condition100%3/3
defined $value and ref $value eq "CODE"
!ll&&!rl&&r
311
MC/DC100%2/2
defined $value and ref $value eq "CODE"
defined $value ref $value eq "CODE"
Truth table100%3/3
A and B
A= defined $valueB= ref $value eq "CODE"
ABresult
0X0
100
111
69 1 *{$subname} = $value;
70 }
71 elsif(defined $old_proto) {
72 1 ▶ eval "*$subname = sub ($old_proto) { \$value };";
Subroutine __ANON__: called CC=1 SCAR=0.0
73 }
74 else {
75 3 ▶ *{$subname} = sub { $value };
Subroutine __ANON__: called CC=1 SCAR=0.0
76 }
77 }
78  
79 #The next two provide some sugary API at the expense of a bleh-like
80 #implementation. the functions are too simple to warrant an
81 #elaborate dispatch scheme, unfortunately
82  
83 5 ▶sub regstubs {
Subroutine regstubs: called CC=2 SCAR=7.1
Pod documented
84 5 my $cls = shift;
85 5 ▶ my $cpkg = $cls eq __PACKAGE__ ? caller : $cls;
Branch: 1 branch
BranchTrueFalseTotal
$cls eq __PACKAGE__ ? : 5 0 5
86 5 get_object($cpkg)->add_specs(@_);
87 }
88  
89 3 ▶sub add_trigger {
Subroutine add_trigger: called CC=2 SCAR=7.1
Pod documented
90 3 my $cls = shift;
91 3 ▶ my $cpkg = $cls eq __PACKAGE__ ? caller : $cls;
Branch: 1 branch
BranchTrueFalseTotal
$cls eq __PACKAGE__ ? : 3 0 3
92 3 get_object($cpkg)->add_trigger(@_);
93 }
94  
95 3 ▶sub mkstubs {
Subroutine mkstubs: called CC=6 SCAR=18.6
Pod documented
96  
97 3 my $cls = shift;
98 3 ▶ my $cpkg = $cls eq __PACKAGE__ ? caller : $cls;
Branch: 1 branch
BranchTrueFalseTotal
$cls eq __PACKAGE__ ? : 1 2 3
99 3 ▶ if(!exists $PkgCache{$cpkg} && @_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $Sub::Stubber::PkgCache{$cpkg} and @_ == 0) 0 3 3
Condition33%1/3
not exists $Sub::Stubber::PkgCache{$cpkg} and @_ == 0
!ll&&!rl&&r
300
MC/DC0%0/2
not exists $Sub::Stubber::PkgCache{$cpkg} and @_ == 0
not exists $Sub::Stubber::PkgCache{$cpkg} @_ == 0
Truth table33%1/3
not(A) and B
A= not exists $Sub::Stubber::PkgCache{$cpkg}B= @_ == 0
ABresult
0X0
100
111
100 0 die("No functions registered and non provided to mkstubs()");
101 }
102 3 my $obj = $PkgCache{$cpkg};
103 3 ▶ return if $obj->triggered();
Branch: 1 branch
BranchTrueFalseTotal
if $obj->triggered 0 3 3
104
105 3 $obj->add_specs(@_);
106 3 ▶ while (my ($subname,$subval) = each %{$obj->names}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$obj->names;}) 5 3 8
107 5 _mk_sub_real($cpkg,$subname,$subval);
108 }
109  
110 3 $obj->triggered(1);
111 }
112  
113 3 ▶sub _import_as_base {
Subroutine _import_as_base: called CC=10 SCAR=24.1
114 3 my ($cls,@options) = @_;
115
116 3 my $user_pkg = caller();
117
118 3 my $obj = $PkgCache{$cls};
119
120 3 ▶ if(!$obj) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 0 3 3
121 0 warn("$cls has inherited from " . __PACKAGE__ . " but has not defined " .
122 "any functions for stubbing");
123 0 print Dumper(\%PkgCache);
124 0 goto GT_EXPORTER;
125 }
126
127 3 ▶ if($obj->triggered) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->triggered) 0 3 3
128 #No need to re-generate stubs
129 0 goto GT_EXPORTER;
130 }
131
132 3 foreach my $env (@{$obj->env_triggers}) {
133 1 ▶ if($ENV{$env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$env}) 1 0 1
134 1 mkstubs($cls);
135 1 goto GT_EXPORTER;
136 }
137 }
138
139 2 my $found_import_trigger = 0;
140 2 foreach my $import (@{$obj->import_triggers}) {
141 2 my $i = 1;
142 2 ▶ while ($i <= $#_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 1 2 3
143 1 ▶ if($_[$i] eq $import) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[$i] eq $import) 1 0 1
144 1 $found_import_trigger = 1;
145 1 splice(@_, $i, 1);
146 }
147 1 $i++;
148 }
149 }
150
151 2 ▶ if($found_import_trigger) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found_import_trigger) 1 1 2
152 1 mkstubs($cls);
153 }
154
155
156 GT_EXPORTER:
157 3 push @{$obj->imported_into}, $user_pkg;
158
159 3 ▶ if($cls->isa('Exporter')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cls->isa("Exporter")) 3 0 3
160 3 goto &Exporter::import;
161 }
162 0 1;
163 }
164  
165 7 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
166 7 my ($cls,@options) = @_;
167
168 7 ▶ if($cls ne __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cls ne __PACKAGE__) 3 4 7
169 3 goto &_import_as_base;
170 }
171 4 1;
172 }
173 1 1;
174  
175 __END__
↑ Summary