~rootmos/lua-hack

4b86e96e11ea235fa1d075086e809cbc449e64d1 — Gustav Behm 1 year, 6 months ago 0349b53 ser
Add tests for deserializing tables
2 files changed, 18 insertions(+), 4 deletions(-)

M ser.lua
M ser.test.lua
M ser.lua => ser.lua +0 -4
@@ 19,10 19,6 @@ local function is_lua_name(s)
        return false
    end

    if s == "" then
        return false
    end

    local b = s:byte()
    if not ((65 <= b and b <= 90) or (97 <= b and b <= 122) or b == 95) then
        return false

M ser.test.lua => ser.test.lua +18 -0
@@ 78,8 78,26 @@ function test_serialize_table()
    lu.assertEquals(S.serialize({x=2,3}), '{[1]=3,x=2}')
end

function test_deserialize_table()
    lu.assertEquals(S.deserialize(S.serialize({})), {})

    lu.assertEquals(S.deserialize(S.serialize({2,3,4})), {2,3,4})
    lu.assertEquals(S.deserialize(S.serialize({2,nil,3})), {2,[3]=3})

    lu.assertEquals(S.deserialize(S.serialize({x=2})), {x=2})
    lu.assertEquals(S.deserialize(S.serialize({["y z"]=3})), {["y z"]=3})
    lu.assertEquals(S.deserialize(S.serialize({["if"]=4})), {["if"]=4})

    lu.assertEquals(S.deserialize(S.serialize({2,x=3})), {2,x=3})
    lu.assertEquals(S.deserialize(S.serialize({x=2,3})), {x=2,3})
end

function test_serialize_nested_tables()
    lu.assertEquals(S.serialize({{}}), "{[1]={}}")
end

function test_deserialize_nested_tables()
    lu.assertEquals(S.deserialize(S.serialize({{}})), {{}})
end

os.exit(lu.LuaUnit.run())