comparison src/testdir/test_vim9_func.vim @ 20279:49b50843e725 v8.2.0695

patch 8.2.0695: Vim9: cannot define a function inside a function Commit: https://github.com/vim/vim/commit/04b12697838b232b8b17c553ccc74cf1f1bdb81c Author: Bram Moolenaar <Bram@vim.org> Date: Mon May 4 23:24:44 2020 +0200 patch 8.2.0695: Vim9: cannot define a function inside a function Problem: Vim9: cannot define a function inside a function. Solution: Initial support for :def inside :def.
author Bram Moolenaar <Bram@vim.org>
date Mon, 04 May 2020 23:30:04 +0200
parents 350bb78345ba
children ab8c8fd0f868
comparison
equal deleted inserted replaced
20278:4148ba869078 20279:49b50843e725
1 " Test various aspects of the Vim9 script language. 1 " Test various aspects of the Vim9 script language.
2 2
3 source check.vim 3 source check.vim
4 source view_util.vim 4 source view_util.vim
5 5 source vim9.vim
6 " Check that "lines" inside ":def" results in an "error" message.
7 func CheckDefFailure(lines, error)
8 call writefile(['def Func()'] + a:lines + ['enddef'], 'Xdef')
9 call assert_fails('so Xdef', a:error, a:lines)
10 call delete('Xdef')
11 endfunc
12
13 func CheckScriptFailure(lines, error)
14 call writefile(a:lines, 'Xdef')
15 call assert_fails('so Xdef', a:error, a:lines)
16 call delete('Xdef')
17 endfunc
18 6
19 func Test_def_basic() 7 func Test_def_basic()
20 def SomeFunc(): string 8 def SomeFunc(): string
21 return 'yes' 9 return 'yes'
22 enddef 10 enddef
93 def Test_call_default_args() 81 def Test_call_default_args()
94 assert_equal('string', MyDefaultArgs()) 82 assert_equal('string', MyDefaultArgs())
95 assert_equal('one', MyDefaultArgs('one')) 83 assert_equal('one', MyDefaultArgs('one'))
96 assert_fails('call MyDefaultArgs("one", "two")', 'E118:') 84 assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
97 85
98 call CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef'], 'E1001:') 86 CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef'], 'E1001:')
99 call CheckScriptFailure(['def Func(arg: number = "text")', 'enddef'], 'E1013: argument 1: type mismatch, expected number but got string') 87 CheckScriptFailure(['def Func(arg: number = "text")', 'enddef'], 'E1013: argument 1: type mismatch, expected number but got string')
88 enddef
89
90 def Test_nested_function()
91 def Nested(arg: string): string
92 return 'nested ' .. arg
93 enddef
94 assert_equal('nested function', Nested('function'))
95
96 CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
100 enddef 97 enddef
101 98
102 func Test_call_default_args_from_func() 99 func Test_call_default_args_from_func()
103 call assert_equal('string', MyDefaultArgs()) 100 call assert_equal('string', MyDefaultArgs())
104 call assert_equal('one', MyDefaultArgs('one')) 101 call assert_equal('one', MyDefaultArgs('one'))
719 716
720 unlet g:UseArg 717 unlet g:UseArg
721 unlet g:UseVararg 718 unlet g:UseVararg
722 enddef 719 enddef
723 720
721 def Test_nested_closure()
722 let local = 'text'
723 def Closure(arg: string): string
724 return local .. arg
725 enddef
726 assert_equal('text!!!', Closure('!!!'))
727 enddef
728
724 729
725 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 730 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker