aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThibault Charbonnier <thibaultcha@me.com>2016-02-28 20:03:12 -0800
committerThibault Charbonnier <thibaultcha@me.com>2016-03-03 10:54:53 -0800
commit7a9c25ee69f38974e99322971eace37ba1753074 (patch)
tree8aded977eea3eca5f7c6ff2934615a602dacbc8f /tests
parent77b8669c95500629dff6e34b4b6b0df5d0041ddf (diff)
downloadlua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.tar.gz
lua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.tar.bz2
lua-cjson-7a9c25ee69f38974e99322971eace37ba1753074.zip
feat: cjson.as_array metamethod to enforce empty array encoding
A proposed improved patch of openresty/lua-cjson#1 (a patch commonly proposed to lua-cjson and its forks), taking into considerations comments from the original PR. - use a lightuserdata key to store the metatable in the Lua Registry (more efficient and avoiding conflicts) - provide a lightuserdata resulting in empty arrays as well - tests cases moved to t/agentzh.t, where cases for 'encode_empty_table_as_object' are already written. It seems like a better place for tests specific to the OpenResty fork's additions. - a more complex test case
Diffstat (limited to 'tests')
-rw-r--r--tests/agentzh.t76
1 files changed, 74 insertions, 2 deletions
diff --git a/tests/agentzh.t b/tests/agentzh.t
index 0b546ff..e76f910 100644
--- a/tests/agentzh.t
+++ b/tests/agentzh.t
@@ -41,7 +41,79 @@ print(cjson.encode({dogs = {}}))
41 41
42 42
43 43
44=== TEST 4: & in JSON 44=== TEST 4: empty_array userdata
45--- lua
46local cjson = require "cjson"
47print(cjson.encode({arr = cjson.empty_array}))
48--- out
49{"arr":[]}
50
51
52
53=== TEST 5: empty_array_mt
54--- lua
55local cjson = require "cjson"
56local empty_arr = setmetatable({}, cjson.empty_array_mt)
57print(cjson.encode({arr = empty_arr}))
58--- out
59{"arr":[]}
60
61
62
63=== TEST 6: empty_array_mt and empty tables as objects (explicit)
64--- lua
65local cjson = require "cjson"
66local empty_arr = setmetatable({}, cjson.empty_array_mt)
67print(cjson.encode({obj = {}, arr = empty_arr}))
68--- out
69{"arr":[],"obj":{}}
70
71
72
73=== TEST 7: empty_array_mt and empty tables as objects (explicit)
74--- lua
75local cjson = require "cjson"
76cjson.encode_empty_table_as_object(true)
77local empty_arr = setmetatable({}, cjson.empty_array_mt)
78local data = {
79 arr = empty_arr,
80 foo = {
81 obj = {},
82 foobar = {
83 arr = cjson.empty_array,
84 obj = {}
85 }
86 }
87}
88print(cjson.encode(data))
89--- out
90{"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":[]}
91
92
93
94=== TEST 8: empty_array_mt on non-empty tables
95--- lua
96local cjson = require "cjson"
97cjson.encode_empty_table_as_object(true)
98local array = {"hello", "world", "lua"}
99setmetatable(array, cjson.empty_array_mt)
100local data = {
101 arr = array,
102 foo = {
103 obj = {},
104 foobar = {
105 arr = cjson.empty_array,
106 obj = {}
107 }
108 }
109}
110print(cjson.encode(data))
111--- out
112{"foo":{"foobar":{"obj":{},"arr":[]},"obj":{}},"arr":["hello","world","lua"]}
113
114
115
116=== TEST 9: & in JSON
45--- lua 117--- lua
46local cjson = require "cjson" 118local cjson = require "cjson"
47local a="[\"a=1&b=2\"]" 119local a="[\"a=1&b=2\"]"
@@ -52,7 +124,7 @@ print(cjson.encode(b))
52 124
53 125
54 126
55=== TEST 5: default and max precision 127=== TEST 10: default and max precision
56--- lua 128--- lua
57local math = require "math" 129local math = require "math"
58local cjson = require "cjson" 130local cjson = require "cjson"