Skip to content

Commit d90b9ac

Browse files
committed
Allow soft expected calls that don't match arguments
1 parent e5a3adf commit d90b9ac

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

spec/mach_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ describe('The mach library', function()
412412
f:may_be_called_with(4):when(function() end)
413413
end)
414414

415+
it('should allow soft expectations with any arguments to be called', function()
416+
f:may_be_called_with_any_arguments():when(function() f(4) end)
417+
end)
418+
419+
it('should allow soft expectations with any arguments to be omitted', function()
420+
f:may_be_called_with_any_arguments():when(function() end)
421+
end)
422+
415423
it('should fail if may_be_called is used after a call has already been specified', function()
416424
should_fail_with('call already specified', function()
417425
f:should_be_called():may_be_called()
@@ -424,6 +432,12 @@ describe('The mach library', function()
424432
end)
425433
end)
426434

435+
it('should fail if may_be_called_with_any_arguments is used after a call has already been specified', function()
436+
should_fail_with('call already specified', function()
437+
f:should_be_called():may_be_called_with_any_arguments()
438+
end)
439+
end)
440+
427441
it('should handle unexpected calls outside of an expectation', function()
428442
should_fail_with('unexpected function call f(1, 2, 3)', function()
429443
mach.mock_function('f')(1, 2, 3)

src/mach/Expectation.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,18 @@ function expectation:should_be_called()
139139
return self:should_be_called_with()
140140
end
141141

142+
function expectation:may_be_called_with_any_arguments()
143+
if self._call_specified then
144+
error('call already specified', 2)
145+
end
146+
147+
self._call_specified = true
148+
table.insert(self._calls, ExpectedCall(self._m, { required = false, ignore_args = true }))
149+
return self
150+
end
151+
142152
function expectation:may_be_called_with(...)
143-
if self._call_specified == true then
153+
if self._call_specified then
144154
error('call already specified', 2)
145155
end
146156

@@ -150,7 +160,7 @@ function expectation:may_be_called_with(...)
150160
end
151161

152162
function expectation:may_be_called()
153-
if self._call_specified == true then
163+
if self._call_specified then
154164
error('call already specified', 2)
155165
end
156166

0 commit comments

Comments
 (0)