File Coverage

blib/lib/Test/Spec/RMock/MessageExpectation.pm
Criterion Covered Total %
statement 41 53 77.3
branch 5 6 83.3
condition n/a
subroutine 15 19 78.9
pod 0 17 0.0
total 61 95 64.2


line stmt bran cond sub pod time code
1             package Test::Spec::RMock::MessageExpectation;
2              
3 1     1   6 use Test::Deep qw();
  1         2  
  1         1116  
4              
5             sub new {
6 25     25 0 36 my ($class, $name) = @_;
7 25         95 my $self = {
8             _name => $name,
9             _return_value => undef,
10             _exception => undef,
11             _number_of_times_called => 0,
12             _call_count_constraint => Test::Spec::RMock::ExactlyConstraint->new(1),
13             _arguments => undef,
14             };
15 25         127 bless $self, $class;
16             }
17              
18             sub call {
19 29     29 0 46 my ($self, @args) = @_;
20 29         38 $self->{_number_of_times_called}++;
21 29 50       60 die $self->{_exception} if $self->{_exception};
22 29         119 $self->{_return_value};
23             }
24              
25             sub is_all_conditions_satisfied {
26 33     33 0 49 my ($self, @args) = @_;
27 33 100       109 $self->{_call_count_constraint}->call($self->{_number_of_times_called}+1)
28             && $self->does_arguments_match(@args);
29             }
30              
31             sub does_arguments_match {
32 32     32 0 50 my ($self, @args) = @_;
33 32 100       59 return 1 if $self->_any_arguments_allowed;
34 11         42 my ($ok, $stack) = Test::Deep::cmp_details(\@args, $self->{_arguments});
35 11         22983 return $ok;
36             }
37              
38             sub is_call_constrint_satisfied {
39 58     58 0 65 my ($self) = @_;
40 58         177 $self->{_call_count_constraint}->call($self->{_number_of_times_called});
41             }
42              
43             sub call_contraint_error_message {
44 6     6 0 9 my ($self, $mock_name) = @_;
45 6         25 $self->{_call_count_constraint}->error_message($mock_name, $self->{_name}, $self->{_number_of_times_called});
46             }
47              
48             sub argument_matching_error_message {
49 2     2 0 5 my ($self, @args) = @_;
50 2         10 my ($ok, $stack) = Test::Deep::cmp_details(\@args, $self->{_arguments});
51 2         1633 "Argument matching failed: " . Test::Deep::deep_diag($stack);
52             }
53              
54             sub _any_arguments_allowed {
55 32     32   33 my ($self) = @_;
56 32         204 !defined $self->{_arguments};
57             }
58              
59              
60             ### RECEIVE COUNTS
61              
62             sub any_number_of_times {
63 8     8 0 11 my ($self) = @_;
64 8         27 $self->{_call_count_constraint} = Test::Spec::RMock::AnyConstraint->new;
65 8         59 $self;
66             }
67              
68             sub at_least_once {
69 3     3 0 4 my ($self) = @_;
70 3         15 $self->{_call_count_constraint} = Test::Spec::RMock::AtLeastConstraint->new(1);
71 3         23 $self;
72             }
73              
74             sub at_least {
75 0     0 0 0 my ($self, $n) = @_;
76 0         0 $self->{_call_count_constraint} = Test::Spec::RMock::AtLeastConstraint->new($n);
77 0         0 $self;
78             }
79              
80             sub once {
81 0     0 0 0 my ($self) = @_;
82 0         0 $self->exactly(1);
83 0         0 $self;
84             }
85              
86             sub twice {
87 0     0 0 0 my ($self) = @_;
88 0         0 $self->exactly(2);
89 0         0 $self;
90             }
91              
92             sub exactly {
93 2     2 0 4 my ($self, $n) = @_;
94 2         6 $self->{_call_count_constraint} = Test::Spec::RMock::ExactlyConstraint->new($n);
95 2         12 $self;
96             }
97              
98             sub times {
99 2     2 0 6 return @_;
100             }
101              
102             ### RESPONSES
103              
104             sub and_return {
105 11     11 0 16 my ($self, $value) = @_;
106 11         19 $self->{_return_value} = $value;
107 11         24 $self;
108             }
109              
110              
111             sub and_raise {
112 0     0 0 0 my ($self, $exception) = @_;
113 0         0 $self->{_exception} = $exception;
114 0         0 $self;
115             }
116              
117              
118             ### ARGUMENT MATCHING
119              
120             sub with {
121 8     8 0 16 my ($self, @args) = @_;
122 8         14 $self->{_arguments} = \@args;
123 8         16 $self;
124             }
125              
126             1;
127              
128             __END__