mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
refactor: add assertion error messages
This commit is contained in:
parent
bcf02a615f
commit
d56672384a
7 changed files with 74 additions and 80 deletions
|
|
@ -19,7 +19,7 @@ test("should sync state payload", function()
|
|||
data = { a = 2 },
|
||||
})
|
||||
|
||||
assert(a() == 2)
|
||||
assert(a() == 2, "should set a to the initial value")
|
||||
end)
|
||||
|
||||
test("should sync state patch", function()
|
||||
|
|
@ -32,16 +32,16 @@ test("should sync state patch", function()
|
|||
data = { a = a() },
|
||||
})
|
||||
|
||||
assert(a() == previous)
|
||||
assert(a() == previous, "should set a to the initial value")
|
||||
|
||||
client:sync({
|
||||
type = "patch",
|
||||
data = { a = { b = 3 } },
|
||||
})
|
||||
|
||||
assert(a() ~= previous)
|
||||
assert(a().b == 3)
|
||||
assert(a().c == 2)
|
||||
assert(a() ~= previous, "should apply patch")
|
||||
assert(a().b == 3, "should set b to the new value")
|
||||
assert(a().c == 2, "should not change c")
|
||||
end)
|
||||
|
||||
test("should fix sparse array patches", function()
|
||||
|
|
@ -61,8 +61,8 @@ test("should fix sparse array patches", function()
|
|||
data = { a = { ["2"] = 2 } },
|
||||
})
|
||||
|
||||
assert(a()["2" :: any] == nil)
|
||||
assert(a()[2] == 2)
|
||||
assert(a()["2" :: any] == nil, "should convert serialized arrays to use numbers")
|
||||
assert(a()[2] == 2, "should convert serialized arrays to use numbers")
|
||||
end)
|
||||
|
||||
test("should apply multiple patches", function()
|
||||
|
|
@ -87,9 +87,8 @@ test("should apply multiple patches", function()
|
|||
data = { a = 3 },
|
||||
})
|
||||
|
||||
assert(a() == 3)
|
||||
assert(calls[1] == 2)
|
||||
assert(calls[2] == 3)
|
||||
assert(a() == 3, "should apply the last patch")
|
||||
assert(table.concat(calls) == "23", `wrong order: {table.concat(calls)}`)
|
||||
end)
|
||||
|
||||
test("should batch patches", function()
|
||||
|
|
@ -116,10 +115,10 @@ test("should batch patches", function()
|
|||
data = { a = 1, b = 2 },
|
||||
})
|
||||
|
||||
assert(a() == 1)
|
||||
assert(b() == 2)
|
||||
assert(states.a == 1)
|
||||
assert(states.b == 2)
|
||||
assert(a() == 1, "should set a to the new value")
|
||||
assert(b() == 2, "should set b to the new value")
|
||||
assert(states.a == 1, "sub to a should run after b updates")
|
||||
assert(states.b == 2, "sub to b should run after a updates")
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -17,12 +17,12 @@ test("should flatten atoms", function()
|
|||
}
|
||||
local result = flatten(input)
|
||||
|
||||
assert(result.a == input.a)
|
||||
assert(result["b/c"] == input.b.c)
|
||||
assert(result["d/e/f"] == input.d.e.f)
|
||||
assert(result.a == input.a, "should flatten the first level")
|
||||
assert(result["b/c"] == input.b.c, "should flatten the second level")
|
||||
assert(result["d/e/f"] == input.d.e.f, "should flatten the third level")
|
||||
|
||||
assert(result.b == nil)
|
||||
assert(result.d == nil)
|
||||
assert(result.b == nil, "should remove the original nested tables")
|
||||
assert(result.d == nil, "should remove the original nested tables")
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -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 {}
|
||||
|
|
|
|||
|
|
@ -25,9 +25,9 @@ test("sends state to new players", function()
|
|||
|
||||
server:hydrate(target)
|
||||
|
||||
assert(player == target)
|
||||
assert(payload.type == "init")
|
||||
assert(payload.data.a == 1)
|
||||
assert(player == target, "should send to the target player")
|
||||
assert(payload.type == "init", "should be an init payload")
|
||||
assert(payload.data.a == 1, "should send the initial state")
|
||||
|
||||
disconnect()
|
||||
end)
|
||||
|
|
@ -44,17 +44,17 @@ test("sends state patches", function()
|
|||
a({ b = 1, c = 5 })
|
||||
server:_sendPatch(target)
|
||||
|
||||
assert(player == target)
|
||||
assert(payload.type == "patch")
|
||||
assert(payload.data.a.b == nil)
|
||||
assert(payload.data.a.c == 5)
|
||||
assert(player == target, "should send to the target player")
|
||||
assert(payload.type == "patch", "should be a patch payload")
|
||||
assert(payload.data.a.b == nil, "should not send unchanged values")
|
||||
assert(payload.data.a.c == 5, "should send changed values")
|
||||
|
||||
a({ b = 1, d = 1 })
|
||||
server:_sendPatch(target)
|
||||
|
||||
assert(payload.data.a.b == nil)
|
||||
assert(patch.isNone(payload.data.a.c) == true)
|
||||
assert(payload.data.a.d == 1)
|
||||
assert(payload.data.a.b == nil, "should not send unchanged values")
|
||||
assert(patch.isNone(payload.data.a.c) == true, "should send None for removed values")
|
||||
assert(payload.data.a.d == 1, "should send new values")
|
||||
|
||||
disconnect()
|
||||
end)
|
||||
|
|
@ -78,23 +78,23 @@ test("preserves history", function()
|
|||
b(2)
|
||||
server:_sendPatch(target)
|
||||
|
||||
assert(#payloads == 1)
|
||||
assert(payloads[1].type == "patch")
|
||||
assert(payloads[1].data.a == 2)
|
||||
assert(payloads[1].data.b == 2)
|
||||
assert(#payloads == 1, "should only have one payload")
|
||||
assert(payloads[1].type == "patch", "should be a patch payload")
|
||||
assert(payloads[1].data.a == 2, "should send the new value of a")
|
||||
assert(payloads[1].data.b == 2, "should send the new value of b")
|
||||
|
||||
a(3)
|
||||
a(4)
|
||||
b(3)
|
||||
server:_sendPatch(target)
|
||||
|
||||
assert(#payloads == 2)
|
||||
assert(payloads[1].type == "patch")
|
||||
assert(payloads[1].data.a == 3)
|
||||
assert(payloads[1].data.b == nil)
|
||||
assert(payloads[2].type == "patch")
|
||||
assert(payloads[2].data.a == 4)
|
||||
assert(payloads[2].data.b == 3)
|
||||
assert(#payloads == 2, "should have two payloads")
|
||||
assert(payloads[1].type == "patch", "1 should be a patch payload")
|
||||
assert(payloads[1].data.a == 3, "1 should send the new value of a")
|
||||
assert(payloads[1].data.b == nil, "1 should not send unchanged values")
|
||||
assert(payloads[2].type == "patch", "2 should be a patch payload")
|
||||
assert(payloads[2].data.a == 4, "2 should send the new value of a")
|
||||
assert(payloads[2].data.b == 3, "2 should send the new value of b")
|
||||
|
||||
disconnect()
|
||||
end)
|
||||
|
|
|
|||
|
|
@ -15,9 +15,9 @@ test("tracks atom state", function()
|
|||
local hook = renderHook(function()
|
||||
return useAtom(state)
|
||||
end)
|
||||
assert(hook.result == 0)
|
||||
assert(hook.result == 0, "should return 0")
|
||||
state(1)
|
||||
assert(hook.result == 1)
|
||||
assert(hook.result == 1, "should return 1")
|
||||
end)
|
||||
|
||||
test("accepts a selector", function()
|
||||
|
|
@ -27,9 +27,9 @@ test("accepts a selector", function()
|
|||
return 2 * state()
|
||||
end)
|
||||
end)
|
||||
assert(hook.result == 0)
|
||||
assert(hook.result == 0, "should return 0")
|
||||
state(1)
|
||||
assert(hook.result == 2)
|
||||
assert(hook.result == 2, "should return 2")
|
||||
end)
|
||||
|
||||
test("tracks dependencies", function()
|
||||
|
|
@ -37,14 +37,14 @@ test("tracks dependencies", function()
|
|||
local hook = renderHook(function()
|
||||
return useAtom(state, { state })
|
||||
end)
|
||||
assert(hook.result == 0)
|
||||
assert(hook.result == 0, "should return 0")
|
||||
state(1)
|
||||
assert(hook.result == 1)
|
||||
assert(hook.result == 1, "should return 1")
|
||||
state = atom(0)
|
||||
hook.rerender()
|
||||
assert(hook.result == 0)
|
||||
assert(hook.result == 0, "should return 0")
|
||||
state(1)
|
||||
assert(hook.result == 1)
|
||||
assert(hook.result == 1, "should return 1")
|
||||
end)
|
||||
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ test("tracks atom state", function()
|
|||
local cleanup = root(function()
|
||||
source = useAtom(state)
|
||||
end)
|
||||
assert(source() == 0)
|
||||
assert(source() == 0, "should return 0")
|
||||
state(1)
|
||||
assert(source() == 1)
|
||||
assert(source() == 1, "should return 1")
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
|
|
@ -31,9 +31,9 @@ test("accepts a selector", function()
|
|||
return 2 * state()
|
||||
end)
|
||||
end)
|
||||
assert(source() == 0)
|
||||
assert(source() == 0, "should return 0")
|
||||
state(1)
|
||||
assert(source() == 2)
|
||||
assert(source() == 2, "should return 2")
|
||||
cleanup()
|
||||
end)
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,3 @@ globals:
|
|||
require:
|
||||
args:
|
||||
- type: string
|
||||
assert:
|
||||
args:
|
||||
- type: any
|
||||
- type: any
|
||||
required: false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue