neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

minmax_functions_spec.lua (2044B)


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