1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
function roundtrip(round0)
local round1 = string.serialize(round0, false):deserialize()
local round2 = string.serialize(round1, true):deserialize()
local round3 = string.serialize(round2, {binary=true}):deserialize()
local round4 = string.serialize(round3, {indent=16}):deserialize()
local round5 = string.serialize(round4, {indent=" \r\n\t"}):deserialize()
return round5
end
function test_number(t)
t:are_equal(roundtrip(12), 12)
t:are_equal(roundtrip(0), 0)
t:are_equal(roundtrip(-1), -1)
t:are_equal(roundtrip(7.25), 7.25)
t:are_equal(roundtrip(math.huge), math.huge)
t:are_equal(roundtrip(-math.huge), -math.huge)
t:are_equal(roundtrip(math.nan), math.nan)
end
function test_boolean(t)
t:are_equal(roundtrip(true), true)
t:are_equal(roundtrip(false), false)
end
function test_nil(t)
t:are_equal(roundtrip(nil), nil)
end
function test_table(t)
t:are_equal(roundtrip({}), {})
t:are_equal(roundtrip({1, 2, 3}), {1, 2, 3})
t:are_equal(roundtrip({1, "", 3}), {1, "", 3})
t:are_equal(roundtrip({{1, 2, 3, nil, 4}}), {{1, 2, 3, nil, 4}})
t:are_equal(roundtrip({{1, 2, 3, nil, 4, [100]=5}}), {{1, 2, 3, nil, 4, [100]=5}})
t:are_equal(roundtrip({{a=1, b=2, c=3, nil, 4}}), {{a=1, b=2, c=3, nil, 4}})
end
function test_function(t)
t:are_equal(roundtrip(function() return {} end)(), {})
t:are_equal(roundtrip(function() return {1, 2, 3} end)(), {1, 2, 3})
t:are_equal(roundtrip(function() return {{1, 2, 3, nil, 4}} end)(), {{1, 2, 3, nil, 4}})
t:are_equal(roundtrip({function() return {{1, 2, 3, nil, 4}} end})[1](), {{1, 2, 3, nil, 4}})
t:are_equal(roundtrip({{function() return {{1, 2, 3, nil, 4}} end}})[1][1](), {{1, 2, 3, nil, 4}})
t:will_raise(function() string.serialize({io.open}):deserialize()[1]("test.lua") end, "attempt to call global 'setmetatable' (a nil value)")
t:will_raise(function() string.serialize({io.open}):deserialize()[1]("not/a/file") end, "failed to open file: not/a/file")
end
function test_refloop(t)
local l1 = {}
l1.l = l1
local r1 = roundtrip(l1)
t:are_same(r1.l, r1)
local l2 = {{1}, {2}, {3}}
l2[1].l = { root = l2, a = l2[1], b = l2[2], c = l2[3] }
local r2 = roundtrip(l2)
t:are_same(r2[1].l.root, r2)
t:are_same(r2[1].l.a, r2[1])
t:are_same(r2[1].l.b, r2[2])
t:are_same(r2[1].l.c, r2[3])
end
|