When writing a test that should verify a table, I used luaunit.assertEquals(table1, table2)
A stack overflow occurs because table1
contains a key with a value set to table1
. The issue is a result of logic in the _is_table_equals()
function in luaunit.lua (currently line 510).
While debugging, I have added a print statement just above this recursive call at line 527:
print("_is_table_equals Testing:", actual, k, v)
if not _is_table_equals(v, expected[k]) then
return false
end
resulting in the following log output:
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
_is_table_equals Testing: table: 0AA87F28 removeEventListener function: 0A4F5070
_is_table_equals Testing: table: 0AA87F28 addEventListener function: 0A4F52F0
_is_table_equals Testing: table: 0AA87F28 __index table: 0AA87F28
ERROR
stack overflow
Here, you can see that the actual
table has the same memory address as the __index
variable (this is a 3rd party library and I presume they have done this to set the metatable as the table itself).
A simplistic fix would be to add a check before the recursive call that verifies whether v
or expected[k]
are the same as actual. Example:
if v ~= actual and expected[k] ~= actual then
if not _is_table_equals(v, expected[k]) then
return false
end
end
And, of course, a corresponding check in the next for loop (for expected
):
if v ~= expected and actual[k] ~= expected then
if not _is_table_equals(v, actual[k]) then
return false
end
end
However, I'm not sure if this has any further implications.
Additional Info:
- My current workaround is to use
luaunit.assertTrue(table1 == table2)
- This issue existed at least as far back as
d80ebece38398866111a0aa197830cc486c3ba90