robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

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