" Test various aspects of the Vim9 script language.
source check.vim
source view_util.vim
source vim9.vim
source screendump.vim
func Test_def_basic()
def SomeFunc(): string
return 'yes'
enddef
call assert_equal('yes', SomeFunc())
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()
assert_equal('string', ReturnString())
assert_equal(123, ReturnNumber())
assert_fails('call ReturnGlobal()', 'E1029: Expected number but got string')
enddef
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
let s:nothing = 0
def ReturnNothing()
s:nothing = 1
if true
return
endif
s:nothing = 2
enddef
def Test_return_nothing()
ReturnNothing()
assert_equal(1, s:nothing)
enddef
func Increment()
let g:counter += 1
endfunc
def Test_call_ufunc_count()
g:counter = 1
Increment()
Increment()
Increment()
# works with and without :call
assert_equal(4, g:counter)
call assert_equal(4, g:counter)
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()
assert_equal('one', MyVarargs('one'))
assert_equal('one,two', MyVarargs('one', 'two'))
assert_equal('one,two,three', MyVarargs('one', 'two', 'three'))
enddef
def MyDefaultArgs(name = 'string'): string
return name
enddef
def MyDefaultSecond(name: string, second: bool = true): string
return second ? name : 'none'
enddef
def Test_call_default_args()
assert_equal('string', MyDefaultArgs())
assert_equal('one', MyDefaultArgs('one'))
assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
assert_equal('test', MyDefaultSecond('test'))
assert_equal('test', MyDefaultSecond('test', true))
assert_equal('none', MyDefaultSecond('test', false))
CheckScriptFailure(['def Func(arg: number = asdf)', 'enddef', 'defcompile'], 'E1001:')
CheckScriptFailure(['def Func(arg: number = "text")', 'enddef', 'defcompile'], 'E1013: argument 1: type mismatch, expected number but got string')
enddef
def Test_nested_function()
def Nested(arg: string): string
return 'nested ' .. arg
enddef
assert_equal('nested function', Nested('function'))
CheckDefFailure(['def Nested()', 'enddef', 'Nested(66)'], 'E118:')
CheckDefFailure(['def Nested(arg: string)', 'enddef', 'Nested()'], 'E119:')
CheckDefFailure(['func Nested()', 'endfunc'], 'E1086:')
enddef
func Test_call_default_args_from_func()
call assert_equal('string', MyDefaultArgs())
call assert_equal('one', MyDefaultArgs('one'))
call assert_fails('call MyDefaultArgs("one", "two")', 'E118:')
endfunc
func TakesOneArg(arg)
echo a:arg
endfunc
def Test_call_wrong_args()
call CheckDefFailure(['TakesOneArg()'], 'E119:')
call CheckDefFailure(['TakesOneArg(11, 22)'], 'E118:')
call CheckDefFailure(['bufnr(xxx)'], 'E1001:')
call CheckScriptFailure(['def Func(Ref: func(s: string))'], 'E475:')
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()
call assert_fails('call MyDefVarargs()', 'E119:')
assert_equal('one,foo', MyDefVarargs('one'))
assert_equal('one,two', MyDefVarargs('one', 'two'))
assert_equal('one,two,three', MyDefVarargs('one', 'two', 'three'))
CheckDefFailure(['MyDefVarargs("one", 22)'],
'E1013: argument 2: type mismatch, expected string but got number')
CheckDefFailure(['MyDefVarargs("one", "two", 123)'],
'E1013: argument 3: type mismatch, expected string but got number')
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: