Spy.pm

filter: Statementstmt 100% 71 / 71 Branchbran 90.0% 9 / 10 Conditioncond 63.6% 7 / 11 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 91.9% 114 / 124 CC 11 SCAR 7.4
CC
11
Cov
95%
call_history
4.2
_build_object
4.0
__ANON__
2.0
CRAP 2.1
« Spy/Object.pm ↑ Summary Spy/Method.pm »
1 package Test::Spy;
2 7 $Test::Spy::VERSION = '0.005';
3 7 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mooish::AttributeBuilder -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Test::Spy::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Test::Spy::Observer;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Test::Spy::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 has param 'interface' => (
16 28 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
17 28 my @allowed = qw(strict lax warn);
18 croak "interface can be any of: @allowed"
19 28 ▶ unless grep { $_[0] eq $_ } @allowed;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_[0] eq $_;} @allowed 0 28 28
20 },
21 26 ▶ default => sub { 'strict' }
Subroutine __ANON__: called CC=1 SCAR=0.0
22 7 );
23  
24 has field '_mocked_subs' => (
25 28 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 7 );
27  
28 7 has field 'object' => (
29 lazy => 1,
30 clearer => -hidden
31 );
32  
33 7 has option 'context' => (
34 writer => 1,
35 clearer => 1,
36 );
37  
38 7 has option 'base' => (
39 writer => 1,
40 trigger => '_clear_object',
41 );
42  
43 7 has option 'imitates' => (
44 writer => 1,
45 trigger => '_clear_object',
46 );
47  
48 7 with qw(Test::Spy::Interface);
49  
50 sub _no_method
51 1 ▶{
Subroutine _no_method: called CC=1 SCAR=0.0
52 1 my ($self, $method_name) = @_;
53  
54 1 croak "method $method_name was not mocked!";
55 }
56  
57 sub _build_object
58 25 ▶{
Subroutine _build_object: called CC=4 SCAR=13.9
59 25 my ($self) = @_;
60  
61 25 my %methods = %{$self->_mocked_subs};
62 25 my %init_hash;
63  
64 25 ▶ my $base = $self->has_base
Branch: 2 branches
BranchTrueFalseTotal
$self->has_base ? : 3 22 25
ref $self->base ? : 2 1 3
65 ? ref $self->base ? $self->base : $self->base->new
66 : undef;
67  
68 return Test::Spy::Object->__new(
69 25 ▶ %{$base // {}},
Condition100%2/2
$base // {}
l!l
322
MC/DC100%1/1
$base // {}
$base
Truth table100%2/2
A
A= $base
Aresult
11
00
70 __base => $base,
71 __spy => $self,
72 );
73 }
74  
75 sub add_method
76 20 ▶{
Subroutine add_method: called CC=2 SCAR=6.9
Pod documented
77 20 my ($self, $method_name, @returns) = @_;
78  
79 20 my $method = $self->_mocked_subs->{$method_name} = Test::Spy::Method->new(method_name => $method_name);
80  
81 20 ▶ if (@returns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@returns) 5 15 20
82 5 $method->should_return(@returns);
83 }
84  
85 20 return $method;
86 }
87  
88 sub add_observer
89 1 ▶{
Subroutine add_observer: called CC=1 SCAR=0.0
Pod documented
90 1 my ($self, $method_name) = @_;
91  
92 1 return $self->_mocked_subs->{$method_name} = Test::Spy::Observer->new(method_name => $method_name);
93 }
94  
95 sub method
96 3 ▶{
Subroutine method: called CC=2 SCAR=7.1
Pod documented
97 3 my ($self, $method_name) = @_;
98  
99 3 ▶ return $self->_mocked_subs->{$method_name}
Condition66%2/3
$self->_mocked_subs->{$method_name} // $self->_no_method($method_name)
l!l&&r!l&&!r
210
MC/DC0%0/2
$self->_mocked_subs->{$method_name} // $self->_no_method($method_name)
$self->_mocked_subs->{$method_name} $self->_no_method($method_name)
Truth table33%1/3
A // B
A= $self->_mocked_subs->{$method_name}B= $self->_no_method($method_name)
ABresult
1X1
011
000
100 // $self->_no_method($method_name);
101 }
102  
103 sub clear_all
104 1 ▶{
Subroutine clear_all: called CC=2 SCAR=6.9
Pod documented
105 1 my ($self) = @_;
106  
107 1 $self->clear_context;
108  
109 1 my %methods = %{$self->_mocked_subs};
110 1 for my $method_name (keys %methods) {
111 2 $methods{$method_name}->clear;
112 }
113  
114 1 return;
115 }
116  
117 sub call_history
118 7 ▶{
Subroutine call_history: called CC=4 SCAR=14.5
Pod documented
119 7 my ($self) = @_;
120  
121 7 my $context = $self->context;
122 7 ▶ croak 'no context was set in ' . ref $self
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_context and $context 1 6 7
Condition66%2/3
$self->has_context and $context
!ll&&!rl&&r
106
MC/DC50%1/2
$self->has_context and $context
$self->has_context $context
Truth table66%2/3
A and B
A= $self->has_contextB= $context
ABresult
0X0
100
111
123 unless $self->has_context && $context;
124  
125 6 ▶ return $self->_mocked_subs->{$context}->call_history
Condition33%1/3
$self->_mocked_subs->{$context}->call_history // $self->_no_method($context)
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->_mocked_subs->{$context}->call_history // $self->_no_method($context)
$self->_mocked_subs->{$context}->call_history $self->_no_method($context)
Truth table33%1/3
A // B
A= $self->_mocked_subs->{$context}->call_historyB= $self->_no_method($context)
ABresult
1X1
011
000
126 // $self->_no_method($context);
127 }
128  
129 sub _clear_call_history
130 1 ▶{
Subroutine _clear_call_history: called CC=1 SCAR=0.0
131 1 my ($self) = @_;
132  
133 1 return $self->_mocked_subs->{$self->context}->_clear_call_history;
134 }
135  
136 7 1;
137  
138 # ABSTRACT: build mocked interfaces and examine call data easily
139  
« Spy/Object.pm ↑ Summary Spy/Method.pm »