Skip to content

Commit 8fbf71e

Browse files
committed
Added optional/soft expectations
1 parent ae96f62 commit 8fbf71e

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

mock.lua

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ end
1414

1515
ExpectedCall = {}
1616

17-
function ExpectedCall:new(f, args)
17+
function ExpectedCall:new(f, required, args)
1818
local o = {
1919
_f = f,
2020
_ordered = false,
21+
_required = required,
2122
_args = args,
2223
_return = {}
2324
}
@@ -58,6 +59,10 @@ function ExpectedCall:hasFixedOrder()
5859
return self._ordered
5960
end
6061

62+
function ExpectedCall:isRequired()
63+
return self._required
64+
end
65+
6166
MockExpectation = {}
6267

6368
function MockExpectation:new(m)
@@ -118,7 +123,11 @@ function MockExpectation:when(thunk)
118123

119124
mockHandle(called, thunk)
120125

121-
assert(#self._calls == 0, 'not all calls occurred')
126+
for _, call in pairs(self._calls) do
127+
if call:isRequired() then
128+
error('not all calls occurred', 2)
129+
end
130+
end
122131
end
123132

124133
function MockExpectation:after(thunk)
@@ -153,7 +162,7 @@ function MockExpectation:shouldBeCalledWith(...)
153162
end
154163

155164
self._callSpecified = true
156-
table.insert(self._calls, ExpectedCall:new(self._m, table.pack(...)))
165+
table.insert(self._calls, ExpectedCall:new(self._m, true, table.pack(...)))
157166
return self
158167
end
159168

@@ -165,6 +174,24 @@ function MockExpectation:shouldBeCalled()
165174
return self:shouldBeCalledWith()
166175
end
167176

177+
function MockExpectation:mayBeCalledWith(...)
178+
if self._callSpecified == true then
179+
error('call already specified', 2)
180+
end
181+
182+
self._callSpecified = true
183+
table.insert(self._calls, ExpectedCall:new(self._m, false, table.pack(...)))
184+
return self
185+
end
186+
187+
function MockExpectation:mayBeCalled()
188+
if self._callSpecified == true then
189+
error('call already specified', 2)
190+
end
191+
192+
return self:mayBeCalledWith()
193+
end
194+
168195
function MockExpectation:multipleTimes(times)
169196
for i = 1, times - 1 do
170197
table.insert(self._calls, self._calls[#self._calls])

spec/mock_spec.lua

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ describe('The mock library', function()
276276
end)
277277
end)
278278

279-
it('should fail if shouldBeCalled is used after a call has already been specified', function()
279+
it('should fail if shouldBeCalledWith is used after a call has already been specified', function()
280280
shouldFailWith('call already specified', function()
281281
local f = mock:mockFunction('f')
282282

283-
mock(f):shouldBeCalled():shouldBeCalledWith()
283+
mock(f):shouldBeCalled():shouldBeCalledWith(4)
284284
end)
285285
end)
286286

@@ -358,5 +358,67 @@ describe('The mock library', function()
358358
end)
359359
end)
360360

361-
-- may be called? ie: calls that are not required
361+
it('should allow soft expectations to be called', function()
362+
local f = mock:mockFunction('f')
363+
364+
mock(f):mayBeCalled():
365+
when(function()
366+
f()
367+
end)
368+
end)
369+
370+
it('should allow soft expectations to be omitted', function()
371+
local f = mock:mockFunction('f')
372+
373+
mock(f):mayBeCalled():
374+
when(function() end)
375+
end)
376+
377+
it('should allow soft expectations with return values', function()
378+
local f = mock:mockFunction('f')
379+
380+
mock(f):mayBeCalled():andWillReturn(3):
381+
when(function()
382+
assert(f() == 3)
383+
end)
384+
end)
385+
386+
it('should allow soft expectations with arguments to be called', function()
387+
local f = mock:mockFunction('f')
388+
389+
mock(f):mayBeCalledWith(4):
390+
when(function()
391+
f(4)
392+
end)
393+
end)
394+
395+
it('should allow soft expectations with arguments to be omitted', function()
396+
local f = mock:mockFunction('f')
397+
398+
mock(f):mayBeCalledWith(4):
399+
when(function() end)
400+
end)
401+
402+
it('should allow soft expectations with arguments to be omitted', function()
403+
local f = mock:mockFunction('f')
404+
405+
mock(f):mayBeCalledWith(4):
406+
when(function() end)
407+
end)
408+
409+
it('should fail if mayBeCalled is used after a call has already been specified', function()
410+
shouldFailWith('call already specified', function()
411+
local f = mock:mockFunction('f')
412+
413+
mock(f):shouldBeCalled():mayBeCalled()
414+
end)
415+
end)
416+
417+
it('should fail if mayBeCalledWith is used after a call has already been specified', function()
418+
shouldFailWith('call already specified', function()
419+
local f = mock:mockFunction('f')
420+
421+
mock(f):shouldBeCalled():mayBeCalledWith(4)
422+
end)
423+
end)
362424
end)

0 commit comments

Comments
 (0)