| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 // {}
MC/DC100%1/1
$base // {}
$base
Truth table100%2/2
A
A= $base
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->has_context and $context
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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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 | |||||||||||||||||||||||||||||