Sub/Child.pm

filter: Statementstmt 100% 117 / 117 Branchbran 96.0% 48 / 50 Conditioncond 89.1% 33 / 37 MC/DCmcdc 79.1% 19 / 24 Subroutinesub 96.1% 25 / 26 Podpod 100% 11 / 11 totaltotal 95.4% 253 / 265 CC 41 SCAR 22.5
CC
41
Cov
97%
_mock
21.0
__ANON__
11.0
unmock
4.0
CRAP 9.5
↑ Summary Sub.pm »
1 package Mock::Sub::Child;
2 20 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 our $VERSION = '1.09';
10  
11 69 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
12 69 my $self = bless {}, shift;
13 69 %{ $self } = @_;
14  
15 69 ▶ if ($self->{side_effect}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"side_effect"}) 2 67 69
16 2 $self->_check_side_effect($self->{side_effect});
17 }
18 68 return $self;
19 }
20 77 ▶sub _mock {
Subroutine _mock: called CC=21 SCAR=30.5
21 77 my $self = shift;
22  
23 # throw away the sub name if it's sent in and we're not called
24 # by Mock::Sub::mock()
25  
26 77 my $sub_passed_in;
27 77 ▶ if ($_[0] && $_[0] =~ /::/){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and $_[0] =~ /::/) 64 13 77
Condition100%3/3
$_[0] and $_[0] =~ /::/
!ll&&!rl&&r
7664
MC/DC100%2/2
$_[0] and $_[0] =~ /::/
$_[0] $_[0] =~ /::/
Truth table100%3/3
A and B
A= $_[0]B= $_[0] =~ /::/
ABresult
0X0
100
111
28 64 $sub_passed_in = 1;
29 }
30  
31 77 ▶ my $caller = (caller(1))[3] || '';
Condition50%1/2
(caller 1)[3] || ''
l!l
770
MC/DC0%0/1
(caller 1)[3] || ''
(caller 1)[3]
Truth table50%1/2
A
A= (caller 1)[3]
Aresult
11
00
32
33 77 ▶ if ($caller ne 'Mock::Sub::mock' && $sub_passed_in){
Branch: 1 branch
BranchTrueFalseTotal
if ($caller ne "Mock::Sub::mock" and $sub_passed_in) 3 74 77
Condition100%3/3
$caller ne "Mock::Sub::mock" and $sub_passed_in
!ll&&!rl&&r
64103
MC/DC100%2/2
$caller ne "Mock::Sub::mock" and $sub_passed_in
$caller ne "Mock::Sub::mock" $sub_passed_in
Truth table100%3/3
A and B
A= $caller ne "Mock::Sub::mock"B= $sub_passed_in
ABresult
0X0
100
111
34 3 undef @_;
35 3 ▶ if(ref($self) eq 'Mock::Sub::Child' && ! $self->{name}){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq "Mock::Sub::Child" and not $self->{"name"}) 1 2 3
Condition66%2/3
ref $self eq "Mock::Sub::Child" and not $self->{"name"}
!ll&&!rl&&r
021
MC/DC50%1/2
ref $self eq "Mock::Sub::Child" and not $self->{"name"}
ref $self eq "Mock::Sub::Child" not $self->{"name"}
Truth table66%2/3
A and not(B)
A= ref $self eq "Mock::Sub::Child"B= not $self->{"name"}
ABresult
0X0
100
111
36 1 confess "can't call mock() on a child object before it is already " .
37 "initialized with the parent mock object. ";
38 }
39 }
40  
41 76 ▶ if ($caller ne 'Mock::Sub::mock' && $caller ne 'Mock::Sub::Child::remock'){
Branch: 1 branch
BranchTrueFalseTotal
if ($caller ne "Mock::Sub::mock" and $caller ne "Mock::Sub::Child::remock") 1 75 76
Condition100%3/3
$caller ne "Mock::Sub::mock" and $caller ne "Mock::Sub::Child::remock"
!ll&&!rl&&r
64111
MC/DC100%2/2
$caller ne "Mock::Sub::mock" and $caller ne "Mock::Sub::Child::remock"
$caller ne "Mock::Sub::mock" $caller ne "Mock::Sub::Child::remock"
Truth table100%3/3
A and B
A= $caller ne "Mock::Sub::mock"B= $caller ne "Mock::Sub::Child::remock"
ABresult
0X0
100
111
42 1 confess "the _mock() method is not a public API call. For re-mocking " .
43 "an existing sub in an existing sub object, use remock().\n";
44 }
45  
46 75 ▶ my $sub = $self->name || shift;
Condition66%2/3
$self->name || shift()
l!l&&r!l&&!r
11640
MC/DC0%0/2
$self->name || shift()
$self->name shift()
Truth table66%2/3
A || B
A= $self->nameB= shift()
ABresult
1X1
011
000
47  
48 75 my %p = @_;
49 75 for (keys %p){
50 3 $self->{$_} = $p{$_};
51 }
52  
53 75 ▶ if ($sub !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub =~ /::/) 3 72 75
54 3 my $core_sub = "CORE::" . $sub;
55  
56 3 ▶ if (defined &$core_sub && ${^GLOBAL_PHASE} eq 'START') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$core_sub and ${^GLOBAL_PHASE} eq 'START') { } 1 2 3
Condition66%2/3
defined &$core_sub and ${^GLOBAL_PHASE} eq "START"
!ll&&!rl&&r
201
MC/DC50%1/2
defined &$core_sub and ${^GLOBAL_PHASE} eq "START"
defined &$core_sub ${^GLOBAL_PHASE} eq "START"
Truth table66%2/3
A and B
A= defined &$core_subB= ${^GLOBAL_PHASE} eq "START"
ABresult
0X0
100
111
57 1 warn "WARNING! we're attempting to override a global core " .
58 "function. You will NOT be able to restore functionality " .
59 "to this function.";
60  
61 1 $sub = "CORE::GLOBAL::" . $sub;
62 }
63 else {
64 2 ▶ $sub = "main::$sub" if $sub !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /::/ 2 0 2
65 }
66 }
67  
68 75 my $fake;
69  
70 75 ▶ if (! exists &$sub && $sub !~ /CORE::GLOBAL/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists &$sub or $sub =~ /CORE::GLOBAL/) 5 70 75
Condition100%3/3
exists &$sub or $sub =~ /CORE::GLOBAL/
l!l&&r!l&&!r
6915
MC/DC100%2/2
exists &$sub or $sub =~ /CORE::GLOBAL/
exists &$sub $sub =~ /CORE::GLOBAL/
Truth table100%3/3
A or B
A= exists &$subB= $sub =~ /CORE::GLOBAL/
ABresult
1X1
011
000
71 5 $fake = 1;
72 5 ▶ if (! $self->_no_warn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_no_warn) 4 1 5
73 4 warn "\n\nWARNING!: we've mocked a non-existent subroutine. ".
74 "the specified sub does not exist.\n\n";
75 }
76 }
77  
78 75 $self->_check_side_effect($self->{side_effect});
79  
80 75 ▶ if (defined $self->{return_value}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"return_value"}) 3 72 75
81 3 push @{ $self->{return} }, $self->{return_value};
82 }
83  
84 75 $self->{name} = $sub;
85 75 ▶ $self->{orig} = \&$sub if ! $fake;
Branch: 1 branch
BranchTrueFalseTotal
unless $fake 70 5 75
86  
87 75 $self->{called_count} = 0;
88  
89 {
90 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 20 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 75 my $mock = $self;
94 75 weaken $mock;
95  
96 94 ▶ *$sub = sub {
Subroutine __ANON__: called CC=11 SCAR=24.0
97  
98 94 @{ $mock->{called_with} } = @_;
99 94 ++$mock->{called_count};
100  
101 94 ▶ if ($mock->{side_effect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mock->{"side_effect"}) 14 80 94
102 14 ▶ if (wantarray){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 12 14
103 2 my @effect = $mock->{side_effect}->(@_);
104 2 ▶ return @effect if @effect;
Branch: 1 branch
BranchTrueFalseTotal
if @effect 2 0 2
105 }
106 else {
107 12 my $effect = $mock->{side_effect}->(@_);
108 11 ▶ return $effect if defined $effect;
Branch: 1 branch
BranchTrueFalseTotal
if defined $effect 10 1 11
109 }
110 }
111  
112 81 ▶ return if ! defined $mock->{return};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mock->{"return"} 6 75 81
113  
114 75 ▶ if ($mock->{return}[0] && $mock->{return}[0] eq 'params'){
Branch: 1 branch
BranchTrueFalseTotal
if ($mock->{'return'}[0] and $mock->{'return'}[0] eq 'params') { } 3 72 75
Condition100%3/3
$mock->{"return"}[0] and $mock->{"return"}[0] eq "params"
!ll&&!rl&&r
38343
MC/DC100%2/2
$mock->{"return"}[0] and $mock->{"return"}[0] eq "params"
$mock->{"return"}[0] $mock->{"return"}[0] eq "params"
Truth table100%3/3
A and B
A= $mock->{"return"}[0]B= $mock->{"return"}[0] eq "params"
ABresult
0X0
100
111
115 3 ▶ return ! wantarray ? $_[0] : @_;
Branch: 1 branch
BranchTrueFalseTotal
!wantarray ? : 2 1 3
116 }
117 else {
118 70 return ! wantarray && @{ $mock->{return} } == 1
119 ? $mock->{return}[0]
120 72 ▶ : @{ $mock->{return} };
Branch: 1 branch
BranchTrueFalseTotal
!wantarray && @{$mock->{'return'};} == 1 ? : 69 3 72
Condition100%3/3
not wantarray and @{$mock->{"return"};} == 1
!ll&&!rl&&r
2169
MC/DC100%2/2
not wantarray and @{$mock->{"return"};} == 1
not wantarray @{$mock->{"return"};} == 1
Truth table100%3/3
not(A) and B
A= not wantarrayB= @{$mock->{"return"};} == 1
ABresult
0X0
100
111
121 }
122 75 };
123 }
124 75 $self->{state} = 1;
125  
126 75 return $self;
127 }
128 11 ▶sub remock {
Subroutine remock: called CC=1 SCAR=0.0
Pod documented
129 11 shift->_mock(@_);
130 }
131 80 ▶sub unmock {
Subroutine unmock: called CC=4 SCAR=13.9
Pod documented
132 80 my $self = shift;
133 80 my $sub = $self->{name};
134  
135 {
136 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
137 20 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
138  
139 80 ▶ if (defined $self->{orig} && $sub !~ /CORE::GLOBAL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'orig'} and not $sub =~ /CORE::GLOBAL/) { } 68 12 80
Condition100%3/3
defined $self->{"orig"} and not $sub =~ /CORE::GLOBAL/
!ll&&!rl&&r
11168
MC/DC100%2/2
defined $self->{"orig"} and not $sub =~ /CORE::GLOBAL/
defined $self->{"orig"} not $sub =~ /CORE::GLOBAL/
Truth table100%3/3
A and not(B)
A= defined $self->{"orig"}B= not $sub =~ /CORE::GLOBAL/
ABresult
0X0
100
111
140 68 *$sub = \&{ $self->{orig} };
141 }
142 else {
143 12 ▶ undef *$sub if $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"name"} 7 5 12
144 }
145 }
146  
147 79 $self->{state} = 0;
148 79 $self->reset;
149 }
150 15 ▶sub called {
Subroutine called: called CC=2 SCAR=6.9
Pod documented
151 15 ▶ return shift->called_count ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(shift())->called_count ? : 11 4 15
152 }
153 34 ▶sub called_count {
Subroutine called_count: called CC=2 SCAR=6.9
Pod documented
154 34 ▶ return shift->{called_count} || 0;
Condition100%2/2
+(shift())->{'called_count'} || 0
l!l
277
MC/DC100%1/1
+(shift())->{'called_count'} || 0
+(shift())->{'called_count'}
Truth table100%2/2
A
A= +(shift())->{'called_count'}
Aresult
11
00
155 }
156 7 ▶sub called_with {
Subroutine called_with: called CC=2 SCAR=6.9
Pod documented
157 7 my $self = shift;
158 7 ▶ if (! $self->called){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->called) 1 6 7
159 1 confess "\n\ncan't call called_with() before the mocked sub has " .
160 "been called. ";
161 }
162 6 return @{ $self->{called_with} };
163 }
164 83 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
165 83 return shift->{name};
166 }
167 84 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
168 84 for (qw(side_effect return_value return called called_count called_with)){
169 504 delete $_[0]->{$_};
170 }
171 }
172 76 ▶sub return_value {
Subroutine return_value: called CC=1 SCAR=0.0
Pod documented
173 76 my $self = shift;
174 76 @{ $self->{return} } = @_;
175 }
176 73 ▶sub side_effect {
Subroutine side_effect: called CC=1 SCAR=0.0
Pod documented
177 73 $_[0]->_check_side_effect($_[1]);
178 70 $_[0]->{side_effect} = $_[1];
179 }
180 150 ▶sub _check_side_effect {
Subroutine _check_side_effect: called CC=3 SCAR=11.0
181 150 ▶ if (defined $_[1] && ref $_[1] ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and ref $_[1] ne "CODE") 4 146 150
Condition100%3/3
defined $_[1] and ref $_[1] ne "CODE"
!ll&&!rl&&r
123234
MC/DC100%2/2
defined $_[1] and ref $_[1] ne "CODE"
defined $_[1] ref $_[1] ne "CODE"
Truth table100%3/3
A and B
A= defined $_[1]B= ref $_[1] ne "CODE"
ABresult
0X0
100
111
182 4 confess "\n\nside_effect parameter must be a code reference. ";
183 }
184 }
185 38 ▶sub mocked_state {
Subroutine mocked_state: called CC=1 SCAR=0.0
Pod documented
186 38 return shift->{state};
187 }
188 5 ▶sub _no_warn {
Subroutine _no_warn: called CC=1 SCAR=0.0
189 5 return $_[0]->{no_warnings};
190 }
191 70 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
192 70 $_[0]->unmock;
193 }
194 0 ▶sub _end {}; # vim fold placeholder
Subroutine _end: not called CC=1 SCAR=0.0
195  
196 __END__
↑ Summary Sub.pm »