refactor: add assertion error messages

This commit is contained in:
littensy 2025-05-06 11:18:19 -07:00
parent bcf02a615f
commit d56672384a
7 changed files with 74 additions and 80 deletions

View file

@ -17,9 +17,9 @@ test("returns a patch", function()
local nextState = { foo = "bar", baz = "quux" }
local patches = patch.diff(prevState, nextState)
assert(patches ~= nil)
assert(patches.baz == "quux")
assert(patches.foo == nil)
assert(patches ~= nil, "should return a patch")
assert(patches.baz == "quux", "should return the changed value")
assert(patches.foo == nil, "should omit unchanged values")
end)
test("applies a patch", function()
@ -27,10 +27,10 @@ test("applies a patch", function()
local patches = { baz = "quux", qaz = "qux" }
local applied = patch.apply(prevState, patches)
assert(applied ~= nil)
assert(applied.baz == "quux")
assert(applied.foo == "bar")
assert(applied.qaz == "qux")
assert(applied ~= nil, "should return a new state")
assert(applied.baz == "quux", "should apply the changed value")
assert(applied.foo == "bar", "should keep the unchanged value")
assert(applied.qaz == "qux", "should add the new value")
end)
test("allows nested patches", function()
@ -39,7 +39,7 @@ test("allows nested patches", function()
local patches = patch.diff(prevState, nextState)
local applied = patch.apply(prevState, patches)
assert(applied.foo.bar == "qux")
assert(applied.foo.bar == "qux", "should apply the nested patch")
end)
test("preserves array density when applying", function()
@ -47,13 +47,13 @@ test("preserves array density when applying", function()
local nextState = { ["2"] = "B", ["4"] = "D" }
local applied = patch.apply(prevState, nextState)
assert(applied[1] == "a")
assert(applied[2] == "B")
assert(applied[3] == "c")
assert(applied[4] == "D")
assert(applied[1] == "a", "should keep a")
assert(applied[2] == "B", "should apply B")
assert(applied[3] == "c", "should keep c")
assert(applied[4] == "D", "should apply D")
assert(applied["2"] == nil)
assert(applied["4"] == nil)
assert(applied["2"] == nil, "should convert serialized arrays to use numbers")
assert(applied["4"] == nil, "should convert serialized arrays to use numbers")
end)
test("preserves array density after serdes", function()
@ -66,11 +66,11 @@ test("preserves array density after serdes", function()
local applied = patch.apply(prevState, patches)
for index in next, prevState do
assert(applied[index] == nextState[index])
assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
end
for index in next, applied do
assert(applied[index] == nextState[index])
assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
end
end)
@ -86,7 +86,7 @@ test("preserves large gaps between keys", function()
assert(os.clock() - start < 10e-3, `Performance is not acceptable ({(os.clock() - start) * 1e3}ms)`)
for index in next, prevState do
assert(applied[index] == nextState[index])
assert(applied[index] == nextState[index], `expected {applied[index]} to be {nextState[index]}`)
end
end)
@ -99,17 +99,17 @@ test("cannot preserve sparse arrays", function()
-- Not enough information is available to determine whether 'patches'
-- should keep the string keys added during remote event serialization.
assert(applied["2"] ~= nil)
assert(applied[2] == nil)
assert(applied["2"] ~= nil, "will fail to convert string keys to numbers")
assert(applied[2] == nil, "will fail to convert string keys to numbers")
end)
test("optionally fixes sparse arrays", function()
for index in next, patch.diff({ 1, 1, 1, 1, 1 }, { 2, 1, 1, 2 }, true) do
assert(type(index) == "string")
assert(type(index) == "string", `expected {type(index)} {index} to be string`)
end
for index in next, patch.diff({ 1, 1, 1, 1, 1 }, { 2, 1, 1, 2 }, false) do
assert(type(index) == "number")
assert(type(index) == "number", `expected {type(index)} {index} to be number`)
end
end)
@ -118,13 +118,13 @@ test("optionally validates state", function()
patch.diff({}, { [0.1] = true }, true)
end)
assert(not success)
assert(not success, "should not allow non-integer keys")
success = pcall(function()
patch.diff({}, { [0.1] = true }, false)
end)
assert(success)
assert(success, "should skip validation if not requested")
end)
return {}