robin/laufey

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

robinmerge: 1dc9728daa1d8a2702

master
30.4 KiB759 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, setup = t.describe, t.it, t.setup
5local clear = n.clear
6local api = n.api
7local eq = t.eq
8local pcall_err = t.pcall_err
9local nvim_eval = n.eval
10local nvim_command = n.command
11local ok = t.ok
12local NIL = vim.NIL
13
14describe('autoload/msgpack.vim', function()
15  setup(function()
16    clear { args = { '-u', 'NORC' } }
17  end)
18
19  local sp = function(typ, val)
20    return ('{"_TYPE": v:msgpack_types.%s, "_VAL": %s}'):format(typ, val)
21  end
22  local mapsp = function(...)
23    local val = ''
24    for i = 1, (select('#', ...) / 2) do
25      val = ('%s[%s,%s],'):format(val, select(i * 2 - 1, ...), select(i * 2, ...))
26    end
27    return sp('map', '[' .. val .. ']')
28  end
29
30  local nan = -(1.0 / 0.0 - 1.0 / 0.0)
31  local inf = 1.0 / 0.0
32  local minus_inf = -(1.0 / 0.0)
33
34  describe('function msgpack#equal', function()
35    local msgpack_eq = function(expected, a, b)
36      eq(expected, nvim_eval(('msgpack#equal(%s, %s)'):format(a, b)))
37      if a ~= b then
38        eq(expected, nvim_eval(('msgpack#equal(%s, %s)'):format(b, a)))
39      end
40    end
41    it('compares raw integers correctly', function()
42      msgpack_eq(1, '1', '1')
43      msgpack_eq(0, '1', '0')
44    end)
45    it('compares integer specials correctly', function()
46      msgpack_eq(1, sp('integer', '[-1, 1, 0, 0]'), sp('integer', '[-1, 1, 0, 0]'))
47      msgpack_eq(0, sp('integer', '[-1, 1, 0, 0]'), sp('integer', '[ 1, 1, 0, 0]'))
48    end)
49    it('compares integer specials with raw integer correctly', function()
50      msgpack_eq(1, sp('integer', '[-1, 0, 0, 1]'), '-1')
51      msgpack_eq(0, sp('integer', '[-1, 0, 0, 1]'), '1')
52      msgpack_eq(0, sp('integer', '[ 1, 0, 0, 1]'), '-1')
53      msgpack_eq(1, sp('integer', '[ 1, 0, 0, 1]'), '1')
54    end)
55    it('compares integer with float correctly', function()
56      msgpack_eq(0, '0', '0.0')
57    end)
58    it('compares raw binaries correctly', function()
59      msgpack_eq(1, '"abc\\ndef"', '"abc\\ndef"')
60      msgpack_eq(0, '"abc\\ndef"', '"abc\\nghi"')
61    end)
62    it('compares string specials correctly', function()
63      msgpack_eq(1, sp('string', '["abc\\n", "def"]'), sp('string', '["abc\\n", "def"]'))
64      msgpack_eq(0, sp('string', '["abc", "def"]'), sp('string', '["abc\\n", "def"]'))
65      msgpack_eq(1, sp('string', '["abc", "def"]'), '"abc\\ndef"')
66      msgpack_eq(1, '"abc\\ndef"', sp('string', '["abc", "def"]'))
67    end)
68    it('compares ext specials correctly', function()
69      msgpack_eq(1, sp('ext', '[1, ["", "ac"]]'), sp('ext', '[1, ["", "ac"]]'))
70      msgpack_eq(0, sp('ext', '[2, ["", "ac"]]'), sp('ext', '[1, ["", "ac"]]'))
71      msgpack_eq(0, sp('ext', '[1, ["", "ac"]]'), sp('ext', '[1, ["", "abc"]]'))
72    end)
73    it('compares raw maps correctly', function()
74      msgpack_eq(1, '{"a": 1, "b": 2}', '{"b": 2, "a": 1}')
75      msgpack_eq(1, '{}', '{}')
76      msgpack_eq(0, '{}', '{"a": 1}')
77      msgpack_eq(0, '{"a": 2}', '{"a": 1}')
78      msgpack_eq(0, '{"a": 1}', '{"b": 1}')
79      msgpack_eq(0, '{"a": 1}', '{"a": 1, "b": 1}')
80      msgpack_eq(0, '{"a": 1, "b": 1}', '{"b": 1}')
81    end)
82    it('compares map specials correctly', function()
83      msgpack_eq(1, mapsp(), mapsp())
84      msgpack_eq(
85        1,
86        mapsp(mapsp('1', '1'), mapsp('1', '1')),
87        mapsp(mapsp('1', '1'), mapsp('1', '1'))
88      )
89      msgpack_eq(0, mapsp(), mapsp('1', '1'))
90      msgpack_eq(
91        0,
92        mapsp(mapsp('1', '1'), mapsp('1', '1')),
93        mapsp(sp('string', '[""]'), mapsp('1', '1'))
94      )
95      msgpack_eq(
96        0,
97        mapsp(mapsp('1', '1'), mapsp('1', '1')),
98        mapsp(mapsp('2', '1'), mapsp('1', '1'))
99      )
100      msgpack_eq(
101        0,
102        mapsp(mapsp('1', '1'), mapsp('1', '1')),
103        mapsp(mapsp('1', '2'), mapsp('1', '1'))
104      )
105      msgpack_eq(
106        0,
107        mapsp(mapsp('1', '1'), mapsp('1', '1')),
108        mapsp(mapsp('1', '1'), mapsp('2', '1'))
109      )
110      msgpack_eq(
111        0,
112        mapsp(mapsp('1', '1'), mapsp('1', '1')),
113        mapsp(mapsp('1', '1'), mapsp('1', '2'))
114      )
115      msgpack_eq(
116        1,
117        mapsp(mapsp('2', '1'), mapsp('1', '1'), mapsp('1', '1'), mapsp('1', '1')),
118        mapsp(mapsp('1', '1'), mapsp('1', '1'), mapsp('2', '1'), mapsp('1', '1'))
119      )
120    end)
121    it('compares map specials with raw maps correctly', function()
122      msgpack_eq(1, mapsp(), '{}')
123      msgpack_eq(1, mapsp(sp('string', '["1"]'), '1'), '{"1": 1}')
124      msgpack_eq(1, mapsp(sp('string', '["1"]'), sp('integer', '[1, 0, 0, 1]')), '{"1": 1}')
125      msgpack_eq(0, mapsp(sp('integer', '[1, 0, 0, 1]'), sp('string', '["1"]')), '{1: "1"}')
126      msgpack_eq(1, mapsp('"1"', sp('integer', '[1, 0, 0, 1]')), '{"1": 1}')
127      msgpack_eq(0, mapsp(sp('string', '["1"]'), '1', sp('string', '["2"]'), '2'), '{"1": 1}')
128      msgpack_eq(0, mapsp(sp('string', '["1"]'), '1'), '{"1": 1, "2": 2}')
129    end)
130    it('compares raw arrays correctly', function()
131      msgpack_eq(1, '[]', '[]')
132      msgpack_eq(0, '[]', '[1]')
133      msgpack_eq(1, '[1]', '[1]')
134      msgpack_eq(1, '[[[1]]]', '[[[1]]]')
135      msgpack_eq(0, '[[[2]]]', '[[[1]]]')
136    end)
137    it('compares array specials correctly', function()
138      msgpack_eq(1, sp('array', '[]'), sp('array', '[]'))
139      msgpack_eq(0, sp('array', '[]'), sp('array', '[1]'))
140      msgpack_eq(1, sp('array', '[1]'), sp('array', '[1]'))
141      msgpack_eq(1, sp('array', '[[[1]]]'), sp('array', '[[[1]]]'))
142      msgpack_eq(0, sp('array', '[[[1]]]'), sp('array', '[[[2]]]'))
143    end)
144    it('compares array specials with raw arrays correctly', function()
145      msgpack_eq(1, sp('array', '[]'), '[]')
146      msgpack_eq(0, sp('array', '[]'), '[1]')
147      msgpack_eq(1, sp('array', '[1]'), '[1]')
148      msgpack_eq(1, sp('array', '[[[1]]]'), '[[[1]]]')
149      msgpack_eq(0, sp('array', '[[[1]]]'), '[[[2]]]')
150    end)
151    it('compares raw floats correctly', function()
152      msgpack_eq(1, '0.0', '0.0')
153      msgpack_eq(1, '(1.0/0.0-1.0/0.0)', '(1.0/0.0-1.0/0.0)')
154      -- both (1.0/0.0-1.0/0.0) and -(1.0/0.0-1.0/0.0) now return
155      -- str2float('nan'). ref: @18d1ba3422d
156      msgpack_eq(1, '(1.0/0.0-1.0/0.0)', '-(1.0/0.0-1.0/0.0)')
157      msgpack_eq(1, '-(1.0/0.0-1.0/0.0)', '-(1.0/0.0-1.0/0.0)')
158      msgpack_eq(1, '1.0/0.0', '1.0/0.0')
159      msgpack_eq(1, '-(1.0/0.0)', '-(1.0/0.0)')
160      msgpack_eq(1, '0.0', '0.0')
161      msgpack_eq(0, '0.0', '1.0')
162      msgpack_eq(0, '0.0', '(1.0/0.0-1.0/0.0)')
163      msgpack_eq(0, '0.0', '1.0/0.0')
164      msgpack_eq(0, '0.0', '-(1.0/0.0)')
165      msgpack_eq(0, '1.0/0.0', '-(1.0/0.0)')
166      msgpack_eq(0, '(1.0/0.0-1.0/0.0)', '-(1.0/0.0)')
167      msgpack_eq(0, '(1.0/0.0-1.0/0.0)', '1.0/0.0')
168    end)
169    it('compares float specials with raw floats correctly', function()
170      msgpack_eq(1, sp('float', '0.0'), '0.0')
171      msgpack_eq(1, sp('float', '(1.0/0.0-1.0/0.0)'), '(1.0/0.0-1.0/0.0)')
172      msgpack_eq(1, sp('float', '(1.0/0.0-1.0/0.0)'), '-(1.0/0.0-1.0/0.0)')
173      msgpack_eq(1, sp('float', '-(1.0/0.0-1.0/0.0)'), '(1.0/0.0-1.0/0.0)')
174      msgpack_eq(1, sp('float', '-(1.0/0.0-1.0/0.0)'), '-(1.0/0.0-1.0/0.0)')
175      msgpack_eq(1, sp('float', '1.0/0.0'), '1.0/0.0')
176      msgpack_eq(1, sp('float', '-(1.0/0.0)'), '-(1.0/0.0)')
177      msgpack_eq(1, sp('float', '0.0'), '0.0')
178      msgpack_eq(0, sp('float', '0.0'), '1.0')
179      msgpack_eq(0, sp('float', '0.0'), '(1.0/0.0-1.0/0.0)')
180      msgpack_eq(0, sp('float', '0.0'), '1.0/0.0')
181      msgpack_eq(0, sp('float', '0.0'), '-(1.0/0.0)')
182      msgpack_eq(0, sp('float', '1.0/0.0'), '-(1.0/0.0)')
183      msgpack_eq(0, sp('float', '(1.0/0.0-1.0/0.0)'), '-(1.0/0.0)')
184      msgpack_eq(0, sp('float', '(1.0/0.0-1.0/0.0)'), '1.0/0.0')
185    end)
186    it('compares float specials correctly', function()
187      msgpack_eq(1, sp('float', '0.0'), sp('float', '0.0'))
188      msgpack_eq(1, sp('float', '(1.0/0.0-1.0/0.0)'), sp('float', '(1.0/0.0-1.0/0.0)'))
189      msgpack_eq(1, sp('float', '1.0/0.0'), sp('float', '1.0/0.0'))
190      msgpack_eq(1, sp('float', '-(1.0/0.0)'), sp('float', '-(1.0/0.0)'))
191      msgpack_eq(1, sp('float', '0.0'), sp('float', '0.0'))
192      msgpack_eq(0, sp('float', '0.0'), sp('float', '1.0'))
193      msgpack_eq(0, sp('float', '0.0'), sp('float', '(1.0/0.0-1.0/0.0)'))
194      msgpack_eq(0, sp('float', '0.0'), sp('float', '1.0/0.0'))
195      msgpack_eq(0, sp('float', '0.0'), sp('float', '-(1.0/0.0)'))
196      msgpack_eq(0, sp('float', '1.0/0.0'), sp('float', '-(1.0/0.0)'))
197      msgpack_eq(0, sp('float', '(1.0/0.0-1.0/0.0)'), sp('float', '-(1.0/0.0)'))
198      msgpack_eq(1, sp('float', '(1.0/0.0-1.0/0.0)'), sp('float', '-(1.0/0.0-1.0/0.0)'))
199      msgpack_eq(1, sp('float', '-(1.0/0.0-1.0/0.0)'), sp('float', '-(1.0/0.0-1.0/0.0)'))
200      msgpack_eq(0, sp('float', '(1.0/0.0-1.0/0.0)'), sp('float', '1.0/0.0'))
201    end)
202    it('compares boolean specials correctly', function()
203      msgpack_eq(1, sp('boolean', '1'), sp('boolean', '1'))
204      msgpack_eq(0, sp('boolean', '1'), sp('boolean', '0'))
205    end)
206    it('compares nil specials correctly', function()
207      msgpack_eq(1, sp('nil', '1'), sp('nil', '0'))
208    end)
209    it('compares nil, boolean and integer values with each other correctly', function()
210      msgpack_eq(0, sp('boolean', '1'), '1')
211      msgpack_eq(0, sp('boolean', '1'), sp('nil', '0'))
212      msgpack_eq(0, sp('boolean', '1'), sp('nil', '1'))
213      msgpack_eq(0, sp('boolean', '0'), sp('nil', '0'))
214      msgpack_eq(0, sp('boolean', '0'), '0')
215      msgpack_eq(0, sp('boolean', '0'), sp('integer', '[1, 0, 0, 0]'))
216      msgpack_eq(0, sp('boolean', '0'), sp('integer', '[1, 0, 0, 1]'))
217      msgpack_eq(0, sp('boolean', '1'), sp('integer', '[1, 0, 0, 1]'))
218      msgpack_eq(0, sp('nil', '0'), sp('integer', '[1, 0, 0, 0]'))
219      msgpack_eq(0, sp('nil', '0'), '0')
220    end)
221  end)
222
223  describe('function msgpack#is_int', function()
224    it('works', function()
225      eq(1, nvim_eval('msgpack#is_int(1)'))
226      eq(1, nvim_eval('msgpack#is_int(-1)'))
227      eq(1, nvim_eval(('msgpack#is_int(%s)'):format(sp('integer', '[1, 0, 0, 1]'))))
228      eq(1, nvim_eval(('msgpack#is_int(%s)'):format(sp('integer', '[-1, 0, 0, 1]'))))
229      eq(0, nvim_eval(('msgpack#is_int(%s)'):format(sp('float', '0.0'))))
230      eq(0, nvim_eval(('msgpack#is_int(%s)'):format(sp('boolean', '0'))))
231      eq(0, nvim_eval(('msgpack#is_int(%s)'):format(sp('nil', '0'))))
232      eq(0, nvim_eval('msgpack#is_int("")'))
233    end)
234  end)
235
236  describe('function msgpack#is_uint', function()
237    it('works', function()
238      eq(1, nvim_eval('msgpack#is_uint(1)'))
239      eq(0, nvim_eval('msgpack#is_uint(-1)'))
240      eq(1, nvim_eval(('msgpack#is_uint(%s)'):format(sp('integer', '[1, 0, 0, 1]'))))
241      eq(0, nvim_eval(('msgpack#is_uint(%s)'):format(sp('integer', '[-1, 0, 0, 1]'))))
242      eq(0, nvim_eval(('msgpack#is_uint(%s)'):format(sp('float', '0.0'))))
243      eq(0, nvim_eval(('msgpack#is_uint(%s)'):format(sp('boolean', '0'))))
244      eq(0, nvim_eval(('msgpack#is_uint(%s)'):format(sp('nil', '0'))))
245      eq(0, nvim_eval('msgpack#is_uint("")'))
246    end)
247  end)
248
249  describe('function msgpack#strftime', function()
250    it('works', function()
251      local epoch = os.date('%Y-%m-%dT%H:%M:%S', 0)
252      eq(epoch, nvim_eval('msgpack#strftime("%Y-%m-%dT%H:%M:%S", 0)'))
253      eq(
254        epoch,
255        nvim_eval(
256          ('msgpack#strftime("%%Y-%%m-%%dT%%H:%%M:%%S", %s)'):format(sp('integer', '[1, 0, 0, 0]'))
257        )
258      )
259    end)
260  end)
261
262  describe('function msgpack#strptime', function()
263    it('works', function()
264      for _, v in ipairs({ 0, 10, 100000, 204, 1000000000 }) do
265        local time = os.date('%Y-%m-%dT%H:%M:%S', v)
266        eq(v, nvim_eval('msgpack#strptime("%Y-%m-%dT%H:%M:%S", ' .. '"' .. time .. '")'))
267      end
268    end)
269  end)
270
271  describe('function msgpack#type', function()
272    local type_eq = function(expected, val)
273      eq(expected, nvim_eval(('msgpack#type(%s)'):format(val)))
274    end
275
276    it('works for special dictionaries', function()
277      type_eq('string', sp('string', '[""]'))
278      type_eq('ext', sp('ext', '[1, [""]]'))
279      type_eq('array', sp('array', '[]'))
280      type_eq('map', sp('map', '[]'))
281      type_eq('integer', sp('integer', '[1, 0, 0, 0]'))
282      type_eq('float', sp('float', '0.0'))
283      type_eq('boolean', sp('boolean', '0'))
284      type_eq('nil', sp('nil', '0'))
285    end)
286
287    it('works for regular values', function()
288      type_eq('string', '""')
289      type_eq('array', '[]')
290      type_eq('map', '{}')
291      type_eq('integer', '1')
292      type_eq('float', '0.0')
293      type_eq('float', '(1.0/0.0)')
294      type_eq('float', '-(1.0/0.0)')
295      type_eq('float', '(1.0/0.0-1.0/0.0)')
296    end)
297  end)
298
299  describe('function msgpack#special_type', function()
300    local sp_type_eq = function(expected, val)
301      eq(expected, nvim_eval(('msgpack#special_type(%s)'):format(val)))
302    end
303
304    it('works for special dictionaries', function()
305      sp_type_eq('string', sp('string', '[""]'))
306      sp_type_eq('ext', sp('ext', '[1, [""]]'))
307      sp_type_eq('array', sp('array', '[]'))
308      sp_type_eq('map', sp('map', '[]'))
309      sp_type_eq('integer', sp('integer', '[1, 0, 0, 0]'))
310      sp_type_eq('float', sp('float', '0.0'))
311      sp_type_eq('boolean', sp('boolean', '0'))
312      sp_type_eq('nil', sp('nil', '0'))
313    end)
314
315    it('works for regular values', function()
316      sp_type_eq(0, '""')
317      sp_type_eq(0, '[]')
318      sp_type_eq(0, '{}')
319      sp_type_eq(0, '1')
320      sp_type_eq(0, '0.0')
321      sp_type_eq(0, '(1.0/0.0)')
322      sp_type_eq(0, '-(1.0/0.0)')
323      sp_type_eq(0, '(1.0/0.0-1.0/0.0)')
324    end)
325  end)
326
327  describe('function msgpack#string', function()
328    local string_eq = function(expected, val)
329      eq(expected, nvim_eval(('msgpack#string(%s)'):format(val)))
330    end
331
332    it('works for special dictionaries', function()
333      string_eq('""', sp('string', '[""]'))
334      string_eq('"\\n"', sp('string', '["", ""]'))
335      string_eq('"ab\\0c\\nde"', sp('string', '["ab\\nc", "de"]'))
336      string_eq('+(2)""', sp('ext', '[2, [""]]'))
337      string_eq('+(2)"\\n"', sp('ext', '[2, ["", ""]]'))
338      string_eq('+(2)"ab\\0c\\nde"', sp('ext', '[2, ["ab\\nc", "de"]]'))
339      string_eq('[]', sp('array', '[]'))
340      string_eq('[[[[{}]]]]', sp('array', '[[[[{}]]]]'))
341      string_eq('{}', sp('map', '[]'))
342      string_eq('{2: 10}', sp('map', '[[2, 10]]'))
343      string_eq(
344        '{{1: 1}: {1: 1}, {2: 1}: {1: 1}}',
345        mapsp(mapsp('2', '1'), mapsp('1', '1'), mapsp('1', '1'), mapsp('1', '1'))
346      )
347      string_eq(
348        '{{1: 1}: {1: 1}, {2: 1}: {1: 1}}',
349        mapsp(mapsp('1', '1'), mapsp('1', '1'), mapsp('2', '1'), mapsp('1', '1'))
350      )
351      string_eq(
352        '{[1, 2, {{1: 2}: 1}]: [1, 2, {{1: 2}: 1}]}',
353        mapsp(
354          ('[1, 2, %s]'):format(mapsp(mapsp('1', '2'), '1')),
355          ('[1, 2, %s]'):format(mapsp(mapsp('1', '2'), '1'))
356        )
357      )
358      string_eq('0x0000000000000000', sp('integer', '[1, 0, 0, 0]'))
359      string_eq('-0x0000000100000000', sp('integer', '[-1, 0, 2, 0]'))
360      string_eq('0x123456789abcdef0', sp('integer', '[ 1, 0,  610839793, 448585456]'))
361      string_eq('-0x123456789abcdef0', sp('integer', '[-1, 0,  610839793, 448585456]'))
362      string_eq('0xf23456789abcdef0', sp('integer', '[ 1, 3, 1684581617, 448585456]'))
363      string_eq('-0x723456789abcdef0', sp('integer', '[-1, 1, 1684581617, 448585456]'))
364      string_eq('0.0', sp('float', '0.0'))
365      string_eq('inf', sp('float', '(1.0/0.0)'))
366      string_eq('-inf', sp('float', '-(1.0/0.0)'))
367      string_eq('nan', sp('float', '(1.0/0.0-1.0/0.0)'))
368      string_eq('nan', sp('float', '-(1.0/0.0-1.0/0.0)'))
369      string_eq('FALSE', sp('boolean', '0'))
370      string_eq('TRUE', sp('boolean', '1'))
371      string_eq('NIL', sp('nil', '0'))
372    end)
373
374    it('works for regular values', function()
375      string_eq('""', '""')
376      string_eq('"\\n"', '"\\n"')
377      string_eq('[]', '[]')
378      string_eq('[[[{}]]]', '[[[{}]]]')
379      string_eq('{}', '{}')
380      string_eq('{"2": 10}', '{2: 10}')
381      string_eq('{"2": [{}]}', '{2: [{}]}')
382      string_eq('1', '1')
383      string_eq('0.0', '0.0')
384      string_eq('inf', '(1.0/0.0)')
385      string_eq('-inf', '-(1.0/0.0)')
386      string_eq('nan', '(1.0/0.0-1.0/0.0)')
387      string_eq('nan', '-(1.0/0.0-1.0/0.0)')
388    end)
389
390    it('works for special v: values like v:true', function()
391      string_eq('TRUE', 'v:true')
392      string_eq('FALSE', 'v:false')
393      string_eq('NIL', 'v:null')
394    end)
395  end)
396
397  describe('function msgpack#deepcopy', function()
398    it('works for special dictionaries', function()
399      nvim_command('let sparr = ' .. sp('array', '[[[]]]'))
400      nvim_command('let spmap = ' .. mapsp('"abc"', '[[]]'))
401      nvim_command('let spint = ' .. sp('integer', '[1, 0, 0, 0]'))
402      nvim_command('let spflt = ' .. sp('float', '1.0'))
403      nvim_command('let spext = ' .. sp('ext', '[2, ["abc", "def"]]'))
404      nvim_command('let spstr = ' .. sp('string', '["abc", "def"]'))
405      nvim_command('let spbln = ' .. sp('boolean', '0'))
406      nvim_command('let spnil = ' .. sp('nil', '0'))
407
408      nvim_command('let sparr2 = msgpack#deepcopy(sparr)')
409      nvim_command('let spmap2 = msgpack#deepcopy(spmap)')
410      nvim_command('let spint2 = msgpack#deepcopy(spint)')
411      nvim_command('let spflt2 = msgpack#deepcopy(spflt)')
412      nvim_command('let spext2 = msgpack#deepcopy(spext)')
413      nvim_command('let spstr2 = msgpack#deepcopy(spstr)')
414      nvim_command('let spbln2 = msgpack#deepcopy(spbln)')
415      nvim_command('let spnil2 = msgpack#deepcopy(spnil)')
416
417      eq('array', nvim_eval('msgpack#type(sparr2)'))
418      eq('map', nvim_eval('msgpack#type(spmap2)'))
419      eq('integer', nvim_eval('msgpack#type(spint2)'))
420      eq('float', nvim_eval('msgpack#type(spflt2)'))
421      eq('ext', nvim_eval('msgpack#type(spext2)'))
422      eq('string', nvim_eval('msgpack#type(spstr2)'))
423      eq('boolean', nvim_eval('msgpack#type(spbln2)'))
424      eq('nil', nvim_eval('msgpack#type(spnil2)'))
425
426      nvim_command('call add(sparr._VAL, 0)')
427      nvim_command('call add(sparr._VAL[0], 0)')
428      nvim_command('call add(sparr._VAL[0][0], 0)')
429      nvim_command('call add(spmap._VAL, [0, 0])')
430      nvim_command('call add(spmap._VAL[0][1], 0)')
431      nvim_command('call add(spmap._VAL[0][1][0], 0)')
432      nvim_command('let spint._VAL[1] = 1')
433      nvim_command('let spflt._VAL = 0.0')
434      nvim_command('let spext._VAL[0] = 3')
435      nvim_command('let spext._VAL[1][0] = "gh"')
436      nvim_command('let spstr._VAL[0] = "gh"')
437      nvim_command('let spbln._VAL = 1')
438      nvim_command('let spnil._VAL = 1')
439
440      eq({ _TYPE = {}, _VAL = { { {} } } }, nvim_eval('sparr2'))
441      eq({ _TYPE = {}, _VAL = { { 'abc', { {} } } } }, nvim_eval('spmap2'))
442      eq({ _TYPE = {}, _VAL = { 1, 0, 0, 0 } }, nvim_eval('spint2'))
443      eq({ _TYPE = {}, _VAL = 1.0 }, nvim_eval('spflt2'))
444      eq({ _TYPE = {}, _VAL = { 2, { 'abc', 'def' } } }, nvim_eval('spext2'))
445      eq({ _TYPE = {}, _VAL = { 'abc', 'def' } }, nvim_eval('spstr2'))
446      eq({ _TYPE = {}, _VAL = 0 }, nvim_eval('spbln2'))
447      eq({ _TYPE = {}, _VAL = 0 }, nvim_eval('spnil2'))
448
449      nvim_command('let sparr._TYPE = []')
450      nvim_command('let spmap._TYPE = []')
451      nvim_command('let spint._TYPE = []')
452      nvim_command('let spflt._TYPE = []')
453      nvim_command('let spext._TYPE = []')
454      nvim_command('let spstr._TYPE = []')
455      nvim_command('let spbln._TYPE = []')
456      nvim_command('let spnil._TYPE = []')
457
458      eq('array', nvim_eval('msgpack#special_type(sparr2)'))
459      eq('map', nvim_eval('msgpack#special_type(spmap2)'))
460      eq('integer', nvim_eval('msgpack#special_type(spint2)'))
461      eq('float', nvim_eval('msgpack#special_type(spflt2)'))
462      eq('ext', nvim_eval('msgpack#special_type(spext2)'))
463      eq('string', nvim_eval('msgpack#special_type(spstr2)'))
464      eq('boolean', nvim_eval('msgpack#special_type(spbln2)'))
465      eq('nil', nvim_eval('msgpack#special_type(spnil2)'))
466    end)
467
468    it('works for regular values', function()
469      nvim_command('let arr = [[[]]]')
470      nvim_command('let map = {1: {}}')
471      nvim_command('let int = 1')
472      nvim_command('let flt = 2.0')
473      nvim_command('let bin = "abc"')
474
475      nvim_command('let arr2 = msgpack#deepcopy(arr)')
476      nvim_command('let map2 = msgpack#deepcopy(map)')
477      nvim_command('let int2 = msgpack#deepcopy(int)')
478      nvim_command('let flt2 = msgpack#deepcopy(flt)')
479      nvim_command('let bin2 = msgpack#deepcopy(bin)')
480
481      eq('array', nvim_eval('msgpack#type(arr2)'))
482      eq('map', nvim_eval('msgpack#type(map2)'))
483      eq('integer', nvim_eval('msgpack#type(int2)'))
484      eq('float', nvim_eval('msgpack#type(flt2)'))
485      eq('string', nvim_eval('msgpack#type(bin2)'))
486
487      nvim_command('call add(arr, 0)')
488      nvim_command('call add(arr[0], 0)')
489      nvim_command('call add(arr[0][0], 0)')
490      nvim_command('let map.a = 1')
491      nvim_command('let map.1.a = 1')
492      nvim_command('let int = 2')
493      nvim_command('let flt = 3.0')
494      nvim_command('let bin = ""')
495
496      eq({ { {} } }, nvim_eval('arr2'))
497      eq({ ['1'] = {} }, nvim_eval('map2'))
498      eq(1, nvim_eval('int2'))
499      eq(2.0, nvim_eval('flt2'))
500      eq('abc', nvim_eval('bin2'))
501    end)
502
503    it('works for special v: values like v:true', function()
504      api.nvim_set_var('true', true)
505      api.nvim_set_var('false', false)
506      api.nvim_set_var('nil', NIL)
507
508      nvim_command('let true2 = msgpack#deepcopy(true)')
509      nvim_command('let false2 = msgpack#deepcopy(false)')
510      nvim_command('let nil2 = msgpack#deepcopy(nil)')
511
512      eq(true, api.nvim_get_var('true'))
513      eq(false, api.nvim_get_var('false'))
514      eq(NIL, api.nvim_get_var('nil'))
515    end)
516  end)
517
518  describe('function msgpack#eval', function()
519    local eval_eq = function(expected_type, expected_val, str, ...)
520      nvim_command(
521        ("let g:__val = msgpack#eval('%s', %s)"):format(str:gsub("'", "''"), select(1, ...) or '{}')
522      )
523      eq(expected_type, nvim_eval('msgpack#type(g:__val)'))
524      local expected_val_full = expected_val
525      if
526        not (({ float = true, integer = true })[expected_type] and type(expected_val) ~= 'table')
527        and expected_type ~= 'array'
528      then
529        expected_val_full = { _TYPE = {}, _VAL = expected_val_full }
530      end
531      if expected_val_full == expected_val_full then
532        eq(expected_val_full, nvim_eval('g:__val'))
533      else -- NaN
534        local nvim_nan = tostring(nvim_eval('g:__val'))
535        -- -NaN is a hardware-specific detail, there's no need to test for it.
536        -- Accept ether 'nan' or '-nan' as the response.
537        ok(nvim_nan == 'nan' or nvim_nan == '-nan')
538      end
539      nvim_command('unlet g:__val')
540    end
541
542    it('correctly loads strings', function()
543      eval_eq('string', { 'abcdef' }, '="abcdef"')
544      eval_eq('string', { 'abc', 'def' }, '="abc\\ndef"')
545      eval_eq('string', { 'abc\ndef' }, '="abc\\0def"')
546      eval_eq('string', { '\nabc\ndef\n' }, '="\\0abc\\0def\\0"')
547      eval_eq('string', { 'abc\n\n\ndef' }, '="abc\\0\\0\\0def"')
548      eval_eq('string', { 'abc\n', '\ndef' }, '="abc\\0\\n\\0def"')
549      eval_eq('string', { 'abc', '', '', 'def' }, '="abc\\n\\n\\ndef"')
550      eval_eq('string', { 'abc', '', '', 'def', '' }, '="abc\\n\\n\\ndef\\n"')
551      eval_eq('string', { '', 'abc', '', '', 'def' }, '="\\nabc\\n\\n\\ndef"')
552      eval_eq('string', { '' }, '=""')
553      eval_eq('string', { '"' }, '="\\""')
554      eval_eq('string', { 'py3 print(sys.version_info)' }, '="py3 print(sys.version_info)"')
555    end)
556
557    it('correctly loads ext values', function()
558      eval_eq('ext', { 0, { 'abcdef' } }, '+(0)"abcdef"')
559      eval_eq('ext', { 0, { 'abc', 'def' } }, '+(0)"abc\\ndef"')
560      eval_eq('ext', { 0, { 'abc\ndef' } }, '+(0)"abc\\0def"')
561      eval_eq('ext', { 0, { '\nabc\ndef\n' } }, '+(0)"\\0abc\\0def\\0"')
562      eval_eq('ext', { 0, { 'abc\n\n\ndef' } }, '+(0)"abc\\0\\0\\0def"')
563      eval_eq('ext', { 0, { 'abc\n', '\ndef' } }, '+(0)"abc\\0\\n\\0def"')
564      eval_eq('ext', { 0, { 'abc', '', '', 'def' } }, '+(0)"abc\\n\\n\\ndef"')
565      eval_eq('ext', { 0, { 'abc', '', '', 'def', '' } }, '+(0)"abc\\n\\n\\ndef\\n"')
566      eval_eq('ext', { 0, { '', 'abc', '', '', 'def' } }, '+(0)"\\nabc\\n\\n\\ndef"')
567      eval_eq('ext', { 0, { '' } }, '+(0)""')
568      eval_eq('ext', { 0, { '"' } }, '+(0)"\\""')
569
570      eval_eq('ext', { -1, { 'abcdef' } }, '+(-1)"abcdef"')
571      eval_eq('ext', { -1, { 'abc', 'def' } }, '+(-1)"abc\\ndef"')
572      eval_eq('ext', { -1, { 'abc\ndef' } }, '+(-1)"abc\\0def"')
573      eval_eq('ext', { -1, { '\nabc\ndef\n' } }, '+(-1)"\\0abc\\0def\\0"')
574      eval_eq('ext', { -1, { 'abc\n\n\ndef' } }, '+(-1)"abc\\0\\0\\0def"')
575      eval_eq('ext', { -1, { 'abc\n', '\ndef' } }, '+(-1)"abc\\0\\n\\0def"')
576      eval_eq('ext', { -1, { 'abc', '', '', 'def' } }, '+(-1)"abc\\n\\n\\ndef"')
577      eval_eq('ext', { -1, { 'abc', '', '', 'def', '' } }, '+(-1)"abc\\n\\n\\ndef\\n"')
578      eval_eq('ext', { -1, { '', 'abc', '', '', 'def' } }, '+(-1)"\\nabc\\n\\n\\ndef"')
579      eval_eq('ext', { -1, { '' } }, '+(-1)""')
580      eval_eq('ext', { -1, { '"' } }, '+(-1)"\\""')
581
582      eval_eq(
583        'ext',
584        { 42, { 'py3 print(sys.version_info)' } },
585        '+(42)"py3 print(sys.version_info)"'
586      )
587    end)
588
589    it('correctly loads floats', function()
590      eval_eq('float', inf, 'inf')
591      eval_eq('float', minus_inf, '-inf')
592      eval_eq('float', nan, 'nan')
593      eval_eq('float', 1.0e10, '1.0e10')
594      eval_eq('float', 1.0e10, '1.0e+10')
595      eval_eq('float', -1.0e10, '-1.0e+10')
596      eval_eq('float', 1.0, '1.0')
597      eval_eq('float', -1.0, '-1.0')
598      eval_eq('float', 1.0e-10, '1.0e-10')
599      eval_eq('float', -1.0e-10, '-1.0e-10')
600    end)
601
602    it('correctly loads integers', function()
603      eval_eq('integer', 10, '10')
604      eval_eq('integer', -10, '-10')
605      eval_eq('integer', { 1, 0, 610839793, 448585456 }, ' 0x123456789ABCDEF0')
606      eval_eq('integer', { -1, 0, 610839793, 448585456 }, '-0x123456789ABCDEF0')
607      eval_eq('integer', { 1, 3, 1684581617, 448585456 }, ' 0xF23456789ABCDEF0')
608      eval_eq('integer', { -1, 1, 1684581617, 448585456 }, '-0x723456789ABCDEF0')
609      eval_eq('integer', { 1, 0, 0, 0x100 }, '0x100')
610      eval_eq('integer', { -1, 0, 0, 0x100 }, '-0x100')
611
612      eval_eq('integer', ('a'):byte(), "'a'")
613      eval_eq('integer', 0xAB, "'«'")
614      eval_eq('integer', 0, "'\\0'")
615      eval_eq('integer', 10246567, "'\\10246567'")
616    end)
617
618    it('correctly loads constants', function()
619      eval_eq('boolean', 1, 'TRUE')
620      eval_eq('boolean', 0, 'FALSE')
621      eval_eq('nil', 0, 'NIL')
622      eval_eq('nil', 0, 'NIL', '{"NIL": 1, "nan": 2, "T": 3}')
623      eval_eq('float', nan, 'nan', '{"NIL": "1", "nan": "2", "T": "3"}')
624      eval_eq('integer', 3, 'T', '{"NIL": "1", "nan": "2", "T": "3"}')
625      eval_eq(
626        'integer',
627        { 1, 0, 0, 0 },
628        'T',
629        ('{"NIL": "1", "nan": "2", "T": \'%s\'}'):format(sp('integer', '[1, 0, 0, 0]'))
630      )
631    end)
632
633    it('correctly loads maps', function()
634      eval_eq('map', {}, '{}')
635      eval_eq(
636        'map',
637        { { { _TYPE = {}, _VAL = { { 1, 2 } } }, { _TYPE = {}, _VAL = { { 3, 4 } } } } },
638        '{{1: 2}: {3: 4}}'
639      )
640      eval_eq(
641        'map',
642        { { { _TYPE = {}, _VAL = { { 1, 2 } } }, { _TYPE = {}, _VAL = { { 3, 4 } } } }, { 1, 2 } },
643        '{{1: 2}: {3: 4}, 1: 2}'
644      )
645
646      eval_eq('map', {
647        {
648          {
649            _TYPE = {},
650            _VAL = {
651              { { _TYPE = {}, _VAL = { 'py3 print(sys.version_info)' } }, 2 },
652            },
653          },
654          { _TYPE = {}, _VAL = { { 3, 4 } } },
655        },
656        { 1, 2 },
657      }, '{{"py3 print(sys.version_info)": 2}: {3: 4}, 1: 2}')
658    end)
659
660    it('correctly loads arrays', function()
661      eval_eq('array', {}, '[]')
662      eval_eq('array', { 1 }, '[1]')
663      eval_eq('array', { { _TYPE = {}, _VAL = 1 } }, '[TRUE]')
664      eval_eq(
665        'array',
666        { { { _TYPE = {}, _VAL = { { 1, 2 } } } }, { _TYPE = {}, _VAL = { { 3, 4 } } } },
667        '[[{1: 2}], {3: 4}]'
668      )
669
670      eval_eq(
671        'array',
672        { { _TYPE = {}, _VAL = { 'py3 print(sys.version_info)' } } },
673        '["py3 print(sys.version_info)"]'
674      )
675    end)
676
677    it('errors out when needed', function()
678      t.matches(
679        'empty:Parsed string is empty',
680        pcall_err(nvim_command, 'call msgpack#eval("", {})')
681      )
682      t.matches(
683        'unknown:Invalid non%-space character: %^',
684        pcall_err(nvim_command, 'call msgpack#eval("^", {})')
685      )
686      t.matches(
687        "char%-invalid:Invalid integer character literal format: ''",
688        pcall_err(nvim_command, 'call msgpack#eval("\'\'", {})')
689      )
690      t.matches(
691        "char%-invalid:Invalid integer character literal format: 'ab'",
692        pcall_err(nvim_command, 'call msgpack#eval("\'ab\'", {})')
693      )
694      t.matches(
695        "char%-invalid:Invalid integer character literal format: '",
696        pcall_err(nvim_command, 'call msgpack#eval("\'", {})')
697      )
698      t.matches(
699        '"%-invalid:Invalid string: "',
700        pcall_err(nvim_command, 'call msgpack#eval("\\"", {})')
701      )
702      t.matches(
703        '"%-invalid:Invalid string: ="',
704        pcall_err(nvim_command, 'call msgpack#eval("=\\"", {})')
705      )
706      t.matches(
707        '"%-invalid:Invalid string: %+%(0%)"',
708        pcall_err(nvim_command, 'call msgpack#eval("+(0)\\"", {})')
709      )
710      t.matches(
711        '0%.%-nodigits:Decimal dot must be followed by digit%(s%): %.e1',
712        pcall_err(nvim_command, 'call msgpack#eval("0.e1", {})')
713      )
714      t.matches(
715        '0x%-long:Must have at most 16 hex digits: FEDCBA98765432100',
716        pcall_err(nvim_command, 'call msgpack#eval("0xFEDCBA98765432100", {})')
717      )
718      t.matches(
719        '0x%-empty:Must have number after 0x: ',
720        pcall_err(nvim_command, 'call msgpack#eval("0x", {})')
721      )
722      t.matches(
723        'name%-unknown:Unknown name FOO: FOO',
724        pcall_err(nvim_command, 'call msgpack#eval("FOO", {})')
725      )
726
727      t.matches(
728        'name%-unknown:Unknown name py3: py3 print%(sys%.version_info%)',
729        pcall_err(nvim_command, 'call msgpack#eval("py3 print(sys.version_info)", {})')
730      )
731      t.matches(
732        'name%-unknown:Unknown name o: o',
733        pcall_err(nvim_command, 'call msgpack#eval("-info", {})')
734      )
735    end)
736  end)
737end)
738
739describe('autoload/msgpack.vim in an eastern timezone', function()
740  setup(function()
741    clear({
742      -- msgpack#strptime() prefers a Python implementation and only falls back to the
743      -- Vimscript one, which is the one under test, when no provider answers.
744      args = { '-u', 'NORC', '--cmd', 'let g:loaded_python3_provider = 0' },
745      -- POSIX TZ signs are inverted: GMT-14 is UTC+14, the easternmost offset.
746      env = { TZ = 'GMT-14' },
747    })
748  end)
749
750  describe('function msgpack#strptime', function()
751    it('works east of UTC+12 #7625', function()
752      eq(0, nvim_eval('has("python3")'))
753      for _, v in ipairs({ 0, 10, 100000, 204, 1000000000 }) do
754        local time = nvim_eval(('strftime("%%Y-%%m-%%dT%%H:%%M:%%S", %d)'):format(v))
755        eq(v, nvim_eval(('msgpack#strptime("%%Y-%%m-%%dT%%H:%%M:%%S", "%s")'):format(time)))
756      end
757    end)
758  end)
759end)