Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 1 | Test for user functions. |
| 2 | Also test an <expr> mapping calling a function. |
Bram Moolenaar | bdb6205 | 2012-07-16 17:31:53 +0200 | [diff] [blame] | 3 | Also test that a builtin function cannot be replaced. |
Bram Moolenaar | 0f8de8d | 2013-11-11 04:25:53 +0100 | [diff] [blame^] | 4 | Also test for regression when calling arbitrary expression. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5 | |
| 6 | STARTTEST |
| 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 Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 24 | :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 Moolenaar | bc42c1e | 2010-05-28 22:06:46 +0200 | [diff] [blame] | 40 | :func FuncWithRef(a) |
| 41 | : unlet g:FuncRef |
| 42 | : return a:a |
| 43 | :endfunc |
| 44 | :let g:FuncRef=function("FuncWithRef") |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 45 | :let counter = 0 |
| 46 | :inoremap <expr> ( ListItem() |
| 47 | :inoremap <expr> [ ListReset() |
| 48 | :imap <expr> + Expr1() |
| 49 | :imap <expr> * Expr2() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 50 | :let retval = "nop" |
| 51 | /^here |
| 52 | C=Table("xxx", 4, "asdf") |
| 53 | =Compute(45, 0, "retval") |
| 54 | =retval |
| 55 | =Compute(45, 5, "retval") |
| 56 | =retval |
Bram Moolenaar | bc42c1e | 2010-05-28 22:06:46 +0200 | [diff] [blame] | 57 | =g:FuncRef(333) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 58 | |
| 59 | XX+-XX |
| 60 | ---*--- |
| 61 | (one |
| 62 | (two |
Bram Moolenaar | bdb6205 | 2012-07-16 17:31:53 +0200 | [diff] [blame] | 63 | [(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 Moolenaar | 0f8de8d | 2013-11-11 04:25:53 +0100 | [diff] [blame^] | 66 | :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 Moolenaar | 9d2c8c1 | 2007-09-25 16:00:00 +0000 | [diff] [blame] | 77 | :delfunc Table |
| 78 | :delfunc Compute |
| 79 | :delfunc Expr1 |
| 80 | :delfunc Expr2 |
| 81 | :delfunc ListItem |
| 82 | :delfunc ListReset |
| 83 | :unlet retval counter |
| 84 | :q! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | ENDTEST |
| 86 | |
| 87 | here |