Annotate

tests/test_util_json.lua @ 8256:cdffe33efae4

mod_limits: Import from prosody-modules 2c59f2f0c37d (fixes #129)
author Matthew Wild <mwild1@gmail.com>
date Sat, 23 Sep 2017 13:29:54 +0100
parent 7233:71ca252d9f69
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7233
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 function encode(encode, json)
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local function test(f, j, e)
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 if e then
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 assert_equal(f(j), e);
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 end
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 assert_equal(f(j), f(json.decode(f(j))));
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 end
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 test(encode, json.null, "null")
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 test(encode, {}, "{}")
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 test(encode, {a=1});
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 test(encode, {a={1,2,3}});
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 test(encode, {1}, "[1]");
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 end
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 function decode(decode)
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 local empty_array = decode("[]");
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 assert_equal(type(empty_array), "table");
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 assert_equal(#empty_array, 0);
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 assert_equal(next(empty_array), nil);
71ca252d9f69 Add tests for util.json
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 end