Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/tiniest_expect.luau
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,57 @@ function tiniest_expect.expect(
return if a == b then tests else fail()
end

function tests.is_shallow_equal(
b: {[any]: any}
)
check(typeof(a) == "table", "expect() value must be a table")
check(typeof(b) == "table", "is_shallow_equal() value must be a table")
local failureReasons = {}
local checkedKeys = {}
for k, v in a do
if b[k] ~= v then
table.insert(failureReasons, `Key [{tostring(k)}] is not equivalent. Got {tostring(v)} and {tostring(b[k])}.`)
end
checkedKeys[k] = true
end
for k, v in b do
if a[k] ~= v and checkedKeys[k] ~= true then
table.insert(failureReasons, `Key [{tostring(k)}] is not equivalent. Got {tostring(a[k])} and {tostring(v)}.`)
end
end
return if #failureReasons == 0 then tests else fail(table.concat(failureReasons, "\n"))
end

function tests.is_deep_equal(
b: {[any]: any}
)
check(typeof(a) == "table", "expect() value must be a table")
check(typeof(b) == "table", "is_deep_equal() value must be a table")
local failureReasons = {}
local checkedKeys = {}
local function deep_check(a: any, b: any, path: string)
for k, v in a do
local new_path = path .. "[" .. tostring(k) .. "]"
checkedKeys[new_path] = true

if typeof(v) == "table" and typeof(b[k]) == "table" then
deep_check(v, b[k], new_path)
elseif b[k] ~= v then
table.insert(failureReasons, `Key {new_path} is not equivalent. Got {tostring(v)} and {tostring(b[k])}.`)
end
end
for k, v in b do
local new_path = path .. "[" .. tostring(k) .. "]"
if checkedKeys[new_path] ~= true and a[k] ~= v then
table.insert(failureReasons, `Key {new_path} is not equivalent. Got {tostring(a[k])} and {tostring(v)}.`)
end
end
end

deep_check(a, b, "")
return if #failureReasons == 0 then tests else fail(table.concat(failureReasons, "\n"))
end

function tests.never_is(
b: any
)
Expand Down Expand Up @@ -173,4 +224,4 @@ function tiniest_expect.expect(
return tests
end

return tiniest_expect
return tiniest_expect