Add tests for util.json
authorMatthew Wild <mwild1@gmail.com>
Fri, 4 Mar 2016 22:17:16 +0000 (22:17 +0000)
committerMatthew Wild <mwild1@gmail.com>
Fri, 4 Mar 2016 22:17:16 +0000 (22:17 +0000)
tests/test.lua
tests/test_util_json.lua [new file with mode: 0644]

index 0fcc49073983eb96c96411ccee835ee9d4128d68..4172b36346b18809ff0bd974216393e2948bbbe3 100644 (file)
@@ -18,6 +18,7 @@ function run_all_tests()
        dotest "core.s2smanager"
        dotest "core.configmanager"
        dotest "util.ip"
+       dotest "util.json"
        dotest "util.stanza"
        dotest "util.sasl.scram"
        dotest "util.cache"
diff --git a/tests/test_util_json.lua b/tests/test_util_json.lua
new file mode 100644 (file)
index 0000000..2c1a9ce
--- /dev/null
@@ -0,0 +1,21 @@
+
+function encode(encode, json)
+       local function test(f, j, e)
+               if e then
+                       assert_equal(f(j), e);
+               end
+               assert_equal(f(j), f(json.decode(f(j))));
+       end
+       test(encode, json.null, "null")
+       test(encode, {}, "{}")
+       test(encode, {a=1});
+       test(encode, {a={1,2,3}});
+       test(encode, {1}, "[1]");
+end
+
+function decode(decode)
+       local empty_array = decode("[]");
+       assert_equal(type(empty_array), "table");
+       assert_equal(#empty_array, 0);
+       assert_equal(next(empty_array), nil);
+end