@@ -13,9 +13,11 @@ function mockCalled(m, name, args)
13
13
end
14
14
15
15
local ExpectedCall = {}
16
- ExpectedCall .__index = ExpectedCall
17
16
18
- function ExpectedCall :new (f , required , args )
17
+ local ExpectedCallApi = {}
18
+ ExpectedCallApi .__index = ExpectedCallApi
19
+
20
+ function ExpectedCall .create (f , required , args )
19
21
local o = {
20
22
_f = f ,
21
23
_ordered = false ,
@@ -24,16 +26,16 @@ function ExpectedCall:new(f, required, args)
24
26
_return = {}
25
27
}
26
28
27
- setmetatable (o , self )
29
+ setmetatable (o , ExpectedCallApi )
28
30
29
31
return o
30
32
end
31
33
32
- function ExpectedCall :functionMatches (f )
34
+ function ExpectedCallApi :functionMatches (f )
33
35
return f == self ._f
34
36
end
35
37
36
- function ExpectedCall :argsMatch (args )
38
+ function ExpectedCallApi :argsMatch (args )
37
39
if # self ._args ~= # args then return false end
38
40
39
41
for k in ipairs (self ._args ) do
@@ -43,42 +45,44 @@ function ExpectedCall:argsMatch(args)
43
45
return true
44
46
end
45
47
46
- function ExpectedCall :setReturnValues (...)
48
+ function ExpectedCallApi :setReturnValues (...)
47
49
self ._return = table.pack (... )
48
50
end
49
51
50
- function ExpectedCall :getReturnValues (...)
52
+ function ExpectedCallApi :getReturnValues (...)
51
53
return table.unpack (self ._return )
52
54
end
53
55
54
- function ExpectedCall :fixOrder ()
56
+ function ExpectedCallApi :fixOrder ()
55
57
self ._ordered = true
56
58
end
57
59
58
- function ExpectedCall :hasFixedOrder ()
60
+ function ExpectedCallApi :hasFixedOrder ()
59
61
return self ._ordered
60
62
end
61
63
62
- function ExpectedCall :isRequired ()
64
+ function ExpectedCallApi :isRequired ()
63
65
return self ._required
64
66
end
65
67
66
68
local MockExpectation = {}
67
- MockExpectation .__index = MockExpectation
68
69
69
- function MockExpectation :new (m )
70
+ local MockExpectationApi = {}
71
+ MockExpectationApi .__index = MockExpectationApi
72
+
73
+ function MockExpectation .create (m )
70
74
local o = {
71
75
_m = m ,
72
76
_callSpecified = false ,
73
77
_calls = {}
74
78
}
75
79
76
- setmetatable (o , self )
80
+ setmetatable (o , MockExpectationApi )
77
81
78
82
return o
79
83
end
80
84
81
- function MockExpectation :andWillReturn (...)
85
+ function MockExpectationApi :andWillReturn (...)
82
86
if not self ._callSpecified then
83
87
error (' cannot set return value for an unspecified call' , 2 )
84
88
end
@@ -88,7 +92,7 @@ function MockExpectation:andWillReturn(...)
88
92
return self
89
93
end
90
94
91
- function MockExpectation :when (thunk )
95
+ function MockExpectationApi :when (thunk )
92
96
if not self ._callSpecified then
93
97
error (' incomplete expectation' , 2 )
94
98
end
@@ -130,15 +134,15 @@ function MockExpectation:when(thunk)
130
134
end
131
135
end
132
136
133
- function MockExpectation :after (thunk )
137
+ function MockExpectationApi :after (thunk )
134
138
if not self ._callSpecified then
135
139
error (' incomplete expectation' , 2 )
136
140
end
137
141
138
142
self :when (thunk )
139
143
end
140
144
141
- function MockExpectation :andThen (other )
145
+ function MockExpectationApi :andThen (other )
142
146
self ._calls [# self ._calls ]:fixOrder ()
143
147
144
148
for _ , call in ipairs (other ._calls ) do
@@ -148,59 +152,59 @@ function MockExpectation:andThen(other)
148
152
return self
149
153
end
150
154
151
- function MockExpectation :andAlso (other )
155
+ function MockExpectationApi :andAlso (other )
152
156
for _ , call in ipairs (other ._calls ) do
153
157
table.insert (self ._calls , call )
154
158
end
155
159
156
160
return self
157
161
end
158
162
159
- function MockExpectation :shouldBeCalledWith (...)
163
+ function MockExpectationApi :shouldBeCalledWith (...)
160
164
if self ._callSpecified == true then
161
165
error (' call already specified' , 2 )
162
166
end
163
167
164
168
self ._callSpecified = true
165
- table.insert (self ._calls , ExpectedCall : new (self ._m , true , table.pack (... )))
169
+ table.insert (self ._calls , ExpectedCall . create (self ._m , true , table.pack (... )))
166
170
return self
167
171
end
168
172
169
- function MockExpectation :shouldBeCalled ()
173
+ function MockExpectationApi :shouldBeCalled ()
170
174
if self ._callSpecified == true then
171
175
error (' call already specified' , 2 )
172
176
end
173
177
174
178
return self :shouldBeCalledWith ()
175
179
end
176
180
177
- function MockExpectation :mayBeCalledWith (...)
181
+ function MockExpectationApi :mayBeCalledWith (...)
178
182
if self ._callSpecified == true then
179
183
error (' call already specified' , 2 )
180
184
end
181
185
182
186
self ._callSpecified = true
183
- table.insert (self ._calls , ExpectedCall : new (self ._m , false , table.pack (... )))
187
+ table.insert (self ._calls , ExpectedCall . create (self ._m , false , table.pack (... )))
184
188
return self
185
189
end
186
190
187
- function MockExpectation :mayBeCalled ()
191
+ function MockExpectationApi :mayBeCalled ()
188
192
if self ._callSpecified == true then
189
193
error (' call already specified' , 2 )
190
194
end
191
195
192
196
return self :mayBeCalledWith ()
193
197
end
194
198
195
- function MockExpectation :multipleTimes (times )
199
+ function MockExpectationApi :multipleTimes (times )
196
200
for i = 1 , times - 1 do
197
201
table.insert (self ._calls , self ._calls [# self ._calls ])
198
202
end
199
203
200
204
return self
201
205
end
202
206
203
- function Mock : mockFunction (name )
207
+ function Mock . mockFunction (name )
204
208
name = name or ' <anonymous>'
205
209
local f = {}
206
210
@@ -213,7 +217,7 @@ function Mock:mockFunction(name)
213
217
return f
214
218
end
215
219
216
- function Mock : mockMethod (name )
220
+ function Mock . mockMethod (name )
217
221
name = name or ' <anonymous>'
218
222
local m = {}
219
223
@@ -233,32 +237,32 @@ function isCallable(x)
233
237
return isFunction or hasCallMetamethod
234
238
end
235
239
236
- function Mock : mockTable (t , name )
240
+ function Mock . mockTable (t , name )
237
241
name = name or ' <anonymous>'
238
242
local mocked = {}
239
243
240
244
for k , v in pairs (t ) do
241
245
if isCallable (v ) then
242
- mocked [k ] = self : mockFunction (name .. ' .' .. tostring (k ))
246
+ mocked [k ] = Mock . mockFunction (name .. ' .' .. tostring (k ))
243
247
end
244
248
end
245
249
246
250
return mocked
247
251
end
248
252
249
- function Mock : mockObject (o , name )
253
+ function Mock . mockObject (o , name )
250
254
name = name or ' <anonymous>'
251
255
local mocked = {}
252
256
253
257
for k , v in pairs (o ) do
254
258
if isCallable (v ) then
255
- mocked [k ] = self : mockMethod (name .. ' :' .. tostring (k ))
259
+ mocked [k ] = Mock . mockMethod (name .. ' :' .. tostring (k ))
256
260
end
257
261
end
258
262
259
263
return mocked
260
264
end
261
265
262
- setmetatable (Mock , { __call = function (_ , ...) return MockExpectation : new (... ) end })
266
+ setmetatable (Mock , { __call = function (_ , ...) return MockExpectation . create (... ) end })
263
267
264
268
return Mock
0 commit comments