tests/test_util_json.lua
author Matthew Wild <mwild1@gmail.com>
Wed, 27 Sep 2017 15:49:41 +0100
changeset 8289 39966cbc29f4
parent 7236 71ca252d9f69
permissions -rw-r--r--
CHANGES: Update for release
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7236
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