Created
September 11, 2022 10:24
-
-
Save Shilo/89760320f7cd284253f1723d3e89eff1 to your computer and use it in GitHub Desktop.
Test for shallow comparing tables in Roblox Studio.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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