Skip to content

Instantly share code, notes, and snippets.

@Shilo
Created September 11, 2022 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/89760320f7cd284253f1723d3e89eff1 to your computer and use it in GitHub Desktop.
Save Shilo/89760320f7cd284253f1723d3e89eff1 to your computer and use it in GitHub Desktop.
Test for shallow comparing tables in Roblox Studio.
local function TestTableComparison(eq)
local function ComparableTable(instance)
return setmetatable(instance, { __eq = eq })
end
local a = ComparableTable({ 1, 2, 3, "banana", "apple", 4, 5 })
local b = ComparableTable({ 1, 2, 3, "banana", "apple", 4, 5 })
local c = ComparableTable({ 1, 2, 3, "banana", "apple", 4, 5 })
local d = ComparableTable({ 1, 2, 3, "banana", "orange", 4, 5 })
assert(a == b, "Tables a and b are not equal.")
assert(c ~= d, "Tables c and d are equal.")
print("All table equality tests have passed.")
end
TestTableComparison(function(a, b)
for i, v in a do
if v ~= b[i] then
return false
end
end
return true
end)
TestTableComparison(function(a, b)
return table.concat(a, ",") == table.concat(b, ",") -- This is slower way of checking equality
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment