summaryrefslogtreecommitdiff
path: root/tests/modules/string/serialize/test.lua
blob: 567bee87f97713e9f97202428367c717e44c640a (plain)
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

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}})
end