serialize_spec.lua 1.8 KB

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