Skip to content

Commit e5a3adf

Browse files
committed
Allow expected calls that don't match arguments. Closes #9.
1 parent 340b072 commit e5a3adf

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

spec/mach_spec.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ describe('The mach library', function()
5555
end)
5656
end)
5757

58+
it('should allow you to expect a function to be called with any arguments', function()
59+
f:should_be_called_with_any_arguments():when(function() f(1, '3') end)
60+
end)
61+
5862
it('should allow you to specify the return value of a mocked function', function()
5963
f:should_be_called():and_will_return(4):when(function()
6064
assert.is.equal(f(), 4)
@@ -318,6 +322,12 @@ describe('The mach library', function()
318322
end)
319323
end)
320324

325+
it('should fail if should_be_called_with_any_arguments is used after a call has already been specified', function()
326+
should_fail_with('call already specified', function()
327+
f:should_be_called():should_be_called_with_any_arguments()
328+
end)
329+
end)
330+
321331
it('should allow calls to happen out of order when and_also is used', function()
322332
f1:should_be_called():
323333
and_also(f2:should_be_called()):

src/mach/Expectation.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,28 @@ function expectation:and_also(other)
111111
return self
112112
end
113113

114+
function expectation:should_be_called_with_any_arguments()
115+
if self._call_specified then
116+
error('call already specified', 2)
117+
end
118+
119+
self._call_specified = true
120+
table.insert(self._calls, ExpectedCall(self._m, { required = true, ignore_args = true }))
121+
return self
122+
end
123+
114124
function expectation:should_be_called_with(...)
115-
if self._call_specified == true then
125+
if self._call_specified then
116126
error('call already specified', 2)
117127
end
118128

119129
self._call_specified = true
120-
table.insert(self._calls, ExpectedCall(self._m, true, table.pack(...)))
130+
table.insert(self._calls, ExpectedCall(self._m, { required = true, args = table.pack(...) }))
121131
return self
122132
end
123133

124134
function expectation:should_be_called()
125-
if self._call_specified == true then
135+
if self._call_specified then
126136
error('call already specified', 2)
127137
end
128138

@@ -135,7 +145,7 @@ function expectation:may_be_called_with(...)
135145
end
136146

137147
self._call_specified = true
138-
table.insert(self._calls, ExpectedCall(self._m, false, table.pack(...)))
148+
table.insert(self._calls, ExpectedCall(self._m, { required = false, args = table.pack(...) }))
139149
return self
140150
end
141151

src/mach/ExpectedCall.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
local expected_call = {}
22
expected_call.__index = expected_call
33

4-
local function create(f, required, args)
4+
local function create(f, config)
55
local o = {
66
_f = f,
77
_ordered = false,
8-
_required = required,
9-
_args = args,
8+
_required = config.required,
9+
_args = config.args,
10+
_ignore_args = config.ignore_args,
1011
_return = {}
1112
}
1213

@@ -20,6 +21,7 @@ function expected_call:function_matches(f)
2021
end
2122

2223
function expected_call:args_match(args)
24+
if self._ignore_args then return true end
2325
if #self._args ~= #args then return false end
2426

2527
for k in ipairs(self._args) do

0 commit comments

Comments
 (0)