neovim

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

034_user_function_spec.lua (2967B)


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