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

@ -19,7 +19,7 @@ test("should sync state payload", function()
data = { a = 2 }, data = { a = 2 },
}) })
assert(a() == 2) assert(a() == 2, "should set a to the initial value")
end) end)
test("should sync state patch", function() test("should sync state patch", function()
@ -32,16 +32,16 @@ test("should sync state patch", function()
data = { a = a() }, data = { a = a() },
}) })
assert(a() == previous) assert(a() == previous, "should set a to the initial value")
client:sync({ client:sync({
type = "patch", type = "patch",
data = { a = { b = 3 } }, data = { a = { b = 3 } },
}) })
assert(a() ~= previous) assert(a() ~= previous, "should apply patch")
assert(a().b == 3) assert(a().b == 3, "should set b to the new value")
assert(a().c == 2) assert(a().c == 2, "should not change c")
end) end)
test("should fix sparse array patches", function() test("should fix sparse array patches", function()
@ -61,8 +61,8 @@ test("should fix sparse array patches", function()
data = { a = { ["2"] = 2 } }, data = { a = { ["2"] = 2 } },
}) })
assert(a()["2" :: any] == nil) assert(a()["2" :: any] == nil, "should convert serialized arrays to use numbers")
assert(a()[2] == 2) assert(a()[2] == 2, "should convert serialized arrays to use numbers")
end) end)
test("should apply multiple patches", function() test("should apply multiple patches", function()
@ -87,9 +87,8 @@ test("should apply multiple patches", function()
data = { a = 3 }, data = { a = 3 },
}) })
assert(a() == 3) assert(a() == 3, "should apply the last patch")
assert(calls[1] == 2) assert(table.concat(calls) == "23", `wrong order: {table.concat(calls)}`)
assert(calls[2] == 3)
end) end)
test("should batch patches", function() test("should batch patches", function()
@ -116,10 +115,10 @@ test("should batch patches", function()
data = { a = 1, b = 2 }, data = { a = 1, b = 2 },
}) })
assert(a() == 1) assert(a() == 1, "should set a to the new value")
assert(b() == 2) assert(b() == 2, "should set b to the new value")
assert(states.a == 1) assert(states.a == 1, "sub to a should run after b updates")
assert(states.b == 2) assert(states.b == 2, "sub to b should run after a updates")
end) end)
return {} return {}

View file

@ -17,12 +17,12 @@ test("should flatten atoms", function()
} }
local result = flatten(input) local result = flatten(input)
assert(result.a == input.a) assert(result.a == input.a, "should flatten the first level")
assert(result["b/c"] == input.b.c) assert(result["b/c"] == input.b.c, "should flatten the second level")
assert(result["d/e/f"] == input.d.e.f) assert(result["d/e/f"] == input.d.e.f, "should flatten the third level")
assert(result.b == nil) assert(result.b == nil, "should remove the original nested tables")
assert(result.d == nil) assert(result.d == nil, "should remove the original nested tables")
end) end)
return {} return {}

View file

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

View file

@ -25,9 +25,9 @@ test("sends state to new players", function()
server:hydrate(target) server:hydrate(target)
assert(player == target) assert(player == target, "should send to the target player")
assert(payload.type == "init") assert(payload.type == "init", "should be an init payload")
assert(payload.data.a == 1) assert(payload.data.a == 1, "should send the initial state")
disconnect() disconnect()
end) end)
@ -44,17 +44,17 @@ test("sends state patches", function()
a({ b = 1, c = 5 }) a({ b = 1, c = 5 })
server:_sendPatch(target) server:_sendPatch(target)
assert(player == target) assert(player == target, "should send to the target player")
assert(payload.type == "patch") assert(payload.type == "patch", "should be a patch payload")
assert(payload.data.a.b == nil) assert(payload.data.a.b == nil, "should not send unchanged values")
assert(payload.data.a.c == 5) assert(payload.data.a.c == 5, "should send changed values")
a({ b = 1, d = 1 }) a({ b = 1, d = 1 })
server:_sendPatch(target) server:_sendPatch(target)
assert(payload.data.a.b == nil) assert(payload.data.a.b == nil, "should not send unchanged values")
assert(patch.isNone(payload.data.a.c) == true) assert(patch.isNone(payload.data.a.c) == true, "should send None for removed values")
assert(payload.data.a.d == 1) assert(payload.data.a.d == 1, "should send new values")
disconnect() disconnect()
end) end)
@ -78,23 +78,23 @@ test("preserves history", function()
b(2) b(2)
server:_sendPatch(target) server:_sendPatch(target)
assert(#payloads == 1) assert(#payloads == 1, "should only have one payload")
assert(payloads[1].type == "patch") assert(payloads[1].type == "patch", "should be a patch payload")
assert(payloads[1].data.a == 2) assert(payloads[1].data.a == 2, "should send the new value of a")
assert(payloads[1].data.b == 2) assert(payloads[1].data.b == 2, "should send the new value of b")
a(3) a(3)
a(4) a(4)
b(3) b(3)
server:_sendPatch(target) server:_sendPatch(target)
assert(#payloads == 2) assert(#payloads == 2, "should have two payloads")
assert(payloads[1].type == "patch") assert(payloads[1].type == "patch", "1 should be a patch payload")
assert(payloads[1].data.a == 3) assert(payloads[1].data.a == 3, "1 should send the new value of a")
assert(payloads[1].data.b == nil) assert(payloads[1].data.b == nil, "1 should not send unchanged values")
assert(payloads[2].type == "patch") assert(payloads[2].type == "patch", "2 should be a patch payload")
assert(payloads[2].data.a == 4) assert(payloads[2].data.a == 4, "2 should send the new value of a")
assert(payloads[2].data.b == 3) assert(payloads[2].data.b == 3, "2 should send the new value of b")
disconnect() disconnect()
end) end)

View file

@ -15,9 +15,9 @@ test("tracks atom state", function()
local hook = renderHook(function() local hook = renderHook(function()
return useAtom(state) return useAtom(state)
end) end)
assert(hook.result == 0) assert(hook.result == 0, "should return 0")
state(1) state(1)
assert(hook.result == 1) assert(hook.result == 1, "should return 1")
end) end)
test("accepts a selector", function() test("accepts a selector", function()
@ -27,9 +27,9 @@ test("accepts a selector", function()
return 2 * state() return 2 * state()
end) end)
end) end)
assert(hook.result == 0) assert(hook.result == 0, "should return 0")
state(1) state(1)
assert(hook.result == 2) assert(hook.result == 2, "should return 2")
end) end)
test("tracks dependencies", function() test("tracks dependencies", function()
@ -37,14 +37,14 @@ test("tracks dependencies", function()
local hook = renderHook(function() local hook = renderHook(function()
return useAtom(state, { state }) return useAtom(state, { state })
end) end)
assert(hook.result == 0) assert(hook.result == 0, "should return 0")
state(1) state(1)
assert(hook.result == 1) assert(hook.result == 1, "should return 1")
state = atom(0) state = atom(0)
hook.rerender() hook.rerender()
assert(hook.result == 0) assert(hook.result == 0, "should return 0")
state(1) state(1)
assert(hook.result == 1) assert(hook.result == 1, "should return 1")
end) end)
return {} return {}

View file

@ -17,9 +17,9 @@ test("tracks atom state", function()
local cleanup = root(function() local cleanup = root(function()
source = useAtom(state) source = useAtom(state)
end) end)
assert(source() == 0) assert(source() == 0, "should return 0")
state(1) state(1)
assert(source() == 1) assert(source() == 1, "should return 1")
cleanup() cleanup()
end) end)
@ -31,9 +31,9 @@ test("accepts a selector", function()
return 2 * state() return 2 * state()
end) end)
end) end)
assert(source() == 0) assert(source() == 0, "should return 0")
state(1) state(1)
assert(source() == 2) assert(source() == 2, "should return 2")
cleanup() cleanup()
end) end)

View file

@ -4,8 +4,3 @@ globals:
require: require:
args: args:
- type: string - type: string
assert:
args:
- type: any
- type: any
required: false