Skip to content

Commit

Permalink
patch 9.1.0415: Some functions are not tested
Browse files Browse the repository at this point in the history
Problem:  Some functions are not tested
Solution: Add a few more tests, fix a few minor problems
          (Yegappan Lakshmanan)

closes: #14789

Signed-off-by: Yegappan Lakshmanan <[email protected]>
Signed-off-by: Christian Brabandt <[email protected]>
  • Loading branch information
yegappan authored and chrisbra committed May 17, 2024
1 parent e595e9c commit fe424d1
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2281,9 +2281,16 @@ tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
return FAIL;

// Blob += Blob
if (tv1->vval.v_blob == NULL || tv2->vval.v_blob == NULL)
if (tv2->vval.v_blob == NULL)
return OK;

if (tv1->vval.v_blob == NULL)
{
tv1->vval.v_blob = tv2->vval.v_blob;
++tv1->vval.v_blob->bv_refcount;
return OK;
}

blob_T *b1 = tv1->vval.v_blob;
blob_T *b2 = tv2->vval.v_blob;
int len = blob_len(b2);
Expand Down Expand Up @@ -2455,12 +2462,6 @@ tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
return FAIL;
}

if (tv2->v_type == VAR_CLASS || tv2->v_type == VAR_TYPEALIAS)
{
check_typval_is_value(tv2);
return FAIL;
}

int retval = FAIL;
switch (tv1->v_type)
{
Expand All @@ -2476,12 +2477,9 @@ tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
case VAR_CHANNEL:
case VAR_INSTR:
case VAR_OBJECT:
break;

case VAR_CLASS:
case VAR_TYPEALIAS:
check_typval_is_value(tv1);
return FAIL;
break;

case VAR_BLOB:
retval = tv_op_blob(tv1, tv2, op);
Expand Down Expand Up @@ -5162,7 +5160,7 @@ eval_method(
{
*arg = name;

// Truncate the name a the "(". Avoid trying to get another line
// Truncate the name at the "(". Avoid trying to get another line
// by making "getline" NULL.
*paren = NUL;
char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
Expand Down Expand Up @@ -5217,6 +5215,9 @@ eval_method(
clear_tv(&base);
vim_free(tofree);

if (alias != NULL)
vim_free(alias);

return ret;
}

Expand Down Expand Up @@ -5434,7 +5435,7 @@ f_slice(typval_T *argvars, typval_T *rettv)
|| check_for_opt_number_arg(argvars, 2) == FAIL))
return;

if (check_can_index(argvars, TRUE, FALSE) != OK)
if (check_can_index(&argvars[0], TRUE, FALSE) != OK)
return;

copy_tv(argvars, rettv);
Expand Down
12 changes: 11 additions & 1 deletion src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -7533,13 +7533,17 @@ indexof_blob(blob_T *b, long startidx, typval_T *expr)
set_vim_var_type(VV_KEY, VAR_NUMBER);
set_vim_var_type(VV_VAL, VAR_NUMBER);

int called_emsg_start = called_emsg;
for (idx = startidx; idx < blob_len(b); ++idx)
{
set_vim_var_nr(VV_KEY, idx);
set_vim_var_nr(VV_VAL, blob_get(b, idx));

if (indexof_eval_expr(expr))
return idx;

if (called_emsg != called_emsg_start)
return -1;
}

return -1;
Expand Down Expand Up @@ -7575,6 +7579,7 @@ indexof_list(list_T *l, long startidx, typval_T *expr)

set_vim_var_type(VV_KEY, VAR_NUMBER);

int called_emsg_start = called_emsg;
for ( ; item != NULL; item = item->li_next, ++idx)
{
set_vim_var_nr(VV_KEY, idx);
Expand All @@ -7585,6 +7590,9 @@ indexof_list(list_T *l, long startidx, typval_T *expr)

if (found)
return idx;

if (called_emsg != called_emsg_start)
return -1;
}

return -1;
Expand All @@ -7608,7 +7616,9 @@ f_indexof(typval_T *argvars, typval_T *rettv)
|| check_for_opt_dict_arg(argvars, 2) == FAIL)
return;

if ((argvars[1].v_type == VAR_STRING && argvars[1].vval.v_string == NULL)
if ((argvars[1].v_type == VAR_STRING &&
(argvars[1].vval.v_string == NULL
|| *argvars[1].vval.v_string == NUL))
|| (argvars[1].v_type == VAR_FUNC
&& argvars[1].vval.v_partial == NULL))
return;
Expand Down
20 changes: 20 additions & 0 deletions src/testdir/test_blob.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func Test_blob_assign()
VAR l = [0z12]
VAR m = deepcopy(l)
LET m[0] = 0z34 #" E742 or E741 should not occur.

VAR blob1 = 0z10
LET blob1 += test_null_blob()
call assert_equal(0z10, blob1)
LET blob1 = test_null_blob()
LET blob1 += 0z20
call assert_equal(0z20, blob1)
END
call v9.CheckLegacyAndVim9Success(lines)

Expand Down Expand Up @@ -343,6 +350,17 @@ func Test_blob_for_loop()
call assert_equal(5, i)
END
call v9.CheckLegacyAndVim9Success(lines)

" Test for skipping the loop var assignment in a for loop
let lines =<< trim END
VAR blob = 0z998877
VAR c = 0
for _ in blob
LET c += 1
endfor
call assert_equal(3, c)
END
call v9.CheckLegacyAndVim9Success(lines)
endfunc

func Test_blob_concatenate()
Expand Down Expand Up @@ -831,6 +849,7 @@ func Test_indexof()
call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))
call assert_equal(-1, indexof(b, test_null_string()))
call assert_equal(-1, indexof(b, test_null_function()))
call assert_equal(-1, indexof(b, ""))

let b = 0z01020102
call assert_equal(1, indexof(b, "v:val == 0x02", #{startidx: 0}))
Expand All @@ -842,6 +861,7 @@ func Test_indexof()
" failure cases
call assert_fails('let i = indexof(b, "val == 0xde")', 'E121:')
call assert_fails('let i = indexof(b, {})', 'E1256:')
call assert_fails('let i = indexof(b, " ")', 'E15:')
endfunc

" vim: shiftwidth=2 sts=2 expandtab
5 changes: 5 additions & 0 deletions src/testdir/test_edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,11 @@ func Test_edit_insert_reg()
let @r = 'sample'
call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
call assert_equal('"', g:Line)

" Test for inserting an null and an empty list
call feedkeys("a\<C-R>=test_null_list()\<CR>", "xt")
call feedkeys("a\<C-R>=[]\<CR>", "xt")
call assert_equal(['r'], getbufline('', 1, '$'))
call test_override('ALL', 0)
close!
endfunc
Expand Down
28 changes: 28 additions & 0 deletions src/testdir/test_fold.vim
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,34 @@ func Test_foldtext_scriptlocal_func()
delfunc s:FoldText
endfunc

" Test for setting 'foldtext' from the modeline and executing the expression
" in a sandbox
func Test_foldtext_in_modeline()
func ModelineFoldText()
call feedkeys('aFoo', 'xt')
return "folded text"
endfunc
let lines =<< trim END
func T()
let i = 1
endfunc
" vim: foldenable foldtext=ModelineFoldText()
END
call writefile(lines, 'Xmodelinefoldtext', 'D')

set modeline modelineexpr
split Xmodelinefoldtext

call cursor(1, 1)
normal! zf3j
call assert_equal('folded text', foldtextresult(1))
call assert_equal(lines, getbufline('', 1, '$'))

bw!
set modeline& modelineexpr&
delfunc ModelineFoldText
endfunc

" Make sure a fold containing a nested fold is split correctly when using
" foldmethod=indent
func Test_fold_split()
Expand Down
2 changes: 2 additions & 0 deletions src/testdir/test_functions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4106,6 +4106,8 @@ func Test_slice()
call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -6))
END
call v9.CheckLegacyAndVim9Success(lines)

call assert_equal(0, slice(v:true, 1))
endfunc

" vim: shiftwidth=2 sts=2 expandtab
21 changes: 21 additions & 0 deletions src/testdir/test_listdict.vim
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func Test_list_slice()
assert_equal([1, 2], l[-3 : -1])
END
call v9.CheckDefAndScriptSuccess(lines)

call assert_fails('let l[[]] = 1', 'E730: Using a List as a String')
call assert_fails('let l[1 : []] = [1]', 'E730: Using a List as a String')
endfunc

" List identity
Expand Down Expand Up @@ -178,6 +181,19 @@ func Test_list_assign()
END
call v9.CheckScriptFailure(['vim9script'] + lines, 'E688:')
call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')

let lines =<< trim END
VAR l = [2]
LET l += test_null_list()
call assert_equal([2], l)
LET l = test_null_list()
LET l += [1]
call assert_equal([1], l)
END
call v9.CheckLegacyAndVim9Success(lines)

let d = {'abc': [1, 2, 3]}
call assert_fails('let d.abc[0:0z10] = [10, 20]', 'E976: Using a Blob as a String')
endfunc

" test for range assign
Expand Down Expand Up @@ -447,6 +463,9 @@ func Test_dict_assign()
n.key = 3
END
call v9.CheckDefFailure(lines, 'E1141:')

let d = {'abc': {}}
call assert_fails("let d.abc[0z10] = 10", 'E976: Using a Blob as a String')
endfunc

" Function in script-local List or Dict
Expand Down Expand Up @@ -1505,6 +1524,8 @@ func Test_indexof()
call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
call assert_equal(-1, indexof(l, test_null_string()))
call assert_equal(-1, indexof(l, test_null_function()))
call assert_equal(-1, indexof(l, ""))
call assert_fails('let i = indexof(l, " ")', 'E15:')

" failure cases
call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
Expand Down
7 changes: 7 additions & 0 deletions src/testdir/test_method.vim
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ func Test_method_syntax()
call assert_fails('eval [1, 2, 3] ->sort ()', 'E274:')
call assert_fails('eval [1, 2, 3]-> sort ()', 'E274:')
call assert_fails('eval [1, 2, 3]-> sort()', 'E274:')

" Test for using a method name containing a curly brace name
let s = 'len'
call assert_equal(4, "xxxx"->str{s}())

" Test for using a method in an interpolated string
call assert_equal('4', $'{"xxxx"->strlen()}')
endfunc

func Test_method_lambda()
Expand Down
14 changes: 14 additions & 0 deletions src/testdir/test_partial.vim
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,18 @@ func Test_compare_partials()
call assert_false(F1 is N1)
endfunc

func Test_partial_method()
func Foo(x, y, z)
return x + y + z
endfunc
let d = {"Fn": function('Foo', [10, 20])}
call assert_fails('echo 30->d.Fn()', 'E1265: Cannot use a partial here')
delfunc Foo
endfunc

func Test_non_callable_type_as_method()
let d = {"Fn": 10}
call assert_fails('echo 30->d.Fn()', 'E1085: Not a callable type: d.Fn')
endfunc

" vim: shiftwidth=2 sts=2 expandtab
12 changes: 12 additions & 0 deletions src/testdir/test_vim9_assign.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3687,4 +3687,16 @@ def Test_final_var_with_blob_value()
v9.CheckScriptSuccess(lines)
enddef

" Test for overwriting a script-local function using the s: dictionary
def Test_override_script_local_func()
var lines =<< trim END
vim9script
def MyFunc()
enddef
var d: dict<any> = s:
d.MyFunc = function('min')
END
v9.CheckScriptFailure(lines, 'E705: Variable name conflicts with existing function: MyFunc', 5)
enddef

" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
1 change: 1 addition & 0 deletions src/testdir/test_vim9_builtin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,7 @@ def Test_indexof()
indexof(l, (i, v) => v.color == 'blue')->assert_equal(1)
indexof(l, (i, v) => v.color == 'blue', {startidx: 1})->assert_equal(1)
indexof(l, (i, v) => v.color == 'blue', {startidx: 2})->assert_equal(3)
indexof(l, "")->assert_equal(-1)
var b = 0zdeadbeef
indexof(b, "v:val == 0xef")->assert_equal(3)

Expand Down
10 changes: 10 additions & 0 deletions src/testdir/test_vim9_expr.vim
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ def Test_expr2()
g:vals = [1]
endif
assert_equal([1], g:vals)

# string interpolation with ||
assert_equal('true', $"{0 || 1}")
END
v9.CheckDefAndScriptSuccess(lines)

Expand Down Expand Up @@ -415,6 +418,7 @@ def Test_expr2_fails()
v9.CheckDefExecAndScriptFailure(['var x = 3', 'if x', 'endif'], 'E1023:', 2)

v9.CheckDefAndScriptFailure(["var x = [] || false"], ['E1012: Type mismatch; expected bool but got list<any>', 'E745:'], 1)
v9.CheckDefAndScriptFailure(["var x = $'{false || []}'"], ['E1012: Type mismatch; expected bool but got list<any>', 'E745:'], 1)

var lines =<< trim END
vim9script
Expand Down Expand Up @@ -475,6 +479,9 @@ def Test_expr3()
failed = true
endif
assert_false(failed)

# string interpolation with &&
assert_equal('false', $"{1 && 0}")
END
v9.CheckDefAndScriptSuccess(lines)
enddef
Expand Down Expand Up @@ -574,6 +581,8 @@ def Test_expr3_fails()
echo true && s
END
v9.CheckDefAndScriptFailure(lines, ['E1012: Type mismatch; expected bool but got string', 'E1135: Using a String as a Bool: "asdf"'])

v9.CheckDefAndScriptFailure(["var x = $'{true && []}'"], ['E1012: Type mismatch; expected bool but got list<any>', 'E745:'], 1)
enddef

" global variables to use for tests with the "any" type
Expand Down Expand Up @@ -2152,6 +2161,7 @@ def Test_expr9_blob()
v9.CheckDefAndScriptSuccess(lines)

v9.CheckDefAndScriptFailure(["var x = 0z123"], 'E973:', 1)
v9.CheckDefAndScriptFailure(["var x = null_blox"], ['E1001:', 'E121:'], 1)
enddef

def Test_expr9_string()
Expand Down
16 changes: 16 additions & 0 deletions src/testdir/test_vim9_import.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3311,4 +3311,20 @@ def Test_import_from_vimrc()
delete('Xoutput.log')
enddef

" Test for changing a locked imported variable
def Test_import_locked_var()
var lines =<< trim END
vim9script
export var Foo: number = 10
lockvar Foo
END
writefile(lines, 'Ximportlockedvar.vim', 'D')
lines =<< trim END
vim9script
import './Ximportlockedvar.vim' as Bar
Bar.Foo = 20
END
v9.CheckScriptFailure(lines, 'E741: Value is locked: Foo', 3)
enddef

" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

0 comments on commit fe424d1

Please sign in to comment.