034_user_function_spec.lua 3.0 KB

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