robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
30.8 KiB818 linesraw
1local t = require('test.testutil')
2local describe, it, setup = t.describe, t.it, t.setup
3local pcall_err = t.pcall_err
4local n = require('test.functional.testnvim')()
5
6local clear = n.clear
7local fn = n.fn
8local eval, eq = n.eval, t.eq
9local command = n.command
10local api = n.api
11
12describe('msgpack*() functions', function()
13  setup(clear)
14
15  local obj_test = function(msg, obj)
16    it(msg, function()
17      api.nvim_set_var('obj', obj)
18      eq(obj, eval('msgpackparse(msgpackdump(g:obj))'))
19      eq(obj, eval('msgpackparse(msgpackdump(g:obj, "B"))'))
20    end)
21  end
22
23  -- Regression test: msgpack_list_write was failing to write buffer with zero
24  -- length.
25  obj_test('are able to dump and restore {"file": ""}', { { file = '' } })
26  -- Regression test: msgpack_list_write was failing to write buffer with NL at
27  -- the end.
28  obj_test('are able to dump and restore {0, "echo mpack"}', { { 0, 'echo mpack' } })
29  obj_test('are able to dump and restore "Test\\n"', { 'Test\n' })
30  -- Regression test: msgpack_list_write was failing to write buffer with NL
31  -- inside.
32  obj_test('are able to dump and restore "Test\\nTest 2"', { 'Test\nTest 2' })
33  -- Test that big objects (requirement: dump to something that is bigger then
34  -- IOSIZE) are also fine. This particular object is obtained by concatenating
35  -- 5 identical shada files.
36  -- stylua: ignore
37  local big_obj = {
38    1, 1436711454, 78, {
39      encoding="utf-8",
40      max_kbyte=10,
41      pid=19269,
42      version="NVIM 0.0.0-alpha+201507121634"
43    },
44    8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
45    8, 1436711391, 8, { file="" },
46    4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
47    4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
48    4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
49    4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
50    4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
51    4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
52    4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
53    4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
54    4, 1436708966, 6, { 0, "cq" },
55    4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
56    4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
57    4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
58    4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
59    4, 1436709634, 57, {
60      0,
61      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
62    },
63    4, 1436709651, 67, {
64      0,
65      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
66    },
67    4, 1436709660, 70, {
68      0,
69      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
70    },
71    4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
72    4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
73    4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
74    4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
75    4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
76    4, 1436711142, 14, { 0, "echo mpack" },
77    4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
78    4, 1436711206, 16, { 0, "echo lengths" },
79    4, 1436711244, 92, {
80      0,
81      ("let sum = len(lengths) - 1 | call map(copy(lengths), "
82       .. "'extend(g:, {\"sum\": sum + v:val})')")
83    },
84    4, 1436711245, 12, { 0, "echo sum" },
85    4, 1436711398, 10, { 0, "echo s" },
86    4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
87    4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
88    4, 1436711415, 22, { 0, "echo shada_objects" },
89    4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
90    4, 1436711454, 6, { 0, "qa" },
91    4, 1436711442, 9, { 1, "test", 47 },
92    4, 1436711443, 15, { 1, "aontsuesan", 47 },
93    2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
94    2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
95    3, 0, 3, { "" },
96    10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
97    1, 1436711454, 78, {
98      encoding="utf-8",
99      max_kbyte=10,
100      pid=19269,
101      version="NVIM 0.0.0-alpha+201507121634"
102    },
103    8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
104    8, 1436711391, 8, { file="" },
105    4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
106    4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
107    4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
108    4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
109    4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
110    4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
111    4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
112    4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
113    4, 1436708966, 6, { 0, "cq" },
114    4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
115    4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
116    4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
117    4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
118    4, 1436709634, 57, {
119      0,
120      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
121    },
122    4, 1436709651, 67, {
123      0,
124      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
125    },
126    4, 1436709660, 70, {
127      0,
128      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
129    },
130    4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
131    4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
132    4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
133    4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
134    4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
135    4, 1436711142, 14, { 0, "echo mpack" },
136    4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
137    4, 1436711206, 16, { 0, "echo lengths" },
138    4, 1436711244, 92, {
139      0,
140      ("let sum = len(lengths) - 1 | call map(copy(lengths), "
141       .. "'extend(g:, {\"sum\": sum + v:val})')")
142    },
143    4, 1436711245, 12, { 0, "echo sum" },
144    4, 1436711398, 10, { 0, "echo s" },
145    4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
146    4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
147    4, 1436711415, 22, { 0, "echo shada_objects" },
148    4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
149    4, 1436711454, 6, { 0, "qa" },
150    4, 1436711442, 9, { 1, "test", 47 },
151    4, 1436711443, 15, { 1, "aontsuesan", 47 },
152    2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
153    2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
154    3, 0, 3, { "" },
155    10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
156    1, 1436711454, 78, {
157      encoding="utf-8",
158      max_kbyte=10,
159      pid=19269,
160      version="NVIM 0.0.0-alpha+201507121634"
161    },
162    8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
163    8, 1436711391, 8, { file="" },
164    4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
165    4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
166    4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
167    4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
168    4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
169    4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
170    4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
171    4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
172    4, 1436708966, 6, { 0, "cq" },
173    4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
174    4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
175    4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
176    4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
177    4, 1436709634, 57, {
178      0,
179      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
180    },
181    4, 1436709651, 67, {
182      0,
183      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
184    },
185    4, 1436709660, 70, {
186      0,
187      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
188    },
189    4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
190    4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
191    4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
192    4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
193    4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
194    4, 1436711142, 14, { 0, "echo mpack" },
195    4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
196    4, 1436711206, 16, { 0, "echo lengths" },
197    4, 1436711244, 92, {
198      0,
199      ("let sum = len(lengths) - 1 | call map(copy(lengths), "
200       .. "'extend(g:, {\"sum\": sum + v:val})')")
201    },
202    4, 1436711245, 12, { 0, "echo sum" },
203    4, 1436711398, 10, { 0, "echo s" },
204    4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
205    4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
206    4, 1436711415, 22, { 0, "echo shada_objects" },
207    4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
208    4, 1436711454, 6, { 0, "qa" },
209    4, 1436711442, 9, { 1, "test", 47 },
210    4, 1436711443, 15, { 1, "aontsuesan", 47 },
211    2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
212    2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
213    3, 0, 3, { "" },
214    10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
215    1, 1436711454, 78, {
216      encoding="utf-8",
217      max_kbyte=10,
218      pid=19269,
219      version="NVIM 0.0.0-alpha+201507121634"
220    },
221    8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
222    8, 1436711391, 8, { file="" },
223    4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
224    4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
225    4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
226    4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
227    4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
228    4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
229    4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
230    4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
231    4, 1436708966, 6, { 0, "cq" },
232    4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
233    4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
234    4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
235    4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
236    4, 1436709634, 57, {
237      0,
238      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
239    },
240    4, 1436709651, 67, {
241      0,
242      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
243    },
244    4, 1436709660, 70, {
245      0,
246      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
247    },
248    4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
249    4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
250    4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
251    4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
252    4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
253    4, 1436711142, 14, { 0, "echo mpack" },
254    4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
255    4, 1436711206, 16, { 0, "echo lengths" },
256    4, 1436711244, 92, {
257      0,
258      ("let sum = len(lengths) - 1 | call map(copy(lengths), "
259       .. "'extend(g:, {\"sum\": sum + v:val})')")
260    },
261    4, 1436711245, 12, { 0, "echo sum" },
262    4, 1436711398, 10, { 0, "echo s" },
263    4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
264    4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
265    4, 1436711415, 22, { 0, "echo shada_objects" },
266    4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
267    4, 1436711454, 6, { 0, "qa" },
268    4, 1436711442, 9, { 1, "test", 47 },
269    4, 1436711443, 15, { 1, "aontsuesan", 47 },
270    2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
271    2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
272    3, 0, 3, { "" },
273    10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
274    1, 1436711454, 78, {
275      encoding="utf-8",
276      max_kbyte=10,
277      pid=19269,
278      version="NVIM 0.0.0-alpha+201507121634"
279    },
280    8, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" },
281    8, 1436711391, 8, { file="" },
282    4, 1436700940, 30, { 0, "call mkdir('/tmp/tty/tty')" },
283    4, 1436701355, 35, { 0, "call mkdir('/tmp/tty/tty', 'p')" },
284    4, 1436701368, 24, { 0, "call mkdir('/', 'p')" },
285    4, 1436701375, 26, { 0, "call mkdir('/tty/tty')" },
286    4, 1436701383, 30, { 0, "call mkdir('/tty/tty/tty')" },
287    4, 1436701407, 35, { 0, "call mkdir('/usr/tty/tty', 'p')" },
288    4, 1436701666, 35, { 0, "call mkdir('/tty/tty/tty', 'p')" },
289    4, 1436708101, 25, { 0, "echo msgpackdump([1])" },
290    4, 1436708966, 6, { 0, "cq" },
291    4, 1436709606, 25, { 0, "echo msgpackdump([5])" },
292    4, 1436709610, 26, { 0, "echo msgpackdump([10])" },
293    4, 1436709615, 31, { 0, "echo msgpackdump([5, 5, 5])" },
294    4, 1436709618, 35, { 0, "echo msgpackdump([5, 5, 5, 10])" },
295    4, 1436709634, 57, {
296      0,
297      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1}]])"
298    },
299    4, 1436709651, 67, {
300      0,
301      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}]])"
302    },
303    4, 1436709660, 70, {
304      0,
305      "echo msgpackdump([5, 5, 5, 10, [10, 20, {\"abc\": 1, \"def\": 0}], 0])"
306    },
307    4, 1436710095, 29, { 0, "echo msgpackparse([\"\\n\"])" },
308    4, 1436710100, 28, { 0, "echo msgpackparse([\"j\"])" },
309    4, 1436710109, 31, { 0, "echo msgpackparse([\"\", \"\"])" },
310    4, 1436710424, 33, { 0, "echo msgpackparse([\"\", \"\\n\"])" },
311    4, 1436710428, 32, { 0, "echo msgpackparse([\"\", \"j\"])" },
312    4, 1436711142, 14, { 0, "echo mpack" },
313    4, 1436711196, 45, { 0, "let lengths = map(mpack[:], 'len(v:val)')" },
314    4, 1436711206, 16, { 0, "echo lengths" },
315    4, 1436711244, 92, {
316      0,
317      ("let sum = len(lengths) - 1 | call map(copy(lengths), "
318       .. "'extend(g:, {\"sum\": sum + v:val})')")
319    },
320    4, 1436711245, 12, { 0, "echo sum" },
321    4, 1436711398, 10, { 0, "echo s" },
322    4, 1436711404, 41, { 0, "let mpack = readfile('/tmp/foo', 'b')" },
323    4, 1436711408, 41, { 0, "let shada_objects=msgpackparse(mpack)" },
324    4, 1436711415, 22, { 0, "echo shada_objects" },
325    4, 1436711451, 30, { 0, "e ~/.nvim/shada/main.shada" },
326    4, 1436711454, 6, { 0, "qa" },
327    4, 1436711442, 9, { 1, "test", 47 },
328    4, 1436711443, 15, { 1, "aontsuesan", 47 },
329    2, 1436711443, 38, { hlsearch=1, pat="aontsuesan", smartcase=1 },
330    2, 0, 31, { islast=0, pat="", smartcase=1, sub=1 },
331    3, 0, 3, { "" },
332    10, 1436711451, 40, { file="/home/zyx/.nvim/shada/main.shada" }
333  }
334  obj_test('are able to dump and restore rather big object', big_obj)
335
336  obj_test('are able to dump and restore floating-point value', { 0.125 })
337
338  it('can restore and dump UINT64_MAX', function()
339    command('let dumped = ["\\xCF" . repeat("\\xFF", 8)]')
340    command('let parsed = msgpackparse(dumped)')
341    command('let dumped2 = msgpackdump(parsed)')
342    eq(1, eval('type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer'))
343    if eval('type(parsed[0]) == type(0)') == 1 then
344      command('call assert_equal(0xFFFFFFFFFFFFFFFF, parsed[0])')
345      eq({}, eval('v:errors'))
346    else
347      eq({ _TYPE = {}, _VAL = { 1, 3, 0x7FFFFFFF, 0x7FFFFFFF } }, eval('parsed[0]'))
348    end
349    eq(1, eval('dumped ==# dumped2'))
350  end)
351
352  it('can restore and dump INT64_MIN', function()
353    command('let dumped = ["\\xD3\\x80" . repeat("\\n", 7)]')
354    command('let parsed = msgpackparse(dumped)')
355    command('let dumped2 = msgpackdump(parsed)')
356    eq(1, eval('type(parsed[0]) == type(0) ' .. '|| parsed[0]._TYPE is v:msgpack_types.integer'))
357    if eval('type(parsed[0]) == type(0)') == 1 then
358      command('call assert_equal(-0x7fffffffffffffff - 1, parsed[0])')
359      eq({}, eval('v:errors'))
360    else
361      eq({ _TYPE = {}, _VAL = { -1, 2, 0, 0 } }, eval('parsed[0]'))
362    end
363    eq(1, eval('dumped ==# dumped2'))
364  end)
365
366  it('can restore and dump BIN string with zero byte', function()
367    command('let dumped = ["\\xC4\\x01\\n"]')
368    command('let parsed = msgpackparse(dumped)')
369    command('let dumped2 = msgpackdump(parsed)')
370    eq({ '\000' }, eval('parsed'))
371    eq(1, eval('dumped ==# dumped2'))
372  end)
373
374  it('can restore and dump STR string contents with zero byte', function()
375    command('let dumped = ["\\xA1\\n"]')
376    command('let parsed = msgpackparse(dumped)')
377    command('let dumped2 = msgpackdump(parsed)')
378    eq({ '\000' }, eval('parsed'))
379    eq(eval('v:t_blob'), eval('type(parsed[0])'))
380    -- type is not preserved: prefer BIN for binary contents
381    eq(0, eval('dumped ==# dumped2'))
382  end)
383
384  it('can restore and dump BIN string with NL', function()
385    command('let dumped = ["\\xC4\\x01", ""]')
386    command('let parsed = msgpackparse(dumped)')
387    command('let dumped2 = msgpackdump(parsed)')
388    eq({ '\n' }, eval('parsed'))
389    eq(1, eval('dumped ==# dumped2'))
390  end)
391
392  it('dump and restore special mapping with floating-point value', function()
393    command('let todump = {"_TYPE": v:msgpack_types.float, "_VAL": 0.125}')
394    eq({ 0.125 }, eval('msgpackparse(msgpackdump([todump]))'))
395  end)
396end)
397
398local blobstr = function(list)
399  local l = {}
400  for i, v in ipairs(list) do
401    l[i] = v:gsub('\n', '\000')
402  end
403  return table.concat(l, '\n')
404end
405
406-- Test msgpackparse() with a readfile()-style list and a blob argument
407local parse_eq = function(expect, list_arg)
408  local blob_expr = '0z'
409    .. blobstr(list_arg):gsub('(.)', function(c)
410      return ('%.2x'):format(c:byte())
411    end)
412  eq(expect, fn.msgpackparse(list_arg))
413  command('let g:parsed = msgpackparse(' .. blob_expr .. ')')
414  eq(expect, eval('g:parsed'))
415end
416
417describe('msgpackparse() function', function()
418  setup(clear)
419
420  it('restores nil as v:null', function()
421    parse_eq(eval('[v:null]'), { '\192' })
422  end)
423
424  it('restores boolean false as v:false', function()
425    parse_eq({ false }, { '\194' })
426  end)
427
428  it('restores boolean true as v:true', function()
429    parse_eq({ true }, { '\195' })
430  end)
431
432  it('restores FIXSTR as string', function()
433    parse_eq({ 'ab' }, { '\162ab' })
434  end)
435
436  it('restores BIN 8 as string', function()
437    parse_eq({ 'ab' }, { '\196\002ab' })
438  end)
439
440  it('restores FIXEXT1 as special dict', function()
441    parse_eq({ { _TYPE = {}, _VAL = { 0x10, { '', '' } } } }, { '\212\016', '' })
442    eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.ext'))
443  end)
444
445  it('restores MAP with BIN key as ordinary dict', function()
446    parse_eq({ { a = '' } }, { '\129\196\001a\196\n' })
447  end)
448
449  it('restores MAP with duplicate STR keys as special dict', function()
450    command('let dumped = ["\\x82\\xA1a\\xC4\\n\\xA1a\\xC4\\n"]')
451    -- FIXME Internal error bug, can't use parse_eq() here
452    command('silent! let parsed = msgpackparse(dumped)')
453    eq({
454      {
455        _TYPE = {},
456        _VAL = {
457          { 'a', '' },
458          { 'a', '' },
459        },
460      },
461    }, eval('parsed'))
462    eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.map'))
463    eq(eval('v:t_string'), eval('type(g:parsed[0]._VAL[0][0])'))
464    eq(eval('v:t_string'), eval('type(g:parsed[0]._VAL[1][0])'))
465  end)
466
467  it('restores MAP with MAP key as special dict', function()
468    parse_eq({ { _TYPE = {}, _VAL = { { {}, '' } } } }, { '\129\128\196\n' })
469    eq(1, eval('g:parsed[0]._TYPE is v:msgpack_types.map'))
470  end)
471
472  it('msgpackparse(systemlist(...)) does not segfault. #3135', function()
473    local cmd = "sort(keys(msgpackparse(systemlist('" .. n.nvim_prog .. " --api-info'))[0]))"
474    eval(cmd)
475    eval(cmd) -- do it again (try to force segfault)
476    local api_info = eval(cmd) -- do it again
477    n.assert_alive()
478    eq({ 'error_types', 'functions', 'types', 'ui_events', 'ui_options', 'version' }, api_info)
479  end)
480
481  it('fails when called with no arguments', function()
482    eq(
483      'Vim(call):E119: Not enough arguments for function: msgpackparse',
484      pcall_err(command, 'call msgpackparse()')
485    )
486  end)
487
488  it('fails when called with two arguments', function()
489    eq(
490      'Vim(call):E118: Too many arguments for function: msgpackparse',
491      pcall_err(command, 'call msgpackparse(["", ""], 1)')
492    )
493  end)
494
495  it('fails to parse a string', function()
496    eq(
497      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
498      pcall_err(command, 'call msgpackparse("abcdefghijklmnopqrstuvwxyz")')
499    )
500  end)
501
502  it('fails to parse a number', function()
503    eq(
504      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
505      pcall_err(command, 'call msgpackparse(127)')
506    )
507  end)
508
509  it('fails to parse a dict', function()
510    eq(
511      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
512      pcall_err(command, 'call msgpackparse({})')
513    )
514  end)
515
516  it('fails to parse a funcref', function()
517    eq(
518      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
519      pcall_err(command, 'call msgpackparse(function("tr"))')
520    )
521  end)
522
523  it('fails to parse a partial', function()
524    command('function! T() dict\nendfunction')
525    eq(
526      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
527      pcall_err(command, 'call msgpackparse(function("T", [1, 2], {}))')
528    )
529  end)
530
531  it('fails to parse a float', function()
532    eq(
533      'Vim(call):E899: Argument of msgpackparse() must be a List or Blob',
534      pcall_err(command, 'call msgpackparse(0.0)')
535    )
536  end)
537
538  it('fails on incomplete msgpack string', function()
539    local expected = 'Vim(call):E475: Invalid argument: Incomplete msgpack string'
540    eq(expected, pcall_err(command, [[call msgpackparse(["\xc4"])]]))
541    eq(expected, pcall_err(command, [[call msgpackparse(["\xca", "\x02\x03"])]]))
542    eq(expected, pcall_err(command, 'call msgpackparse(0zc4)'))
543    eq(expected, pcall_err(command, 'call msgpackparse(0zca0a0203)'))
544  end)
545
546  it('fails when unable to parse msgpack string', function()
547    local expected = 'Vim(call):E475: Invalid argument: Failed to parse msgpack string'
548    eq(expected, pcall_err(command, [[call msgpackparse(["\xc1"])]]))
549    eq(expected, pcall_err(command, 'call msgpackparse(0zc1)'))
550  end)
551end)
552
553describe('msgpackdump() function', function()
554  setup(clear)
555
556  local dump_eq = function(exp_list, arg_expr)
557    eq(exp_list, eval('msgpackdump(' .. arg_expr .. ')'))
558    eq(blobstr(exp_list), eval('msgpackdump(' .. arg_expr .. ', "B")'))
559  end
560
561  it('dumps string as BIN 8', function()
562    dump_eq({ '\196\004Test' }, '["Test"]')
563  end)
564
565  it('dumps blob as BIN 8', function()
566    dump_eq({ '\196\005Bl\nb!' }, '[0z426c006221]')
567  end)
568
569  it('can dump generic mapping with generic mapping keys and values', function()
570    command('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
571    command('let todumpv1 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
572    command('let todumpv2 = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
573    command('call add(todump._VAL, [todumpv1, todumpv2])')
574    dump_eq({ '\129\128\128' }, '[todump]')
575  end)
576
577  it('can dump v:true', function()
578    dump_eq({ '\195' }, '[v:true]')
579  end)
580
581  it('can dump v:false', function()
582    dump_eq({ '\194' }, '[v:false]')
583  end)
584
585  it('can dump v:null', function()
586    dump_eq({ '\192' }, '[v:null]')
587  end)
588
589  it('can dump special bool mapping (true)', function()
590    command('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 1}')
591    dump_eq({ '\195' }, '[todump]')
592  end)
593
594  it('can dump special bool mapping (false)', function()
595    command('let todump = {"_TYPE": v:msgpack_types.boolean, "_VAL": 0}')
596    dump_eq({ '\194' }, '[todump]')
597  end)
598
599  it('can dump special nil mapping', function()
600    command('let todump = {"_TYPE": v:msgpack_types.nil, "_VAL": 0}')
601    dump_eq({ '\192' }, '[todump]')
602  end)
603
604  it('can dump special ext mapping', function()
605    command('let todump = {"_TYPE": v:msgpack_types.ext, "_VAL": [5, ["",""]]}')
606    dump_eq({ '\212\005', '' }, '[todump]')
607  end)
608
609  it('can dump special array mapping', function()
610    command('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": [5, [""]]}')
611    dump_eq({ '\146\005\145\196\n' }, '[todump]')
612  end)
613
614  it('can dump special UINT64_MAX mapping', function()
615    command('let todump = {"_TYPE": v:msgpack_types.integer}')
616    command('let todump._VAL = [1, 3, 0x7FFFFFFF, 0x7FFFFFFF]')
617    dump_eq({ '\207\255\255\255\255\255\255\255\255' }, '[todump]')
618  end)
619
620  it('can dump special INT64_MIN mapping', function()
621    command('let todump = {"_TYPE": v:msgpack_types.integer}')
622    command('let todump._VAL = [-1, 2, 0, 0]')
623    dump_eq({ '\211\128\n\n\n\n\n\n\n' }, '[todump]')
624  end)
625
626  it('fails to dump a function reference', function()
627    command('let Todump = function("tr")')
628    eq(
629      'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, itself: attempt to dump function reference',
630      pcall_err(command, 'call msgpackdump([Todump])')
631    )
632  end)
633
634  it('fails to dump a partial', function()
635    command('function! T() dict\nendfunction')
636    command('let Todump = function("T", [1, 2], {})')
637    eq(
638      'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, itself: attempt to dump function reference',
639      pcall_err(command, 'call msgpackdump([Todump])')
640    )
641  end)
642
643  it('fails to dump a function reference in a list', function()
644    command('let todump = [function("tr")]')
645    eq(
646      'Vim(call):E5004: Error while dumping msgpackdump() argument, index 0, index 0: attempt to dump function reference',
647      pcall_err(command, 'call msgpackdump([todump])')
648    )
649  end)
650
651  it('fails to dump a recursive list', function()
652    command('let todump = [[[]]]')
653    command('call add(todump[0][0], todump)')
654    eq(
655      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0, index 0, index 0',
656      pcall_err(command, 'call msgpackdump([todump])')
657    )
658  end)
659
660  it('fails to dump a recursive dict', function()
661    command('let todump = {"d": {"d": {}}}')
662    command('call extend(todump.d.d, {"d": todump})')
663    eq(
664      "Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key 'd', key 'd', key 'd'",
665      pcall_err(command, 'call msgpackdump([todump])')
666    )
667  end)
668
669  it('can dump dict with two same dicts inside', function()
670    command('let inter = {}')
671    command('let todump = {"a": inter, "b": inter}')
672    dump_eq({ '\130\161a\128\161b\128' }, '[todump]')
673  end)
674
675  it('can dump list with two same lists inside', function()
676    command('let inter = []')
677    command('let todump = [inter, inter]')
678    dump_eq({ '\146\144\144' }, '[todump]')
679  end)
680
681  it('fails to dump a recursive list in a special dict', function()
682    command('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
683    command('call add(todump._VAL, todump)')
684    eq(
685      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0',
686      pcall_err(command, 'call msgpackdump([todump])')
687    )
688  end)
689
690  it('fails to dump a recursive (key) map in a special dict', function()
691    command('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
692    command('call add(todump._VAL, [todump, 0])')
693    eq(
694      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0',
695      pcall_err(command, 'call msgpackdump([todump])')
696    )
697  end)
698
699  it('fails to dump a recursive (val) map in a special dict', function()
700    command('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": []}')
701    command('call add(todump._VAL, [0, todump])')
702    eq(
703      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key 0 at index 0 from special map',
704      pcall_err(command, 'call msgpackdump([todump])')
705    )
706  end)
707
708  it('fails to dump a recursive (key) map in a special dict, _VAL reference', function()
709    command('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[[], []]]}')
710    command('call add(todump._VAL[0][0], todump._VAL)')
711    eq(
712      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key [[[[...@0], []]]] at index 0 from special map, index 0',
713      pcall_err(command, 'call msgpackdump([todump])')
714    )
715  end)
716
717  it('fails to dump a recursive (val) map in a special dict, _VAL reference', function()
718    command('let todump = {"_TYPE": v:msgpack_types.map, "_VAL": [[[], []]]}')
719    command('call add(todump._VAL[0][1], todump._VAL)')
720    eq(
721      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in key [] at index 0 from special map, index 0',
722      pcall_err(command, 'call msgpackdump([todump])')
723    )
724  end)
725
726  it('fails to dump a recursive (val) special list in a special dict', function()
727    command('let todump = {"_TYPE": v:msgpack_types.array, "_VAL": []}')
728    command('call add(todump._VAL, [0, todump._VAL])')
729    eq(
730      'Vim(call):E5005: Unable to dump msgpackdump() argument, index 0: container references itself in index 0, index 1',
731      pcall_err(command, 'call msgpackdump([todump])')
732    )
733  end)
734
735  it('fails when called with no arguments', function()
736    eq(
737      'Vim(call):E119: Not enough arguments for function: msgpackdump',
738      pcall_err(command, 'call msgpackdump()')
739    )
740  end)
741
742  it('fails when called with three arguments', function()
743    eq(
744      'Vim(call):E118: Too many arguments for function: msgpackdump',
745      pcall_err(command, 'call msgpackdump(["", ""], 1, 2)')
746    )
747  end)
748
749  it('fails to dump a string', function()
750    eq(
751      'Vim(call):E686: Argument of msgpackdump() must be a List',
752      pcall_err(command, 'call msgpackdump("abcdefghijklmnopqrstuvwxyz")')
753    )
754  end)
755
756  it('fails to dump a number', function()
757    eq(
758      'Vim(call):E686: Argument of msgpackdump() must be a List',
759      pcall_err(command, 'call msgpackdump(127)')
760    )
761  end)
762
763  it('fails to dump a dict', function()
764    eq(
765      'Vim(call):E686: Argument of msgpackdump() must be a List',
766      pcall_err(command, 'call msgpackdump({})')
767    )
768  end)
769
770  it('fails to dump a funcref', function()
771    eq(
772      'Vim(call):E686: Argument of msgpackdump() must be a List',
773      pcall_err(command, 'call msgpackdump(function("tr"))')
774    )
775  end)
776
777  it('fails to dump a partial', function()
778    command('function! T() dict\nendfunction')
779    eq(
780      'Vim(call):E686: Argument of msgpackdump() must be a List',
781      pcall_err(command, 'call msgpackdump(function("T", [1, 2], {}))')
782    )
783  end)
784
785  it('fails to dump a float', function()
786    eq(
787      'Vim(call):E686: Argument of msgpackdump() must be a List',
788      pcall_err(command, 'call msgpackdump(0.0)')
789    )
790  end)
791
792  it('fails to dump special value', function()
793    for _, val in ipairs({ 'v:true', 'v:false', 'v:null' }) do
794      eq(
795        'Vim(call):E686: Argument of msgpackdump() must be a List',
796        pcall_err(command, 'call msgpackdump(' .. val .. ')')
797      )
798    end
799  end)
800
801  it('can dump NULL string', function()
802    dump_eq({ '\196\n' }, '[$XXX_UNEXISTENT_VAR_XXX]')
803    dump_eq({ '\196\n' }, '[v:_null_blob]')
804    dump_eq({ '\160' }, '[{"_TYPE": v:msgpack_types.string, "_VAL": [$XXX_UNEXISTENT_VAR_XXX]}]')
805  end)
806
807  it('can dump NULL blob', function()
808    eq({ '\196\n' }, eval('msgpackdump([v:_null_blob])'))
809  end)
810
811  it('can dump NULL list', function()
812    eq({ '\144' }, eval('msgpackdump([v:_null_list])'))
813  end)
814
815  it('can dump NULL dict', function()
816    eq({ '\128' }, eval('msgpackdump([v:_null_dict])'))
817  end)
818end)