diff options
author | Thibault Charbonnier <thibaultcha@me.com> | 2017-07-08 21:54:18 -0700 |
---|---|---|
committer | Yichun Zhang (agentzh) <agentzh@gmail.com> | 2017-11-15 20:41:57 -0800 |
commit | 5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (patch) | |
tree | 319cdf13f99c753f85820c3fabb89bfa50496dae /tests | |
parent | a9af7965219710ac1ec1e2bbf1a63eb77be622b7 (diff) | |
download | lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.tar.gz lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.tar.bz2 lua-cjson-5f9efa4829a72935ddcd40c7da6b1a9e10939b65.zip |
feature: added new cjson.array_mt metatable to allow enforcing JSON array encoding.
Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/agentzh.t | 79 |
1 files changed, 75 insertions, 4 deletions
diff --git a/tests/agentzh.t b/tests/agentzh.t index 0b281a1..dd70fb8 100644 --- a/tests/agentzh.t +++ b/tests/agentzh.t | |||
@@ -116,7 +116,78 @@ print(cjson.encode(data)) | |||
116 | 116 | ||
117 | 117 | ||
118 | 118 | ||
119 | === TEST 9: multiple calls to lua_cjson_new (1/2) | 119 | === TEST 9: array_mt on empty tables |
120 | --- lua | ||
121 | local cjson = require "cjson" | ||
122 | local data = {} | ||
123 | setmetatable(data, cjson.array_mt) | ||
124 | print(cjson.encode(data)) | ||
125 | --- out | ||
126 | [] | ||
127 | |||
128 | |||
129 | |||
130 | === TEST 10: array_mt on non-empty tables | ||
131 | --- lua | ||
132 | local cjson = require "cjson" | ||
133 | local data = { "foo", "bar" } | ||
134 | setmetatable(data, cjson.array_mt) | ||
135 | print(cjson.encode(data)) | ||
136 | --- out | ||
137 | ["foo","bar"] | ||
138 | |||
139 | |||
140 | |||
141 | === TEST 11: array_mt on non-empty tables with holes | ||
142 | --- lua | ||
143 | local cjson = require "cjson" | ||
144 | local data = {} | ||
145 | data[1] = "foo" | ||
146 | data[2] = "bar" | ||
147 | data[4] = "last" | ||
148 | data[9] = "none" | ||
149 | setmetatable(data, cjson.array_mt) | ||
150 | print(cjson.encode(data)) | ||
151 | --- out | ||
152 | ["foo","bar",null,"last"] | ||
153 | |||
154 | |||
155 | |||
156 | === TEST 12: array_mt on tables with hash part | ||
157 | --- lua | ||
158 | local cjson = require "cjson" | ||
159 | local data | ||
160 | |||
161 | if jit and string.find(jit.version, "LuaJIT 2.1.0", nil, true) then | ||
162 | local new_tab = require "table.new" | ||
163 | data = new_tab(0, 2) -- allocating hash part only | ||
164 | |||
165 | else | ||
166 | data = {} | ||
167 | end | ||
168 | |||
169 | data.foo = "bar" | ||
170 | data[1] = "hello" | ||
171 | setmetatable(data, cjson.array_mt) | ||
172 | print(cjson.encode(data)) | ||
173 | --- out | ||
174 | ["hello"] | ||
175 | |||
176 | |||
177 | |||
178 | === TEST 13: multiple calls to lua_cjson_new (1/3) | ||
179 | --- lua | ||
180 | local cjson = require "cjson" | ||
181 | package.loaded["cjson"] = nil | ||
182 | require "cjson" | ||
183 | local arr = setmetatable({}, cjson.array_mt) | ||
184 | print(cjson.encode(arr)) | ||
185 | --- out | ||
186 | [] | ||
187 | |||
188 | |||
189 | |||
190 | === TEST 14: multiple calls to lua_cjson_new (2/3) | ||
120 | --- lua | 191 | --- lua |
121 | local cjson = require "cjson" | 192 | local cjson = require "cjson" |
122 | package.loaded["cjson"] = nil | 193 | package.loaded["cjson"] = nil |
@@ -128,7 +199,7 @@ print(cjson.encode(arr)) | |||
128 | 199 | ||
129 | 200 | ||
130 | 201 | ||
131 | === TEST 10: multiple calls to lua_cjson_new (2/2) | 202 | === TEST 15: multiple calls to lua_cjson_new (3/3) |
132 | --- lua | 203 | --- lua |
133 | local cjson = require "cjson.safe" | 204 | local cjson = require "cjson.safe" |
134 | -- load another cjson instance (not in package.loaded) | 205 | -- load another cjson instance (not in package.loaded) |
@@ -140,7 +211,7 @@ print(cjson.encode(arr)) | |||
140 | 211 | ||
141 | 212 | ||
142 | 213 | ||
143 | === TEST 11: & in JSON | 214 | === TEST 16: & in JSON |
144 | --- lua | 215 | --- lua |
145 | local cjson = require "cjson" | 216 | local cjson = require "cjson" |
146 | local a="[\"a=1&b=2\"]" | 217 | local a="[\"a=1&b=2\"]" |
@@ -151,7 +222,7 @@ print(cjson.encode(b)) | |||
151 | 222 | ||
152 | 223 | ||
153 | 224 | ||
154 | === TEST 12: default and max precision | 225 | === TEST 17: default and max precision |
155 | --- lua | 226 | --- lua |
156 | local math = require "math" | 227 | local math = require "math" |
157 | local cjson = require "cjson" | 228 | local cjson = require "cjson" |