aboutsummaryrefslogtreecommitdiff
path: root/tests/agentzh.t
blob: 88a94b82110d0bb50a812506dd825656e5660216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# vim:ft=

use lib '.';
use TestLua;

plan tests => 2 * blocks();

run_tests();

__DATA__

=== TEST 1: empty tables as objects
--- lua
local cjson = require "cjson"
print(cjson.encode({}))
print(cjson.encode({dogs = {}}))
--- out
{}
{"dogs":{}}



=== TEST 2: empty tables as arrays
--- lua
local cjson = require "cjson"
cjson.encode_empty_table_as_object(false)
print(cjson.encode({}))
print(cjson.encode({dogs = {}}))
--- out
[]
{"dogs":[]}



=== TEST 3: empty tables as objects (explicit)
--- lua
local cjson = require "cjson"
cjson.encode_empty_table_as_object(true)
print(cjson.encode({}))
print(cjson.encode({dogs = {}}))
--- out
{}
{"dogs":{}}



=== TEST 4: empty_array userdata
--- lua
local cjson = require "cjson"
print(cjson.encode({arr = cjson.empty_array}))
--- out
{"arr":[]}



=== TEST 5: empty_array_mt
--- lua
local cjson = require "cjson"
local empty_arr = setmetatable({}, cjson.empty_array_mt)
print(cjson.encode({arr = empty_arr}))
--- out
{"arr":[]}



=== TEST 6: empty_array_mt and empty tables as objects (explicit)
--- lua
local cjson = require "cjson"
local sort_json = require "tests.sort_json"
local empty_arr = setmetatable({}, cjson.empty_array_mt)
print(sort_json(cjson.encode({obj = {}, arr = empty_arr})))
--- out
{"arr":[],"obj":{}}



=== TEST 7: empty_array_mt and empty tables as objects (explicit)
--- lua
local cjson = require "cjson"
local sort_json = require "tests.sort_json"
cjson.encode_empty_table_as_object(true)
local empty_arr = setmetatable({}, cjson.empty_array_mt)
local data = {
  arr = empty_arr,
  foo = {
    obj = {},
    foobar = {
      arr = cjson.empty_array,
      obj = {}
    }
  }
}
print(sort_json(cjson.encode(data)))
--- out
{"arr":[],"foo":{"foobar":{"arr":[],"obj":{}},"obj":{}}}



=== TEST 8: empty_array_mt on non-empty tables
--- lua
local cjson = require "cjson"
local sort_json = require "tests.sort_json"
cjson.encode_empty_table_as_object(true)
local array = {"hello", "world", "lua"}
setmetatable(array, cjson.empty_array_mt)
local data = {
  arr = array,
  foo = {
    obj = {},
    foobar = {
      arr = cjson.empty_array,
      obj = {}
    }
  }
}
print(sort_json(cjson.encode(data)))
--- out
{"arr":["hello","world","lua"],"foo":{"foobar":{"arr":[],"obj":{}},"obj":{}}}



=== TEST 9: array_mt on empty tables
--- lua
local cjson = require "cjson"
local data = {}
setmetatable(data, cjson.array_mt)
print(cjson.encode(data))
--- out
[]



=== TEST 10: array_mt on non-empty tables
--- lua
local cjson = require "cjson"
local data = { "foo", "bar" }
setmetatable(data, cjson.array_mt)
print(cjson.encode(data))
--- out
["foo","bar"]



=== TEST 11: array_mt on non-empty tables with holes
--- lua
local cjson = require "cjson"
local data = {}
data[1] = "foo"
data[2] = "bar"
data[4] = "last"
data[9] = "none"
setmetatable(data, cjson.array_mt)
print(cjson.encode(data))
--- out
["foo","bar",null,"last"]



=== TEST 12: decode() by default does not set array_mt on empty arrays
--- lua
local cjson = require "cjson"
local json = [[{"my_array":[]}]]
local t = cjson.decode(json)
local has_metatable = getmetatable(t.my_array) == cjson.array_mt
print("decoded JSON array has metatable: " .. tostring(has_metatable))
print(cjson.encode(t))
--- out
decoded JSON array has metatable: false
{"my_array":{}}



=== TEST 13: decode() sets array_mt on non-empty arrays if enabled
--- lua
local cjson = require "cjson"
cjson.decode_array_with_array_mt(true)
local json = [[{"my_array":["hello","world"]}]]
local t = cjson.decode(json)
t.my_array.hash_value = "adding a hash value"
-- emptying the array part
t.my_array[1] = nil
t.my_array[2] = nil
local has_metatable = getmetatable(t.my_array) == cjson.array_mt
print("decoded JSON array has metatable: " .. tostring(has_metatable))
print(cjson.encode(t))
--- out
decoded JSON array has metatable: true
{"my_array":[]}



=== TEST 14: cfg can enable/disable setting array_mt
--- lua
local cjson = require "cjson"
cjson.decode_array_with_array_mt(true)
cjson.decode_array_with_array_mt(false)
local json = [[{"my_array":[]}]]
local t = cjson.decode(json)
local has_metatable = getmetatable(t.my_array) == cjson.array_mt
print("decoded JSON array has metatable: " .. tostring(has_metatable))
print(cjson.encode(t))
--- out
decoded JSON array has metatable: false
{"my_array":{}}



=== TEST 15: array_mt on tables with hash part
--- lua
local cjson = require "cjson"
local data

if jit and string.find(jit.version, "LuaJIT 2.1.0", nil, true) then
    local new_tab = require "table.new"
    data = new_tab(0, 2) -- allocating hash part only

else
    data = {}
end

data.foo = "bar"
data[1] = "hello"
setmetatable(data, cjson.array_mt)
print(cjson.encode(data))
--- out
["hello"]



=== TEST 16: multiple calls to lua_cjson_new (1/3)
--- lua
local cjson = require "cjson"
package.loaded["cjson"] = nil
require "cjson"
local arr = setmetatable({}, cjson.array_mt)
print(cjson.encode(arr))
--- out
[]



=== TEST 17: multiple calls to lua_cjson_new (2/3)
--- lua
local cjson = require "cjson"
package.loaded["cjson"] = nil
require "cjson"
local arr = setmetatable({}, cjson.empty_array_mt)
print(cjson.encode(arr))
--- out
[]



=== TEST 18: multiple calls to lua_cjson_new (3/3)
--- lua
local cjson = require "cjson.safe"
-- load another cjson instance (not in package.loaded)
require "cjson"
local arr = setmetatable({}, cjson.empty_array_mt)
print(cjson.encode(arr))
--- out
[]



=== TEST 19: & in JSON
--- lua
local cjson = require "cjson"
local a="[\"a=1&b=2\"]"
local b=cjson.decode(a)
print(cjson.encode(b))
--- out
["a=1&b=2"]



=== TEST 20: default and max precision
--- lua
local math = require "math"
local cjson = require "cjson"
local double = math.pow(2, 53)
print(cjson.encode(double))
cjson.encode_number_precision(16)
print(cjson.encode(double))
print(string.format("%16.0f", cjson.decode("9007199254740992")))
--- out
9.007199254741e+15
9007199254740992
9007199254740992



=== TEST 21: / in string
--- lua
local cjson = require "cjson"
local a={test = "http://google.com/google"}
local b=cjson.encode(a)
print(b)
cjson.encode_escape_forward_slash(false)
local b=cjson.encode(a)
print(b)
cjson.encode_escape_forward_slash(true)
local b=cjson.encode(a)
print(b)
--- out
{"test":"http:\/\/google.com\/google"}
{"test":"http://google.com/google"}
{"test":"http:\/\/google.com\/google"}



=== TEST 22: disable error on invalid type
--- lua
local cjson = require "cjson"
local f = function (x) return 2*x end
local res, err = pcall(cjson.encode, f)
print(err)
local t = {f = f, valid = "valid"}
local res, err = pcall(cjson.encode, t)
print(err)
local arr = {"one", "two", f, "three"}
local res, err = pcall(cjson.encode, arr)
print(err)
cjson.encode_skip_unsupported_value_types(true)
print(cjson.encode(f))
print(cjson.encode(t))
print(cjson.encode(arr))
--- out
Cannot serialise function: type not supported
Cannot serialise function: type not supported
Cannot serialise function: type not supported

{"valid":"valid"}
["one","two","three"]



=== TEST 23: array-like proxy object with __len and __index
--- lua
local cjson = require "cjson"
local real_array = {"foo", "bar", "baz"}
local proxy_array = {}
setmetatable(proxy_array, {
  __len = function() return 3 end,
  __index = function(t, k)
    return real_array[k]
  end,
})

print(cjson.encode(proxy_array))
--- out
["foo","bar","baz"]



=== TEST 24: check that integers are handled correctly on Lua 5.3+
--- lua
local lv = tonumber((_VERSION):match("Lua 5%.([0-9]+)"))

if lv >= 3 then
  local cjson = require "cjson"
  local array = cjson.decode [[ [10, 10.0, 3.5] ]]
  for i = 1, 4 do
    print(tostring(i) .. ": " .. tostring(math.type(array[i])))
  end
else
  print("1: integer")
  print("2: float")
  print("3: float")
  print("4: nil")
end
--- out
1: integer
2: float
3: float
4: nil