blob: 71ee5f63b2c9afca58bae8e16d784608090b9304 [file] [log] [blame]
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001Test for user functions.
2Also test an <expr> mapping calling a function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02003Also test that a builtin function cannot be replaced.
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01004Also test for regression when calling arbitrary expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6STARTTEST
7:so small.vim
8:function Table(title, ...)
9: let ret = a:title
10: let idx = 1
11: while idx <= a:0
12: exe "let ret = ret . a:" . idx
13: let idx = idx + 1
14: endwhile
15: return ret
16:endfunction
17:function Compute(n1, n2, divname)
18: if a:n2 == 0
19: return "fail"
20: endif
21: exe "let g:" . a:divname . " = ". a:n1 / a:n2
22: return "ok"
23:endfunction
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000024:func Expr1()
25: normal! v
26: return "111"
27:endfunc
28:func Expr2()
29: call search('XX', 'b')
30: return "222"
31:endfunc
32:func ListItem()
33: let g:counter += 1
34: return g:counter . '. '
35:endfunc
36:func ListReset()
37: let g:counter = 0
38: return ''
39:endfunc
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +020040:func FuncWithRef(a)
41: unlet g:FuncRef
42: return a:a
43:endfunc
44:let g:FuncRef=function("FuncWithRef")
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000045:let counter = 0
46:inoremap <expr> ( ListItem()
47:inoremap <expr> [ ListReset()
48:imap <expr> + Expr1()
49:imap <expr> * Expr2()
Bram Moolenaar071d4272004-06-13 20:20:40 +000050:let retval = "nop"
51/^here
52C=Table("xxx", 4, "asdf")
53 =Compute(45, 0, "retval")
54 =retval
55 =Compute(45, 5, "retval")
56 =retval
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +020057 =g:FuncRef(333)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000058
59XX+-XX
60---*---
61(one
62(two
Bram Moolenaarbdb62052012-07-16 17:31:53 +020063[(one again:call append(line('$'), max([1, 2, 3]))
64:call extend(g:, {'max': function('min')})
65:call append(line('$'), max([1, 2, 3]))
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010066:try
67: " Regression: the first line below used to throw ?E110: Missing ')'?
68: " Second is here just to prove that this line is correct when not skipping
69: " rhs of &&.
70: $put =(0&&(function('tr'))(1, 2, 3))
71: $put =(1&&(function('tr'))(1, 2, 3))
72:catch
73: $put ='!!! Unexpected exception:'
74: $put =v:exception
75:endtry
76:$-9,$w! test.out
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000077:delfunc Table
78:delfunc Compute
79:delfunc Expr1
80:delfunc Expr2
81:delfunc ListItem
82:delfunc ListReset
83:unlet retval counter
84:q!
Bram Moolenaar071d4272004-06-13 20:20:40 +000085ENDTEST
86
87here