Add unit test to validate to_json()

This commit is contained in:
Glen Fraser 2018-01-11 19:43:32 +01:00
parent 783b8b7361
commit 3d97c93e49

18
unittests/json_15.chai Normal file
View File

@ -0,0 +1,18 @@
// Various to_json() tests
assert_equal(to_json(-13570), "-13570")
assert_equal(to_json(0.654321), "0.654321")
assert_equal(to_json("ChaiScript"), "\"ChaiScript\"")
assert_equal(to_json(true), "true")
assert_equal(to_json([1, 2, 3]), "[1, 2, 3]")
assert_equal(to_json(Vector()), "[]") // empty vector
assert_equal(to_json([]), "[]") // empty vector
assert_equal(to_json(Map()), "{\n\n}") // empty map
assert_equal(to_json(Dynamic_Object()), "{\n\n}") // empty object
// Round-trip JSON tests
assert_equal(from_json(to_json([])), [])
assert_equal(from_json(to_json(Map())), Map())
assert_equal(to_json(from_json("null")), "null")
assert_equal(from_json(to_json(["a": 5, "b": "stuff"])), ["a": 5, "b": "stuff"])
auto x = [3.5, true, false, "test", [], Vector(), Map()]
assert_equal(from_json(to_json(x)), x)