Advertisement
wokste

Unit Tests Serialize

Apr 8th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. function unitTest(name, success)
  2.     if (success) then
  3.         print (name .. ': successful')
  4.     else
  5.         print (name .. ': failed')
  6.     end
  7. end
  8.  
  9. -- ========================
  10. -- = TEST 1
  11. -- = Serialise and deserialise table and they should be similar.
  12. -- ========================
  13. unittest_input = {cat={sound="nyan", speed=400}, dog={sound="woof"}}
  14. unittest_output = deserialize(serialize(unittest_input))
  15.  
  16. print(serialize(unittest_input))
  17. print(serialize(unittest_output))
  18. unitTest("test 1a", unittest_input.cat.sound == unittest_output.cat.sound)
  19. unitTest("test 1b", unittest_input.cat.speed == unittest_output.cat.speed)
  20. unitTest("test 1c", unittest_input.dog.sound == unittest_output.dog.sound)
  21.  
  22. -- ========================
  23. -- = TEST 2
  24. -- = Integers booleans edc.
  25. -- ========================
  26. unittest_input = {"Nyancat"}
  27. unittest_output = deserialize(serialize(unittest_input))
  28.  
  29. print(serialize(unittest_input))
  30. unitTest("test 2a", unittest_input[1] == unittest_output[1])
  31. --unitTest("test 2b", unittest_input[true] == unittest_output[true])
  32. --unitTest("test 2c", unittest_input[nil] == unittest_output[nil])
  33. --unitTest("test 2d", unittest_output.is4(2+2))
  34. --unitTest("test 2e", not unittest_output.is4(2+3))
  35. -- ========================
  36. -- = TEST 3
  37. -- = Difficult Escape Characters
  38. -- ========================
  39. unittest_input = {escapechars="\n\r\t\v\\\"\'\[\]", noneuropean="θשׁ٩∂"}
  40. unittest_output = deserialize(serialize(unittest_input))
  41. unitTest("test 3a", unittest_input.escapechars == unittest_output.escapechars)
  42. unitTest("test 3b", unittest_input.noneuropean == unittest_output.noneuropean)
  43.  
  44.  
  45. -- ========================
  46. -- = TEST 4
  47. -- = Executing unwanted functions
  48. -- ========================
  49. --t = deserialize("function foo() print(\"hacked\") end")
  50. --t.foo()
  51.  
  52. --t = deserialize("function foo() print(\"hacked\") end; mt = {__index = foo}; t = {}; setmetatable(t, mt)")
  53. t = deserialize("function foo() print(\"hacked\") end")
  54. --print(t)
  55. --code = deserialize("print('test 4a failed')")
  56. --val = deserialize(serialize({code=function() print('test 4b failed') end }))
  57. --val.code();
  58.  
  59. --unitTest("test 4a", unittest_input.escapechars == unittest_output.escapechars)
  60. --[[
  61. minetest_copy.register_node("unittests:testblock", {
  62.     description = "Testblock",
  63.     tile_images = {"default_cobble.png"},
  64.     is_ground_content = true,
  65.     groups = {cracky=3},
  66. })
  67.  
  68. minetest_copy.register_craft({
  69.     output = 'unittests:testblock',
  70.     recipe = {
  71.         {'default:chest'},
  72.     }
  73. })
  74. ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement