Skip to content

Commit 95a1c3f

Browse files
mandeserolocker
authored andcommitted
Fix assert_covers treat arrays as maps
`assert_covers` now correctly treats arrays as scalars, ensuring that one array is not considered to cover another unless they are equal. Closes #405
1 parent de42344 commit 95a1c3f

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
- Fixed a bug when `assert_covers` treats arrays as maps (gh-405).
56
- Added the ability to keep and adjust cluster declarative configuration
67
with `cluster:modify_config()` and apply it later via
78
`cluster:apply_config_changes()` without passing an explicit

luatest/assertions.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ local function table_slice(actual, expected)
443443
if type(expected) ~= 'table' or type(actual) ~= 'table' then
444444
return actual
445445
end
446+
447+
if utils.table_is_array(actual) or utils.table_is_array(expected) then
448+
return actual
449+
end
450+
446451
local sliced = {}
447452
for k, _ in pairs(expected) do
448453
sliced[k] = table_slice(actual[k], expected[k])

luatest/utils.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,13 @@ function utils.pathjoin(a, b)
242242
return fio.pathjoin(a, b)
243243
end
244244

245+
function utils.table_is_array(t)
246+
local i = 0
247+
for _ in pairs(t) do
248+
i = i + 1
249+
if t[i] == nil then return false end
250+
end
251+
return true
252+
end
253+
245254
return utils

test/luatest_test.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,27 @@ end
105105

106106
g.test_assert_covers = function()
107107
local subject = t.assert_covers
108-
subject({a = 1, b = 2, c = 3}, {})
109108
subject({a = 1, b = 2, c = 3}, {a = 1})
110109
subject({a = 1, b = 2, c = 3}, {a = 1, c = 3})
111110
subject({a = 1, b = 2, c = 3}, {a = 1, b = 2, c = 3})
112111
subject({a = {b = 1, c = 2}}, {a = {b = 1}})
113112
subject({a = 1, b = {c = 2, d = {e = 3, f = 4}}}, {b = {d = {f = 4}}})
114113
subject({a = box.NULL}, {a = box.NULL})
115114
subject({a = box.tuple.new({1})}, {a = box.tuple.new({1})})
115+
subject({1, 2}, {1, 2})
116+
subject({[0] = 'zero', [1] = 'one'}, {[0] = 'zero'})
116117

117118
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 2})
118119
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 1, b = 1})
119120
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 1, b = 2, c = 3, d = 4})
120121
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {d = 1})
121122
helper.assert_failure(subject, {a = {b = 1, c = 2}}, {a = {b = 2, c = 2}})
122123
helper.assert_failure(subject, {a = {b = 1, c = 2}}, {a = {b = 1, c = 2, d = 3}})
124+
helper.assert_failure(subject, {1, 2}, {})
125+
helper.assert_failure(subject, {1, 2, 3}, {1, 2})
126+
helper.assert_failure(subject, {1, 2, foo = 'bar'}, {1, 2})
127+
helper.assert_failure(subject, {a = {1, 2, 3}}, {a = {1, 2}})
128+
helper.assert_failure(subject, {a = {1, 2}}, {a = {}})
123129
helper.assert_failure(subject, {a = nil}, {a = box.NULL})
124130
helper.assert_failure(subject, {a = box.tuple.new({1})}, {a = box.tuple.new({2})})
125131
helper.assert_failure_contains('Argument 1 and 2 must be tables', subject, {a = 1, b = 2, c = 3}, nil)
@@ -133,7 +139,6 @@ g.test_assert_not_covers = function()
133139
subject({a = 1, b = 2, c = 3}, {d = 1})
134140
subject({a = nil}, {a = box.NULL})
135141

136-
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {})
137142
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 1})
138143
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 1, c = 3})
139144
helper.assert_failure(subject, {a = 1, b = 2, c = 3}, {a = 1, b = 2, c = 3})

0 commit comments

Comments
 (0)