Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 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 | func Table(title, ...) |
| 7 | let ret = a:title |
| 8 | let idx = 1 |
| 9 | while idx <= a:0 |
| 10 | exe "let ret = ret . a:" . idx |
| 11 | let idx = idx + 1 |
| 12 | endwhile |
| 13 | return ret |
| 14 | endfunc |
| 15 | |
| 16 | func Compute(n1, n2, divname) |
| 17 | if a:n2 == 0 |
| 18 | return "fail" |
| 19 | endif |
| 20 | exe "let g:" . a:divname . " = ". a:n1 / a:n2 |
| 21 | return "ok" |
| 22 | endfunc |
| 23 | |
| 24 | func Expr1() |
| 25 | silent! normal! v |
| 26 | return "111" |
| 27 | endfunc |
| 28 | |
| 29 | func Expr2() |
| 30 | call search('XX', 'b') |
| 31 | return "222" |
| 32 | endfunc |
| 33 | |
| 34 | func ListItem() |
| 35 | let g:counter += 1 |
| 36 | return g:counter . '. ' |
| 37 | endfunc |
| 38 | |
| 39 | func ListReset() |
| 40 | let g:counter = 0 |
| 41 | return '' |
| 42 | endfunc |
| 43 | |
| 44 | func FuncWithRef(a) |
| 45 | unlet g:FuncRef |
| 46 | return a:a |
| 47 | endfunc |
| 48 | |
| 49 | func Test_user_func() |
| 50 | let g:FuncRef=function("FuncWithRef") |
| 51 | let g:counter = 0 |
| 52 | inoremap <expr> ( ListItem() |
| 53 | inoremap <expr> [ ListReset() |
| 54 | imap <expr> + Expr1() |
| 55 | imap <expr> * Expr2() |
| 56 | let g:retval = "nop" |
| 57 | |
| 58 | call assert_equal('xxx4asdf', Table("xxx", 4, "asdf")) |
| 59 | call assert_equal('fail', Compute(45, 0, "retval")) |
| 60 | call assert_equal('nop', g:retval) |
| 61 | call assert_equal('ok', Compute(45, 5, "retval")) |
| 62 | call assert_equal(9, g:retval) |
| 63 | call assert_equal(333, g:FuncRef(333)) |
| 64 | |
| 65 | enew |
| 66 | |
| 67 | normal oXX+-XX |
| 68 | call assert_equal('XX111-XX', getline('.')) |
| 69 | normal o---*--- |
| 70 | call assert_equal('---222---', getline('.')) |
| 71 | normal o(one |
| 72 | call assert_equal('1. one', getline('.')) |
| 73 | normal o(two |
| 74 | call assert_equal('2. two', getline('.')) |
| 75 | normal o[(one again |
| 76 | call assert_equal('1. one again', getline('.')) |
| 77 | |
| 78 | call assert_equal(3, max([1, 2, 3])) |
| 79 | call assert_fails("call extend(g:, {'max': function('min')})", 'E704') |
| 80 | call assert_equal(3, max([1, 2, 3])) |
| 81 | |
| 82 | " Regression: the first line below used to throw ?E110: Missing ')'? |
| 83 | " Second is here just to prove that this line is correct when not skipping |
| 84 | " rhs of &&. |
| 85 | call assert_equal(0, (0 && (function('tr'))(1, 2, 3))) |
| 86 | call assert_equal(1, (1 && (function('tr'))(1, 2, 3))) |
| 87 | |
| 88 | delfunc Table |
| 89 | delfunc Compute |
| 90 | delfunc Expr1 |
| 91 | delfunc Expr2 |
| 92 | delfunc ListItem |
| 93 | delfunc ListReset |
| 94 | unlet g:retval g:counter |
| 95 | enew! |
| 96 | endfunc |