Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Merge 6bd217e into 9493406
Browse files Browse the repository at this point in the history
  • Loading branch information
jeparlefrancais committed Aug 8, 2019
2 parents 9493406 + 6bd217e commit ec3f986
Show file tree
Hide file tree
Showing 30 changed files with 2,836 additions and 131 deletions.
51 changes: 1 addition & 50 deletions src/assertDeepEqual.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,7 @@
This should only be used in tests.
]]

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
local deepEqual = require(script.Parent.deepEqual)

local function assertDeepEqual(a, b)
local success, innerMessageTemplate = deepEqual(a, b)
Expand Down
89 changes: 8 additions & 81 deletions src/assertDeepEqual.spec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
return function()
local assertDeepEqual = require(script.Parent.assertDeepEqual)

it("should fail with a message when args are not equal", function()
it("should not throw if the args are equal", function()
assertDeepEqual(1, 1)
assertDeepEqual("hello", "hello")
end)

it("should throw and format the error message when args are not equal", function()
local success, message = pcall(assertDeepEqual, 1, 2)

expect(success).to.equal(false)
Expand All @@ -15,85 +20,7 @@ return function()

expect(success).to.equal(false)
expect(message:find("first%[foo%] ~= second%[foo%]")).to.be.ok()
end)

it("should compare non-table values using standard '==' equality", function()
assertDeepEqual(1, 1)
assertDeepEqual("hello", "hello")
assertDeepEqual(nil, nil)

local someFunction = function() end
local theSameFunction = someFunction

assertDeepEqual(someFunction, theSameFunction)

local A = {
foo = someFunction
}
local B = {
foo = theSameFunction
}

assertDeepEqual(A, B)
end)

it("should fail when types differ", function()
local success, message = pcall(assertDeepEqual, 1, "1")

expect(success).to.equal(false)
expect(message:find("first is of type number, but second 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,
}
}

assertDeepEqual(A, B)

local C = {
foo = "bar",
nested = {
foo = 1,
bar = 3,
}
}

local success, message = pcall(assertDeepEqual, A, C)

expect(success).to.equal(false)
expect(message:find("first%[nested%]%[bar%] ~= second%[nested%]%[bar%]")).to.be.ok()
end)

it("should be commutative", function()
local equalArgsA = {
foo = "bar",
hello = "world",
}
local equalArgsB = {
foo = "bar",
hello = "world",
}

assertDeepEqual(equalArgsA, equalArgsB)
assertDeepEqual(equalArgsB, equalArgsA)

local nonEqualArgs = {
foo = "bar",
}

expect(function() assertDeepEqual(equalArgsA, nonEqualArgs) end).to.throw()
expect(function() assertDeepEqual(nonEqualArgs, equalArgsA) end).to.throw()
expect(message:find("{1}")).never.to.be.ok()
expect(message:find("{2}")).never.to.be.ok()
end)
end
51 changes: 51 additions & 0 deletions src/deepEqual.lua
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
99 changes: 99 additions & 0 deletions src/deepEqual.spec.lua
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
2 changes: 2 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local createReconcilerCompat = require(script.createReconcilerCompat)
local RobloxRenderer = require(script.RobloxRenderer)
local strict = require(script.strict)
local Binding = require(script.Binding)
local shallow = require(script.shallow)

local robloxReconciler = createReconciler(RobloxRenderer)
local reconcilerCompat = createReconcilerCompat(robloxReconciler)
Expand All @@ -32,6 +33,7 @@ local Roact = strict {
mount = robloxReconciler.mountVirtualTree,
unmount = robloxReconciler.unmountVirtualTree,
update = robloxReconciler.updateVirtualTree,
shallow = shallow,

reify = reconcilerCompat.reify,
teardown = reconcilerCompat.teardown,
Expand Down
1 change: 1 addition & 0 deletions src/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return function()
mount = "function",
unmount = "function",
update = "function",
shallow = "function",
oneChild = "function",
setGlobalConfig = "function",

Expand Down
Loading

0 comments on commit ec3f986

Please sign in to comment.