Skip to content

Commit b19862b

Browse files
committed
Added mach.any. Closes #18.
1 parent b03ad39 commit b19862b

File tree

8 files changed

+55
-20
lines changed

8 files changed

+55
-20
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ mocked_object.foo:should_be_called():when(function()
5757
end)
5858
```
5959

60+
## Ignoring Arguments
61+
62+
```lua
63+
local mach = require 'mach'
64+
65+
local f = mach.mock_function('f')
66+
67+
f:should_be_called_with_any_arguments():when(function() f('any', 'args', 'are', 'fine') end)
68+
```
69+
6070
## Returning Values
6171

6272
```lua
@@ -104,7 +114,7 @@ local mach = require 'mach'
104114

105115
local f = mach.mock_function('f')
106116

107-
f:mayBeCalled():when(function() end)
117+
f:may_be_called():when(function() end)
108118
```
109119

110120
## Optional Ordering
@@ -180,7 +190,20 @@ f:should_be_called_with(mach.match({ 1, 2, 3 }, custom_matcher)):
180190
end)
181191
```
182192

183-
## Ignoring Other calls
193+
## Matching Any Single Argument
194+
195+
```lua
196+
local mach = require 'mach'
197+
198+
local f = mach.mockFunction();
199+
200+
f:should_be_called_with(mach.any, 42):
201+
when(function()
202+
f({ 'whatever' }, 42)
203+
end)
204+
```
205+
206+
## Ignoring Other Calls
184207

185208
```javascript
186209
local mach = require 'mach'

rockspecs/mach-git-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ build = {
2525
['mach.format_arguments'] = 'mach/format_arguments.lua',
2626
['mach.deep_compare_matcher'] = 'mach/deep_compare_matcher.lua',
2727
['mach.match'] = 'mach/match.lua',
28+
['mach.any'] = 'mach/any.lua',
2829
}
2930
}

spec/mach_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,18 @@ describe('The mach library', function()
671671
end)
672672
end)
673673
end)
674+
675+
it('should match any argument with mach.any', function()
676+
f:should_be_called_with(mach.any, 2, 3):when(function()
677+
f({ a = 11, b = 22 }, 2, 3)
678+
end)
679+
end)
680+
681+
it('should match other arguments when mach.any is used for an argument', function()
682+
should_fail(function()
683+
f:should_be_called_with(mach.any, 2, 3):when(function()
684+
f(false, 2, 4)
685+
end)
686+
end)
687+
end)
674688
end)

src/mach.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ local mach_match = require 'mach.match'
66

77
local mach = {}
88

9+
mach.any = require 'mach.any'
10+
911
function unexpected_call(m, name, args)
1012
unexpected_call_error(name, args, {}, {}, 2)
1113
end

src/mach/CompletedCall.lua

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1+
local format_arguments = require 'mach.format_arguments'
2+
13
local completed_call = {}
24
completed_call.__index = completed_call
35

46
completed_call.__tostring = function(self)
5-
local arg_strings = {}
6-
for _, arg in ipairs(self._args) do
7-
table.insert(arg_strings, tostring(arg))
8-
end
9-
10-
return self._name .. '(' .. table.concat(arg_strings, ', ') .. ')'
7+
return self._name .. format_arguments(self._args)
118
end
129

1310
local function create(name, args)

src/mach/ExpectedCall.lua

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
local mach_match = require 'mach.match'
2+
local mach_any = require 'mach.any'
3+
local format_arguments = require 'mach.format_arguments'
24

35
local expected_call = {}
46
expected_call.__index = expected_call
57

68
expected_call.__tostring = function(self)
7-
local arg_strings = {}
8-
for _, arg in ipairs(self._args) do
9-
table.insert(arg_strings, tostring(arg))
10-
end
11-
12-
local s = self._f._name .. '(' .. table.concat(arg_strings, ', ') .. ')'
9+
local s = self._f._name .. format_arguments(self._args)
1310

1411
if not self._required then
1512
s = s .. ' (optional)'
@@ -41,10 +38,10 @@ function expected_call:args_match(args)
4138
if self._ignore_args then return true end
4239
if #self._args ~= #args then return false end
4340

44-
for k in ipairs(self._args) do
45-
if getmetatable(self._args[k]) == mach_match then
46-
if not self._args[k].matcher(self._args[k].value, args[k]) then return false end
47-
elseif self._args[k] ~= args[k] then
41+
for i = 1, self._args.n do
42+
if getmetatable(self._args[i]) == mach_match then
43+
if not self._args[i].matcher(self._args[i].value, args[i]) then return false end
44+
elseif self._args[i] ~= mach_any and self._args[i] ~= args[i] then
4845
return false
4946
end
5047
end

src/mach/any.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
return {}

src/mach/format_arguments.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
return function(args)
22
local arg_strings = {}
3-
for _, arg in ipairs(args) do
4-
table.insert(arg_strings, tostring(arg))
3+
for i = 1, args.n do
4+
table.insert(arg_strings, tostring(args[i]))
55
end
66

77
return '(' .. table.concat(arg_strings, ', ') .. ')'

0 commit comments

Comments
 (0)