" 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
var l: list<number> = test_null_list()
add(l, 123)
END
CheckDefExecFailure(lines, 'E1130:', 2)
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)
assert_equal(0z67, b2)
var lines =<< trim END
var b: blob
add(b, "x")
END
CheckDefFailure(lines, 'E1012:', 2)
lines =<< trim END
var b: blob = test_null_blob()
add(b, 123)
END
CheckDefExecFailure(lines, 'E1131:', 2)
enddef
def Test_append()
new
setline(1, range(3))
var res1: number = append(1, 'one')
assert_equal(0, res1)
var res2: bool = append(3, 'two')
assert_equal(false, res2)
assert_equal(['0', 'one', '1', 'two', '2'], getline(1, 6))
append(0, 'zero')
assert_equal('zero', getline(1))
bwipe!
enddef
def Test_balloon_show()
CheckGui
CheckFeature balloon_eval
assert_fails('balloon_show(true)', 'E1174:')
enddef
def Test_balloon_split()
CheckFeature balloon_eval_term
assert_fails('balloon_split(true)', 'E1174:')
enddef
def Test_browse()
CheckFeature browse
var lines =<< trim END
browse(1, 2, 3, 4)
END
CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 2')
lines =<< trim END
browse(1, 'title', 3, 4)
END
CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 3')
lines =<< trim END
browse(1, 'title', 'dir', 4)
END
CheckDefExecAndScriptFailure(lines, 'E1174: String required for argument 4')
enddef
def Test_bufexists()
assert_fails('bufexists(true)', 'E1174')
enddef
def Test_buflisted()
var res: bool = buflisted('asdf')
assert_equal(false, res)
assert_fails('buflisted(true)', 'E1174')
enddef
def Test_bufname()
split SomeFile
bufname('%')->assert_equal