serialize_spec.lua 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. _G.core = {}
  2. _G.setfenv = require 'busted.compatibility'.setfenv
  3. dofile("builtin/common/serialize.lua")
  4. describe("serialize", function()
  5. it("works", function()
  6. local test_in = {cat={sound="nyan", speed=400}, dog={sound="woof"}}
  7. local test_out = core.deserialize(core.serialize(test_in))
  8. assert.same(test_in, test_out)
  9. end)
  10. it("handles characters", function()
  11. local test_in = {escape_chars="\n\r\t\v\\\"\'", non_european="θשׁ٩∂"}
  12. local test_out = core.deserialize(core.serialize(test_in))
  13. assert.same(test_in, test_out)
  14. end)
  15. it("handles precise numbers", function()
  16. local test_in = 0.2695949158945771
  17. local test_out = core.deserialize(core.serialize(test_in))
  18. assert.same(test_in, test_out)
  19. end)
  20. it("handles big integers", function()
  21. local test_in = 269594915894577
  22. local test_out = core.deserialize(core.serialize(test_in))
  23. assert.same(test_in, test_out)
  24. end)
  25. it("handles recursive structures", function()
  26. local test_in = { hello = "world" }
  27. test_in.foo = test_in
  28. local test_out = core.deserialize(core.serialize(test_in))
  29. assert.same(test_in, test_out)
  30. end)
  31. it("strips functions in safe mode", function()
  32. local test_in = {
  33. func = function(a, b)
  34. error("test")
  35. end,
  36. foo = "bar"
  37. }
  38. local str = core.serialize(test_in)
  39. assert.not_nil(str:find("loadstring"))
  40. local test_out = core.deserialize(str, true)
  41. assert.is_nil(test_out.func)
  42. assert.equals(test_out.foo, "bar")
  43. end)
  44. end)