Annotate

tests/test_util_json.lua @ 8220:a1b0fa38fca7

util.pubsub: Don't record publisher when superuser privileges are used (eg by modules)
author Kim Alvefur <zash@zash.se>
date Sat, 29 Jul 2017 13:09:57 +0200
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