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