blib/lib/Test2/Tools/Mock.pm

filter: Statementstmt 99.3% 161 / 162 Branchbran 84.8% 56 / 66 Conditioncond 70.6% 41 / 58 MC/DCmcdc 45.4% 15 / 33 Subroutinesub 100% 27 / 27 Podpod 92.8% 13 / 14 totaltotal 86.9% 313 / 360 CC 56 SCAR 18.2
CC
56
Cov
90%
_delegate
13.2
mock_build
8.5
mock_class
8.1
CRAP 6.2
« blib/lib/Test2/Util/Times.pm ↑ Summary blib/lib/Test2/Compare/Delta.pm »
1 package Test2::Tools::Mock;
2 162 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 162 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 162 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 162 ▶use Scalar::Util qw/blessed reftype weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 162 ▶use Test2::Util qw/try/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 162 ▶use Test2::Util::Sub qw/gen_accessor gen_reader gen_writer/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 162 ▶use Test2::Mock();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 162 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 162 our $VERSION = '1.302226';
15  
16 162 our @CARP_NOT = (__PACKAGE__, 'Test2::Mock');
17 162 our @EXPORT = qw/mock mocked/;
18 162 our @EXPORT_OK = qw{
19 mock_obj mock_class
20 mock_do mock_build
21 mock_accessor mock_accessors
22 mock_getter mock_getters
23 mock_setter mock_setters
24 mock_building
25 };
26  
27 162 my %HANDLERS;
28 my %MOCKS;
29 162 my @BUILD;
30  
31 35 ▶sub add_handler {
Subroutine add_handler: called CC=4 SCAR=15.0
Pod undocumented
32 35 my $class = shift;
33 35 my ($for, $code) = @_;
34  
35 35 ▶ croak "Must specify a package for the mock handler"
Branch: 1 branch
BranchTrueFalseTotal
unless $for 0 35 35
36 unless $for;
37  
38 35 ▶ croak "Handlers must be code references (got: $code)"
Branch: 1 branch
BranchTrueFalseTotal
unless $code and ref $code eq "CODE" 0 35 35
Condition33%1/3
$code and ref $code eq "CODE"
!ll&&!rl&&r
0035
MC/DC0%0/2
$code and ref $code eq "CODE"
$code ref $code eq "CODE"
Truth table33%1/3
A and B
A= $codeB= ref $code eq "CODE"
ABresult
0X0
100
111
39 unless $code && ref($code) eq 'CODE';
40  
41 35 push @{$HANDLERS{$for}} => $code;
42 }
43  
44 7 ▶sub mock_building {
Subroutine mock_building: called CC=2 SCAR=6.9
Pod documented
45 7 ▶ return unless @BUILD;
Branch: 1 branch
BranchTrueFalseTotal
unless @BUILD 4 3 7
46 3 return $BUILD[-1];
47 }
48  
49 44 ▶sub mocked {
Subroutine mocked: called CC=6 SCAR=17.9
Pod documented
50 44 my $proto = shift;
51 44 ▶ my $class = blessed($proto) || $proto;
Condition66%2/3
builtin::blessed($proto) || $proto
l!l&&r!l&&!r
12320
MC/DC0%0/2
builtin::blessed($proto) || $proto
builtin::blessed($proto) $proto
Truth table66%2/3
A || B
A= builtin::blessed($proto)B= $proto
ABresult
1X1
011
000
52  
53 # Check if we have any mocks.
54 44 ▶ my $set = $MOCKS{$class} || return;
Condition100%2/2
$MOCKS{$class} || (return)
l!l
2222
MC/DC100%1/1
$MOCKS{$class} || (return)
$MOCKS{$class}
Truth table100%2/2
A
A= $MOCKS{$class}
Aresult
11
00
55  
56 # Remove dead mocks (undef due to weaken)
57 22 ▶ pop @$set while @$set && !defined($set->[-1]);
Branch: 1 branch
BranchTrueFalseTotal
@$set and not defined $set->[-1] and do { pop @$set } 7 22 29
Condition100%3/3
@$set and not defined $set->[-1]
!ll&&!rl&&r
7157
MC/DC100%2/2
@$set and not defined $set->[-1]
@$set not defined $set->[-1]
Truth table100%3/3
A and not(B)
A= @$setB= not defined $set->[-1]
ABresult
0X0
100
111
58  
59 # Remove the list if it is empty
60 22 ▶ delete $MOCKS{$class} unless @$set;
Branch: 1 branch
BranchTrueFalseTotal
unless @$set 7 15 22
61  
62 # Return the controls (may be empty list)
63 22 return @$set;
64 }
65  
66 24 ▶sub _delegate {
Subroutine _delegate: called CC=13 SCAR=25.8
67 24 my ($args) = @_;
68  
69 24 my $do = __PACKAGE__->can('mock_do');
70 24 my $obj = __PACKAGE__->can('mock_obj');
71 24 my $class = __PACKAGE__->can('mock_class');
72 24 my $build = __PACKAGE__->can('mock_build');
73  
74 24 ▶ return $obj unless @$args;
Branch: 1 branch
BranchTrueFalseTotal
unless @$args 1 23 24
75  
76 23 my ($proto, $arg1) = @$args;
77  
78 23 ▶ return $obj if ref($proto) && !blessed($proto);
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto and not builtin::blessed($proto) 1 22 23
Condition100%3/3
ref $proto and not builtin::blessed($proto)
!ll&&!rl&&r
2021
MC/DC100%2/2
ref $proto and not builtin::blessed($proto)
ref $proto not builtin::blessed($proto)
Truth table100%3/3
A and not(B)
A= ref $protoB= not builtin::blessed($proto)
ABresult
0X0
100
111
79  
80 22 ▶ if (blessed($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($proto)) 2 20 22
81 2 ▶ return $class unless $proto->isa('Test2::Mock');
Branch: 1 branch
BranchTrueFalseTotal
unless $proto->isa("Test2::Mock") 1 1 2
82 1 ▶ return $build if $arg1 && ref($arg1) && reftype($arg1) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if $arg1 and ref $arg1 and builtin::reftype($arg1) eq "CODE" 1 0 1
Condition33%2/6
$arg1 and ref $arg1 and builtin::reftype($arg1) eq "CODE"
!ll&&!rl&&r
001
$arg1 and ref $arg1
!ll&&!rl&&r
001
MC/DC0%0/3
$arg1 and ref $arg1 and builtin::reftype($arg1) eq "CODE"
$arg1 ref $arg1 builtin::reftype($arg1) eq "CODE"
Truth table25%1/4
A and B and C
A= $arg1B= ref $arg1C= builtin::reftype($arg1) eq "CODE"
ABCresult
0XX0
10X0
1100
1111
83 }
84  
85 20 ▶ return $class if $proto =~ m/(?:::|')/;
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ /(?:::|')/ 5 15 20
86 15 ▶ return $class if $proto =~ m/^_*[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ /^_*[A-Z]/ 12 3 15
87  
88 3 ▶ return $do if Test2::Mock->can($proto);
Branch: 1 branch
BranchTrueFalseTotal
if "Test2::Mock"->can($proto) 1 2 3
89  
90 2 ▶ if (my $sub = __PACKAGE__->can("mock_$proto")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = __PACKAGE__->can("mock_$proto")) 1 1 2
91 1 shift @$args;
92 1 return $sub;
93 }
94  
95 1 return undef;
96 }
97  
98 25 ▶sub mock {
Subroutine mock: called CC=4 SCAR=13.9
Pod documented
99 25 ▶ croak "undef is not a valid first argument to mock()"
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not defined $_[0] 1 24 25
Condition100%3/3
@_ and not defined $_[0]
!ll&&!rl&&r
1231
MC/DC100%2/2
@_ and not defined $_[0]
@_ not defined $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not defined $_[0]
ABresult
0X0
100
111
100 if @_ && !defined($_[0]);
101  
102 24 my $sub = _delegate(\@_);
103  
104 24 ▶ croak "'$_[0]' does not look like a package name, and is not a valid control method"
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 1 23 24
105 unless $sub;
106  
107 23 $sub->(@_);
108 }
109  
110 8 ▶sub mock_build {
Subroutine mock_build: called CC=8 SCAR=21.4
Pod documented
111 8 my ($control, $sub) = @_;
112  
113 8 ▶ croak "mock_build requires a Test2::Mock object as its first argument"
Branch: 1 branch
BranchTrueFalseTotal
unless $control and builtin::blessed($control) and $control->isa("Test2::Mock") 2 6 8
Condition66%4/6
$control and builtin::blessed($control) and $control->isa("Test2::Mock")
!ll&&!rl&&r
206
$control and builtin::blessed($control)
!ll&&!rl&&r
206
MC/DC33%1/3
$control and builtin::blessed($control) and $control->isa("Test2::Mock")
$control builtin::blessed($control) $control->isa("Test2::Mock")
Truth table50%2/4
A and B and C
A= $controlB= builtin::blessed($control)C= $control->isa("Test2::Mock")
ABCresult
0XX0
10X0
1100
1111
114 unless $control && blessed($control) && $control->isa('Test2::Mock');
115  
116 6 ▶ croak "mock_build requires a coderef as its second argument"
Branch: 1 branch
BranchTrueFalseTotal
unless $sub and ref $sub and builtin::reftype($sub) eq "CODE" 2 4 6
Condition66%4/6
$sub and ref $sub and builtin::reftype($sub) eq "CODE"
!ll&&!rl&&r
204
$sub and ref $sub
!ll&&!rl&&r
204
MC/DC33%1/3
$sub and ref $sub and builtin::reftype($sub) eq "CODE"
$sub ref $sub builtin::reftype($sub) eq "CODE"
Truth table50%2/4
A and B and C
A= $subB= ref $subC= builtin::reftype($sub) eq "CODE"
ABCresult
0XX0
10X0
1100
1111
117 unless $sub && ref($sub) && reftype($sub) eq 'CODE';
118  
119 4 push @BUILD => $control;
120 4 my ($ok, $err) = &try($sub);
121 4 pop @BUILD;
122 4 ▶ die $err unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
123 }
124  
125 7 ▶sub mock_do {
Subroutine mock_do: called CC=4 SCAR=13.9
Pod documented
126 7 my ($meth, @args) = @_;
127  
128 7 ▶ croak "Not currently building a mock"
Branch: 1 branch
BranchTrueFalseTotal
unless @BUILD 2 5 7
129 unless @BUILD;
130  
131 5 my $build = $BUILD[-1];
132  
133 5 ▶ croak "'$meth' is not a valid action for mock_do()"
Branch: 1 branch
BranchTrueFalseTotal
if $meth =~ /^_/ or not $build->can($meth) 2 3 5
Condition66%2/3
$meth =~ /^_/ or not $build->can($meth)
l!l&&r!l&&!r
023
MC/DC50%1/2
$meth =~ /^_/ or not $build->can($meth)
$meth =~ /^_/ not $build->can($meth)
Truth table66%2/3
A or not(B)
A= $meth =~ /^_/B= not $build->can($meth)
ABresult
1X1
011
000
134 if $meth =~ m/^_/ || !$build->can($meth);
135  
136 3 $build->$meth(@args);
137 }
138  
139 6 ▶sub mock_obj {
Subroutine mock_obj: called CC=6 SCAR=17.9
Pod documented
140 6 my ($proto) = @_;
141  
142 6 ▶ if ($proto && ref($proto) && reftype($proto) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto and ref $proto and builtin::reftype($proto) ne 'CODE') { } 4 2 6
Condition83%5/6
$proto and ref $proto and builtin::reftype($proto) ne "CODE"
!ll&&!rl&&r
114
$proto and ref $proto
!ll&&!rl&&r
105
MC/DC66%2/3
$proto and ref $proto and builtin::reftype($proto) ne "CODE"
$proto ref $proto builtin::reftype($proto) ne "CODE"
Truth table75%3/4
A and B and C
A= $protoB= ref $protoC= builtin::reftype($proto) ne "CODE"
ABCresult
0XX0
10X0
1100
1111
143 4 shift @_;
144 }
145 else {
146 2 $proto = {};
147 }
148  
149 6 my $class = _generate_class();
150 6 my $control;
151  
152 6 ▶ if (@_ == 1 && reftype($_[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and builtin::reftype($_[0]) eq 'CODE') { } 1 5 6
Condition66%2/3
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
!ll&&!rl&&r
501
MC/DC50%1/2
@_ == 1 and builtin::reftype($_[0]) eq "CODE"
@_ == 1 builtin::reftype($_[0]) eq "CODE"
Truth table66%2/3
A and B
A= @_ == 1B= builtin::reftype($_[0]) eq "CODE"
ABresult
0X0
100
111
153 1 my $orig = shift @_;
154 $control = mock_class(
155 $class,
156 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 1 my $c = mock_building;
158  
159 # We want to do these BEFORE anything that the sub may do.
160 1 $c->block_load(1);
161 1 $c->purge_on_destroy(1);
162 1 $c->autoload(1);
163  
164 1 $orig->(@_);
165 },
166 1 );
167 }
168 else {
169 5 $control = mock_class(
170 $class,
171 # Do these before anything the user specified.
172 block_load => 1,
173 purge_on_destroy => 1,
174 autoload => 1,
175 @_,
176 );
177 }
178  
179 6 my $new = bless($proto, $control->class);
180  
181 # We need to ensure there is a reference to the control object, and we want
182 # it to go away with the object.
183 6 $new->{'~~MOCK~CONTROL~~'} = $control;
184 6 return $new;
185 }
186  
187 6 ▶sub _generate_class {
Subroutine _generate_class: called CC=5 SCAR=16.3
188 6 my $prefix = __PACKAGE__;
189  
190 6 for (1 .. 100) {
191 my $postfix = join '', map {
192 6 my $cp = rand(26) + 65;
193 192 ▶ $cp = utf8::unicode_to_native($cp) if "$]" >= 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if "$]" >= "5.008" 192 0 192
194 192 chr($cp);
195 } 1 .. 32;
196 6 my $class = $prefix . '::__TEMP__::' . $postfix;
197 6 my $file = $class;
198 6 $file =~ s{::}{/}g;
199 6 $file .= '.pm';
200 6 ▶ next if $INC{$file};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{$file} 0 6 6
201 162 ▶ my $stash = do { no strict 'refs'; \%{"${class}\::"} };
Subroutine BEGIN: called CC=1 SCAR=0.0
202 6 ▶ next if keys %$stash;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$stash 0 6 6
203 6 return $class;
204 }
205  
206 0 croak "Could not generate a unique class name after 100 attempts";
207 }
208  
209 29 ▶sub mock_class {
Subroutine mock_class: called CC=8 SCAR=20.9
Pod documented
210 29 my $proto = shift;
211 29 ▶ my $class = blessed($proto) || $proto;
Condition66%2/3
builtin::blessed($proto) || $proto
l!l&&r!l&&!r
2270
MC/DC0%0/2
builtin::blessed($proto) || $proto
builtin::blessed($proto) $proto
Truth table66%2/3
A || B
A= builtin::blessed($proto)B= $proto
ABresult
1X1
011
000
212 29 my @args = @_;
213  
214 29 my $void = !defined(wantarray);
215  
216 30 ▶ my $callback = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
217 30 my ($parent) = reverse mocked($class);
218 30 my $control;
219  
220 30 ▶ if (@args == 1 && ref($args[0]) && reftype($args[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] and builtin::reftype($args[0]) eq 'CODE') { } 1 29 30
Condition66%4/6
@args == 1 and ref $args[0] and builtin::reftype($args[0]) eq "CODE"
!ll&&!rl&&r
2901
@args == 1 and ref $args[0]
!ll&&!rl&&r
2901
MC/DC33%1/3
@args == 1 and ref $args[0] and builtin::reftype($args[0]) eq "CODE"
@args == 1 ref $args[0] builtin::reftype($args[0]) eq "CODE"
Truth table50%2/4
A and B and C
A= @args == 1B= ref $args[0]C= builtin::reftype($args[0]) eq "CODE"
ABCresult
0XX0
10X0
1100
1111
221 1 $control = Test2::Mock->new(class => $class);
222 1 mock_build($control, @args);
223 }
224 else {
225 29 $control = Test2::Mock->new(class => $class, @args);
226 }
227  
228 29 ▶ if ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 3 26 29
229 3 $control->{parent} = $parent;
230 3 weaken($parent->{child} = $control);
231 }
232  
233 29 ▶ $MOCKS{$class} ||= [];
Condition100%2/2
$MOCKS{$class} ||= []
l!l
326
MC/DC100%1/1
$MOCKS{$class} ||= []
$MOCKS{$class}
Truth table100%2/2
A
A= $MOCKS{$class}
Aresult
11
00
234 29 push @{$MOCKS{$class}} => $control;
235 29 weaken($MOCKS{$class}->[-1]);
236  
237 29 return $control;
238 29 };
239  
240 29 ▶ return $callback->() unless $void;
Branch: 1 branch
BranchTrueFalseTotal
unless $void 21 8 29
241  
242 8 my $level = 0;
243 8 my $caller;
244 8 ▶ while (my @call = caller($level++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@call) = caller $level++) 16 0 16
245 16 ▶ next if $call[0] eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if $call[0] eq __PACKAGE__ 8 8 16
246 8 $caller = \@call;
247 8 last;
248 }
249  
250 8 my $handled;
251 8 for my $handler (@{$HANDLERS{$caller->[0]}}) {
252 8 ▶ $handled++ if $handler->(
Branch: 1 branch
BranchTrueFalseTotal
if &$handler("class", $class, "caller", $caller, "builder", $callback, "args", \@args) 8 0 8
253 class => $class,
254 caller => $caller,
255 builder => $callback,
256 args => \@args,
257 );
258 }
259  
260 8 ▶ croak "mock_class should not be called in a void context without a registered handler"
Branch: 1 branch
BranchTrueFalseTotal
unless $handled 0 8 8
261 unless $handled;
262 }
263  
264 1 ▶sub mock_accessors {
Subroutine mock_accessors: called CC=1 SCAR=0.0
Pod documented
265 1 return map {( $_ => gen_accessor($_) )} @_;
266 }
267  
268 2 ▶sub mock_accessor {
Subroutine mock_accessor: called CC=1 SCAR=0.0
Pod documented
269 2 my ($field) = @_;
270 2 return gen_accessor($field);
271 }
272  
273 1 ▶sub mock_getters {
Subroutine mock_getters: called CC=1 SCAR=0.0
Pod documented
274 1 my ($prefix, @list) = @_;
275 1 return map {( "$prefix$_" => gen_reader($_) )} @list;
276 }
277  
278 1 ▶sub mock_getter {
Subroutine mock_getter: called CC=1 SCAR=0.0
Pod documented
279 1 my ($field) = @_;
280 1 return gen_reader($field);
281 }
282  
283 1 ▶sub mock_setters {
Subroutine mock_setters: called CC=1 SCAR=0.0
Pod documented
284 1 my ($prefix, @list) = @_;
285 1 return map {( "$prefix$_" => gen_writer($_) )} @list;
286 }
287  
288 1 ▶sub mock_setter {
Subroutine mock_setter: called CC=1 SCAR=0.0
Pod documented
289 1 my ($field) = @_;
290 1 return gen_writer($field);
291 }
292  
293 162 1;
294  
295 __END__
« blib/lib/Test2/Util/Times.pm ↑ Summary blib/lib/Test2/Compare/Delta.pm »