summaryrefslogtreecommitdiff
path: root/tests/modules/string/serialize/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-26 09:23:24 +0800
committerGitHub <[email protected]>2019-07-26 09:23:24 +0800
commita77e8ad6db511c7adbb6b982cd5958febcb989cf (patch)
tree1fccf65b87e1d1ae89390d2292855fdd3c7deb2d /tests/modules/string/serialize/test.lua
parentb03883547d69d2173ffc46f2c8b9e4d579247a60 (diff)
parent7d4023cf698bff82b2c0f2b4aad86286d4890b49 (diff)
Merge pull request #509 from OpportunityLiu/dev
Improve string.serialize; add unit test
Diffstat (limited to 'tests/modules/string/serialize/test.lua')
-rw-r--r--tests/modules/string/serialize/test.lua45
1 files changed, 45 insertions, 0 deletions
diff --git a/tests/modules/string/serialize/test.lua b/tests/modules/string/serialize/test.lua
new file mode 100644
index 000000000..567bee87f
--- /dev/null
+++ b/tests/modules/string/serialize/test.lua
@@ -0,0 +1,45 @@
+
+function roundtrip(round0)
+ local round1 = string.serialize(round0, false):deserialize()
+ local round2 = string.serialize(round1, true):deserialize()
+ local round3 = string.serialize(round2, {binary=true}):deserialize()
+ local round4 = string.serialize(round3, {indent=16}):deserialize()
+ local round5 = string.serialize(round4, {indent=" \r\n\t"}):deserialize()
+ return round5
+end
+
+function test_number(t)
+ t:are_equal(roundtrip(12), 12)
+ t:are_equal(roundtrip(0), 0)
+ t:are_equal(roundtrip(-1), -1)
+ t:are_equal(roundtrip(7.25), 7.25)
+ t:are_equal(roundtrip(math.huge), math.huge)
+ t:are_equal(roundtrip(-math.huge), -math.huge)
+ t:are_equal(roundtrip(math.nan), math.nan)
+end
+
+function test_boolean(t)
+ t:are_equal(roundtrip(true), true)
+ t:are_equal(roundtrip(false), false)
+end
+
+function test_nil(t)
+ t:are_equal(roundtrip(nil), nil)
+end
+
+function test_table(t)
+ t:are_equal(roundtrip({}), {})
+ t:are_equal(roundtrip({1, 2, 3}), {1, 2, 3})
+ t:are_equal(roundtrip({1, "", 3}), {1, "", 3})
+ t:are_equal(roundtrip({{1, 2, 3, nil, 4}}), {{1, 2, 3, nil, 4}})
+ t:are_equal(roundtrip({{1, 2, 3, nil, 4, [100]=5}}), {{1, 2, 3, nil, 4, [100]=5}})
+ t:are_equal(roundtrip({{a=1, b=2, c=3, nil, 4}}), {{a=1, b=2, c=3, nil, 4}})
+end
+
+function test_function(t)
+ t:are_equal(roundtrip(function() return {} end)(), {})
+ t:are_equal(roundtrip(function() return {1, 2, 3} end)(), {1, 2, 3})
+ t:are_equal(roundtrip(function() return {{1, 2, 3, nil, 4}} end)(), {{1, 2, 3, nil, 4}})
+ t:are_equal(roundtrip({function() return {{1, 2, 3, nil, 4}} end})[1](), {{1, 2, 3, nil, 4}})
+ t:are_equal(roundtrip({{function() return {{1, 2, 3, nil, 4}} end}})[1][1](), {{1, 2, 3, nil, 4}})
+end