aboutsummaryrefslogtreecommitdiff
path: root/test/functional/eval/json_functions_spec.lua
blob: 5e1c6a39847ba33c75b5d581432ce16d9136bc1b (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
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('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)