blib/lib/Jasmine/Spy.pm

filter: Statementstmt 97.0% 296 / 305 Branchbran 78.9% 30 / 38 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 97.5% 81 / 83 Podpod 100% 4 / 4 totaltotal 92.0% 418 / 454 SCAR 32.7
CC
26
Cov
93%
__callFake
7.4
new
5.0
__returnFromValue
4.0
CRAP 26.2
↑ Summary
1 package Jasmine::Spy;
2 # ABSTRACT: Mocking library for perl inspired by Jasmine's spies
3 1 $Jasmine::Spy::VERSION = '1.01';
4 =head1 NAME
5  
6 Jasmine::Spy
7  
8 =head1 VERSION
9  
10 version 1.01
11  
12 =head1 SYNOPSIS
13  
14 use Test::Spec;
15 use Jasmine::Spy qw(spyOn stopSpying expectSpy);
16  
17 describe "FooClass" => sub {
18 before each => sub {
19 spyOn("BarClass", "bazMethod")->andReturn("Bop");
20 spyOn("BarClass", "bam")->andCallThrough;
21 };
22 it "calls BarClass" => sub {
23 FooClass->doTheThing();
24 expectSpy("BarClass", "bazMethod")->toHaveBeenCalled();
25 };
26 it "calls BarClass->bat method with 'bam'" => sub {
27 FooClass->doTheThing();
28 expectSpy("BarClass", "bat")->toHaveBeenCalledWith('bam')
29 };
30 after each => sub {
31 stopSpying("BarClass");
32 };
33 };
34  
35 =head1 Methods
36  
37 Nothing is exported by default, but they cann all be pulled in with the :all tag
38  
39 =head2 Base Class Methods
40  
41 =over 1
42  
43 =item spyOn($invocant, $method)
44  
45 This is the setup method to begin spying. $invocant may be either an object instance or the name of
46 a class. Spying on a Class will automatically spy on all instances of the class, even those created
47 before setting up the spy. Spyng on an instance only effects that instance, not the class or
48 other instances of that class.
49  
50 A "spy" object is returned from this call which will allow introspection and testing of
51 calls. However there is no need to catch this, as other convience methods provide a better
52 way of performing the same introspection later.
53  
54 =item stopSpying($invocant)
55  
56 Use this call to stop spying and restore original functionality to the object or class.
57  
58 =item expectSpy($invocant, $method)
59  
60 Use this to retrieve the "spy" object created by spyOn. It also sets the spy object to
61 introspect of the provided C<$method>. There is only one spy object created for each
62 distinct $invocant beign spied on, even if multiple methods are being watched. This is why
63 C<expectSpy> is the recomended way to start introspection on a spied method.
64  
65 =item getCalls($invocant, $method)
66  
67 This will fetch an array of array's containing the arguments passed each time the C<$method>
68 was called. This is a tied array ref which also provides convience methods C<first> and
69 C<mostRecent>.
70  
71 =back
72  
73 =head2 Spy object methods
74  
75 =over 1
76  
77 =item toHaveBeenCalled
78  
79 Test that the spied method has been called atleast once.
80  
81 =item notToHaveBeenCalled
82  
83 Test that the spied method was never called.
84  
85 =item toHaveBeenCalledWith($matchers)
86  
87 Expects that the spied method has been called with arguments matching C<$matchers> atleast once.
88 This is done with deep comparison via L<Test::Deep>.
89  
90 =item notToHaveBeenCalledWith($matchers)
91  
92 Inverse of toHaveBeenCalledWith.
93  
94 =item andReturn($value)
95  
96 Sets the spied method to return the supplied value. Usually this would be called directly
97 on the return from C<spyOn>.
98  
99 For example:
100  
101 spyOn($foo, 'bar')->andReturn('baz')
102  
103 =item andCallThrough
104  
105 Sets the spied method to call through to the original method, recording arguments passed along
106 the way.
107  
108 =item andCallFake(sub {})
109  
110 Sets the spied method to invoke the supplied code reference in place of the original method.
111 It does also record the arguments along the way.
112  
113 =back
114  
115 =head2 Qunatifiers
116  
117 Quantifiers may be called on the return of either C<toHaveBeenCalled> or C<toHaveBeenCalledWith>
118 to stipulate how many times the method should have been called.
119  
120 =over 1
121  
122 =item once()
123  
124 The method was called exactly one time
125  
126 =item atleast(X)
127  
128 Method was called atleast X times
129  
130 =item atMost(X)
131  
132 Method was not called more than X times
133  
134 =item exactly(X)
135  
136 Method was called exactly X times.
137  
138 =back
139  
140 =head1 TODO
141  
142 =over 1
143  
144 =item Convience Method for andThrow
145  
146 Having put some thought into this, I haven't come up with a clean robust way to handle it.
147 In the end, I think you are better off using C<andCallFake> to throw an exception
148 if you need to test that.
149  
150 =back
151  
152 =head1 See also
153  
154 L<Test::Spec>, L<Test::Deep>
155  
156 =cut
157  
158 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 1 ▶use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
161 1 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
162 1 ▶use Class::MOP;
Subroutine BEGIN: called CC=1 SCAR=0.0
163  
164  
165 1 my (%spies) = ();
166  
167 BEGIN {
168 1 ▶ @EXPORT = ();
Subroutine BEGIN: called CC=1 SCAR=0.0
169 1 @EXPORT_OK = qw(
170 spyOn
171 stopSpying
172 expectSpy
173 getCalls
174 );
175 1 %EXPORT_TAGS = (
176 all => \@EXPORT_OK,
177 );
178 }
179  
180 sub spyOn {
181 40 ▶ my ($proto, $method) = @_;
Subroutine spyOn: called CC=2 SCAR=7.2
Pod documented
182 40 ▶ if(exists($spies{$proto})){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $spies{$proto}) { } 0 40 40
183 0 $spies{$proto}->spyOnMethod($proto, $method);
184 }
185 else {
186 40 my $spy = Jasmine::Spy::Instance->new($proto, $method);
187 40 $spies{$proto} = $spy;
188 }
189 40 return $spies{$proto};
190 }
191  
192 sub stopSpying {
193 22 ▶ my ($proto) = @_;
Subroutine stopSpying: called CC=2 SCAR=6.9
Pod documented
194 22 my $spy = delete $spies{$proto};
195 22 ▶ if($spy){
Branch: 1 branch
BranchTrueFalseTotal
if ($spy) 21 1 22
196 21 $spy->stopSpying;
197 }
198 }
199  
200 sub expectSpy {
201 22 ▶ my($proto, $method) = @_;
Subroutine expectSpy: called CC=1 SCAR=0.0
Pod documented
202 22 $spies{$proto}->setCurrentMethod($method);
203 22 return $spies{$proto};
204 }
205  
206 sub getCalls {
207 4 ▶ expectSpy(@_)->calls;
Subroutine getCalls: called CC=1 SCAR=0.0
Pod documented
208 }
209  
210 package Jasmine::Spy::Instance;
211 1 $Jasmine::Spy::Instance::VERSION = '1.01';
212 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
213 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
214 1 ▶use base qw(Test::Builder::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
215 1 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 ▶use Want;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 sub new {
219 40 ▶ my ($mp, $proto, $method) = @_;
Subroutine new: called CC=5 SCAR=16.1
220 40 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
20200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
221 1 ▶ eval "package $class; use metaclass;" unless ($proto->can("metaclass"));
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless $proto->can("metaclass") 40 0 40
222  
223  
224 40 ▶ my $self = bless(
Condition33%1/3
ref $mp || $mp
l!l&&r!l&&!r
0400
MC/DC0%0/2
ref $mp || $mp
ref $mp $mp
Truth table33%1/3
A || B
A= ref $mpB= $mp
ABresult
1X1
011
000
225 {
226 proto => $proto,
227 class => $class,
228 },
229 ref($mp) || $mp
230 );
231 40 ▶ if (ref($proto)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 20 20 40
232 20 my $spyClass = Class::MOP::Class->create_anon_class(superclasses => [$class]);
233 20 $spyClass->rebless_instance($proto);
234 20 $self->{spyClass} = $spyClass;
235 }
236  
237 40 $self->spyOnMethod($proto, $method);
238  
239 40 return $self;
240 }
241  
242 sub stopSpying {
243 21 ▶ my $self = shift;
Subroutine stopSpying: called CC=3 SCAR=11.0
244 21 ▶ if(ref($self->{proto})){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'proto'}) { } 1 20 21
245 1 $self->{class}->meta->rebless_instance_back($self->{proto});
246 }
247 else {
248 20 foreach my $method (keys %{$self->{original_methods}}){
249 20 $self->{class}->meta->remove_method($method);
250 20 $self->{class}->meta->add_method($method, $self->{original_methods}{$method});
251 }
252 }
253 }
254  
255 sub spyOnMethod {
256 40 ▶ my($self, $proto, $method) = @_;
Subroutine spyOnMethod: called CC=3 SCAR=11.0
257  
258 40 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
20200
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
259 40 my $metaclass = $proto->meta;
260 40 ▶ $metaclass->make_mutable if ($metaclass->is_immutable);
Branch: 1 branch
BranchTrueFalseTotal
if $metaclass->is_immutable 0 40 40
261  
262 40 $self->{current_method} = $method;
263 40 $self->{original_methods}{$method} = $metaclass->get_method($method);
264 40 $metaclass->remove_method($method);
265 40 $self->{spyClass} = $metaclass;
266 40 $self->{responses}{$method} = undef;
267 40 ▶ $metaclass->add_method($method, sub { $self->__callFake($method, @_); });
Subroutine foo: called CC=1 SCAR=0.0
268 }
269  
270 sub setCurrentMethod {
271 22 ▶ my $self = shift;
Subroutine setCurrentMethod: called CC=1 SCAR=0.0
272 22 $self->{current_method} = shift;
273 }
274  
275 sub __callFake {
276 49 ▶ my $self = shift;
Subroutine __callFake: called CC=7 SCAR=20.0
277 49 my $method = shift;
278 49 ▶ if($_[0] eq $self->{proto}){
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq $self->{'proto'}) { } 48 1 49
elsif (ref $_[0] and not ref $self->{'proto'} and $_[0]->isa($self->{'class'})) { } 1 0 1
279 48 shift;
280 }
281 ▶ elsif(ref($_[0]) && !ref($self->{proto}) && $_[0]->isa($self->{class})){
Condition33%2/6
ref $_[0] and not ref $self->{"proto"} and $_[0]->isa($self->{"class"})
!ll&&!rl&&r
001
ref $_[0] and not ref $self->{"proto"}
!ll&&!rl&&r
001
MC/DC0%0/3
ref $_[0] and not ref $self->{"proto"} and $_[0]->isa($self->{"class"})
ref $_[0] not ref $self->{"proto"} $_[0]->isa($self->{"class"})
Truth table25%1/4
A and not(B) and C
A= ref $_[0]B= not ref $self->{"proto"}C= $_[0]->isa($self->{"class"})
ABCresult
0XX0
10X0
1100
1111
282 1 shift;
283 }
284  
285 49 push @{ $self->calls }, [@_];
286 49 ▶ if(ref($self->{responses}{$method}) eq 'CODE'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'responses'}{$method} eq 'CODE') { } 8 41 49
elsif (ref $self->{'responses'}{$method} eq 'Class::MOP::Method') { } 2 39 41
287 8 return $self->{responses}{$method}->(@_);
288 }
289 elsif (ref($self->{responses}{$method}) eq 'Class::MOP::Method') {
290 2 return $self->{responses}{$method}->execute(@_);
291 }
292 39 return $self->__returnFromValue($self->{responses}{$method});
293 }
294  
295 sub __returnFromValue {
296 45 ▶ my $self = shift;
Subroutine __returnFromValue: called CC=4 SCAR=13.9
297 45 my $value = shift;
298 45 ▶ if(wantarray){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 4 41 45
299 4 ▶ if(ref($value) eq 'ARRAY'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 2 4
elsif (ref $value eq 'HASH') { } 2 0 2
300 2 return @$value;
301 }
302 elsif(ref($value) eq 'HASH'){
303 2 return %$value;
304 }
305 }
306 41 return $value;
307 }
308  
309 sub andReturn {
310 10 ▶ my $self = shift;
Subroutine andReturn: called CC=1 SCAR=0.0
311 10 my($ret) = @_;
312 10 $self->{responses}{ $self->{current_method} } = $ret;
313 }
314  
315 sub andReturnValues {
316 2 ▶ my $self = shift;
Subroutine andReturnValues: called CC=1 SCAR=0.0
317 2 my(@returns) = @_;
318 2 ▶ $self->{responses}{ $self->{current_method} } = sub { return $self->__returnFromValue(shift( @returns )) };
Subroutine __ANON__: called CC=1 SCAR=0.0
319 }
320  
321 sub calls {
322 73 ▶ my $self = shift;
Subroutine calls: called CC=2 SCAR=6.9
323  
324 73 ▶ if(!exists $self->{calls}{ $self->{current_method} }){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"calls"}{$self->{"current_method"}}) 35 38 73
325 35 my(@calls) = ();
326 35 $self->{calls}{ $self->{current_method} } = tie @calls, 'Jasmine::Spy::Instance::Calls';
327 }
328  
329 73 return $self->{calls}{ $self->{current_method} };
330 }
331  
332 sub andCallThrough {
333 2 ▶ my $self = shift;
Subroutine andCallThrough: called CC=2 SCAR=6.9
334 2 my $toCall;
335 2 ▶ if(ref($self->{proto})){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'proto'}) { } 1 1 2
336 1 $toCall = $self->{class}->meta->get_method($self->{current_method});
337 }
338 else {
339 1 $toCall = $self->{original_methods}{ $self->{current_method} };
340 }
341  
342 2 $self->andReturn($toCall);
343 }
344  
345 sub andCallFake {
346 2 ▶ shift->andReturn(@_);
Subroutine andCallFake: called CC=1 SCAR=0.0
347 }
348  
349 sub toHaveBeenCalled {
350 10 ▶ my($self) = shift;
Subroutine toHaveBeenCalled: called CC=2 SCAR=6.9
351  
352 10 my $tb = __PACKAGE__->builder;
353  
354 10 ▶ if(want('VOID')){
Branch: 1 branch
BranchTrueFalseTotal
if (want("VOID")) 2 8 10
355 2 $tb->ok(scalar(@{ $self->calls }));
356 }
357 10 return $self->calls;
358 }
359  
360 sub notToHaveBeenCalled {
361 4 ▶ my($self) = shift;
Subroutine notToHaveBeenCalled: called CC=2 SCAR=7.5
362  
363 4 my $tb = __PACKAGE__->builder;
364  
365 4 ▶ if (scalar(@{ $self->calls })){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->calls;}) 0 4 4
366 0 $tb->ok(0);
367 0 return 0;
368 }
369 4 $tb->ok(1);
370 4 return 1;
371 }
372  
373 sub toHaveBeenCalledWith {
374 2 ▶ my($self) = shift;
Subroutine toHaveBeenCalledWith: called CC=2 SCAR=7.0
375  
376 2 my $tb = __PACKAGE__->builder;
377  
378 2 my $calls = $self->__callsMatching(@_);
379  
380 2 ▶ if(want('VOID')){
Branch: 1 branch
BranchTrueFalseTotal
if (want("VOID")) 2 0 2
381 2 $tb->ok(scalar(@$calls));
382 }
383  
384 2 return $calls;
385 }
386  
387 sub notToHaveBeenCalledWith {
388 2 ▶ my($self) = shift;
Subroutine notToHaveBeenCalledWith: called CC=2 SCAR=7.5
389  
390 2 my $tb = __PACKAGE__->builder;
391  
392  
393 2 ▶ if (scalar(@{ $self->__callsMatching(@_) })){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->__callsMatching(@_);}) 0 2 2
394 0 $tb->ok(0);
395 0 return 0;
396 }
397 2 $tb->ok(1);
398 2 return 1;
399 }
400  
401 sub __callsMatching {
402 4 ▶ my $self = shift;
Subroutine __callsMatching: called CC=1 SCAR=0.0
403 4 my(@calls) = ();
404 4 my $calls = tie @calls, 'Jasmine::Spy::Instance::Calls';
405 4 push @$calls, grep({eq_deeply([@_], $_)} @{ $self->calls } );
406 4 return $calls;
407 }
408  
409 package Jasmine::Spy::Instance::Calls;
410 1 $Jasmine::Spy::Instance::Calls::VERSION = '1.01';
411 1 ▶use Tie::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 1 ▶use Test::Builder::Module;
Subroutine BEGIN: called CC=1 SCAR=0.0
413 1 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
414 1 @ISA = qw(Tie::StdArray Test::Builder::Module);
415  
416 sub first {
417 0 ▶ my $self = shift;
Subroutine first: not called CC=1 SCAR=6.9
418 0 return $self->[0];
419 }
420  
421 sub mostRecent {
422 0 ▶ my $self = shift;
Subroutine mostRecent: not called CC=1 SCAR=6.9
423 0 return $self->[$#$self];
424 }
425  
426 sub reset {
427 2 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
428 2 @$self = ();
429 }
430  
431 sub once {
432 2 ▶ my $self = shift;
Subroutine once: called CC=1 SCAR=0.0
433 2 my $tb = __PACKAGE__->builder;
434 2 $tb->ok(scalar(@$self) == 1);
435 }
436 sub atleast {
437 2 ▶ my $self = shift;
Subroutine atleast: called CC=1 SCAR=0.0
438 2 my $times = shift;
439 2 my $tb = __PACKAGE__->builder;
440 2 $tb->ok(scalar(@$self) >= $times);
441 }
442  
443 sub atMost {
444 2 ▶ my $self = shift;
Subroutine atMost: called CC=1 SCAR=0.0
445 2 my $times = shift;
446 2 my $tb = __PACKAGE__->builder;
447 2 $tb->ok(scalar(@$self) <= $times);
448 }
449  
450 sub exactly {
451 2 ▶ my $self = shift;
Subroutine exactly: called CC=1 SCAR=0.0
452 2 my $times = shift;
453 2 my $tb = __PACKAGE__->builder;
454 2 $tb->ok(scalar(@$self) == $times);
455 }
456  
457 1 return 42;
↑ Summary