robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
2.0 KiB62 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, setup = t.describe, t.it, t.setup
5local eq = t.eq
6local eval = n.eval
7local command = n.command
8local clear = n.clear
9local fn = n.fn
10local pcall_err = t.pcall_err
11
12setup(clear)
13for _, func in ipairs({ 'min', 'max' }) do
14  describe(func .. '()', function()
15    it('gives a single error message when multiple values failed conversions', function()
16      eq(
17        'Vim(echo):E745: Using a List as a Number',
18        pcall_err(command, 'echo ' .. func .. '([-5, [], [], [], 5])')
19      )
20      eq(
21        'Vim(echo):E745: Using a List as a Number',
22        pcall_err(command, 'echo ' .. func .. '({1:-5, 2:[], 3:[], 4:[], 5:5})')
23      )
24      for errmsg, errinput in pairs({
25        ['Vim(echo):E745: Using a List as a Number'] = '[]',
26        ['Vim(echo):E805: Using a Float as a Number'] = '0.0',
27        ['Vim(echo):E703: Using a Funcref as a Number'] = 'function("tr")',
28        ['Vim(echo):E728: Using a Dictionary as a Number'] = '{}',
29      }) do
30        eq(errmsg, pcall_err(command, 'echo ' .. func .. '([' .. errinput .. '])'))
31        eq(errmsg, pcall_err(command, 'echo ' .. func .. '({1:' .. errinput .. '})'))
32      end
33    end)
34    it('works with arrays/dictionaries with zero items', function()
35      eq(0, fn[func]({}))
36      eq(0, eval(func .. '({})'))
37    end)
38    it('works with arrays/dictionaries with one item', function()
39      eq(5, fn[func]({ 5 }))
40      eq(5, fn[func]({ test = 5 }))
41    end)
42    it('works with NULL arrays/dictionaries', function()
43      eq(0, eval(func .. '(v:_null_list)'))
44      eq(0, eval(func .. '(v:_null_dict)'))
45    end)
46    it('errors out for invalid types', function()
47      for _, errinput in ipairs({
48        '1',
49        'v:true',
50        'v:false',
51        'v:null',
52        'function("tr")',
53        '""',
54      }) do
55        eq(
56          ('Vim(echo):E712: Argument of %s() must be a List or Dictionary'):format(func),
57          pcall_err(command, 'echo ' .. func .. '(' .. errinput .. ')')
58        )
59      end
60    end)
61  end)
62end