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 | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4 | |
| 5 | STARTTEST |
| 6 | :so small.vim |
| 7 | :function Table(title, ...) |
| 8 | : let ret = a:title |
| 9 | : let idx = 1 |
| 10 | : while idx <= a:0 |
| 11 | : exe "let ret = ret . a:" . idx |
| 12 | : let idx = idx + 1 |
| 13 | : endwhile |
| 14 | : return ret |
| 15 | :endfunction |
| 16 | :function 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 | :endfunction |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 23 | :func Expr1() |
| 24 | : normal! v |
| 25 | : return "111" |
| 26 | :endfunc |
| 27 | :func Expr2() |
| 28 | : call search('XX', 'b') |
| 29 | : return "222" |
| 30 | :endfunc |
| 31 | :func ListItem() |
| 32 | : let g:counter += 1 |
| 33 | : return g:counter . '. ' |
| 34 | :endfunc |
| 35 | :func ListReset() |
| 36 | : let g:counter = 0 |
| 37 | : return '' |
| 38 | :endfunc |
Bram Moolenaar | bc42c1e | 2010-05-28 22:06:46 +0200 | [diff] [blame] | 39 | :func FuncWithRef(a) |
| 40 | : unlet g:FuncRef |
| 41 | : return a:a |
| 42 | :endfunc |
| 43 | :let g:FuncRef=function("FuncWithRef") |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 44 | :let counter = 0 |
| 45 | :inoremap <expr> ( ListItem() |
| 46 | :inoremap <expr> [ ListReset() |
| 47 | :imap <expr> + Expr1() |
| 48 | :imap <expr> * Expr2() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | :let retval = "nop" |
| 50 | /^here |
| 51 | C=Table("xxx", 4, "asdf") |
| 52 | =Compute(45, 0, "retval") |
| 53 | =retval |
| 54 | =Compute(45, 5, "retval") |
| 55 | =retval |
Bram Moolenaar | bc42c1e | 2010-05-28 22:06:46 +0200 | [diff] [blame] | 56 | =g:FuncRef(333) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 57 | |
| 58 | XX+-XX |
| 59 | ---*--- |
| 60 | (one |
| 61 | (two |
Bram Moolenaar | bdb6205 | 2012-07-16 17:31:53 +0200 | [diff] [blame^] | 62 | [(one again:call append(line('$'), max([1, 2, 3])) |
| 63 | :call extend(g:, {'max': function('min')}) |
| 64 | :call append(line('$'), max([1, 2, 3])) |
| 65 | :$-7,$w! test.out |
Bram Moolenaar | 9d2c8c1 | 2007-09-25 16:00:00 +0000 | [diff] [blame] | 66 | :delfunc Table |
| 67 | :delfunc Compute |
| 68 | :delfunc Expr1 |
| 69 | :delfunc Expr2 |
| 70 | :delfunc ListItem |
| 71 | :delfunc ListReset |
| 72 | :unlet retval counter |
| 73 | :q! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | ENDTEST |
| 75 | |
| 76 | here |