line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::EasyMock; |
2
|
9
|
|
|
9
|
|
272092
|
use strict; |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
323
|
|
3
|
9
|
|
|
9
|
|
50
|
use warnings; |
|
9
|
|
|
|
|
19
|
|
|
9
|
|
|
|
|
276
|
|
4
|
9
|
|
|
9
|
|
8361
|
use version; our $VERSION = '0.10'; |
|
9
|
|
|
|
|
20060
|
|
|
9
|
|
|
|
|
50
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Test::EasyMock - A mock library which is usable easily. |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
use Test::EasyMock qw( |
13
|
|
|
|
|
|
|
create_mock |
14
|
|
|
|
|
|
|
expect |
15
|
|
|
|
|
|
|
replay |
16
|
|
|
|
|
|
|
verify |
17
|
|
|
|
|
|
|
reset |
18
|
|
|
|
|
|
|
); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $mock = create_mock(); |
21
|
|
|
|
|
|
|
expect($mock->foo(1))->and_scalar_return('a'); |
22
|
|
|
|
|
|
|
expect($mock->foo(2))->and_scalar_return('b'); |
23
|
|
|
|
|
|
|
replay($mock); |
24
|
|
|
|
|
|
|
$mock->foo(1); # return 'a' |
25
|
|
|
|
|
|
|
$mock->foo(2); # return 'b' |
26
|
|
|
|
|
|
|
$mock->foo(3); # Unexpected method call.(A test is failed) |
27
|
|
|
|
|
|
|
verify($mock); # verify all expectations is invoked. |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
reset($mock); |
30
|
|
|
|
|
|
|
expect($mock->foo(1, 2)->and_array_return('a', 'b'); |
31
|
|
|
|
|
|
|
expect($mock->foo({ value => 3 })->and_array_return('c'); |
32
|
|
|
|
|
|
|
replay($mock); |
33
|
|
|
|
|
|
|
$mock->foo(1, 2); # return ('a', 'b') |
34
|
|
|
|
|
|
|
$mock->foo({ value => 3 }); # return ('c') |
35
|
|
|
|
|
|
|
verify($mock); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
reset($mock); |
38
|
|
|
|
|
|
|
expect($mock->foo(1))->and_scalar_return('a'); |
39
|
|
|
|
|
|
|
expect($mock->foo(1))->and_scalar_return('b'); |
40
|
|
|
|
|
|
|
replay($mock); |
41
|
|
|
|
|
|
|
$mock->foo(1); # return 'a' |
42
|
|
|
|
|
|
|
$mock->foo(1); # return 'b' |
43
|
|
|
|
|
|
|
$mock->foo(1); # Unexpected method call.(A test is failed) |
44
|
|
|
|
|
|
|
verify($mock); |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Using C's special comparisons. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
use Test::EasyMock qw( |
49
|
|
|
|
|
|
|
create_mock |
50
|
|
|
|
|
|
|
expect |
51
|
|
|
|
|
|
|
replay |
52
|
|
|
|
|
|
|
verify |
53
|
|
|
|
|
|
|
reset |
54
|
|
|
|
|
|
|
whole |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
use Test::Deep qw( |
57
|
|
|
|
|
|
|
ignore |
58
|
|
|
|
|
|
|
); |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
my $mock = create_mock(); |
61
|
|
|
|
|
|
|
expect($mock->foo(1, ignore())->and_scalar_return('a'); |
62
|
|
|
|
|
|
|
expect($mock->foo({ value => 1, random => ignore() })->and_scalar_return('b'); |
63
|
|
|
|
|
|
|
replay($mock); |
64
|
|
|
|
|
|
|
$mock->foo(1, 1234); # return 'a' |
65
|
|
|
|
|
|
|
$mock->foo({ value => 1, random => 1234 }); # return 'b' |
66
|
|
|
|
|
|
|
verify($mock); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
reset($mock); |
69
|
|
|
|
|
|
|
expect($mock->foo(whole(ignore())))->and_stub_scalar_return('a'); |
70
|
|
|
|
|
|
|
replay($mock); |
71
|
|
|
|
|
|
|
$mock->foo(); # return 'a' |
72
|
|
|
|
|
|
|
$mock->foo(1, 2, 3); # return 'a' |
73
|
|
|
|
|
|
|
$mock->foo({ arg1 => 1, arg2 => 2 }); # return 'a' |
74
|
|
|
|
|
|
|
verify($mock); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Mock to class method. |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
use Test::EasyMock qw( |
79
|
|
|
|
|
|
|
expect |
80
|
|
|
|
|
|
|
replay |
81
|
|
|
|
|
|
|
verify |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
use Test::EasyMock::Class qw( |
84
|
|
|
|
|
|
|
create_class_mock |
85
|
|
|
|
|
|
|
); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $mock = create_class_mock('Foo::Bar'); |
88
|
|
|
|
|
|
|
expect($mock->foo(1))->and_scalar_return('a'); |
89
|
|
|
|
|
|
|
replay($mock); |
90
|
|
|
|
|
|
|
Foo::Bar->foo(1); # return 'a' |
91
|
|
|
|
|
|
|
Foo::Bar->foo(2); # Unexpected method call.(A test is failed) |
92
|
|
|
|
|
|
|
verify($mock); # verify all expectations is invoked. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 DESCRIPTION |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This is mock library modeled on 'EasyMock' in Java. |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
9
|
|
|
9
|
|
1194
|
use Carp qw(confess); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
683
|
|
100
|
9
|
|
|
9
|
|
45
|
use Exporter qw(import); |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
259
|
|
101
|
9
|
|
|
9
|
|
49
|
use Scalar::Util qw(blessed); |
|
9
|
|
|
|
|
18
|
|
|
9
|
|
|
|
|
912
|
|
102
|
9
|
|
|
9
|
|
5270
|
use Test::EasyMock::ArgumentsMatcher; |
|
9
|
|
|
|
|
25
|
|
|
9
|
|
|
|
|
281
|
|
103
|
9
|
|
|
9
|
|
5210
|
use Test::EasyMock::MockControl; |
|
9
|
|
|
|
|
169
|
|
|
9
|
|
|
|
|
3686
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
106
|
|
|
|
|
|
|
create_mock |
107
|
|
|
|
|
|
|
expect |
108
|
|
|
|
|
|
|
replay |
109
|
|
|
|
|
|
|
reset |
110
|
|
|
|
|
|
|
verify |
111
|
|
|
|
|
|
|
whole |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all => [@EXPORT_OK]); |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 FUNCTIONS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 create_mock([$module_name|$object]) |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Creates a mock object. |
120
|
|
|
|
|
|
|
If specified the I<$module_name> then a I method of the mock object returns true. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
sub create_mock { |
124
|
7
|
|
|
7
|
1
|
7497
|
my $control = Test::EasyMock::MockControl->create_control(@_); |
125
|
7
|
|
|
|
|
41
|
return $control->create_mock; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 expect() |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Record a method invocation and behavior. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
The following example is expecting the I method invocation with I<$arguments> |
133
|
|
|
|
|
|
|
and a result of the invocation is I<123>. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
expect($mock->foo($arguments)) |
136
|
|
|
|
|
|
|
->and_scalar_return(123); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
And the next example is expecting the I method invocation without an argument |
139
|
|
|
|
|
|
|
and a result of the invocation is I<(1, 2, 3)>. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
expect($mock->foo()) |
142
|
|
|
|
|
|
|
->and_array_return(1, 2, 3); |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head3 A list of I methods. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=over |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=item and_scalar_return($value) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Add scalar result to the expectation. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=item and_array_return(@values) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Add array result to the expectation. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=item and_list_return(@values) |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Add list result to the expectation. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=item and_answer($code) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Add code to the expectation, it calculate an answer. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=item and_die([$message]) |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Add I behavior to the expectation. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item and_stub_scalar_return($value) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Set scalar result as a stub to the expectation. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item and_stub_array_return(@values) |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Set array result as a stub to the expectation. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item and_stub_list_return(@values) |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Set list result as a stub to the expectation. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=item and_stub_answer($code) |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
Add code as a stub to the expectation, it calculate an answer. |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=item and_stub_die([$message]) |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
Set I behavior as as stub to the expectation. |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=back |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
sub expect { |
192
|
54
|
|
|
54
|
1
|
4073
|
return __delegate(expect => @_); |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 replay($mock [, $mock2 ...]) |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Replay the mock object behaviors which is recorded by the I function. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
replay($mock); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=cut |
202
|
|
|
|
|
|
|
sub replay { |
203
|
36
|
|
|
36
|
1
|
5160
|
__delegate(replay => $_) for @_; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 verify($mock) |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Verify the mock method invocations. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
sub verify { |
212
|
30
|
|
|
30
|
1
|
23678
|
__delegate(verify => $_) for @_; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 reset($mock) |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Reset the mock. |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
220
|
|
|
|
|
|
|
sub reset { |
221
|
33
|
|
|
33
|
1
|
36557
|
__delegate(reset => $_) for @_; |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 whole($arguments) |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
It is a kind of an argument matcher. |
227
|
|
|
|
|
|
|
The matcher considers that the whole argument is array ref. |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# same as `expect($mock->foo(1, 2))` |
230
|
|
|
|
|
|
|
expect($mock->foo( whole([1, 2]) )); |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# matches any arguments. (eg. foo(), foo(1,2), foo({}), etc...) |
233
|
|
|
|
|
|
|
expect($mock->foo( whole(ignore()) )); |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=cut |
236
|
|
|
|
|
|
|
sub whole { |
237
|
1
|
|
|
1
|
1
|
7
|
my ($args) = @_; |
238
|
1
|
|
|
|
|
5
|
return Test::EasyMock::ArgumentsMatcher->new($args); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
sub __delegate { |
242
|
153
|
|
|
153
|
|
295
|
my ($method, $mock, @args) = @_; |
243
|
153
|
100
|
|
|
|
307
|
my $control = __control_of($mock) |
244
|
|
|
|
|
|
|
or confess('Speocified mock is not under management'); |
245
|
145
|
|
|
|
|
590
|
return $control->$method($mock, @args); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
sub __control_of { |
249
|
153
|
|
|
153
|
|
188
|
my ($mock) = @_; |
250
|
153
|
|
|
|
|
455
|
my $class = blessed $mock; |
251
|
153
|
100
|
100
|
|
|
902
|
return unless $class && $class eq 'Test::EasyMock::MockObject'; |
252
|
145
|
|
|
|
|
447
|
return $mock->{_control}; |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
1; |
256
|
|
|
|
|
|
|
__END__ |