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.67';
4 13     13   5695 use strict;
  13         29  
  13         367  
5 13     13   64 use warnings;
  13         25  
  13         377  
6              
7 13     13   70 use Carp 1.22 ();
  13         345  
  13         5625  
8              
9             # class attributes
10             our $__CaptureMode = q{};
11             our @__CapturedMethodCalls = ();
12              
13             ## no critic (NamingConventions::Capitalization)
14             sub __CaptureMode {
15 393     393   772 my ( $class, $value ) = @_;
16 393         1127 return $__CaptureMode;
17             }
18              
19             sub __CaptureMethodCall {
20 193     193   355 my ( $class, $method_call ) = @_;
21 193         386 push @__CapturedMethodCalls, $method_call;
22 193         354 return;
23             }
24             ## use critic
25              
26             sub __new {
27 29     29   125 my %args = (
28             calls => [], # ArrayRef[ MethodCall ]
29             stubs => {}, # $method_name => ArrayRef[ MethodStub ]
30             );
31 29         79 return \%args;
32             }
33              
34             sub __calls {
35 335     335   2736 my ($self) = @_;
36 335         861 return $self->{calls};
37             }
38              
39             sub __stubs {
40 275     275   461 my ($self) = @_;
41 275         582 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   368 my ( $self, $method_call ) = @_;
50 194         409 my $stubs = $self->__stubs;
51              
52 194 100       497 return if !defined $stubs->{ $method_call->name };
53              
54 101         170 foreach my $stub ( @{ $stubs->{ $method_call->name } } ) {
  101         213  
55 115 100       300 return $stub if $stub->__satisfied_by($method_call);
56             }
57 6         20 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   500 my ( $class, $coderef, $action ) = @_;
67              
68             ### assert: !$__CaptureMode
69 206         307 my @method_calls;
70             {
71 206         295 local $__CaptureMode = $action;
  206         357  
72 206         391 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         590 $coderef->();
77              
78 188 100       686 Carp::croak 'Coderef must have a method invoked on a mock or spy object'
79             if @__CapturedMethodCalls == 0;
80              
81 186         497 @method_calls = @__CapturedMethodCalls;
82             }
83              
84 186         537 return @method_calls;
85             }
86              
87             1;