Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function unitTest(name, success)
- if (success) then
- print (name .. ': successful')
- else
- print (name .. ': failed')
- end
- end
- -- ========================
- -- = TEST 1
- -- = Serialise and deserialise table and they should be similar.
- -- ========================
- unittest_input = {cat={sound="nyan", speed=400}, dog={sound="woof"}}
- unittest_output = deserialize(serialize(unittest_input))
- print(serialize(unittest_input))
- print(serialize(unittest_output))
- unitTest("test 1a", unittest_input.cat.sound == unittest_output.cat.sound)
- unitTest("test 1b", unittest_input.cat.speed == unittest_output.cat.speed)
- unitTest("test 1c", unittest_input.dog.sound == unittest_output.dog.sound)
- -- ========================
- -- = TEST 2
- -- = Integers booleans edc.
- -- ========================
- unittest_input = {"Nyancat"}
- unittest_output = deserialize(serialize(unittest_input))
- print(serialize(unittest_input))
- unitTest("test 2a", unittest_input[1] == unittest_output[1])
- --unitTest("test 2b", unittest_input[true] == unittest_output[true])
- --unitTest("test 2c", unittest_input[nil] == unittest_output[nil])
- --unitTest("test 2d", unittest_output.is4(2+2))
- --unitTest("test 2e", not unittest_output.is4(2+3))
- -- ========================
- -- = TEST 3
- -- = Difficult Escape Characters
- -- ========================
- unittest_input = {escapechars="\n\r\t\v\\\"\'\[\]", noneuropean="θשׁ٩∂"}
- unittest_output = deserialize(serialize(unittest_input))
- unitTest("test 3a", unittest_input.escapechars == unittest_output.escapechars)
- unitTest("test 3b", unittest_input.noneuropean == unittest_output.noneuropean)
- -- ========================
- -- = TEST 4
- -- = Executing unwanted functions
- -- ========================
- --t = deserialize("function foo() print(\"hacked\") end")
- --t.foo()
- --t = deserialize("function foo() print(\"hacked\") end; mt = {__index = foo}; t = {}; setmetatable(t, mt)")
- t = deserialize("function foo() print(\"hacked\") end")
- --print(t)
- --code = deserialize("print('test 4a failed')")
- --val = deserialize(serialize({code=function() print('test 4b failed') end }))
- --val.code();
- --unitTest("test 4a", unittest_input.escapechars == unittest_output.escapechars)
- --[[
- minetest_copy.register_node("unittests:testblock", {
- description = "Testblock",
- tile_images = {"default_cobble.png"},
- is_ground_content = true,
- groups = {cracky=3},
- })
- minetest_copy.register_craft({
- output = 'unittests:testblock',
- recipe = {
- {'default:chest'},
- }
- })
- ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement