034_user_function_spec.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. -- Test for user functions.
  2. -- Also test an <expr> mapping calling a function.
  3. -- Also test that a builtin function cannot be replaced.
  4. -- Also test for regression when calling arbitrary expression.
  5. local n = require('test.functional.testnvim')()
  6. local feed, insert, source = n.feed, n.insert, n.source
  7. local clear, feed_command, expect = n.clear, n.feed_command, n.expect
  8. describe(
  9. 'user functions, expr-mappings, overwrite protected builtin functions and regression on calling expressions',
  10. function()
  11. setup(clear)
  12. it('are working', function()
  13. insert('here')
  14. source([[
  15. function Table(title, ...)
  16. let ret = a:title
  17. let idx = 1
  18. while idx <= a:0
  19. exe "let ret = ret . a:" . idx
  20. let idx = idx + 1
  21. endwhile
  22. return ret
  23. endfunction
  24. function Compute(n1, n2, divname)
  25. if a:n2 == 0
  26. return "fail"
  27. endif
  28. exe "let g:" . a:divname . " = ". a:n1 / a:n2
  29. return "ok"
  30. endfunction
  31. func Expr1()
  32. normal! v
  33. return "111"
  34. endfunc
  35. func Expr2()
  36. call search('XX', 'b')
  37. return "222"
  38. endfunc
  39. func ListItem()
  40. let g:counter += 1
  41. return g:counter . '. '
  42. endfunc
  43. func ListReset()
  44. let g:counter = 0
  45. return ''
  46. endfunc
  47. func FuncWithRef(a)
  48. unlet g:FuncRef
  49. return a:a
  50. endfunc
  51. let g:FuncRef=function("FuncWithRef")
  52. let counter = 0
  53. inoremap <expr> ( ListItem()
  54. inoremap <expr> [ ListReset()
  55. imap <expr> + Expr1()
  56. imap <expr> * Expr2()
  57. let retval = "nop"
  58. /^here
  59. ]])
  60. feed('C<C-R>=Table("xxx", 4, "asdf")<cr>')
  61. -- Using a actual space will not work as feed() calls dedent on the input.
  62. feed('<space><C-R>=Compute(45, 0, "retval")<cr>')
  63. feed('<space><C-R>=retval<cr>')
  64. feed('<space><C-R>=Compute(45, 5, "retval")<cr>')
  65. feed('<space><C-R>=retval<cr>')
  66. feed('<space><C-R>=g:FuncRef(333)<cr>')
  67. feed('<cr>')
  68. feed('XX+-XX<cr>')
  69. feed('---*---<cr>')
  70. feed('(one<cr>')
  71. feed('(two<cr>')
  72. feed('[(one again<esc>')
  73. feed_command('call append(line("$"), max([1, 2, 3]))')
  74. feed_command('call extend(g:, {"max": function("min")})')
  75. feed_command('call append(line("$"), max([1, 2, 3]))')
  76. feed_command('try')
  77. -- Regression: the first line below used to throw "E110: Missing ')'"
  78. -- Second is here just to prove that this line is correct when not
  79. -- skipping rhs of &&.
  80. feed_command([[ $put =(0&&(function('tr'))(1, 2, 3))]])
  81. feed_command([[ $put =(1&&(function('tr'))(1, 2, 3))]])
  82. feed_command('catch')
  83. feed_command([[ $put ='!!! Unexpected exception:']])
  84. feed_command(' $put =v:exception')
  85. feed_command('endtry')
  86. -- Assert buffer contents.
  87. expect([[
  88. xxx4asdf fail nop ok 9 333
  89. XX111-XX
  90. ---222---
  91. 1. one
  92. 2. two
  93. 1. one again
  94. 3
  95. 3
  96. 0
  97. 1]])
  98. end)
  99. end
  100. )