" 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))
enddef
def Test_buflisted()
var res: bool = buflisted('asdf')
assert_equal(false, res)
enddef
def Test_bufname()
split SomeFile
bufname('%')->assert_equal('SomeFile')
edit OtherFile
bufname('#')->assert_equal('SomeFile')
close
enddef
def Test_bufnr()
var buf = bufnr()
bufnr('%')->assert_equal(buf)
buf = bufnr('Xdummy', true)
buf->assert_notequal(-1)
exe 'bwipe! ' .. buf
enddef
def Test_bufwinid()
var origwin = win_getid()
below split SomeFile
var SomeFileID = win_getid()
below split OtherFile
below split SomeFile
bufwinid('SomeFile')->assert_equal(SomeFileID)
win_gotoid(origwin)
only
bwipe SomeFile
bwipe OtherFile
enddef
def Test_call_call()
var l = [3, 2, 1]
call('reverse', [l])
l->assert_equal([1, 2, 3])
enddef
def Test_char2nr()
char2nr('あ', true)->assert_equal(12354)
enddef
def Test_col()
new
setline(1, 'asdf')
col([1, '$'])->assert_equal(5)
enddef
def Test_copy_return_type()
var l = copy([1, 2, 3])
var res = 0
for n in l
res += n
endfor
res->assert_equal(6)
var dl = deepcopy([1, 2, 3])
res = 0
for n in dl
res += n
endfor
res->assert_equal(6)
dl = deepcopy([1, 2, 3], true)
enddef
def Test_count()
count('ABC ABC ABC', 'b', true)->assert_equal(3)
count('ABC ABC ABC', 'b', false)->assert_equal(0)
enddef
def Test_cursor()
new
setline(1, range(4))
cursor(2, 1)
assert_equal(2, getcurpos()[1])
cursor('$', 1)
assert_equal(4, getcurpos()[1])
var lines =<< trim END
cursor('2', 1)
END
CheckDefExecAndScriptFailure(lines, 'E475:')
enddef
def Test_delete()
var res: bool = delete('doesnotexist')
assert_equal(true, res)
enddef
def Test_executable()
assert_false(executable(""))
assert_false(executable(test_null_string()))
CheckDefExecFailure(['echo executable(123)'], 'E928:')
CheckDefExecFailure(['echo executable(true)']