File Coverage

blib/lib/Test/Mocha/SpyBase.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 52 52 100.0


line stmt bran cond sub pod time code
1             package Test::Mocha::SpyBase;
2             # ABSTRACT: Abstract base class for Spy and Mock
3             $Test::Mocha::SpyBase::VERSION = '0.66';
4 13     13   5460 use strict;
  13         27  
  13         402  
5 13     13   71 use warnings;
  13         23  
  13         364  
6              
7 13     13   63 use Carp 1.22 ();
  13         319  
  13         5548  
8              
9             # class attributes
10             our $__CaptureMode = q{};
11             our @__CapturedMethodCalls = ();
12              
13             ## no critic (NamingConventions::Capitalization)
14             sub __CaptureMode {
15 393     393   775 my ( $class, $value ) = @_;
16 393         1053 return $__CaptureMode;
17             }
18              
19             sub __CaptureMethodCall {
20 193     193   336 my ( $class, $method_call ) = @_;
21 193         380 push @__CapturedMethodCalls, $method_call;
22 193         381 return;
23             }
24             ## use critic
25              
26             sub __new {
27 29     29   124 my %args = (
28             calls => [], # ArrayRef[ MethodCall ]
29             stubs => {}, # $method_name => ArrayRef[ MethodStub ]
30             );
31 29         87 return \%args;
32             }
33              
34             sub __calls {
35 335     335   2434 my ($self) = @_;
36 335         845 return $self->{calls};
37             }
38              
39             sub __stubs {
40 275     275   451 my ($self) = @_;
41 275         583 return $self->{stubs};
42             }
43              
44             sub __find_stub {
45             # """
46             # Returns the first stub that satisfies the given method call.
47             # Returns undef if no stub is found.
48             # """
49 194     194   377 my ( $self, $method_call ) = @_;
50 194         406 my $stubs = $self->__stubs;
51              
52 194 100       489 return if !defined $stubs->{ $method_call->name };
53              
54 101         158 foreach my $stub ( @{ $stubs->{ $method_call->name } } ) {
  101         209  
55 115 100       290 return $stub if $stub->__satisfied_by($method_call);
56             }
57 6         22 return;
58             }
59              
60             sub __capture_method_calls {
61             # """
62             # Get the last method called on a mock object,
63             # removes it from the invocation history,
64             # and restores the last method stub response.
65             # """
66 206     206   446 my ( $class, $coderef, $action ) = @_;
67              
68             ### assert: !$__CaptureMode
69 206         303 my @method_calls;
70             {
71 206         284 local $__CaptureMode = $action;
  206         344  
72 206         385 local @__CapturedMethodCalls = ();
73              
74             # Execute the coderef. This should in turn include a method call on
75             # mock, which should be handled by its AUTOLOAD method.
76 206         543 $coderef->();
77              
78 188 100       679 Carp::croak 'Coderef must have a method invoked on a mock or spy object'
79             if @__CapturedMethodCalls == 0;
80              
81 186         476 @method_calls = @__CapturedMethodCalls;
82             }
83              
84 186         495 return @method_calls;
85             }
86              
87             1;