aboutsummaryrefslogtreecommitdiff
path: root/test/functional/eval/json_functions_spec.lua
blob: 9167cb2fef407e1b3c4a0d0aec560fac54e12fab (plain) (blame)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
local helpers = require('test.functional.helpers')
local clear = helpers.clear
local funcs = helpers.funcs
local eq = helpers.eq
local eval = helpers.eval
local execute = helpers.execute
local exc_exec = helpers.exc_exec

describe('jsondecode() function', function()
  before_each(clear)

  it('accepts readfile()-style list', function()
    eq({Test=1}, funcs.jsondecode({
      '{',
      '\t"Test": 1',
      '}',
    }))
  end)

  it('accepts strings with newlines', function()
    eq({Test=1}, funcs.jsondecode([[
      {
        "Test": 1
      }
    ]]))
  end)

  it('parses null, true, false', function()
    eq(nil, funcs.jsondecode('null'))
    eq(true, funcs.jsondecode('true'))
    eq(false, funcs.jsondecode('false'))
  end)

  it('fails to parse incomplete null, true, false', function()
    eq('Vim(call):E474: Expected null: n',
       exc_exec('call jsondecode("n")'))
    eq('Vim(call):E474: Expected null: nu',
       exc_exec('call jsondecode("nu")'))
    eq('Vim(call):E474: Expected null: nul',
       exc_exec('call jsondecode("nul")'))
    eq('Vim(call):E474: Expected null: nul\n\t',
       exc_exec('call jsondecode("nul\\n\\t")'))

    eq('Vim(call):E474: Expected true: t',
       exc_exec('call jsondecode("t")'))
    eq('Vim(call):E474: Expected true: tr',
       exc_exec('call jsondecode("tr")'))
    eq('Vim(call):E474: Expected true: tru',
       exc_exec('call jsondecode("tru")'))
    eq('Vim(call):E474: Expected true: tru\t\n',
       exc_exec('call jsondecode("tru\\t\\n")'))

    eq('Vim(call):E474: Expected false: f',
       exc_exec('call jsondecode("f")'))
    eq('Vim(call):E474: Expected false: fa',
       exc_exec('call jsondecode("fa")'))
    eq('Vim(call):E474: Expected false: fal',
       exc_exec('call jsondecode("fal")'))
    eq('Vim(call):E474: Expected false: fal   <',
       exc_exec('call jsondecode("   fal   <")'))
    eq('Vim(call):E474: Expected false: fals',
       exc_exec('call jsondecode("fals")'))
  end)

  it('parses integer numbers', function()
    eq(100000, funcs.jsondecode('100000'))
    eq(-100000, funcs.jsondecode('-100000'))
    eq(100000, funcs.jsondecode('  100000  '))
    eq(-100000, funcs.jsondecode('  -100000  '))
  end)

  it('fails to parse +numbers', function()
    eq('Vim(call):E474: Unidentified byte: +1000',
       exc_exec('call jsondecode("+1000")'))
  end)

  it('fails to parse negative numbers with space after -', function()
    eq('Vim(call):E474: Missing number after minus sign: - 1000',
       exc_exec('call jsondecode("- 1000")'))
  end)

  it('fails to parse -', function()
    eq('Vim(call):E474: Missing number after minus sign: -',
       exc_exec('call jsondecode("-")'))
  end)

  it('parses floating-point numbers', function()
    eq('100000.0', eval('string(jsondecode("100000.0"))'))
    eq(100000.5, funcs.jsondecode('100000.5'))
    eq(-100000.5, funcs.jsondecode('-100000.5'))
    eq(-100000.5e50, funcs.jsondecode('-100000.5e50'))
    eq(100000.5e50, funcs.jsondecode('100000.5e50'))
    eq(100000.5e50, funcs.jsondecode('100000.5e+50'))
    eq(-100000.5e-50, funcs.jsondecode('-100000.5e-50'))
    eq(100000.5e-50, funcs.jsondecode('100000.5e-50'))
  end)

  it('fails to parse incomplete floating-point numbers', function()
    eq('Vim(call):E474: Missing number after decimal dot: 0.',
       exc_exec('call jsondecode("0.")'))
    eq('Vim(call):E474: Missing exponent: 0.0e',
       exc_exec('call jsondecode("0.0e")'))
    eq('Vim(call):E474: Missing exponent: 0.0e+',
       exc_exec('call jsondecode("0.0e+")'))
    eq('Vim(call):E474: Missing exponent: 0.0e-',
       exc_exec('call jsondecode("0.0e-")'))
  end)

  it('fails to parse floating-point numbers with spaces inside', function()
    eq('Vim(call):E474: Missing number after decimal dot: 0. ',
       exc_exec('call jsondecode("0. ")'))
    eq('Vim(call):E474: Missing number after decimal dot: 0. 0',
       exc_exec('call jsondecode("0. 0")'))
    eq('Vim(call):E474: Missing exponent: 0.0e 1',
       exc_exec('call jsondecode("0.0e 1")'))
    eq('Vim(call):E474: Missing exponent: 0.0e+ 1',
       exc_exec('call jsondecode("0.0e+ 1")'))
    eq('Vim(call):E474: Missing exponent: 0.0e- 1',
       exc_exec('call jsondecode("0.0e- 1")'))
  end)

  it('fails to parse "," and ":"', function()
    eq('Vim(call):E474: Comma not inside container: ,  ',
       exc_exec('call jsondecode("  ,  ")'))
    eq('Vim(call):E474: Colon not inside container: :  ',
       exc_exec('call jsondecode("  :  ")'))
  end)

  it('parses empty containers', function()
    eq({}, funcs.jsondecode('[]'))
    eq('[]', eval('string(jsondecode("[]"))'))
  end)

  it('fails to parse "[" and "{"', function()
    eq('Vim(call):E474: Unexpected end of input: {',
       exc_exec('call jsondecode("{")'))
    eq('Vim(call):E474: Unexpected end of input: [',
       exc_exec('call jsondecode("[")'))
  end)

  it('fails to parse "}" and "]"', function()
    eq('Vim(call):E474: No container to close: ]',
       exc_exec('call jsondecode("]")'))
    eq('Vim(call):E474: No container to close: }',
       exc_exec('call jsondecode("}")'))
  end)

  it('fails to parse containers which are closed by different brackets',
  function()
    eq('Vim(call):E474: Closing dictionary with bracket: ]',
       exc_exec('call jsondecode("{]")'))
    eq('Vim(call):E474: Closing list with figure brace: }',
       exc_exec('call jsondecode("[}")'))
  end)

  it('fails to parse containers with leading comma or colon', function()
    eq('Vim(call):E474: Leading comma: ,}',
       exc_exec('call jsondecode("{,}")'))
    eq('Vim(call):E474: Leading comma: ,]',
       exc_exec('call jsondecode("[,]")'))
    eq('Vim(call):E474: Using colon not in dictionary: :]',
       exc_exec('call jsondecode("[:]")'))
    eq('Vim(call):E474: Unexpected colon: :}',
       exc_exec('call jsondecode("{:}")'))
  end)

  it('fails to parse containers with trailing comma', function()
    eq('Vim(call):E474: Trailing comma: ]',
       exc_exec('call jsondecode("[1,]")'))
    eq('Vim(call):E474: Trailing comma: }',
       exc_exec('call jsondecode("{\\"1\\": 2,}")'))
  end)

  it('fails to parse dictionaries with missing value', function()
    eq('Vim(call):E474: Expected value after colon: }',
       exc_exec('call jsondecode("{\\"1\\":}")'))
    eq('Vim(call):E474: Expected value: }',
       exc_exec('call jsondecode("{\\"1\\"}")'))
  end)

  it('fails to parse containers with two commas or colons', function()
    eq('Vim(call):E474: Duplicate comma: , "2": 2}',
       exc_exec('call jsondecode("{\\"1\\": 1,, \\"2\\": 2}")'))
    eq('Vim(call):E474: Duplicate comma: , "2", 2]',
       exc_exec('call jsondecode("[\\"1\\", 1,, \\"2\\", 2]")'))
    eq('Vim(call):E474: Duplicate colon: : 2}',
       exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":: 2}")'))
    eq('Vim(call):E474: Comma after colon: , 2}',
       exc_exec('call jsondecode("{\\"1\\": 1, \\"2\\":, 2}")'))
    eq('Vim(call):E474: Unexpected colon: : "2": 2}',
       exc_exec('call jsondecode("{\\"1\\": 1,: \\"2\\": 2}")'))
    eq('Vim(call):E474: Unexpected colon: :, "2": 2}',
       exc_exec('call jsondecode("{\\"1\\": 1:, \\"2\\": 2}")'))
  end)

  it('fails to parse concat of two values', function()
    eq('Vim(call):E474: Trailing characters: []',
       exc_exec('call jsondecode("{}[]")'))
  end)

  it('parses containers', function()
    eq({1}, funcs.jsondecode('[1]'))
    eq({nil, 1}, funcs.jsondecode('[null, 1]'))
    eq({['1']=2}, funcs.jsondecode('{"1": 2}'))
    eq({['1']=2, ['3']={{['4']={['5']={{}, 1}}}}},
       funcs.jsondecode('{"1": 2, "3": [{"4": {"5": [[], 1]}}]}'))
  end)

  it('fails to parse incomplete strings', function()
    eq('Vim(call):E474: Expected string end: \t"',
       exc_exec('call jsondecode("\\t\\"")'))
    eq('Vim(call):E474: Expected string end: \t"abc',
       exc_exec('call jsondecode("\\t\\"abc")'))
    eq('Vim(call):E474: Unfinished escape sequence: \t"abc\\',
       exc_exec('call jsondecode("\\t\\"abc\\\\")'))
    eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u',
       exc_exec('call jsondecode("\\t\\"abc\\\\u")'))
    eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u0',
       exc_exec('call jsondecode("\\t\\"abc\\\\u0")'))
    eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u00',
       exc_exec('call jsondecode("\\t\\"abc\\\\u00")'))
    eq('Vim(call):E474: Unfinished unicode escape sequence: \t"abc\\u000',
       exc_exec('call jsondecode("\\t\\"abc\\\\u000")'))
    eq('Vim(call):E474: Expected string end: \t"abc\\u0000',
       exc_exec('call jsondecode("\\t\\"abc\\\\u0000")'))
  end)

  it('fails to parse unknown escape sequnces', function()
    eq('Vim(call):E474: Unknown escape sequence: \\a"',
       exc_exec('call jsondecode("\\t\\"\\\\a\\"")'))
  end)

  it('parses strings properly', function()
    eq('\n', funcs.jsondecode('"\\n"'))
    eq('', funcs.jsondecode('""'))
    eq('\\/"\t\b\n\r\f', funcs.jsondecode([["\\\/\"\t\b\n\r\f"]]))
    eq('/a', funcs.jsondecode([["\/a"]]))
    -- Unicode characters: 2-byte, 3-byte, 4-byte
    eq({
      '«',
      'ફ',
      '\xF0\x90\x80\x80',
    }, funcs.jsondecode({
      '[',
      '"«",',
      '"ફ",',
      '"\xF0\x90\x80\x80"',
      ']',
    }))
  end)

  it('fails on strings with invalid bytes', function()
    eq('Vim(call):E474: Only UTF-8 strings allowed: \255"',
       exc_exec('call jsondecode("\\t\\"\\xFF\\"")'))
    eq('Vim(call):E474: ASCII control characters cannot be present inside string: ',
       exc_exec('call jsondecode(["\\"\\n\\""])'))
    -- 0xC2 starts 2-byte unicode character
    eq('Vim(call):E474: Only UTF-8 strings allowed: \194"',
       exc_exec('call jsondecode("\\t\\"\\xC2\\"")'))
    -- 0xE0 0xAA starts 3-byte unicode character
    eq('Vim(call):E474: Only UTF-8 strings allowed: \224"',
       exc_exec('call jsondecode("\\t\\"\\xE0\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \224\170"',
       exc_exec('call jsondecode("\\t\\"\\xE0\\xAA\\"")'))
    -- 0xF0 0x90 0x80 starts 4-byte unicode character
    eq('Vim(call):E474: Only UTF-8 strings allowed: \240"',
       exc_exec('call jsondecode("\\t\\"\\xF0\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144"',
       exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \240\144\128"',
       exc_exec('call jsondecode("\\t\\"\\xF0\\x90\\x80\\"")'))
    -- 0xF9 0x80 0x80 0x80 starts 5-byte unicode character
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9"',
       exc_exec('call jsondecode("\\t\\"\\xF9\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80"',
       exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xF9\x80\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\"")'))
    -- 0xFC 0x90 0x80 0x80 0x80 starts 6-byte unicode character
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\"")'))
    eq('Vim(call):E474: Only UTF-8 strings allowed: \xFC\x90\x80\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\"")'))
    -- Specification does not allow unquoted characters above 0x10FFFF
    eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xF9\x80\x80\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xF9\\x80\\x80\\x80\\x80\\"")'))
    eq('Vim(call):E474: Only UTF-8 code points up to U+10FFFF are allowed to appear unescaped: \xFC\x90\x80\x80\x80\x80"',
       exc_exec('call jsondecode("\\t\\"\\xFC\\x90\\x80\\x80\\x80\\x80\\"")'))
    -- '"\xF9\x80\x80\x80\x80"',
    -- '"\xFC\x90\x80\x80\x80\x80"',
  end)
end)

describe('jsonencode() function', function()
  before_each(clear)

  it('dumps strings', function()
    eq('"Test"', funcs.jsonencode('Test'))
    eq('""', funcs.jsonencode(''))
    eq('"\\t"', funcs.jsonencode('\t'))
    eq('"\\n"', funcs.jsonencode('\n'))
    eq('"\\u001B"', funcs.jsonencode('\27'))
  end)

  it('dumps numbers', function()
    eq('0', funcs.jsonencode(0))
    eq('10', funcs.jsonencode(10))
    eq('-10', funcs.jsonencode(-10))
  end)

  it('dumps floats', function()
    eq('0.0', eval('jsonencode(0.0)'))
    eq('10.5', funcs.jsonencode(10.5))
    eq('-10.5', funcs.jsonencode(-10.5))
    eq('-1.0e-5', funcs.jsonencode(-1e-5))
    eq('1.0e50', eval('jsonencode(1.0e50)'))
  end)

  it('dumps lists', function()
    eq('[]', funcs.jsonencode({}))
    eq('[[]]', funcs.jsonencode({{}}))
    eq('[[], []]', funcs.jsonencode({{}, {}}))
  end)

  it('dumps dictionaries', function()
    eq('{}', eval('jsonencode({})'))
    eq('{"d": []}', funcs.jsonencode({d={}}))
    eq('{"d": [], "e": []}', funcs.jsonencode({d={}, e={}}))
  end)

  it('cannot dump generic mapping with generic mapping keys and values',
  function()
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
    execute('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
    execute('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
    execute('call add(todump._VAL, [todumpv1, todumpv2])')
    eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)'))
  end)

  it('cannot dump generic mapping with ext key', function()
    execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)'))
  end)

  it('cannot dump generic mapping with array key', function()
    execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)'))
  end)

  it('cannot dump generic mapping with UINT64_MAX key', function()
    execute('let todump = {"_TYPE": v:msgpack_types.integer}')
    execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)'))
  end)

  it('cannot dump generic mapping with floating-point key', function()
    execute('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('Vim(call):E474: Invalid key in special dictionary', exc_exec('call jsonencode(todump)'))
  end)

  it('can dump generic mapping with STR special key and NUL', function()
    execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n"]}')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('{"\\u0000": 1}', eval('jsonencode(todump)'))
  end)

  it('can dump generic mapping with BIN special key and NUL', function()
    execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n"]}')
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[todump, 1]]}')
    eq('{"\\u0000": 1}', eval('jsonencode(todump)'))
  end)

  it('can dump STR special mapping with NUL and NL', function()
    execute('let todump = {"_TYPE": v:msgpack_types.string, "_VAL": ["\\n", ""]}')
    eq('"\\u0000\\n"', eval('jsonencode(todump)'))
  end)

  it('can dump BIN special mapping with NUL and NL', function()
    execute('let todump = {"_TYPE": v:msgpack_types.binary, "_VAL": ["\\n", ""]}')
    eq('"\\u0000\\n"', eval('jsonencode(todump)'))
  end)

  it('cannot dump special ext mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
    eq('Vim(call):E474: Unable to convert EXT string to JSON', exc_exec('call jsonencode(todump)'))
  end)

  it('can dump special array mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
    eq('[5, [""]]', eval('jsonencode(todump)'))
  end)

  it('can dump special UINT64_MAX mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.integer}')
    execute('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
    eq('18446744073709551615', eval('jsonencode(todump)'))
  end)

  it('can dump special INT64_MIN mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.integer}')
    execute('let todump._VAL = [-1, 2, 0, 0]')
    eq('-9223372036854775808', eval('jsonencode(todump)'))
  end)

  it('can dump special BOOLEAN true mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}')
    eq('true', eval('jsonencode(todump)'))
  end)

  it('can dump special BOOLEAN false mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}')
    eq('false', eval('jsonencode(todump)'))
  end)

  it('can dump special NIL mapping', function()
    execute('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}')
    eq('null', eval('jsonencode(todump)'))
  end)

  it('fails to dump a function reference', function()
    eq('Vim(call):E474: Error while dumping encode_tv2json() argument, itself: attempt to dump function reference',
       exc_exec('call jsonencode(function("tr"))'))
  end)

  it('fails to dump a function reference in a list', function()
    eq('Vim(call):E474: Error while dumping encode_tv2json() argument, index 0: attempt to dump function reference',
       exc_exec('call jsonencode([function("tr")])'))
  end)

  it('fails to dump a recursive list', function()
    execute('let todump = [[[]]]')
    execute('call add(todump[0][0], todump)')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode(todump)'))
  end)

  it('fails to dump a recursive dict', function()
    execute('let todump = {"d": {"d": {}}}')
    execute('call extend(todump.d.d, {"d": todump})')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode([todump])'))
  end)

  it('can dump dict with two same dicts inside', function()
    execute('let inter = {}')
    execute('let todump = {"a": inter, "b": inter}')
    eq('{"a": {}, "b": {}}', eval('jsonencode(todump)'))
  end)

  it('can dump list with two same lists inside', function()
    execute('let inter = []')
    execute('let todump = [inter, inter]')
    eq('[[], []]', eval('jsonencode(todump)'))
  end)

  it('fails to dump a recursive list in a special dict', function()
    execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
    execute('call add(todump._VAL, todump)')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode(todump)'))
  end)

  it('fails to dump a recursive (val) map in a special dict', function()
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
    execute('call add(todump._VAL, ["", todump])')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode([todump])'))
  end)

  it('fails to dump a recursive (val) map in a special dict, _VAL reference', function()
    execute('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [["", []]]}')
    execute('call add(todump._VAL[0][1], todump._VAL)')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode(todump)'))
  end)

  it('fails to dump a recursive (val) special list in a special dict',
  function()
    execute('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
    execute('call add(todump._VAL, ["", todump._VAL])')
    eq('Vim(call):E724: unable to correctly dump variable with self-referencing container',
       exc_exec('call jsonencode(todump)'))
  end)

  it('fails when called with no arguments', function()
    eq('Vim(call):E119: Not enough arguments for function: jsonencode',
       exc_exec('call jsonencode()'))
  end)

  it('fails when called with two arguments', function()
    eq('Vim(call):E118: Too many arguments for function: jsonencode',
       exc_exec('call jsonencode(["", ""], 1)'))
  end)
end)