minmax_functions_spec.lua 2.0 KB

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