Skip to content

Commit a2b3d47

Browse files
committed
Cleaned up API and object model
1 parent 4e83c53 commit a2b3d47

File tree

2 files changed

+83
-79
lines changed

2 files changed

+83
-79
lines changed

mock.lua

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ function mockCalled(m, name, args)
1313
end
1414

1515
local ExpectedCall = {}
16-
ExpectedCall.__index = ExpectedCall
1716

18-
function ExpectedCall:new(f, required, args)
17+
local ExpectedCallApi = {}
18+
ExpectedCallApi.__index = ExpectedCallApi
19+
20+
function ExpectedCall.create(f, required, args)
1921
local o = {
2022
_f = f,
2123
_ordered = false,
@@ -24,16 +26,16 @@ function ExpectedCall:new(f, required, args)
2426
_return = {}
2527
}
2628

27-
setmetatable(o, self)
29+
setmetatable(o, ExpectedCallApi)
2830

2931
return o
3032
end
3133

32-
function ExpectedCall:functionMatches(f)
34+
function ExpectedCallApi:functionMatches(f)
3335
return f == self._f
3436
end
3537

36-
function ExpectedCall:argsMatch(args)
38+
function ExpectedCallApi:argsMatch(args)
3739
if #self._args ~= #args then return false end
3840

3941
for k in ipairs(self._args) do
@@ -43,42 +45,44 @@ function ExpectedCall:argsMatch(args)
4345
return true
4446
end
4547

46-
function ExpectedCall:setReturnValues(...)
48+
function ExpectedCallApi:setReturnValues(...)
4749
self._return = table.pack(...)
4850
end
4951

50-
function ExpectedCall:getReturnValues(...)
52+
function ExpectedCallApi:getReturnValues(...)
5153
return table.unpack(self._return)
5254
end
5355

54-
function ExpectedCall:fixOrder()
56+
function ExpectedCallApi:fixOrder()
5557
self._ordered = true
5658
end
5759

58-
function ExpectedCall:hasFixedOrder()
60+
function ExpectedCallApi:hasFixedOrder()
5961
return self._ordered
6062
end
6163

62-
function ExpectedCall:isRequired()
64+
function ExpectedCallApi:isRequired()
6365
return self._required
6466
end
6567

6668
local MockExpectation = {}
67-
MockExpectation.__index = MockExpectation
6869

69-
function MockExpectation:new(m)
70+
local MockExpectationApi = {}
71+
MockExpectationApi.__index = MockExpectationApi
72+
73+
function MockExpectation.create(m)
7074
local o = {
7175
_m = m,
7276
_callSpecified = false,
7377
_calls = {}
7478
}
7579

76-
setmetatable(o, self)
80+
setmetatable(o, MockExpectationApi)
7781

7882
return o
7983
end
8084

81-
function MockExpectation:andWillReturn(...)
85+
function MockExpectationApi:andWillReturn(...)
8286
if not self._callSpecified then
8387
error('cannot set return value for an unspecified call', 2)
8488
end
@@ -88,7 +92,7 @@ function MockExpectation:andWillReturn(...)
8892
return self
8993
end
9094

91-
function MockExpectation:when(thunk)
95+
function MockExpectationApi:when(thunk)
9296
if not self._callSpecified then
9397
error('incomplete expectation', 2)
9498
end
@@ -130,15 +134,15 @@ function MockExpectation:when(thunk)
130134
end
131135
end
132136

133-
function MockExpectation:after(thunk)
137+
function MockExpectationApi:after(thunk)
134138
if not self._callSpecified then
135139
error('incomplete expectation', 2)
136140
end
137141

138142
self:when(thunk)
139143
end
140144

141-
function MockExpectation:andThen(other)
145+
function MockExpectationApi:andThen(other)
142146
self._calls[#self._calls]:fixOrder()
143147

144148
for _, call in ipairs(other._calls) do
@@ -148,59 +152,59 @@ function MockExpectation:andThen(other)
148152
return self
149153
end
150154

151-
function MockExpectation:andAlso(other)
155+
function MockExpectationApi:andAlso(other)
152156
for _, call in ipairs(other._calls) do
153157
table.insert(self._calls, call)
154158
end
155159

156160
return self
157161
end
158162

159-
function MockExpectation:shouldBeCalledWith(...)
163+
function MockExpectationApi:shouldBeCalledWith(...)
160164
if self._callSpecified == true then
161165
error('call already specified', 2)
162166
end
163167

164168
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(...)))
166170
return self
167171
end
168172

169-
function MockExpectation:shouldBeCalled()
173+
function MockExpectationApi:shouldBeCalled()
170174
if self._callSpecified == true then
171175
error('call already specified', 2)
172176
end
173177

174178
return self:shouldBeCalledWith()
175179
end
176180

177-
function MockExpectation:mayBeCalledWith(...)
181+
function MockExpectationApi:mayBeCalledWith(...)
178182
if self._callSpecified == true then
179183
error('call already specified', 2)
180184
end
181185

182186
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(...)))
184188
return self
185189
end
186190

187-
function MockExpectation:mayBeCalled()
191+
function MockExpectationApi:mayBeCalled()
188192
if self._callSpecified == true then
189193
error('call already specified', 2)
190194
end
191195

192196
return self:mayBeCalledWith()
193197
end
194198

195-
function MockExpectation:multipleTimes(times)
199+
function MockExpectationApi:multipleTimes(times)
196200
for i = 1, times - 1 do
197201
table.insert(self._calls, self._calls[#self._calls])
198202
end
199203

200204
return self
201205
end
202206

203-
function Mock:mockFunction(name)
207+
function Mock.mockFunction(name)
204208
name = name or '<anonymous>'
205209
local f = {}
206210

@@ -213,7 +217,7 @@ function Mock:mockFunction(name)
213217
return f
214218
end
215219

216-
function Mock:mockMethod(name)
220+
function Mock.mockMethod(name)
217221
name = name or '<anonymous>'
218222
local m = {}
219223

@@ -233,32 +237,32 @@ function isCallable(x)
233237
return isFunction or hasCallMetamethod
234238
end
235239

236-
function Mock:mockTable(t, name)
240+
function Mock.mockTable(t, name)
237241
name = name or '<anonymous>'
238242
local mocked = {}
239243

240244
for k, v in pairs(t) do
241245
if isCallable(v) then
242-
mocked[k] = self:mockFunction(name .. '.' .. tostring(k))
246+
mocked[k] = Mock.mockFunction(name .. '.' .. tostring(k))
243247
end
244248
end
245249

246250
return mocked
247251
end
248252

249-
function Mock:mockObject(o, name)
253+
function Mock.mockObject(o, name)
250254
name = name or '<anonymous>'
251255
local mocked = {}
252256

253257
for k, v in pairs(o) do
254258
if isCallable(v) then
255-
mocked[k] = self:mockMethod(name .. ':' .. tostring(k))
259+
mocked[k] = Mock.mockMethod(name .. ':' .. tostring(k))
256260
end
257261
end
258262

259263
return mocked
260264
end
261265

262-
setmetatable(Mock, { __call = function(_, ...) return MockExpectation:new(...) end })
266+
setmetatable(Mock, { __call = function(_, ...) return MockExpectation.create(...) end })
263267

264268
return Mock

0 commit comments

Comments
 (0)