" Test using builtin functions in the Vim9 script language.
source check.vim
source vim9.vim
" Test for passing too many or too few arguments to builtin functions
func Test_internalfunc_arg_error()
let l =<< trim END
def! FArgErr(): float
return ceil(1.1, 2)
enddef
defcompile
END
call writefile(l, 'Xinvalidarg')
call assert_fails('so Xinvalidarg', 'E118:', '', 1, 'FArgErr')
let l =<< trim END
def! FArgErr(): float
return ceil()
enddef
defcompile
END
call writefile(l, 'Xinvalidarg')
call assert_fails('so Xinvalidarg', 'E119:', '', 1, 'FArgErr')
call delete('Xinvalidarg')
endfunc
" Test for builtin functions returning different types
func Test_InternalFuncRetType()
let lines =<< trim END
def RetFloat(): float
return ceil(1.456)
enddef
def RetListAny(): list<any>
return items({k: 'v'})
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
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({})
call delete('Xscript')
endfunc
def Test_abs()
assert_equal(0, abs(0))
assert_equal(2, abs(-2))
assert_equal(3, abs(3))
CheckDefFailure(['abs("text")'], 'E1013: Argument 1: type mismatch, expected number but got string', 1)
if has('float')
assert_equal(0, abs(0))
assert_equal(2.0, abs(-2.0))
assert_equal(3.0, abs(3.0))
endif
enddef
def Test_add_list()
var l: list<number> # defaults to empty list
add(l, 9)
assert_equal([9], l)
var lines =<< trim END
var l: list<number>
add(l, "x")
END
CheckDefFailure(lines, 'E1012:', 2)
lines =<< trim END
add(test_null_list(), 123)
END
CheckDefExecAndScriptFailure(lines, 'E1130:', 1)
lines =<< trim END
var l: list<number> = test_null_list()
add(l, 123)
END
CheckDefExecFailure(lines, 'E1130:', 2)
# Getting variable with NULL list allocates a new list at script level
lines =<< trim END
vim9script
var l: list<number> = test_null_list()
add(l, 123)
END
CheckScriptSuccess(lines)
enddef
def Test_add_blob()
var b1: blob = 0z12
add(b1, 0x34)
assert_equal(0z1234, b1)
var b2: blob # defaults to empty blob
add(b2, 0x67)