Skip to content

Commit 3b71a83

Browse files
committed
Added deep compare matcher
1 parent efb5f0d commit 3b71a83

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

rockspecs/mach-git-0.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ build = {
2323
['mach.out_of_order_call_error'] = 'mach/out_of_order_call_error.lua',
2424
['mach.format_call_status'] = 'mach/format_call_status.lua',
2525
['mach.format_arguments'] = 'mach/format_arguments.lua',
26+
['mach.deep_compare_matcher'] = 'mach/deep_compare_matcher.lua',
2627
}
2728
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
describe('mach.deep_compare_matcher', function()
2+
local match = require 'mach.deep_compare_matcher'
3+
4+
it('should indicate that identical numbers match', function()
5+
assert.is_true(match(13, 13))
6+
end)
7+
8+
it('should indicate that different numbers do not match', function()
9+
assert.is_false(match(13, 12))
10+
end)
11+
12+
it('should indicate that identical strings match', function()
13+
assert.is_true(match('hello', 'hello'))
14+
end)
15+
16+
it('should indicate that different strings do not match', function()
17+
assert.is_false(match('mach', 'mock'))
18+
end)
19+
20+
it('should indicate that identical booleans match', function()
21+
assert.is_true(match(true, true))
22+
assert.is_true(match(false, false))
23+
end)
24+
25+
it('should indicate that true and false do not match', function()
26+
assert.is_false(match(true, false))
27+
assert.is_false(match(false, true))
28+
end)
29+
30+
it('should indicate that nothing matches nil except nil', function()
31+
assert.is_false(match(nil, 13))
32+
assert.is_false(match(13, nil))
33+
assert.is_true(match(nil, nil))
34+
end)
35+
36+
it('should indicate that a table matches itself', function()
37+
local t = {}
38+
assert.is_true(match(t, t))
39+
end)
40+
41+
it('should indicate that tables with the same contents are the same', function()
42+
assert.is_true(match({ a = 1, b = 2 }, { a = 1, b = 2 }))
43+
end)
44+
45+
it('should indicate that tables with different keys are not the same', function()
46+
assert.is_true(match({ a = 1, b = 2 }, { a = 1, b = 2 }))
47+
end)
48+
49+
it('should indicate that two tables, one with an extra key, are not the same', function()
50+
assert.is_false(match({ a = 1, b = 2 }, { a = 1 }))
51+
assert.is_false(match({ a = 1 }, { a = 1, b = 2 }))
52+
end)
53+
54+
it('should indicate that tables with the same keys but different values are not the same', function()
55+
assert.is_false(match({ a = 1, b = 2 }, { a = 11, b = 22 }))
56+
end)
57+
58+
it('should indicate that deep tables match when their contents are the same', function()
59+
local t1 = {
60+
a = { b = 2 },
61+
c = { 1, 2, 3 }
62+
}
63+
64+
local t2 = {
65+
a = { b = 2 },
66+
c = { 1, 2, 3 }
67+
}
68+
69+
assert.is_true(match(t1, t2))
70+
end)
71+
72+
it('should indicate that deep tables match when their contents are different', function()
73+
local t1 = {
74+
a = { b = 2 },
75+
c = { 1, 2, 3 }
76+
}
77+
78+
local t2 = {
79+
a = { b = '2' },
80+
c = { 1, 2, 3 }
81+
}
82+
83+
assert.is_false(match(t1, t2))
84+
end)
85+
end)

src/mach/deep_compare_matcher.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local function matches(o1, o2)
2+
if o1 == o2 then return true end
3+
4+
if type(o1) == 'table' and type(o2) == 'table' then
5+
for k in pairs(o1) do
6+
if not matches(o1[k], o2[k]) then return false end
7+
end
8+
9+
for k in pairs(o2) do
10+
if not matches(o1[k], o2[k]) then return false end
11+
end
12+
13+
return true
14+
end
15+
16+
return false
17+
end
18+
19+
return matches

0 commit comments

Comments
 (0)