Skip to content

Commit 4e3f610

Browse files
committed
Allow for table contents to be matched. Closes #5.
1 parent 3b71a83 commit 4e3f610

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

rockspecs/mach-git-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ build = {
2424
['mach.format_call_status'] = 'mach/format_call_status.lua',
2525
['mach.format_arguments'] = 'mach/format_arguments.lua',
2626
['mach.deep_compare_matcher'] = 'mach/deep_compare_matcher.lua',
27+
['mach.match'] = 'mach/match.lua',
2728
}
2829
}

spec/mach_spec.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,4 +629,18 @@ describe('The mach library', function()
629629
end)
630630
end)
631631
end)
632+
633+
it('should allow the contents of tables arguments to be matched with the default matcher', function()
634+
f:should_be_called_with(mach.match({ a = 1, b = 2 })):when(function()
635+
f({ a = 1, b = 2 })
636+
end)
637+
end)
638+
639+
it('should not allow table contents to be different when using the default matcher', function()
640+
should_fail(function()
641+
f:should_be_called_with(mach.match({ a = 1, b = 2 })):when(function()
642+
f({ a = 11, b = 22 })
643+
end)
644+
end)
645+
end)
632646
end)

src/mach.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local ExpectedCall = require 'mach.ExpectedCall'
22
local Expectation = require 'mach.Expectation'
33
local unexpected_call_error = require 'mach.unexpected_call_error'
4+
local default_matcher = require 'mach.deep_compare_matcher'
5+
local mach_match = require 'mach.match'
46

57
local mach = {}
68

@@ -90,4 +92,8 @@ function mach.mock_object(o, name)
9092
return mocked
9193
end
9294

95+
function mach.match(value)
96+
return setmetatable({ value = value, matcher = default_matcher }, mach_match)
97+
end
98+
9399
return setmetatable(mach, { __call = function(_, ...) return Expectation(...) end })

src/mach/ExpectedCall.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local mach_match = require 'mach.match'
2+
13
local expected_call = {}
24
expected_call.__index = expected_call
35

@@ -40,7 +42,11 @@ function expected_call:args_match(args)
4042
if #self._args ~= #args then return false end
4143

4244
for k in ipairs(self._args) do
43-
if self._args[k] ~= args[k] then return false end
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
48+
return false
49+
end
4450
end
4551

4652
return true

0 commit comments

Comments
 (0)