2020-04-05 17:08:17 +02:00
|
|
|
" Test various aspects of the Vim9 script language.
|
|
|
|
|
|
|
|
source check.vim
|
2020-09-06 18:22:53 +02:00
|
|
|
source term_util.vim
|
2020-04-05 17:08:17 +02:00
|
|
|
source view_util.vim
|
2020-05-04 23:24:44 +02:00
|
|
|
source vim9.vim
|
2020-07-05 18:18:42 +02:00
|
|
|
source screendump.vim
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
func Test_def_basic()
|
|
|
|
def SomeFunc(): string
|
|
|
|
return 'yes'
|
|
|
|
enddef
|
2020-09-11 19:09:48 +02:00
|
|
|
call SomeFunc()->assert_equal('yes')
|
2020-04-05 17:08:17 +02:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
def ReturnString(): string
|
|
|
|
return 'string'
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def ReturnNumber(): number
|
|
|
|
return 123
|
|
|
|
enddef
|
|
|
|
|
|
|
|
let g:notNumber = 'string'
|
|
|
|
|
|
|
|
def ReturnGlobal(): number
|
|
|
|
return g:notNumber
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_return_something()
|
2020-09-11 19:09:48 +02:00
|
|
|
ReturnString()->assert_equal('string')
|
|
|
|
ReturnNumber()->assert_equal(123)
|
2020-09-16 15:22:00 +02:00
|
|
|
assert_fails('ReturnGlobal()', 'E1012: Type mismatch; expected number but got string', '', 1, 'ReturnGlobal')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-06-18 20:50:10 +02:00
|
|
|
def Test_missing_return()
|
|
|
|
CheckDefFailure(['def Missing(): number',
|
|
|
|
' if g:cond',
|
|
|
|
' echo "no return"',
|
|
|
|
' else',
|
|
|
|
' return 0',
|
|
|
|
' endif'
|
|
|
|
'enddef'], 'E1027:')
|
|
|
|
CheckDefFailure(['def Missing(): number',
|
|
|
|
' if g:cond',
|
|
|
|
' return 1',
|
|
|
|
' else',
|
|
|
|
' echo "no return"',
|
|
|
|
' endif'
|
|
|
|
'enddef'], 'E1027:')
|
|
|
|
CheckDefFailure(['def Missing(): number',
|
|
|
|
' if g:cond',
|
|
|
|
' return 1',
|
|
|
|
' else',
|
|
|
|
' return 2',
|
|
|
|
' endif'
|
|
|
|
' return 3'
|
|
|
|
'enddef'], 'E1095:')
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
let s:nothing = 0
|
|
|
|
def ReturnNothing()
|
|
|
|
s:nothing = 1
|
|
|
|
if true
|
|
|
|
return
|
|
|
|
endif
|
|
|
|
s:nothing = 2
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_return_nothing()
|
|
|
|
ReturnNothing()
|
2020-09-11 19:09:48 +02:00
|
|
|
s:nothing->assert_equal(1)
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
func Increment()
|
|
|
|
let g:counter += 1
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
def Test_call_ufunc_count()
|
|
|
|
g:counter = 1
|
|
|
|
Increment()
|
|
|
|
Increment()
|
|
|
|
Increment()
|
2020-07-17 20:36:00 +02:00
|
|
|
# works with and without :call
|
2020-09-11 19:09:48 +02:00
|
|
|
g:counter->assert_equal(4)
|
|
|
|
eval g:counter->assert_equal(4)
|
2020-04-05 17:08:17 +02:00
|
|
|
unlet g:counter
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def MyVarargs(arg: string, ...rest: list<string>): string
|
|
|
|
let res = arg
|
|
|
|
for s in rest
|
|
|
|
res ..= ',' .. s
|
|
|
|
endfor
|
|
|
|
return res
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_call_varargs()
|
2020-09-11 19:09:48 +02:00
|
|
|
MyVarargs('one')->assert_equal('one')
|
|
|
|
MyVarargs('one', 'two')->assert_equal('one,two')
|
|
|
|
MyVarargs('one', 'two', 'three')->assert_equal('one,two,three')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def MyDefaultArgs(name = 'string'): string
|
|
|
|
return name
|
|
|
|
enddef
|
|
|
|
|
2020-07-15 19:48:20 +02:00
|
|
|
def MyDefaultSecond(name: string, second: bool = true): string
|
|
|
|
return second ? name : 'none'
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_call_default_args()
|
2020-09-11 19:09:48 +02:00
|
|
|
MyDefaultArgs()->assert_equal('string')
|
|
|
|
MyDefaultArgs('one')->assert_equal('one')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
MyDefaultSecond('test')->assert_equal('test')
|
|
|
|
MyDefaultSecond('test', true)->assert_equal('test')
|
|
|
|
MyDefaultSecond('test', false)->assert_equal('none')
|
2020-07-15 19:48:20 +02:00
|
|
|
|
2020-05-24 23:00:18 +02:00
|
|
|
CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: Argument 1: type mismatch, expected number but got string')
|
2020-05-04 23:24:44 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_nested_function()
|
|
|
|
def Nested(arg: string): string
|
|
|
|
return 'nested ' .. arg
|
|
|
|
enddef
|
2020-09-11 19:09:48 +02:00
|
|
|
Nested('function')->assert_equal('nested function')
|
2020-05-04 23:24:44 +02:00
|
|
|
|
2020-05-05 17:53:16 +02:00
|
|
|
CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
|
|
|
|
CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
|
|
|
|
|
2020-05-04 23:24:44 +02:00
|
|
|
CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
|
2020-08-01 22:35:13 +02:00
|
|
|
CheckDefFailure(['def s:Nested()', 'enddef'], 'E1075:')
|
|
|
|
CheckDefFailure(['def b:Nested()', 'enddef'], 'E1075:')
|
2020-09-10 22:28:01 +02:00
|
|
|
|
|
|
|
CheckDefFailure([
|
|
|
|
'def Outer()',
|
|
|
|
' def Inner()',
|
|
|
|
' # comment',
|
|
|
|
' enddef',
|
|
|
|
' def Inner()',
|
|
|
|
' enddef',
|
|
|
|
'enddef'], 'E1073:')
|
|
|
|
CheckDefFailure([
|
|
|
|
'def Outer()',
|
|
|
|
' def Inner()',
|
|
|
|
' # comment',
|
|
|
|
' enddef',
|
|
|
|
' def! Inner()',
|
|
|
|
' enddef',
|
|
|
|
'enddef'], 'E1117:')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-01 00:03:09 +02:00
|
|
|
func Test_call_default_args_from_func()
|
2020-09-11 19:09:48 +02:00
|
|
|
call MyDefaultArgs()->assert_equal('string')
|
|
|
|
call MyDefaultArgs('one')->assert_equal('one')
|
2020-09-06 21:47:48 +02:00
|
|
|
call assert_fails('call MyDefaultArgs("one", "two")', 'E118:', '', 3, 'Test_call_default_args_from_func')
|
2020-08-01 00:03:09 +02:00
|
|
|
endfunc
|
|
|
|
|
2020-07-31 22:05:04 +02:00
|
|
|
def Test_nested_global_function()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Outer()
|
|
|
|
def g:Inner(): string
|
|
|
|
return 'inner'
|
|
|
|
enddef
|
|
|
|
enddef
|
2020-08-01 00:03:09 +02:00
|
|
|
defcompile
|
|
|
|
Outer()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Inner()->assert_equal('inner')
|
2020-08-01 00:03:09 +02:00
|
|
|
delfunc g:Inner
|
|
|
|
Outer()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Inner()->assert_equal('inner')
|
2020-08-01 00:03:09 +02:00
|
|
|
delfunc g:Inner
|
|
|
|
Outer()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Inner()->assert_equal('inner')
|
2020-08-01 00:03:09 +02:00
|
|
|
delfunc g:Inner
|
2020-07-31 22:05:04 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-08-01 18:57:52 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Outer()
|
|
|
|
def g:Inner(): string
|
|
|
|
return 'inner'
|
|
|
|
enddef
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
Outer()
|
|
|
|
Outer()
|
|
|
|
END
|
|
|
|
CheckScriptFailure(lines, "E122:")
|
2020-08-01 23:22:18 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func()
|
|
|
|
echo 'script'
|
|
|
|
enddef
|
|
|
|
def Outer()
|
|
|
|
def Func()
|
|
|
|
echo 'inner'
|
|
|
|
enddef
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
|
|
|
CheckScriptFailure(lines, "E1073:")
|
2020-07-31 22:05:04 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-01 18:53:07 +02:00
|
|
|
def Test_global_local_function()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def g:Func(): string
|
|
|
|
return 'global'
|
|
|
|
enddef
|
|
|
|
def Func(): string
|
|
|
|
return 'local'
|
|
|
|
enddef
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Func()->assert_equal('global')
|
|
|
|
Func()->assert_equal('local')
|
2020-08-01 18:53:07 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-08-11 22:30:42 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def g:Funcy()
|
|
|
|
echo 'funcy'
|
|
|
|
enddef
|
|
|
|
s:Funcy()
|
|
|
|
END
|
|
|
|
CheckScriptFailure(lines, 'E117:')
|
2020-08-01 18:53:07 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-12 18:32:34 +02:00
|
|
|
def Test_local_function_shadows_global()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def g:Gfunc(): string
|
|
|
|
return 'global'
|
|
|
|
enddef
|
|
|
|
def AnotherFunc(): number
|
|
|
|
let Gfunc = function('len')
|
|
|
|
return Gfunc('testing')
|
|
|
|
enddef
|
|
|
|
g:Gfunc()->assert_equal('global')
|
|
|
|
AnotherFunc()->assert_equal(7)
|
|
|
|
delfunc g:Gfunc
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def g:Func(): string
|
|
|
|
return 'global'
|
|
|
|
enddef
|
|
|
|
def AnotherFunc()
|
|
|
|
g:Func = function('len')
|
|
|
|
enddef
|
|
|
|
AnotherFunc()
|
|
|
|
END
|
|
|
|
CheckScriptFailure(lines, 'E705:')
|
|
|
|
delfunc g:Func
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
func TakesOneArg(arg)
|
|
|
|
echo a:arg
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
def Test_call_wrong_args()
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckDefFailure(['TakesOneArg()'], 'E119:')
|
|
|
|
CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
|
|
|
|
CheckDefFailure(['bufnr(xxx)'], 'E1001:')
|
|
|
|
CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
|
2020-08-28 17:19:07 +02:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(s: string)
|
|
|
|
echo s
|
|
|
|
enddef
|
|
|
|
Func([])
|
|
|
|
END
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got list<unknown>', 5)
|
2020-09-18 22:42:00 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def FuncOne(nr: number)
|
|
|
|
echo nr
|
|
|
|
enddef
|
|
|
|
def FuncTwo()
|
|
|
|
FuncOne()
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
|
|
|
writefile(lines, 'Xscript')
|
|
|
|
let didCatch = false
|
|
|
|
try
|
|
|
|
source Xscript
|
|
|
|
catch
|
|
|
|
assert_match('E119: Not enough arguments for function: <SNR>\d\+_FuncOne', v:exception)
|
|
|
|
assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
|
|
|
|
didCatch = true
|
|
|
|
endtry
|
|
|
|
assert_true(didCatch)
|
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def FuncOne(nr: number)
|
|
|
|
echo nr
|
|
|
|
enddef
|
|
|
|
def FuncTwo()
|
|
|
|
FuncOne(1, 2)
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
|
|
|
writefile(lines, 'Xscript')
|
|
|
|
didCatch = false
|
|
|
|
try
|
|
|
|
source Xscript
|
|
|
|
catch
|
|
|
|
assert_match('E118: Too many arguments for function: <SNR>\d\+_FuncOne', v:exception)
|
|
|
|
assert_match('Xscript\[8\]..function <SNR>\d\+_FuncTwo, line 1', v:throwpoint)
|
|
|
|
didCatch = true
|
|
|
|
endtry
|
|
|
|
assert_true(didCatch)
|
|
|
|
|
|
|
|
delete('Xscript')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
" Default arg and varargs
|
|
|
|
def MyDefVarargs(one: string, two = 'foo', ...rest: list<string>): string
|
|
|
|
let res = one .. ',' .. two
|
|
|
|
for s in rest
|
|
|
|
res ..= ',' .. s
|
|
|
|
endfor
|
|
|
|
return res
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_call_def_varargs()
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('MyDefVarargs()', 'E119:', '', 1, 'Test_call_def_varargs')
|
2020-09-11 19:09:48 +02:00
|
|
|
MyDefVarargs('one')->assert_equal('one,foo')
|
|
|
|
MyDefVarargs('one', 'two')->assert_equal('one,two')
|
|
|
|
MyDefVarargs('one', 'two', 'three')->assert_equal('one,two,three')
|
2020-07-25 16:33:02 +02:00
|
|
|
CheckDefFailure(['MyDefVarargs("one", 22)'],
|
2020-09-16 17:55:40 +02:00
|
|
|
'E1013: Argument 2: type mismatch, expected string but got number')
|
2020-07-25 16:33:02 +02:00
|
|
|
CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
|
2020-09-16 17:55:40 +02:00
|
|
|
'E1013: Argument 3: type mismatch, expected string but got number')
|
2020-07-25 16:33:02 +02:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: list<string>)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func('a', 'b', 'c')
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: list<string>)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func()
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
|
|
|
|
2020-09-16 17:22:59 +02:00
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: any)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func(0)
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
|
|
|
|
2020-07-25 16:33:02 +02:00
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: list<string>)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func(1, 2, 3)
|
|
|
|
END
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
|
2020-07-25 16:33:02 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: list<string>)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func('a', 9)
|
|
|
|
END
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(lines, 'E1013: Argument 2: type mismatch')
|
2020-07-25 16:33:02 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func(...l: list<string>)
|
|
|
|
echo l
|
|
|
|
enddef
|
|
|
|
Func(1, 'a')
|
|
|
|
END
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-12 14:21:11 +02:00
|
|
|
def Test_call_call()
|
|
|
|
let l = [3, 2, 1]
|
|
|
|
call('reverse', [l])
|
2020-09-11 19:09:48 +02:00
|
|
|
l->assert_equal([1, 2, 3])
|
2020-08-12 14:21:11 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-11 20:50:33 +02:00
|
|
|
let s:value = ''
|
|
|
|
|
|
|
|
def FuncOneDefArg(opt = 'text')
|
|
|
|
s:value = opt
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def FuncTwoDefArg(nr = 123, opt = 'text'): string
|
|
|
|
return nr .. opt
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def FuncVarargs(...arg: list<string>): string
|
|
|
|
return join(arg, ',')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_func_type_varargs()
|
|
|
|
let RefDefArg: func(?string)
|
|
|
|
RefDefArg = FuncOneDefArg
|
|
|
|
RefDefArg()
|
2020-09-11 19:09:48 +02:00
|
|
|
s:value->assert_equal('text')
|
2020-04-11 20:50:33 +02:00
|
|
|
RefDefArg('some')
|
2020-09-11 19:09:48 +02:00
|
|
|
s:value->assert_equal('some')
|
2020-04-11 20:50:33 +02:00
|
|
|
|
|
|
|
let RefDef2Arg: func(?number, ?string): string
|
|
|
|
RefDef2Arg = FuncTwoDefArg
|
2020-09-11 19:09:48 +02:00
|
|
|
RefDef2Arg()->assert_equal('123text')
|
|
|
|
RefDef2Arg(99)->assert_equal('99text')
|
|
|
|
RefDef2Arg(77, 'some')->assert_equal('77some')
|
2020-04-11 20:50:33 +02:00
|
|
|
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckDefFailure(['let RefWrong: func(string?)'], 'E1010:')
|
|
|
|
CheckDefFailure(['let RefWrong: func(?string, string)'], 'E1007:')
|
2020-04-11 20:50:33 +02:00
|
|
|
|
|
|
|
let RefVarargs: func(...list<string>): string
|
|
|
|
RefVarargs = FuncVarargs
|
2020-09-11 19:09:48 +02:00
|
|
|
RefVarargs()->assert_equal('')
|
|
|
|
RefVarargs('one')->assert_equal('one')
|
|
|
|
RefVarargs('one', 'two')->assert_equal('one,two')
|
2020-04-11 20:50:33 +02:00
|
|
|
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckDefFailure(['let RefWrong: func(...list<string>, string)'], 'E110:')
|
|
|
|
CheckDefFailure(['let RefWrong: func(...list<string>, ?string)'], 'E110:')
|
2020-04-11 20:50:33 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-07 22:05:08 +02:00
|
|
|
" Only varargs
|
|
|
|
def MyVarargsOnly(...args: list<string>): string
|
|
|
|
return join(args, ',')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_call_varargs_only()
|
2020-09-11 19:09:48 +02:00
|
|
|
MyVarargsOnly()->assert_equal('')
|
|
|
|
MyVarargsOnly('one')->assert_equal('one')
|
|
|
|
MyVarargsOnly('one', 'two')->assert_equal('one,two')
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckDefFailure(['MyVarargsOnly(1)'], 'E1013: Argument 1: type mismatch, expected string but got number')
|
|
|
|
CheckDefFailure(['MyVarargsOnly("one", 2)'], 'E1013: Argument 2: type mismatch, expected string but got number')
|
2020-04-07 22:05:08 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_using_var_as_arg()
|
2020-09-06 15:58:36 +02:00
|
|
|
writefile(['def Func(x: number)', 'let x = 234', 'enddef', 'defcompile'], 'Xdef')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('so Xdef', 'E1006:', '', 1, 'Func')
|
2020-09-06 15:58:36 +02:00
|
|
|
delete('Xdef')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-05-10 22:53:56 +02:00
|
|
|
def DictArg(arg: dict<string>)
|
|
|
|
arg['key'] = 'value'
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def ListArg(arg: list<string>)
|
|
|
|
arg[0] = 'value'
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_assign_to_argument()
|
2020-07-17 20:36:00 +02:00
|
|
|
# works for dict and list
|
2020-05-10 22:53:56 +02:00
|
|
|
let d: dict<string> = {}
|
|
|
|
DictArg(d)
|
2020-09-11 19:09:48 +02:00
|
|
|
d['key']->assert_equal('value')
|
2020-05-10 22:53:56 +02:00
|
|
|
let l: list<string> = []
|
|
|
|
ListArg(l)
|
2020-09-11 19:09:48 +02:00
|
|
|
l[0]->assert_equal('value')
|
2020-05-10 22:53:56 +02:00
|
|
|
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckScriptFailure(['def Func(arg: number)', 'arg = 3', 'enddef', 'defcompile'], 'E1090:')
|
2020-05-10 22:53:56 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-20 22:04:00 +02:00
|
|
|
" These argument names are reserved in legacy functions.
|
|
|
|
def WithReservedNames(firstline: string, lastline: string): string
|
|
|
|
return firstline .. lastline
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_argument_names()
|
|
|
|
assert_equal('OK', WithReservedNames('O', 'K'))
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_call_func_defined_later()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:DefinedLater('one')->assert_equal('one')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('NotDefined("one")', 'E117:', '', 2, 'Test_call_func_defined_later')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-23 18:13:23 +02:00
|
|
|
func DefinedLater(arg)
|
|
|
|
return a:arg
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
def Test_call_funcref()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:SomeFunc('abc')->assert_equal(3)
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('NotAFunc()', 'E117:', '', 2, 'Test_call_funcref') # comment after call
|
|
|
|
assert_fails('g:NotAFunc()', 'E117:', '', 3, 'Test_call_funcref')
|
2020-07-22 19:30:06 +02:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def RetNumber(): number
|
|
|
|
return 123
|
|
|
|
enddef
|
|
|
|
let Funcref: func: number = function('RetNumber')
|
2020-09-11 19:09:48 +02:00
|
|
|
Funcref()->assert_equal(123)
|
2020-07-22 19:30:06 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-07-22 20:16:11 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def RetNumber(): number
|
|
|
|
return 123
|
|
|
|
enddef
|
|
|
|
def Bar(F: func: number): number
|
|
|
|
return F()
|
|
|
|
enddef
|
|
|
|
let Funcref = function('RetNumber')
|
2020-09-11 19:09:48 +02:00
|
|
|
Bar(Funcref)->assert_equal(123)
|
2020-07-22 20:16:11 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-07-23 20:09:10 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def UseNumber(nr: number)
|
|
|
|
echo nr
|
|
|
|
enddef
|
|
|
|
let Funcref: func(number) = function('UseNumber')
|
|
|
|
Funcref(123)
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-07-23 20:56:04 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def UseNumber(nr: number)
|
|
|
|
echo nr
|
|
|
|
enddef
|
|
|
|
let Funcref: func(string) = function('UseNumber')
|
|
|
|
END
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckScriptFailure(lines, 'E1012: Type mismatch; expected func(string) but got func(number)')
|
2020-07-26 17:56:25 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def EchoNr(nr = 34)
|
|
|
|
g:echo = nr
|
|
|
|
enddef
|
|
|
|
let Funcref: func(?number) = function('EchoNr')
|
|
|
|
Funcref()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:echo->assert_equal(34)
|
2020-07-26 17:56:25 +02:00
|
|
|
Funcref(123)
|
2020-09-11 19:09:48 +02:00
|
|
|
g:echo->assert_equal(123)
|
2020-07-26 17:56:25 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-07-26 18:16:58 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def EchoList(...l: list<number>)
|
|
|
|
g:echo = l
|
|
|
|
enddef
|
|
|
|
let Funcref: func(...list<number>) = function('EchoList')
|
|
|
|
Funcref()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:echo->assert_equal([])
|
2020-07-26 18:16:58 +02:00
|
|
|
Funcref(1, 2, 3)
|
2020-09-11 19:09:48 +02:00
|
|
|
g:echo->assert_equal([1, 2, 3])
|
2020-07-26 18:16:58 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-07-26 18:33:09 +02:00
|
|
|
|
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def OptAndVar(nr: number, opt = 12, ...l: list<number>): number
|
|
|
|
g:optarg = opt
|
|
|
|
g:listarg = l
|
|
|
|
return nr
|
|
|
|
enddef
|
|
|
|
let Funcref: func(number, ?number, ...list<number>): number = function('OptAndVar')
|
2020-09-11 19:09:48 +02:00
|
|
|
Funcref(10)->assert_equal(10)
|
|
|
|
g:optarg->assert_equal(12)
|
|
|
|
g:listarg->assert_equal([])
|
2020-07-26 18:33:09 +02:00
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
Funcref(11, 22)->assert_equal(11)
|
|
|
|
g:optarg->assert_equal(22)
|
|
|
|
g:listarg->assert_equal([])
|
2020-07-26 18:33:09 +02:00
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
Funcref(17, 18, 1, 2, 3)->assert_equal(17)
|
|
|
|
g:optarg->assert_equal(18)
|
|
|
|
g:listarg->assert_equal([1, 2, 3])
|
2020-07-26 18:33:09 +02:00
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-04-23 18:13:23 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
let SomeFunc = function('len')
|
|
|
|
let NotAFunc = 'text'
|
|
|
|
|
2020-04-12 14:39:53 +02:00
|
|
|
def CombineFuncrefTypes()
|
2020-07-17 20:36:00 +02:00
|
|
|
# same arguments, different return type
|
2020-04-12 14:39:53 +02:00
|
|
|
let Ref1: func(bool): string
|
|
|
|
let Ref2: func(bool): number
|
|
|
|
let Ref3: func(bool): any
|
|
|
|
Ref3 = g:cond ? Ref1 : Ref2
|
|
|
|
|
2020-07-17 20:36:00 +02:00
|
|
|
# different number of arguments
|
2020-04-12 14:39:53 +02:00
|
|
|
let Refa1: func(bool): number
|
|
|
|
let Refa2: func(bool, number): number
|
|
|
|
let Refa3: func: number
|
|
|
|
Refa3 = g:cond ? Refa1 : Refa2
|
|
|
|
|
2020-07-17 20:36:00 +02:00
|
|
|
# different argument types
|
2020-04-12 14:39:53 +02:00
|
|
|
let Refb1: func(bool, string): number
|
|
|
|
let Refb2: func(string, number): number
|
|
|
|
let Refb3: func(any, any): number
|
|
|
|
Refb3 = g:cond ? Refb1 : Refb2
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def FuncWithForwardCall()
|
2020-04-23 18:13:23 +02:00
|
|
|
return g:DefinedEvenLater("yes")
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def DefinedEvenLater(arg: string): string
|
|
|
|
return arg
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_error_in_nested_function()
|
2020-07-17 20:36:00 +02:00
|
|
|
# Error in called function requires unwinding the call stack.
|
2020-09-06 22:26:57 +02:00
|
|
|
assert_fails('FuncWithForwardCall()', 'E1096:', '', 1, 'FuncWithForwardCall')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_return_type_wrong()
|
2020-08-08 16:47:30 +02:00
|
|
|
CheckScriptFailure([
|
|
|
|
'def Func(): number',
|
|
|
|
'return "a"',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'], 'expected number but got string')
|
|
|
|
CheckScriptFailure([
|
|
|
|
'def Func(): string',
|
|
|
|
'return 1',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'], 'expected string but got number')
|
|
|
|
CheckScriptFailure([
|
|
|
|
'def Func(): void',
|
|
|
|
'return "a"',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'],
|
|
|
|
'E1096: Returning a value in a function without a return type')
|
|
|
|
CheckScriptFailure([
|
|
|
|
'def Func()',
|
|
|
|
'return "a"',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'],
|
|
|
|
'E1096: Returning a value in a function without a return type')
|
|
|
|
|
|
|
|
CheckScriptFailure([
|
|
|
|
'def Func(): number',
|
|
|
|
'return',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'], 'E1003:')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
CheckScriptFailure(['def Func(): list', 'return []', 'enddef'], 'E1008:')
|
|
|
|
CheckScriptFailure(['def Func(): dict', 'return {}', 'enddef'], 'E1008:')
|
2020-04-06 21:35:05 +02:00
|
|
|
CheckScriptFailure(['def Func()', 'return 1'], 'E1057:')
|
2020-08-08 16:47:30 +02:00
|
|
|
|
|
|
|
CheckScriptFailure([
|
|
|
|
'vim9script',
|
|
|
|
'def FuncB()',
|
|
|
|
' return 123',
|
|
|
|
'enddef',
|
|
|
|
'def FuncA()',
|
|
|
|
' FuncB()',
|
|
|
|
'enddef',
|
|
|
|
'defcompile'], 'E1096:')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_arg_type_wrong()
|
|
|
|
CheckScriptFailure(['def Func3(items: list)', 'echo "a"', 'enddef'], 'E1008: Missing <type>')
|
2020-04-06 21:35:05 +02:00
|
|
|
CheckScriptFailure(['def Func4(...)', 'echo "a"', 'enddef'], 'E1055: Missing name after ...')
|
2020-04-23 22:16:53 +02:00
|
|
|
CheckScriptFailure(['def Func5(items:string)', 'echo "a"'], 'E1069:')
|
2020-04-13 17:21:00 +02:00
|
|
|
CheckScriptFailure(['def Func5(items)', 'echo "a"'], 'E1077:')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_vim9script_call()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
let var = ''
|
|
|
|
def MyFunc(arg: string)
|
|
|
|
var = arg
|
|
|
|
enddef
|
|
|
|
MyFunc('foobar')
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('foobar')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
let str = 'barfoo'
|
|
|
|
str->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('barfoo')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
2020-06-20 22:50:47 +02:00
|
|
|
g:value = 'value'
|
2020-04-05 17:08:17 +02:00
|
|
|
g:value->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('value')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
let listvar = []
|
|
|
|
def ListFunc(arg: list<number>)
|
|
|
|
listvar = arg
|
|
|
|
enddef
|
|
|
|
[1, 2, 3]->ListFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
listvar->assert_equal([1, 2, 3])
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
let dictvar = {}
|
|
|
|
def DictFunc(arg: dict<number>)
|
|
|
|
dictvar = arg
|
|
|
|
enddef
|
|
|
|
{'a': 1, 'b': 2}->DictFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
dictvar->assert_equal(#{a: 1, b: 2})
|
2020-04-05 17:08:17 +02:00
|
|
|
def CompiledDict()
|
|
|
|
{'a': 3, 'b': 4}->DictFunc()
|
|
|
|
enddef
|
|
|
|
CompiledDict()
|
2020-09-11 19:09:48 +02:00
|
|
|
dictvar->assert_equal(#{a: 3, b: 4})
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
#{a: 3, b: 4}->DictFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
dictvar->assert_equal(#{a: 3, b: 4})
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
('text')->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('text')
|
2020-04-05 17:08:17 +02:00
|
|
|
("some")->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('some')
|
2020-07-01 17:28:33 +02:00
|
|
|
|
2020-07-24 18:47:22 +02:00
|
|
|
# line starting with single quote is not a mark
|
2020-07-29 20:00:38 +02:00
|
|
|
# line starting with double quote can be a method call
|
2020-07-15 14:15:52 +02:00
|
|
|
'asdfasdf'->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('asdfasdf')
|
2020-07-29 20:00:38 +02:00
|
|
|
"xyz"->MyFunc()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('xyz')
|
2020-07-15 14:15:52 +02:00
|
|
|
|
|
|
|
def UseString()
|
|
|
|
'xyork'->MyFunc()
|
|
|
|
enddef
|
|
|
|
UseString()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('xyork')
|
2020-07-15 14:15:52 +02:00
|
|
|
|
2020-07-29 20:00:38 +02:00
|
|
|
def UseString2()
|
|
|
|
"knife"->MyFunc()
|
|
|
|
enddef
|
|
|
|
UseString2()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal('knife')
|
2020-07-29 20:00:38 +02:00
|
|
|
|
2020-07-24 18:47:22 +02:00
|
|
|
# prepending a colon makes it a mark
|
|
|
|
new
|
|
|
|
setline(1, ['aaa', 'bbb', 'ccc'])
|
|
|
|
normal! 3Gmt1G
|
|
|
|
:'t
|
2020-09-11 19:09:48 +02:00
|
|
|
getcurpos()[1]->assert_equal(3)
|
2020-07-24 18:47:22 +02:00
|
|
|
bwipe!
|
|
|
|
|
2020-07-01 17:28:33 +02:00
|
|
|
MyFunc(
|
|
|
|
'continued'
|
|
|
|
)
|
|
|
|
assert_equal('continued',
|
|
|
|
var
|
|
|
|
)
|
|
|
|
|
|
|
|
call MyFunc(
|
|
|
|
'more'
|
|
|
|
..
|
|
|
|
'lines'
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
'morelines',
|
|
|
|
var)
|
2020-04-05 17:08:17 +02:00
|
|
|
END
|
|
|
|
writefile(lines, 'Xcall.vim')
|
|
|
|
source Xcall.vim
|
|
|
|
delete('Xcall.vim')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_vim9script_call_fail_decl()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
let var = ''
|
|
|
|
def MyFunc(arg: string)
|
|
|
|
let var = 123
|
|
|
|
enddef
|
2020-05-24 23:00:18 +02:00
|
|
|
defcompile
|
2020-04-05 17:08:17 +02:00
|
|
|
END
|
2020-07-23 18:26:30 +02:00
|
|
|
CheckScriptFailure(lines, 'E1054:')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-19 14:03:09 +02:00
|
|
|
def Test_vim9script_call_fail_type()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def MyFunc(arg: string)
|
|
|
|
echo arg
|
|
|
|
enddef
|
|
|
|
MyFunc(1234)
|
|
|
|
END
|
2020-09-16 17:55:40 +02:00
|
|
|
CheckScriptFailure(lines, 'E1013: Argument 1: type mismatch, expected string but got number')
|
2020-07-19 14:03:09 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_vim9script_call_fail_const()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
const var = ''
|
|
|
|
def MyFunc(arg: string)
|
|
|
|
var = 'asdf'
|
|
|
|
enddef
|
2020-05-24 23:00:18 +02:00
|
|
|
defcompile
|
2020-04-05 17:08:17 +02:00
|
|
|
END
|
|
|
|
writefile(lines, 'Xcall_const.vim')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('source Xcall_const.vim', 'E46:', '', 1, 'MyFunc')
|
2020-04-05 17:08:17 +02:00
|
|
|
delete('Xcall_const.vim')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
" Test that inside :function a Python function can be defined, :def is not
|
|
|
|
" recognized.
|
|
|
|
func Test_function_python()
|
|
|
|
CheckFeature python3
|
|
|
|
let py = 'python3'
|
|
|
|
execute py "<< EOF"
|
|
|
|
def do_something():
|
|
|
|
return 1
|
|
|
|
EOF
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
def Test_delfunc()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
2020-04-27 22:47:51 +02:00
|
|
|
def g:GoneSoon()
|
2020-04-05 17:08:17 +02:00
|
|
|
echo 'hello'
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def CallGoneSoon()
|
|
|
|
GoneSoon()
|
|
|
|
enddef
|
2020-05-24 23:00:18 +02:00
|
|
|
defcompile
|
2020-04-05 17:08:17 +02:00
|
|
|
|
2020-04-27 22:47:51 +02:00
|
|
|
delfunc g:GoneSoon
|
2020-04-05 17:08:17 +02:00
|
|
|
CallGoneSoon()
|
|
|
|
END
|
|
|
|
writefile(lines, 'XToDelFunc')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
|
|
|
|
assert_fails('so XToDelFunc', 'E933:', '', 1, 'CallGoneSoon')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
delete('XToDelFunc')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_redef_failure()
|
2020-09-06 15:58:36 +02:00
|
|
|
writefile(['def Func0(): string', 'return "Func0"', 'enddef'], 'Xdef')
|
2020-04-05 17:08:17 +02:00
|
|
|
so Xdef
|
2020-09-06 15:58:36 +02:00
|
|
|
writefile(['def Func1(): string', 'return "Func1"', 'enddef'], 'Xdef')
|
2020-04-05 17:08:17 +02:00
|
|
|
so Xdef
|
2020-09-06 15:58:36 +02:00
|
|
|
writefile(['def! Func0(): string', 'enddef', 'defcompile'], 'Xdef')
|
2020-09-06 21:47:48 +02:00
|
|
|
assert_fails('so Xdef', 'E1027:', '', 1, 'Func0')
|
2020-09-06 15:58:36 +02:00
|
|
|
writefile(['def Func2(): string', 'return "Func2"', 'enddef'], 'Xdef')
|
2020-04-05 17:08:17 +02:00
|
|
|
so Xdef
|
2020-09-06 15:58:36 +02:00
|
|
|
delete('Xdef')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Func0()->assert_equal(0)
|
|
|
|
g:Func1()->assert_equal('Func1')
|
|
|
|
g:Func2()->assert_equal('Func2')
|
2020-04-05 17:08:17 +02:00
|
|
|
|
|
|
|
delfunc! Func0
|
|
|
|
delfunc! Func1
|
|
|
|
delfunc! Func2
|
|
|
|
enddef
|
|
|
|
|
2020-04-23 22:16:53 +02:00
|
|
|
def Test_vim9script_func()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
func Func(arg)
|
|
|
|
echo a:arg
|
|
|
|
endfunc
|
|
|
|
Func('text')
|
|
|
|
END
|
|
|
|
writefile(lines, 'XVim9Func')
|
|
|
|
so XVim9Func
|
|
|
|
|
|
|
|
delete('XVim9Func')
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
" Test for internal functions returning different types
|
|
|
|
func Test_InternalFuncRetType()
|
|
|
|
let lines =<< trim END
|
|
|
|
def RetFloat(): float
|
|
|
|
return ceil(1.456)
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def RetListAny(): list<any>
|
2020-08-12 17:35:58 +02:00
|
|
|
return items({'k': 'v'})
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def RetListString(): list<string>
|
|
|
|
return split('a:b:c', ':')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def RetListDictAny(): list<dict<any>>
|
|
|
|
return getbufinfo()
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def RetDictNumber(): dict<number>
|
|
|
|
return wordcount()
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def RetDictString(): dict<string>
|
|
|
|
return environ()
|
|
|
|
enddef
|
|
|
|
END
|
|
|
|
call writefile(lines, 'Xscript')
|
|
|
|
source Xscript
|
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
call RetFloat()->assert_equal(2.0)
|
|
|
|
call RetListAny()->assert_equal([['k', 'v']])
|
|
|
|
call RetListString()->assert_equal(['a', 'b', 'c'])
|
|
|
|
call RetListDictAny()->assert_notequal([])
|
|
|
|
call RetDictNumber()->assert_notequal({})
|
|
|
|
call RetDictString()->assert_notequal({})
|
2020-04-05 17:08:17 +02:00
|
|
|
call delete('Xscript')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Test for passing too many or too few arguments to internal functions
|
|
|
|
func Test_internalfunc_arg_error()
|
|
|
|
let l =<< trim END
|
|
|
|
def! FArgErr(): float
|
|
|
|
return ceil(1.1, 2)
|
|
|
|
enddef
|
2020-05-24 23:00:18 +02:00
|
|
|
defcompile
|
2020-04-05 17:08:17 +02:00
|
|
|
END
|
|
|
|
call writefile(l, 'Xinvalidarg')
|
2020-09-06 21:47:48 +02:00
|
|
|
call assert_fails('so Xinvalidarg', 'E118:', '', 1, 'FArgErr')
|
2020-04-05 17:08:17 +02:00
|
|
|
let l =<< trim END
|
|
|
|
def! FArgErr(): float
|
|
|
|
return ceil()
|
|
|
|
enddef
|
2020-05-24 23:00:18 +02:00
|
|
|
defcompile
|
2020-04-05 17:08:17 +02:00
|
|
|
END
|
|
|
|
call writefile(l, 'Xinvalidarg')
|
2020-09-06 21:47:48 +02:00
|
|
|
call assert_fails('so Xinvalidarg', 'E119:', '', 1, 'FArgErr')
|
2020-04-05 17:08:17 +02:00
|
|
|
call delete('Xinvalidarg')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
let s:funcResult = 0
|
|
|
|
|
|
|
|
def FuncNoArgNoRet()
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 11
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def FuncNoArgRetNumber(): number
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 22
|
2020-04-05 17:08:17 +02:00
|
|
|
return 1234
|
|
|
|
enddef
|
|
|
|
|
2020-04-07 20:53:39 +02:00
|
|
|
def FuncNoArgRetString(): string
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 45
|
2020-04-07 20:53:39 +02:00
|
|
|
return 'text'
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def FuncOneArgNoRet(arg: number)
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = arg
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def FuncOneArgRetNumber(arg: number): number
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = arg
|
2020-04-05 17:08:17 +02:00
|
|
|
return arg
|
|
|
|
enddef
|
|
|
|
|
2020-04-11 23:17:17 +02:00
|
|
|
def FuncTwoArgNoRet(one: bool, two: number)
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = two
|
2020-04-11 23:17:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-07 20:53:39 +02:00
|
|
|
def FuncOneArgRetString(arg: string): string
|
|
|
|
return arg
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 22:14:54 +02:00
|
|
|
def FuncOneArgRetAny(arg: any): any
|
|
|
|
return arg
|
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_func_type()
|
|
|
|
let Ref1: func()
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 0
|
2020-04-05 17:08:17 +02:00
|
|
|
Ref1 = FuncNoArgNoRet
|
|
|
|
Ref1()
|
2020-09-11 19:09:48 +02:00
|
|
|
s:funcResult->assert_equal(11)
|
2020-04-05 21:38:23 +02:00
|
|
|
|
|
|
|
let Ref2: func
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 0
|
2020-04-05 21:38:23 +02:00
|
|
|
Ref2 = FuncNoArgNoRet
|
|
|
|
Ref2()
|
2020-09-11 19:09:48 +02:00
|
|
|
s:funcResult->assert_equal(11)
|
2020-04-05 21:38:23 +02:00
|
|
|
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 0
|
2020-04-05 21:38:23 +02:00
|
|
|
Ref2 = FuncOneArgNoRet
|
|
|
|
Ref2(12)
|
2020-09-11 19:09:48 +02:00
|
|
|
s:funcResult->assert_equal(12)
|
2020-04-05 21:38:23 +02:00
|
|
|
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 0
|
2020-04-05 21:38:23 +02:00
|
|
|
Ref2 = FuncNoArgRetNumber
|
2020-09-11 19:09:48 +02:00
|
|
|
Ref2()->assert_equal(1234)
|
|
|
|
s:funcResult->assert_equal(22)
|
2020-04-05 21:38:23 +02:00
|
|
|
|
2020-08-22 19:02:02 +02:00
|
|
|
s:funcResult = 0
|
2020-04-05 21:38:23 +02:00
|
|
|
Ref2 = FuncOneArgRetNumber
|
2020-09-11 19:09:48 +02:00
|
|
|
Ref2(13)->assert_equal(13)
|
|
|
|
s:funcResult->assert_equal(13)
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 16:01:56 +02:00
|
|
|
def Test_repeat_return_type()
|
|
|
|
let res = 0
|
|
|
|
for n in repeat([1], 3)
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(3)
|
2020-07-05 16:07:21 +02:00
|
|
|
|
|
|
|
res = 0
|
|
|
|
for n in add([1, 2], 3)
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 16:01:56 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 17:04:13 +02:00
|
|
|
def Test_argv_return_type()
|
|
|
|
next fileone filetwo
|
|
|
|
let res = ''
|
|
|
|
for name in argv()
|
|
|
|
res ..= name
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal('fileonefiletwo')
|
2020-07-05 17:04:13 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-07 20:53:39 +02:00
|
|
|
def Test_func_type_part()
|
|
|
|
let RefVoid: func: void
|
|
|
|
RefVoid = FuncNoArgNoRet
|
|
|
|
RefVoid = FuncOneArgNoRet
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...) but got func(): number')
|
|
|
|
CheckDefFailure(['let RefVoid: func: void', 'RefVoid = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...) but got func(): string')
|
2020-04-07 20:53:39 +02:00
|
|
|
|
|
|
|
let RefAny: func(): any
|
|
|
|
RefAny = FuncNoArgRetNumber
|
|
|
|
RefAny = FuncNoArgRetString
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func()')
|
|
|
|
CheckDefFailure(['let RefAny: func(): any', 'RefAny = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func(): any but got func(number)')
|
2020-04-07 20:53:39 +02:00
|
|
|
|
|
|
|
let RefNr: func: number
|
|
|
|
RefNr = FuncNoArgRetNumber
|
|
|
|
RefNr = FuncOneArgRetNumber
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): number but got func()')
|
|
|
|
CheckDefFailure(['let RefNr: func: number', 'RefNr = FuncNoArgRetString'], 'E1012: Type mismatch; expected func(...): number but got func(): string')
|
2020-04-07 20:53:39 +02:00
|
|
|
|
|
|
|
let RefStr: func: string
|
|
|
|
RefStr = FuncNoArgRetString
|
|
|
|
RefStr = FuncOneArgRetString
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgNoRet'], 'E1012: Type mismatch; expected func(...): string but got func()')
|
|
|
|
CheckDefFailure(['let RefStr: func: string', 'RefStr = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func(...): string but got func(): number')
|
2020-04-07 20:53:39 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
def Test_func_type_fails()
|
|
|
|
CheckDefFailure(['let ref1: func()'], 'E704:')
|
|
|
|
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncNoArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(): number')
|
|
|
|
CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgNoRet'], 'E1012: Type mismatch; expected func() but got func(number)')
|
|
|
|
CheckDefFailure(['let Ref1: func()', 'Ref1 = FuncOneArgRetNumber'], 'E1012: Type mismatch; expected func() but got func(number): number')
|
|
|
|
CheckDefFailure(['let Ref1: func(bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(bool) but got func(bool, number)')
|
|
|
|
CheckDefFailure(['let Ref1: func(?bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(?bool) but got func(bool, number)')
|
|
|
|
CheckDefFailure(['let Ref1: func(...bool)', 'Ref1 = FuncTwoArgNoRet'], 'E1012: Type mismatch; expected func(...bool) but got func(bool, number)')
|
2020-04-11 23:17:17 +02:00
|
|
|
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckDefFailure(['let RefWrong: func(string ,number)'], 'E1068:')
|
|
|
|
CheckDefFailure(['let RefWrong: func(string,number)'], 'E1069:')
|
|
|
|
CheckDefFailure(['let RefWrong: func(bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool)'], 'E1005:')
|
|
|
|
CheckDefFailure(['let RefWrong: func(bool):string'], 'E1069:')
|
2020-04-05 17:08:17 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-05 22:14:54 +02:00
|
|
|
def Test_func_return_type()
|
|
|
|
let nr: number
|
|
|
|
nr = FuncNoArgRetNumber()
|
2020-09-11 19:09:48 +02:00
|
|
|
nr->assert_equal(1234)
|
2020-04-05 22:14:54 +02:00
|
|
|
|
|
|
|
nr = FuncOneArgRetAny(122)
|
2020-09-11 19:09:48 +02:00
|
|
|
nr->assert_equal(122)
|
2020-04-05 22:14:54 +02:00
|
|
|
|
|
|
|
let str: string
|
|
|
|
str = FuncOneArgRetAny('yes')
|
2020-09-11 19:09:48 +02:00
|
|
|
str->assert_equal('yes')
|
2020-04-05 22:14:54 +02:00
|
|
|
|
2020-09-16 15:22:00 +02:00
|
|
|
CheckDefFailure(['let str: string', 'str = FuncNoArgRetNumber()'], 'E1012: Type mismatch; expected string but got number')
|
2020-04-05 22:14:54 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-12 21:53:00 +02:00
|
|
|
def MultiLine(
|
|
|
|
arg1: string,
|
|
|
|
arg2 = 1234,
|
|
|
|
...rest: list<string>
|
|
|
|
): string
|
|
|
|
return arg1 .. arg2 .. join(rest, '-')
|
|
|
|
enddef
|
|
|
|
|
2020-04-13 14:41:35 +02:00
|
|
|
def MultiLineComment(
|
|
|
|
arg1: string, # comment
|
|
|
|
arg2 = 1234, # comment
|
|
|
|
...rest: list<string> # comment
|
|
|
|
): string # comment
|
|
|
|
return arg1 .. arg2 .. join(rest, '-')
|
|
|
|
enddef
|
|
|
|
|
2020-04-12 21:53:00 +02:00
|
|
|
def Test_multiline()
|
2020-09-11 19:09:48 +02:00
|
|
|
MultiLine('text')->assert_equal('text1234')
|
|
|
|
MultiLine('text', 777)->assert_equal('text777')
|
|
|
|
MultiLine('text', 777, 'one')->assert_equal('text777one')
|
|
|
|
MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
|
2020-04-12 21:53:00 +02:00
|
|
|
enddef
|
|
|
|
|
2020-04-12 22:22:31 +02:00
|
|
|
func Test_multiline_not_vim9()
|
2020-09-11 19:09:48 +02:00
|
|
|
call MultiLine('text')->assert_equal('text1234')
|
|
|
|
call MultiLine('text', 777)->assert_equal('text777')
|
|
|
|
call MultiLine('text', 777, 'one')->assert_equal('text777one')
|
|
|
|
call MultiLine('text', 777, 'one', 'two')->assert_equal('text777one-two')
|
2020-04-12 22:22:31 +02:00
|
|
|
endfunc
|
|
|
|
|
2020-04-12 21:53:00 +02:00
|
|
|
|
2020-04-06 21:35:05 +02:00
|
|
|
" When using CheckScriptFailure() for the below test, E1010 is generated instead
|
|
|
|
" of E1056.
|
|
|
|
func Test_E1056_1059()
|
|
|
|
let caught_1056 = 0
|
|
|
|
try
|
|
|
|
def F():
|
|
|
|
return 1
|
|
|
|
enddef
|
|
|
|
catch /E1056:/
|
|
|
|
let caught_1056 = 1
|
|
|
|
endtry
|
2020-09-11 19:09:48 +02:00
|
|
|
eval caught_1056->assert_equal(1)
|
2020-04-06 21:35:05 +02:00
|
|
|
|
|
|
|
let caught_1059 = 0
|
|
|
|
try
|
|
|
|
def F5(items : list)
|
|
|
|
echo 'a'
|
|
|
|
enddef
|
|
|
|
catch /E1059:/
|
|
|
|
let caught_1059 = 1
|
|
|
|
endtry
|
2020-09-11 19:09:48 +02:00
|
|
|
eval caught_1059->assert_equal(1)
|
2020-04-06 21:35:05 +02:00
|
|
|
endfunc
|
2020-04-05 17:08:17 +02:00
|
|
|
|
2020-05-05 21:25:22 +02:00
|
|
|
func DelMe()
|
|
|
|
echo 'DelMe'
|
|
|
|
endfunc
|
|
|
|
|
2020-08-08 14:26:31 +02:00
|
|
|
def Test_error_reporting()
|
|
|
|
# comment lines at the start of the function
|
|
|
|
let lines =<< trim END
|
|
|
|
" comment
|
|
|
|
def Func()
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
invalid
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
2020-09-14 17:04:31 +02:00
|
|
|
writefile(lines, 'Xdef')
|
2020-08-08 14:26:31 +02:00
|
|
|
try
|
|
|
|
source Xdef
|
2020-08-14 18:35:07 +02:00
|
|
|
assert_report('should have failed')
|
2020-08-08 14:26:31 +02:00
|
|
|
catch /E476:/
|
2020-09-11 19:09:48 +02:00
|
|
|
v:exception->assert_match('Invalid command: invalid')
|
|
|
|
v:throwpoint->assert_match(', line 3$')
|
2020-08-08 14:26:31 +02:00
|
|
|
endtry
|
|
|
|
|
|
|
|
# comment lines after the start of the function
|
|
|
|
lines =<< trim END
|
|
|
|
" comment
|
|
|
|
def Func()
|
|
|
|
let x = 1234
|
|
|
|
# comment
|
|
|
|
# comment
|
|
|
|
invalid
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
2020-09-14 17:04:31 +02:00
|
|
|
writefile(lines, 'Xdef')
|
2020-08-08 14:26:31 +02:00
|
|
|
try
|
|
|
|
source Xdef
|
2020-08-14 18:35:07 +02:00
|
|
|
assert_report('should have failed')
|
2020-08-08 14:26:31 +02:00
|
|
|
catch /E476:/
|
2020-09-11 19:09:48 +02:00
|
|
|
v:exception->assert_match('Invalid command: invalid')
|
|
|
|
v:throwpoint->assert_match(', line 4$')
|
2020-08-08 14:26:31 +02:00
|
|
|
endtry
|
|
|
|
|
2020-08-14 18:35:07 +02:00
|
|
|
lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func()
|
|
|
|
let db = #{foo: 1, bar: 2}
|
|
|
|
# comment
|
|
|
|
let x = db.asdf
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
Func()
|
|
|
|
END
|
2020-09-14 17:04:31 +02:00
|
|
|
writefile(lines, 'Xdef')
|
2020-08-14 18:35:07 +02:00
|
|
|
try
|
|
|
|
source Xdef
|
|
|
|
assert_report('should have failed')
|
|
|
|
catch /E716:/
|
2020-09-11 19:09:48 +02:00
|
|
|
v:throwpoint->assert_match('_Func, line 3$')
|
2020-08-14 18:35:07 +02:00
|
|
|
endtry
|
|
|
|
|
2020-09-14 17:04:31 +02:00
|
|
|
delete('Xdef')
|
2020-08-08 14:26:31 +02:00
|
|
|
enddef
|
|
|
|
|
2020-05-05 21:25:22 +02:00
|
|
|
def Test_deleted_function()
|
|
|
|
CheckDefExecFailure([
|
|
|
|
'let RefMe: func = function("g:DelMe")',
|
|
|
|
'delfunc g:DelMe',
|
|
|
|
'echo RefMe()'], 'E117:')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_unknown_function()
|
|
|
|
CheckDefExecFailure([
|
|
|
|
'let Ref: func = function("NotExist")',
|
2020-07-11 22:14:59 +02:00
|
|
|
'delfunc g:NotExist'], 'E700:')
|
2020-05-05 21:25:22 +02:00
|
|
|
enddef
|
|
|
|
|
2020-05-01 19:29:08 +02:00
|
|
|
def RefFunc(Ref: func(string): string): string
|
|
|
|
return Ref('more')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_simple()
|
|
|
|
let local = 'some '
|
2020-09-11 19:09:48 +02:00
|
|
|
RefFunc({s -> local .. s})->assert_equal('some more')
|
2020-05-01 19:29:08 +02:00
|
|
|
enddef
|
|
|
|
|
2020-05-02 17:52:42 +02:00
|
|
|
def MakeRef()
|
|
|
|
let local = 'some '
|
|
|
|
g:Ref = {s -> local .. s}
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_ref_after_return()
|
|
|
|
MakeRef()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Ref('thing')->assert_equal('some thing')
|
2020-05-02 17:52:42 +02:00
|
|
|
unlet g:Ref
|
|
|
|
enddef
|
|
|
|
|
2020-05-02 23:12:58 +02:00
|
|
|
def MakeTwoRefs()
|
|
|
|
let local = ['some']
|
|
|
|
g:Extend = {s -> local->add(s)}
|
|
|
|
g:Read = {-> local}
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_two_refs()
|
|
|
|
MakeTwoRefs()
|
2020-09-11 19:09:48 +02:00
|
|
|
join(g:Read(), ' ')->assert_equal('some')
|
2020-05-02 23:12:58 +02:00
|
|
|
g:Extend('more')
|
2020-09-11 19:09:48 +02:00
|
|
|
join(g:Read(), ' ')->assert_equal('some more')
|
2020-05-02 23:12:58 +02:00
|
|
|
g:Extend('even')
|
2020-09-11 19:09:48 +02:00
|
|
|
join(g:Read(), ' ')->assert_equal('some more even')
|
2020-05-02 23:12:58 +02:00
|
|
|
|
|
|
|
unlet g:Extend
|
|
|
|
unlet g:Read
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def ReadRef(Ref: func(): list<string>): string
|
|
|
|
return join(Ref(), ' ')
|
|
|
|
enddef
|
|
|
|
|
2020-09-16 15:22:00 +02:00
|
|
|
def ExtendRef(Ref: func(string): list<string>, add: string)
|
2020-05-02 23:12:58 +02:00
|
|
|
Ref(add)
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_two_indirect_refs()
|
2020-05-03 15:38:16 +02:00
|
|
|
MakeTwoRefs()
|
2020-09-11 19:09:48 +02:00
|
|
|
ReadRef(g:Read)->assert_equal('some')
|
2020-05-02 23:12:58 +02:00
|
|
|
ExtendRef(g:Extend, 'more')
|
2020-09-11 19:09:48 +02:00
|
|
|
ReadRef(g:Read)->assert_equal('some more')
|
2020-05-02 23:12:58 +02:00
|
|
|
ExtendRef(g:Extend, 'even')
|
2020-09-11 19:09:48 +02:00
|
|
|
ReadRef(g:Read)->assert_equal('some more even')
|
2020-05-02 23:12:58 +02:00
|
|
|
|
|
|
|
unlet g:Extend
|
|
|
|
unlet g:Read
|
|
|
|
enddef
|
2020-05-02 17:52:42 +02:00
|
|
|
|
2020-05-03 22:30:49 +02:00
|
|
|
def MakeArgRefs(theArg: string)
|
|
|
|
let local = 'loc_val'
|
|
|
|
g:UseArg = {s -> theArg .. '/' .. local .. '/' .. s}
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def MakeArgRefsVarargs(theArg: string, ...rest: list<string>)
|
|
|
|
let local = 'the_loc'
|
|
|
|
g:UseVararg = {s -> theArg .. '/' .. local .. '/' .. s .. '/' .. join(rest)}
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_using_argument()
|
|
|
|
MakeArgRefs('arg_val')
|
2020-09-11 19:09:48 +02:00
|
|
|
g:UseArg('call_val')->assert_equal('arg_val/loc_val/call_val')
|
2020-05-03 22:30:49 +02:00
|
|
|
|
|
|
|
MakeArgRefsVarargs('arg_val', 'one', 'two')
|
2020-09-11 19:09:48 +02:00
|
|
|
g:UseVararg('call_val')->assert_equal('arg_val/the_loc/call_val/one two')
|
2020-05-03 22:30:49 +02:00
|
|
|
|
|
|
|
unlet g:UseArg
|
|
|
|
unlet g:UseVararg
|
|
|
|
enddef
|
|
|
|
|
2020-05-06 21:06:30 +02:00
|
|
|
def MakeGetAndAppendRefs()
|
|
|
|
let local = 'a'
|
|
|
|
|
|
|
|
def Append(arg: string)
|
|
|
|
local ..= arg
|
|
|
|
enddef
|
|
|
|
g:Append = Append
|
|
|
|
|
|
|
|
def Get(): string
|
|
|
|
return local
|
|
|
|
enddef
|
|
|
|
g:Get = Get
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_append_get()
|
|
|
|
MakeGetAndAppendRefs()
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Get()->assert_equal('a')
|
2020-05-06 21:06:30 +02:00
|
|
|
g:Append('-b')
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Get()->assert_equal('a-b')
|
2020-05-06 21:06:30 +02:00
|
|
|
g:Append('-c')
|
2020-09-11 19:09:48 +02:00
|
|
|
g:Get()->assert_equal('a-b-c')
|
2020-05-06 21:06:30 +02:00
|
|
|
|
|
|
|
unlet g:Append
|
|
|
|
unlet g:Get
|
|
|
|
enddef
|
|
|
|
|
2020-05-04 23:24:44 +02:00
|
|
|
def Test_nested_closure()
|
|
|
|
let local = 'text'
|
|
|
|
def Closure(arg: string): string
|
|
|
|
return local .. arg
|
|
|
|
enddef
|
2020-09-11 19:09:48 +02:00
|
|
|
Closure('!!!')->assert_equal('text!!!')
|
2020-05-04 23:24:44 +02:00
|
|
|
enddef
|
|
|
|
|
2020-05-16 21:20:12 +02:00
|
|
|
func GetResult(Ref)
|
|
|
|
return a:Ref('some')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
def Test_call_closure_not_compiled()
|
|
|
|
let text = 'text'
|
|
|
|
g:Ref = {s -> s .. text}
|
2020-09-11 19:09:48 +02:00
|
|
|
GetResult(g:Ref)->assert_equal('sometext')
|
2020-05-16 21:20:12 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-18 21:25:32 +02:00
|
|
|
def Test_double_closure_fails()
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def Func()
|
|
|
|
let var = 0
|
|
|
|
for i in range(2)
|
|
|
|
timer_start(0, {-> var})
|
|
|
|
endfor
|
|
|
|
enddef
|
|
|
|
Func()
|
|
|
|
END
|
|
|
|
CheckScriptFailure(lines, 'Multiple closures not supported yet')
|
|
|
|
enddef
|
|
|
|
|
2020-06-18 18:45:49 +02:00
|
|
|
def Test_sort_return_type()
|
|
|
|
let res: list<number>
|
|
|
|
res = [1, 2, 3]->sort()
|
|
|
|
enddef
|
|
|
|
|
2020-09-16 23:23:36 +02:00
|
|
|
def Test_sort_argument()
|
|
|
|
let res = ['b', 'a', 'c']->sort('i')
|
|
|
|
res->assert_equal(['a', 'b', 'c'])
|
|
|
|
enddef
|
|
|
|
|
2020-06-30 13:38:01 +02:00
|
|
|
def Test_getqflist_return_type()
|
|
|
|
let l = getqflist()
|
2020-09-11 19:09:48 +02:00
|
|
|
l->assert_equal([])
|
2020-06-30 13:38:01 +02:00
|
|
|
|
|
|
|
let d = getqflist(#{items: 0})
|
2020-09-11 19:09:48 +02:00
|
|
|
d->assert_equal(#{items: []})
|
2020-06-30 13:38:01 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_getloclist_return_type()
|
|
|
|
let l = getloclist(1)
|
2020-09-11 19:09:48 +02:00
|
|
|
l->assert_equal([])
|
2020-06-30 13:38:01 +02:00
|
|
|
|
|
|
|
let d = getloclist(1, #{items: 0})
|
2020-09-11 19:09:48 +02:00
|
|
|
d->assert_equal(#{items: []})
|
2020-06-30 13:38:01 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 18:41:08 +02:00
|
|
|
def Test_copy_return_type()
|
|
|
|
let l = copy([1, 2, 3])
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 18:41:08 +02:00
|
|
|
|
|
|
|
let dl = deepcopy([1, 2, 3])
|
|
|
|
res = 0
|
|
|
|
for n in dl
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-09-05 17:18:28 +02:00
|
|
|
|
|
|
|
dl = deepcopy([1, 2, 3], true)
|
2020-07-05 18:41:08 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 20:08:39 +02:00
|
|
|
def Test_extend_return_type()
|
|
|
|
let l = extend([1, 2], [3])
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
2020-07-05 20:47:18 +02:00
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 20:47:18 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 17:30:44 +02:00
|
|
|
def Test_garbagecollect()
|
|
|
|
garbagecollect(true)
|
|
|
|
enddef
|
|
|
|
|
2020-07-05 20:47:18 +02:00
|
|
|
def Test_insert_return_type()
|
|
|
|
let l = insert([2, 1], 3)
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
2020-07-05 20:08:39 +02:00
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 20:08:39 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-14 18:56:45 +02:00
|
|
|
def Test_keys_return_type()
|
|
|
|
const var: list<string> = #{a: 1, b: 2}->keys()
|
2020-09-11 19:09:48 +02:00
|
|
|
var->assert_equal(['a', 'b'])
|
2020-08-14 18:56:45 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 21:10:24 +02:00
|
|
|
def Test_reverse_return_type()
|
|
|
|
let l = reverse([1, 2, 3])
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 21:10:24 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 20:55:29 +02:00
|
|
|
def Test_remove_return_type()
|
|
|
|
let l = remove(#{one: [1, 2], two: [3, 4]}, 'one')
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(3)
|
2020-07-05 20:55:29 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 20:16:41 +02:00
|
|
|
def Test_filter_return_type()
|
|
|
|
let l = filter([1, 2, 3], {-> 1})
|
|
|
|
let res = 0
|
|
|
|
for n in l
|
|
|
|
res += n
|
|
|
|
endfor
|
2020-09-11 19:09:48 +02:00
|
|
|
res->assert_equal(6)
|
2020-07-05 20:16:41 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-17 22:21:36 +02:00
|
|
|
def Test_bufnr()
|
|
|
|
let buf = bufnr()
|
2020-09-11 19:09:48 +02:00
|
|
|
bufnr('%')->assert_equal(buf)
|
2020-09-04 18:35:26 +02:00
|
|
|
|
|
|
|
buf = bufnr('Xdummy', true)
|
2020-09-11 19:09:48 +02:00
|
|
|
buf->assert_notequal(-1)
|
2020-09-04 18:35:26 +02:00
|
|
|
exe 'bwipe! ' .. buf
|
2020-08-17 22:21:36 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-20 22:29:12 +02:00
|
|
|
def Test_col()
|
|
|
|
new
|
|
|
|
setline(1, 'asdf')
|
2020-09-11 19:09:48 +02:00
|
|
|
col([1, '$'])->assert_equal(5)
|
2020-08-20 22:29:12 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-04 19:50:57 +02:00
|
|
|
def Test_char2nr()
|
2020-09-11 19:09:48 +02:00
|
|
|
char2nr('あ', true)->assert_equal(12354)
|
2020-09-04 19:50:57 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-06 21:26:59 +02:00
|
|
|
def Test_getreg_return_type()
|
|
|
|
let s1: string = getreg('"')
|
|
|
|
let s2: string = getreg('"', 1)
|
|
|
|
let s3: list<string> = getreg('"', 1, 1)
|
|
|
|
enddef
|
|
|
|
|
2020-07-13 18:55:48 +02:00
|
|
|
def Wrong_dict_key_type(items: list<number>): list<number>
|
|
|
|
return filter(items, {_, val -> get({val: 1}, 'x')})
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_wrong_dict_key_type()
|
2020-09-16 15:22:00 +02:00
|
|
|
assert_fails('Wrong_dict_key_type([1, 2, 3])', 'E1012:')
|
2020-07-13 18:55:48 +02:00
|
|
|
enddef
|
|
|
|
|
2020-06-22 19:39:03 +02:00
|
|
|
def Line_continuation_in_def(dir: string = ''): string
|
|
|
|
let path: string = empty(dir)
|
|
|
|
\ ? 'empty'
|
|
|
|
\ : 'full'
|
|
|
|
return path
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_line_continuation_in_def()
|
2020-09-11 19:09:48 +02:00
|
|
|
Line_continuation_in_def('.')->assert_equal('full')
|
2020-06-22 19:39:03 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-16 15:22:00 +02:00
|
|
|
def Line_continuation_in_lambda(): list<string>
|
2020-07-08 17:36:21 +02:00
|
|
|
let x = range(97, 100)
|
2020-07-11 15:20:48 +02:00
|
|
|
->map({_, v -> nr2char(v)
|
2020-07-08 17:36:21 +02:00
|
|
|
->toupper()})
|
|
|
|
->reverse()
|
|
|
|
return x
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_line_continuation_in_lambda()
|
2020-09-11 19:09:48 +02:00
|
|
|
Line_continuation_in_lambda()->assert_equal(['D', 'C', 'B', 'A'])
|
2020-07-08 17:36:21 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-05 18:48:23 +02:00
|
|
|
func Test_silent_echo()
|
2020-07-05 18:18:42 +02:00
|
|
|
CheckScreendump
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
def EchoNothing()
|
|
|
|
silent echo ''
|
|
|
|
enddef
|
|
|
|
defcompile
|
|
|
|
END
|
2020-07-05 18:48:23 +02:00
|
|
|
call writefile(lines, 'XTest_silent_echo')
|
2020-07-05 18:18:42 +02:00
|
|
|
|
|
|
|
" Check that the balloon shows up after a mouse move
|
|
|
|
let buf = RunVimInTerminal('-S XTest_silent_echo', {'rows': 6})
|
2020-07-05 18:48:23 +02:00
|
|
|
call term_sendkeys(buf, ":abc")
|
2020-07-05 18:18:42 +02:00
|
|
|
call VerifyScreenDump(buf, 'Test_vim9_silent_echo', {})
|
|
|
|
|
|
|
|
" clean up
|
|
|
|
call StopVimInTerminal(buf)
|
|
|
|
call delete('XTest_silent_echo')
|
2020-07-05 18:48:23 +02:00
|
|
|
endfunc
|
2020-07-05 18:18:42 +02:00
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
""""""" builtin functions that behave differently in Vim9
|
2020-09-01 23:16:32 +02:00
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_bufname()
|
|
|
|
split SomeFile
|
2020-09-11 19:09:48 +02:00
|
|
|
bufname('%')->assert_equal('SomeFile')
|
2020-09-05 21:57:53 +02:00
|
|
|
edit OtherFile
|
2020-09-11 19:09:48 +02:00
|
|
|
bufname('#')->assert_equal('SomeFile')
|
2020-09-05 21:57:53 +02:00
|
|
|
close
|
2020-08-19 21:20:49 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-02 21:02:35 +02:00
|
|
|
def Test_bufwinid()
|
|
|
|
let origwin = win_getid()
|
|
|
|
below split SomeFile
|
|
|
|
let SomeFileID = win_getid()
|
|
|
|
below split OtherFile
|
|
|
|
below split SomeFile
|
2020-09-11 19:09:48 +02:00
|
|
|
bufwinid('SomeFile')->assert_equal(SomeFileID)
|
2020-09-02 21:02:35 +02:00
|
|
|
|
|
|
|
win_gotoid(origwin)
|
|
|
|
only
|
|
|
|
bwipe SomeFile
|
|
|
|
bwipe OtherFile
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_count()
|
2020-09-11 19:09:48 +02:00
|
|
|
count('ABC ABC ABC', 'b', true)->assert_equal(3)
|
|
|
|
count('ABC ABC ABC', 'b', false)->assert_equal(0)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_expand()
|
|
|
|
split SomeFile
|
2020-09-11 19:09:48 +02:00
|
|
|
expand('%', true, true)->assert_equal(['SomeFile'])
|
2020-09-05 21:57:53 +02:00
|
|
|
close
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_getbufinfo()
|
|
|
|
let bufinfo = getbufinfo(bufnr())
|
2020-09-11 19:09:48 +02:00
|
|
|
getbufinfo('%')->assert_equal(bufinfo)
|
2020-09-05 21:57:53 +02:00
|
|
|
|
|
|
|
edit Xtestfile1
|
|
|
|
hide edit Xtestfile2
|
|
|
|
hide enew
|
|
|
|
getbufinfo(#{bufloaded: true, buflisted: true, bufmodified: false})
|
|
|
|
->len()->assert_equal(3)
|
|
|
|
bwipe Xtestfile1 Xtestfile2
|
|
|
|
enddef
|
|
|
|
|
2020-09-02 21:02:35 +02:00
|
|
|
def Test_getbufline()
|
|
|
|
e SomeFile
|
|
|
|
let buf = bufnr()
|
|
|
|
e #
|
|
|
|
let lines = ['aaa', 'bbb', 'ccc']
|
|
|
|
setbufline(buf, 1, lines)
|
2020-09-11 19:09:48 +02:00
|
|
|
getbufline('#', 1, '$')->assert_equal(lines)
|
2020-09-02 21:02:35 +02:00
|
|
|
|
|
|
|
bwipe!
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_getchangelist()
|
|
|
|
new
|
|
|
|
setline(1, 'some text')
|
|
|
|
let changelist = bufnr()->getchangelist()
|
2020-09-11 19:09:48 +02:00
|
|
|
getchangelist('%')->assert_equal(changelist)
|
2020-09-02 21:02:35 +02:00
|
|
|
bwipe!
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 17:51:23 +02:00
|
|
|
def Test_getchar()
|
2020-09-05 18:48:57 +02:00
|
|
|
while getchar(0)
|
|
|
|
endwhile
|
2020-09-11 19:09:48 +02:00
|
|
|
getchar(true)->assert_equal(0)
|
2020-09-05 17:51:23 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 18:31:33 +02:00
|
|
|
def Test_getcompletion()
|
|
|
|
set wildignore=*.vim,*~
|
|
|
|
let l = getcompletion('run', 'file', true)
|
2020-09-11 19:09:48 +02:00
|
|
|
l->assert_equal([])
|
2020-09-05 18:31:33 +02:00
|
|
|
set wildignore&
|
|
|
|
enddef
|
|
|
|
|
2020-09-02 21:45:54 +02:00
|
|
|
def Test_getreg()
|
|
|
|
let lines = ['aaa', 'bbb', 'ccc']
|
|
|
|
setreg('a', lines)
|
2020-09-11 19:09:48 +02:00
|
|
|
getreg('a', true, true)->assert_equal(lines)
|
2020-09-02 21:45:54 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-02 21:53:11 +02:00
|
|
|
def Test_glob()
|
2020-09-11 19:09:48 +02:00
|
|
|
glob('runtest.vim', true, true, true)->assert_equal(['runtest.vim'])
|
2020-09-02 21:53:11 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-02 21:57:07 +02:00
|
|
|
def Test_globpath()
|
2020-09-11 19:09:48 +02:00
|
|
|
globpath('.', 'runtest.vim', true, true, true)->assert_equal(['./runtest.vim'])
|
2020-09-02 21:57:07 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_has()
|
2020-09-11 19:09:48 +02:00
|
|
|
has('eval', true)->assert_equal(1)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-02 22:25:35 +02:00
|
|
|
def Test_hasmapto()
|
2020-09-11 19:09:48 +02:00
|
|
|
hasmapto('foobar', 'i', true)->assert_equal(0)
|
2020-09-02 22:25:35 +02:00
|
|
|
iabbrev foo foobar
|
2020-09-11 19:09:48 +02:00
|
|
|
hasmapto('foobar', 'i', true)->assert_equal(1)
|
2020-09-02 22:25:35 +02:00
|
|
|
iunabbrev foo
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_index()
|
2020-09-11 19:09:48 +02:00
|
|
|
index(['a', 'b', 'a', 'B'], 'b', 2, true)->assert_equal(3)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_list2str_str2list_utf8()
|
|
|
|
let s = "\u3042\u3044"
|
|
|
|
let l = [0x3042, 0x3044]
|
2020-09-11 19:09:48 +02:00
|
|
|
str2list(s, true)->assert_equal(l)
|
|
|
|
list2str(l, true)->assert_equal(s)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-02 22:25:35 +02:00
|
|
|
def SID(): number
|
|
|
|
return expand('<SID>')
|
|
|
|
->matchstr('<SNR>\zs\d\+\ze_$')
|
|
|
|
->str2nr()
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_maparg()
|
|
|
|
let lnum = str2nr(expand('<sflnum>'))
|
|
|
|
map foo bar
|
2020-09-11 19:09:48 +02:00
|
|
|
maparg('foo', '', false, true)->assert_equal(#{
|
2020-09-02 22:25:35 +02:00
|
|
|
lnum: lnum + 1,
|
|
|
|
script: 0,
|
|
|
|
mode: ' ',
|
|
|
|
silent: 0,
|
|
|
|
noremap: 0,
|
|
|
|
lhs: 'foo',
|
|
|
|
lhsraw: 'foo',
|
|
|
|
nowait: 0,
|
|
|
|
expr: 0,
|
|
|
|
sid: SID(),
|
|
|
|
rhs: 'bar',
|
2020-09-11 19:09:48 +02:00
|
|
|
buffer: 0})
|
2020-09-02 22:25:35 +02:00
|
|
|
unmap foo
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_mapcheck()
|
|
|
|
iabbrev foo foobar
|
2020-09-11 19:09:48 +02:00
|
|
|
mapcheck('foo', 'i', true)->assert_equal('foobar')
|
2020-09-02 22:25:35 +02:00
|
|
|
iunabbrev foo
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_nr2char()
|
2020-09-11 19:09:48 +02:00
|
|
|
nr2char(97, true)->assert_equal('a')
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_readdir()
|
|
|
|
eval expand('sautest')->readdir({e -> e[0] !=# '.'})
|
|
|
|
eval expand('sautest')->readdirex({e -> e.name[0] !=# '.'})
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_search()
|
|
|
|
new
|
|
|
|
setline(1, ['foo', 'bar'])
|
|
|
|
let val = 0
|
|
|
|
# skip expr returns boolean
|
2020-09-11 19:09:48 +02:00
|
|
|
search('bar', 'W', 0, 0, {-> val == 1})->assert_equal(2)
|
2020-09-05 21:57:53 +02:00
|
|
|
:1
|
2020-09-11 19:09:48 +02:00
|
|
|
search('bar', 'W', 0, 0, {-> val == 0})->assert_equal(0)
|
2020-09-05 21:57:53 +02:00
|
|
|
# skip expr returns number, only 0 and 1 are accepted
|
|
|
|
:1
|
2020-09-11 19:09:48 +02:00
|
|
|
search('bar', 'W', 0, 0, {-> 0})->assert_equal(2)
|
2020-09-05 21:57:53 +02:00
|
|
|
:1
|
2020-09-11 19:09:48 +02:00
|
|
|
search('bar', 'W', 0, 0, {-> 1})->assert_equal(0)
|
2020-09-05 21:57:53 +02:00
|
|
|
assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:')
|
|
|
|
assert_fails("search('bar', '', 0, 0, {-> -1})", 'E1023:')
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_searchcount()
|
|
|
|
new
|
|
|
|
setline(1, "foo bar")
|
|
|
|
:/foo
|
2020-09-11 19:09:48 +02:00
|
|
|
searchcount(#{recompute: true})
|
|
|
|
->assert_equal(#{
|
|
|
|
exact_match: 1,
|
|
|
|
current: 1,
|
|
|
|
total: 1,
|
|
|
|
maxcount: 99,
|
|
|
|
incomplete: 0})
|
2020-09-05 21:57:53 +02:00
|
|
|
bwipe!
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_searchdecl()
|
2020-09-11 19:09:48 +02:00
|
|
|
searchdecl('blah', true, true)->assert_equal(1)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_setbufvar()
|
2020-09-11 19:09:48 +02:00
|
|
|
setbufvar(bufnr('%'), '&syntax', 'vim')
|
|
|
|
&syntax->assert_equal('vim')
|
|
|
|
setbufvar(bufnr('%'), '&ts', 16)
|
|
|
|
&ts->assert_equal(16)
|
|
|
|
settabwinvar(1, 1, '&syntax', 'vam')
|
|
|
|
&syntax->assert_equal('vam')
|
|
|
|
settabwinvar(1, 1, '&ts', 15)
|
|
|
|
&ts->assert_equal(15)
|
|
|
|
setlocal ts=8
|
2020-09-05 21:57:53 +02:00
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
setbufvar('%', 'myvar', 123)
|
|
|
|
getbufvar('%', 'myvar')->assert_equal(123)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 22:37:39 +02:00
|
|
|
def Test_setloclist()
|
|
|
|
let items = [#{filename: '/tmp/file', lnum: 1, valid: true}]
|
|
|
|
let what = #{items: items}
|
|
|
|
setqflist([], ' ', what)
|
|
|
|
setloclist(0, [], ' ', what)
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_setreg()
|
|
|
|
setreg('a', ['aaa', 'bbb', 'ccc'])
|
|
|
|
let reginfo = getreginfo('a')
|
|
|
|
setreg('a', reginfo)
|
2020-09-11 19:09:48 +02:00
|
|
|
getreginfo('a')->assert_equal(reginfo)
|
2020-09-05 21:57:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 22:45:55 +02:00
|
|
|
def Test_spellsuggest()
|
|
|
|
if !has('spell')
|
|
|
|
MissingFeature 'spell'
|
|
|
|
else
|
|
|
|
spellsuggest('marrch', 1, true)->assert_equal(['March'])
|
|
|
|
endif
|
|
|
|
enddef
|
|
|
|
|
2020-09-06 16:09:04 +02:00
|
|
|
def Test_split()
|
|
|
|
split(' aa bb ', '\W\+', true)->assert_equal(['', 'aa', 'bb', ''])
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_str2nr()
|
|
|
|
str2nr("1'000'000", 10, true)->assert_equal(1000000)
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_strchars()
|
|
|
|
strchars("A\u20dd", true)->assert_equal(1)
|
|
|
|
enddef
|
|
|
|
|
2020-09-06 18:22:53 +02:00
|
|
|
def Test_submatch()
|
|
|
|
let pat = 'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)'
|
|
|
|
let Rep = {-> range(10)->map({_, v -> submatch(v, true)})->string()}
|
|
|
|
let actual = substitute('A123456789', pat, Rep, '')
|
|
|
|
let expected = "[['A123456789'], ['1'], ['2'], ['3'], ['4'], ['5'], ['6'], ['7'], ['8'], ['9']]"
|
2020-09-11 19:09:48 +02:00
|
|
|
actual->assert_equal(expected)
|
2020-09-06 18:22:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_synID()
|
|
|
|
new
|
|
|
|
setline(1, "text")
|
2020-09-11 19:09:48 +02:00
|
|
|
synID(1, 1, true)->assert_equal(0)
|
2020-09-05 21:57:53 +02:00
|
|
|
bwipe!
|
|
|
|
enddef
|
|
|
|
|
2020-09-06 18:22:53 +02:00
|
|
|
def Test_term_gettty()
|
2020-09-06 20:06:59 +02:00
|
|
|
if !has('terminal')
|
|
|
|
MissingFeature 'terminal'
|
|
|
|
else
|
|
|
|
let buf = Run_shell_in_terminal({})
|
2020-09-11 19:09:48 +02:00
|
|
|
term_gettty(buf, true)->assert_notequal('')
|
2020-09-06 20:06:59 +02:00
|
|
|
StopShellInTerminal(buf)
|
|
|
|
endif
|
2020-09-06 18:22:53 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_term_start()
|
2020-09-06 20:06:59 +02:00
|
|
|
if !has('terminal')
|
|
|
|
MissingFeature 'terminal'
|
|
|
|
else
|
|
|
|
botright new
|
|
|
|
let winnr = winnr()
|
|
|
|
term_start(&shell, #{curwin: true})
|
2020-09-11 19:09:48 +02:00
|
|
|
winnr()->assert_equal(winnr)
|
2020-09-06 20:06:59 +02:00
|
|
|
bwipe!
|
|
|
|
endif
|
2020-09-06 18:22:53 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-06 18:39:38 +02:00
|
|
|
def Test_timer_paused()
|
|
|
|
let id = timer_start(50, {-> 0})
|
|
|
|
timer_pause(id, true)
|
|
|
|
let info = timer_info(id)
|
2020-09-11 19:09:48 +02:00
|
|
|
info[0]['paused']->assert_equal(1)
|
2020-09-06 18:39:38 +02:00
|
|
|
timer_stop(id)
|
|
|
|
enddef
|
|
|
|
|
2020-09-05 21:57:53 +02:00
|
|
|
def Test_win_splitmove()
|
|
|
|
split
|
|
|
|
win_splitmove(1, 2, #{vertical: true, rightbelow: true})
|
|
|
|
close
|
|
|
|
enddef
|
|
|
|
|
|
|
|
""""""" end of builtin functions
|
|
|
|
|
|
|
|
def Fibonacci(n: number): number
|
|
|
|
if n < 2
|
|
|
|
return n
|
|
|
|
else
|
|
|
|
return Fibonacci(n - 1) + Fibonacci(n - 2)
|
|
|
|
endif
|
|
|
|
enddef
|
|
|
|
|
2020-07-12 17:31:09 +02:00
|
|
|
def Test_recursive_call()
|
2020-09-11 19:09:48 +02:00
|
|
|
Fibonacci(20)->assert_equal(6765)
|
2020-07-12 17:31:09 +02:00
|
|
|
enddef
|
|
|
|
|
2020-07-13 20:41:08 +02:00
|
|
|
def TreeWalk(dir: string): list<any>
|
|
|
|
return readdir(dir)->map({_, val ->
|
|
|
|
fnamemodify(dir .. '/' .. val, ':p')->isdirectory()
|
2020-08-05 10:53:21 +02:00
|
|
|
? {val: TreeWalk(dir .. '/' .. val)}
|
2020-07-13 20:41:08 +02:00
|
|
|
: val
|
|
|
|
})
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_closure_in_map()
|
|
|
|
mkdir('XclosureDir/tdir', 'p')
|
|
|
|
writefile(['111'], 'XclosureDir/file1')
|
|
|
|
writefile(['222'], 'XclosureDir/file2')
|
|
|
|
writefile(['333'], 'XclosureDir/tdir/file3')
|
|
|
|
|
2020-09-11 19:09:48 +02:00
|
|
|
TreeWalk('XclosureDir')->assert_equal(['file1', 'file2', {'tdir': ['file3']}])
|
2020-07-13 20:41:08 +02:00
|
|
|
|
|
|
|
delete('XclosureDir', 'rf')
|
|
|
|
enddef
|
|
|
|
|
2020-07-15 22:38:56 +02:00
|
|
|
def Test_partial_call()
|
|
|
|
let Xsetlist = function('setloclist', [0])
|
|
|
|
Xsetlist([], ' ', {'title': 'test'})
|
2020-09-11 19:09:48 +02:00
|
|
|
getloclist(0, {'title': 1})->assert_equal({'title': 'test'})
|
2020-07-15 22:38:56 +02:00
|
|
|
|
|
|
|
Xsetlist = function('setloclist', [0, [], ' '])
|
|
|
|
Xsetlist({'title': 'test'})
|
2020-09-11 19:09:48 +02:00
|
|
|
getloclist(0, {'title': 1})->assert_equal({'title': 'test'})
|
2020-07-15 22:38:56 +02:00
|
|
|
|
|
|
|
Xsetlist = function('setqflist')
|
|
|
|
Xsetlist([], ' ', {'title': 'test'})
|
2020-09-11 19:09:48 +02:00
|
|
|
getqflist({'title': 1})->assert_equal({'title': 'test'})
|
2020-07-15 22:38:56 +02:00
|
|
|
|
|
|
|
Xsetlist = function('setqflist', [[], ' '])
|
|
|
|
Xsetlist({'title': 'test'})
|
2020-09-11 19:09:48 +02:00
|
|
|
getqflist({'title': 1})->assert_equal({'title': 'test'})
|
2020-07-15 22:38:56 +02:00
|
|
|
enddef
|
|
|
|
|
2020-08-08 15:10:27 +02:00
|
|
|
def Test_cmd_modifier()
|
|
|
|
tab echo '0'
|
2020-09-06 15:58:36 +02:00
|
|
|
CheckDefFailure(['5tab echo 3'], 'E16:')
|
2020-08-08 15:10:27 +02:00
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_restore_modifiers()
|
|
|
|
# check that when compiling a :def function command modifiers are not messed
|
|
|
|
# up.
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
set eventignore=
|
|
|
|
autocmd QuickFixCmdPost * copen
|
|
|
|
def AutocmdsDisabled()
|
|
|
|
eval 0
|
|
|
|
enddef
|
|
|
|
func Func()
|
|
|
|
noautocmd call s:AutocmdsDisabled()
|
|
|
|
let g:ei_after = &eventignore
|
|
|
|
endfunc
|
|
|
|
Func()
|
|
|
|
END
|
|
|
|
CheckScriptSuccess(lines)
|
2020-09-11 19:09:48 +02:00
|
|
|
g:ei_after->assert_equal('')
|
2020-08-08 15:10:27 +02:00
|
|
|
enddef
|
|
|
|
|
2020-09-10 22:05:08 +02:00
|
|
|
def StackTop()
|
|
|
|
eval 1
|
|
|
|
eval 2
|
|
|
|
# call not on fourth line
|
|
|
|
StackBot()
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def StackBot()
|
|
|
|
# throw an error
|
|
|
|
eval [][0]
|
|
|
|
enddef
|
|
|
|
|
|
|
|
def Test_callstack_def()
|
|
|
|
try
|
|
|
|
StackTop()
|
|
|
|
catch
|
2020-09-11 19:09:48 +02:00
|
|
|
v:throwpoint->assert_match('Test_callstack_def\[2\]..StackTop\[4\]..StackBot, line 2')
|
2020-09-10 22:05:08 +02:00
|
|
|
endtry
|
|
|
|
enddef
|
|
|
|
|
2020-05-03 15:38:16 +02:00
|
|
|
|
2020-04-05 17:08:17 +02:00
|
|
|
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
|