aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThibault Charbonnier <thibaultcha@me.com>2017-07-08 21:54:18 -0700
committerYichun Zhang (agentzh) <agentzh@gmail.com>2017-11-15 20:41:57 -0800
commit5f9efa4829a72935ddcd40c7da6b1a9e10939b65 (patch)
tree319cdf13f99c753f85820c3fabb89bfa50496dae /tests
parenta9af7965219710ac1ec1e2bbf1a63eb77be622b7 (diff)
downloadlua-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.t79
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
121local cjson = require "cjson"
122local data = {}
123setmetatable(data, cjson.array_mt)
124print(cjson.encode(data))
125--- out
126[]
127
128
129
130=== TEST 10: array_mt on non-empty tables
131--- lua
132local cjson = require "cjson"
133local data = { "foo", "bar" }
134setmetatable(data, cjson.array_mt)
135print(cjson.encode(data))
136--- out
137["foo","bar"]
138
139
140
141=== TEST 11: array_mt on non-empty tables with holes
142--- lua
143local cjson = require "cjson"
144local data = {}
145data[1] = "foo"
146data[2] = "bar"
147data[4] = "last"
148data[9] = "none"
149setmetatable(data, cjson.array_mt)
150print(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
158local cjson = require "cjson"
159local data
160
161if 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
165else
166 data = {}
167end
168
169data.foo = "bar"
170data[1] = "hello"
171setmetatable(data, cjson.array_mt)
172print(cjson.encode(data))
173--- out
174["hello"]
175
176
177
178=== TEST 13: multiple calls to lua_cjson_new (1/3)
179--- lua
180local cjson = require "cjson"
181package.loaded["cjson"] = nil
182require "cjson"
183local arr = setmetatable({}, cjson.array_mt)
184print(cjson.encode(arr))
185--- out
186[]
187
188
189
190=== TEST 14: multiple calls to lua_cjson_new (2/3)
120--- lua 191--- lua
121local cjson = require "cjson" 192local cjson = require "cjson"
122package.loaded["cjson"] = nil 193package.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
133local cjson = require "cjson.safe" 204local 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
145local cjson = require "cjson" 216local cjson = require "cjson"
146local a="[\"a=1&b=2\"]" 217local 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
156local math = require "math" 227local math = require "math"
157local cjson = require "cjson" 228local cjson = require "cjson"