This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
2,836 additions
and
131 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
local function deepEqual(a, b) | ||
if typeof(a) ~= typeof(b) then | ||
local message = ("{1} is of type %s, but {2} is of type %s"):format( | ||
typeof(a), | ||
typeof(b) | ||
) | ||
return false, message | ||
end | ||
|
||
if typeof(a) == "table" then | ||
local visitedKeys = {} | ||
|
||
for key, value in pairs(a) do | ||
visitedKeys[key] = true | ||
|
||
local success, innerMessage = deepEqual(value, b[key]) | ||
if not success then | ||
local message = innerMessage | ||
:gsub("{1}", ("{1}[%s]"):format(tostring(key))) | ||
:gsub("{2}", ("{2}[%s]"):format(tostring(key))) | ||
|
||
return false, message | ||
end | ||
end | ||
|
||
for key, value in pairs(b) do | ||
if not visitedKeys[key] then | ||
local success, innerMessage = deepEqual(value, a[key]) | ||
|
||
if not success then | ||
local message = innerMessage | ||
:gsub("{1}", ("{1}[%s]"):format(tostring(key))) | ||
:gsub("{2}", ("{2}[%s]"):format(tostring(key))) | ||
|
||
return false, message | ||
end | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
if a == b then | ||
return true | ||
end | ||
|
||
local message = "{1} ~= {2}" | ||
return false, message | ||
end | ||
|
||
return deepEqual |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
return function() | ||
local deepEqual = require(script.Parent.deepEqual) | ||
|
||
it("should compare non-table values using standard '==' equality", function() | ||
expect(deepEqual(1, 1)).to.equal(true) | ||
expect(deepEqual("hello", "hello")).to.equal(true) | ||
expect(deepEqual(nil, nil)).to.equal(true) | ||
|
||
local someFunction = function() end | ||
local theSameFunction = someFunction | ||
|
||
expect(deepEqual(someFunction, theSameFunction)).to.equal(true) | ||
|
||
local A = { | ||
foo = someFunction | ||
} | ||
local B = { | ||
foo = theSameFunction | ||
} | ||
|
||
expect(deepEqual(A, B)).to.equal(true) | ||
end) | ||
|
||
it("should fail with a message when args are not equal", function() | ||
local success, message = deepEqual(1, 2) | ||
|
||
expect(success).to.equal(false) | ||
expect(message:find("{1} ~= {2}")).to.be.ok() | ||
|
||
success, message = deepEqual({ | ||
foo = 1, | ||
}, { | ||
foo = 2, | ||
}) | ||
|
||
expect(success).to.equal(false) | ||
expect(message:find("{1}%[foo%] ~= {2}%[foo%]")).to.be.ok() | ||
end) | ||
|
||
it("should fail when types differ", function() | ||
local success, message = deepEqual(1, "1") | ||
|
||
expect(success).to.equal(false) | ||
expect(message:find("{1} is of type number, but {2} is of type string")).to.be.ok() | ||
end) | ||
|
||
it("should compare (and report about) nested tables", function() | ||
local A = { | ||
foo = "bar", | ||
nested = { | ||
foo = 1, | ||
bar = 2, | ||
} | ||
} | ||
local B = { | ||
foo = "bar", | ||
nested = { | ||
foo = 1, | ||
bar = 2, | ||
} | ||
} | ||
|
||
deepEqual(A, B) | ||
|
||
local C = { | ||
foo = "bar", | ||
nested = { | ||
foo = 1, | ||
bar = 3, | ||
} | ||
} | ||
|
||
local success, message = deepEqual(A, C) | ||
|
||
expect(success).to.equal(false) | ||
expect(message:find("{1}%[nested%]%[bar%] ~= {2}%[nested%]%[bar%]")).to.be.ok() | ||
end) | ||
|
||
it("should be commutative", function() | ||
local equalArgsA = { | ||
foo = "bar", | ||
hello = "world", | ||
} | ||
local equalArgsB = { | ||
foo = "bar", | ||
hello = "world", | ||
} | ||
|
||
expect(deepEqual(equalArgsA, equalArgsB)).to.equal(true) | ||
expect(deepEqual(equalArgsB, equalArgsA)).to.equal(true) | ||
|
||
local nonEqualArgs = { | ||
foo = "bar", | ||
} | ||
|
||
expect(deepEqual(equalArgsA, nonEqualArgs)).to.equal(false) | ||
expect(deepEqual(nonEqualArgs, equalArgsA)).to.equal(false) | ||
end) | ||
end |
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
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
Oops, something went wrong.