sort_spec.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local eq = t.eq
  4. local NIL = vim.NIL
  5. local eval = n.eval
  6. local clear = n.clear
  7. local api = n.api
  8. local fn = n.fn
  9. local command = n.command
  10. local exc_exec = n.exc_exec
  11. local pcall_err = t.pcall_err
  12. before_each(clear)
  13. describe('sort()', function()
  14. it('errors out when sorting special values', function()
  15. eq(
  16. 'Vim(call):E362: Using a boolean value as a Float',
  17. exc_exec('call sort([v:true, v:false], "f")')
  18. )
  19. end)
  20. it('sorts “wrong” values between -0.0001 and 0.0001, preserving order', function()
  21. api.nvim_set_var('list', {
  22. true,
  23. false,
  24. NIL,
  25. {},
  26. { a = 42 },
  27. 'check',
  28. 0.0001,
  29. -0.0001,
  30. })
  31. command('call insert(g:list, function("tr"))')
  32. local error_lines = fn.split(fn.execute('silent! call sort(g:list, "f")'), '\n')
  33. local errors = {}
  34. for _, err in ipairs(error_lines) do
  35. errors[err] = true
  36. end
  37. eq({
  38. ['E362: Using a boolean value as a Float'] = true,
  39. ['E891: Using a Funcref as a Float'] = true,
  40. ['E892: Using a String as a Float'] = true,
  41. ['E893: Using a List as a Float'] = true,
  42. ['E894: Using a Dictionary as a Float'] = true,
  43. ['E907: Using a special value as a Float'] = true,
  44. }, errors)
  45. eq(
  46. "[-1.0e-4, function('tr'), v:true, v:false, v:null, [], {'a': 42}, 'check', 1.0e-4]",
  47. eval('string(g:list)')
  48. )
  49. end)
  50. it('can yield E702 and stop sorting after that', function()
  51. command([[
  52. function Cmp(a, b)
  53. if type(a:a) == type([]) || type(a:b) == type([])
  54. return []
  55. endif
  56. return (a:a > a:b) - (a:a < a:b)
  57. endfunction
  58. ]])
  59. eq(
  60. 'Vim(let):E745: Using a List as a Number',
  61. pcall_err(command, 'let sl = sort([1, 0, [], 3, 2], "Cmp")')
  62. )
  63. end)
  64. end)