RMock/MockObject.pm

filter: Statementstmt 100% 72 / 72 Branchbran 93.7% 15 / 16 Conditioncond 71.4% 5 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 6 totaltotal 89.9% 107 / 119 CC 17 SCAR 11.2
CC
17
Cov
97%
__find_method_proxy
6.0
__check
4.0
__teardown
3.1
CRAP 3.1
↑ Summary RMock/ExactlyConstraint.pm »
1 package Test::Spec::RMock::MockObject;
2  
3 26 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
4 26 my ($class, $name) = @_;
5 26 my $self = {
6 _name => $name,
7 _messages => {},
8 _problems_found => [],
9 _canceled => 0,
10 _is_null_object => 0,
11 };
12 26 bless $self, $class;
13 26 ▶ my $context = Test::Spec->current_context
Condition50%1/2
'Test::Spec'->current_context || Carp::croak('Test::Spec::RMocks only works in conjunction with Test::Spec')
l!l
260
MC/DC0%0/1
'Test::Spec'->current_context || Carp::croak('Test::Spec::RMocks only works in conjunction with Test::Spec')
'Test::Spec'->current_context
Truth table50%1/2
A
A= 'Test::Spec'->current_context
Aresult
11
00
14 || Carp::croak "Test::Spec::RMocks only works in conjunction with Test::Spec";
15 26 ▶ $context->on_leave(sub { $self->__teardown });
Subroutine __ANON__: called CC=1 SCAR=0.0
16 26 $self;
17 }
18  
19 25 ▶sub should_receive {
Subroutine should_receive: called CC=1 SCAR=0.0
Pod undocumented
20 25 my ($self, $message) = @_;
21 25 my $expectation = Test::Spec::RMock::MessageExpectation->new($message);
22 25 $self->__register_expectation($message, $expectation);
23 25 $expectation;
24 }
25  
26 2 ▶sub should_not_receive {
Subroutine should_not_receive: called CC=1 SCAR=0.0
Pod undocumented
27 2 my ($self, $message) = @_;
28 2 $self->should_receive($message)->exactly(0)->times;
29 }
30  
31 6 ▶sub stub {
Subroutine stub: called CC=2 SCAR=6.9
Pod undocumented
32 6 my ($self, %spec) = @_;
33 6 my $last_double;
34 6 ▶ while (my ($method_name, $return_value) = each %spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %spec) 8 6 14
35 8 $last_double = $self->should_receive($method_name)->and_return($return_value)->any_number_of_times;
36 }
37 6 $last_double;
38 }
39  
40 3 ▶sub stub_chain {
Subroutine stub_chain: called CC=2 SCAR=6.9
Pod undocumented
41 3 my ($self, @chain) = @_;
42 3 my $name = shift @chain;
43 3 ▶ if (@chain) {
Branch: 1 branch
BranchTrueFalseTotal
if (@chain) 2 1 3
44 2 my $next = __PACKAGE__->new('StubChainMockObject');
45 2 $self->stub($name => $next);
46 2 return $next->stub_chain(@chain);
47 }
48 1 $self->stub($name => undef);
49 }
50  
51 2 ▶sub as_null_object {
Subroutine as_null_object: called CC=1 SCAR=0.0
Pod undocumented
52 2 my ($self) = @_;
53 2 $self->{_is_null_object} = 1;
54 2 $self;
55 }
56  
57 14 ▶sub __cancel {
Subroutine __cancel: called CC=1 SCAR=0.0
58 14 my ($self) = @_;
59 14 $self->{_canceled} = 1;
60 14 $self;
61 }
62  
63 25 ▶sub __register_expectation {
Subroutine __register_expectation: called CC=2 SCAR=6.9
64 25 my ($self, $message, $expectation) = @_;
65 25 ▶ $self->{_messages}{$message} ||= [];
Condition100%2/2
$self->{"_messages"}{$message} ||= []
l!l
322
MC/DC100%1/1
$self->{"_messages"}{$message} ||= []
$self->{"_messages"}{$message}
Truth table100%2/2
A
A= $self->{"_messages"}{$message}
Aresult
11
00
66 25 push @{$self->{_messages}{$message}}, $expectation;
67 }
68  
69  
70 46 ▶sub __teardown {
Subroutine __teardown: called CC=3 SCAR=11.3
71 46 my ($self) = @_;
72 46 my $report = $self->__check;
73 46 ▶ die $report if !$self->{_canceled} && $report;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_canceled"} and $report 0 46 46
Condition66%2/3
not $self->{"_canceled"} and $report
!ll&&!rl&&r
31150
MC/DC0%0/2
not $self->{"_canceled"} and $report
not $self->{"_canceled"} $report
Truth table66%2/3
not(A) and B
A= not $self->{"_canceled"}B= $report
ABresult
0X0
100
111
74 46 return 1;
75 }
76  
77 61 ▶sub __check {
Subroutine __check: called CC=4 SCAR=13.9
78 61 my ($self) = @_;
79 61 for my $ms (values %{$self->{_messages}}) {
80 52 for my $m (@$ms) {
81 58 ▶ push @{$self->{_problems_found}}, $m->call_contraint_error_message($self->{_name}) unless $m->is_call_constrint_satisfied;
Branch: 1 branch
BranchTrueFalseTotal
unless $m->is_call_constrint_satisfied 6 52 58
82 }
83 }
84 61 join("\n", @{$self->{_problems_found}});
85 }
86  
87 29 ▶sub __find_method_proxy {
Subroutine __find_method_proxy: called CC=6 SCAR=17.9
88 29 my ($self, $expectations, @args) = @_;
89 29 for my $e (@$expectations) {
90 33 ▶ return $e if $e->is_all_conditions_satisfied(@args);
Branch: 1 branch
BranchTrueFalseTotal
if $e->is_all_conditions_satisfied(@args) 25 8 33
91 }
92 4 for my $e (@$expectations) {
93 4 ▶ return $e if $e->does_arguments_match(@args);
Branch: 1 branch
BranchTrueFalseTotal
if $e->does_arguments_match(@args) 2 2 4
94 }
95 2 for my $e (@$expectations) {
96 2 push @{$self->{_problems_found}}, $e->argument_matching_error_message(@args);
97 }
98 2 return $expectations->[0];
99 }
100  
101 1 our $AUTOLOAD;
102 60 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.0
103 60 my ($self, @args) = @_;
104 60 my $message_name = $self->__get_message_name;
105 60 my $expectations = $self->{_messages}{$message_name};
106 60 ▶ unless ($expectations) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expectations) 31 29 60
107 31 ▶ return $self if $self->{_is_null_object};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_is_null_object"} 5 26 31
108 26 push @{$self->{_problems_found}},
109 sprintf("Unmocked method '%s' called on '%s' with (%s)",
110 $message_name,
111 $self->{_name},
112 26 join(', ', map {"'$_'"} @args));
113 26 return;
114 }
115 29 my $proxy = $self->__find_method_proxy($expectations, @args);
116 29 return $proxy->call(@args);
117 }
118  
119 60 ▶sub __get_message_name {
Subroutine __get_message_name: called CC=1 SCAR=0.0
120 60 my $name = $AUTOLOAD;
121 60 $name =~ s/.*:://;
122 60 $name;
123 }
124  
125 1 1;
126  
127 __END__
↑ Summary RMock/ExactlyConstraint.pm »