Skip to content

Commit fe424d1

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.0415: Some functions are not tested
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 <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent e595e9c commit fe424d1

16 files changed

Lines changed: 184 additions & 14 deletions

src/eval.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,9 +2281,16 @@ tv_op_blob(typval_T *tv1, typval_T *tv2, char_u *op)
22812281
return FAIL;
22822282

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

2287+
if (tv1->vval.v_blob == NULL)
2288+
{
2289+
tv1->vval.v_blob = tv2->vval.v_blob;
2290+
++tv1->vval.v_blob->bv_refcount;
2291+
return OK;
2292+
}
2293+
22872294
blob_T *b1 = tv1->vval.v_blob;
22882295
blob_T *b2 = tv2->vval.v_blob;
22892296
int len = blob_len(b2);
@@ -2455,12 +2462,6 @@ tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
24552462
return FAIL;
24562463
}
24572464

2458-
if (tv2->v_type == VAR_CLASS || tv2->v_type == VAR_TYPEALIAS)
2459-
{
2460-
check_typval_is_value(tv2);
2461-
return FAIL;
2462-
}
2463-
24642465
int retval = FAIL;
24652466
switch (tv1->v_type)
24662467
{
@@ -2476,12 +2477,9 @@ tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
24762477
case VAR_CHANNEL:
24772478
case VAR_INSTR:
24782479
case VAR_OBJECT:
2479-
break;
2480-
24812480
case VAR_CLASS:
24822481
case VAR_TYPEALIAS:
2483-
check_typval_is_value(tv1);
2484-
return FAIL;
2482+
break;
24852483

24862484
case VAR_BLOB:
24872485
retval = tv_op_blob(tv1, tv2, op);
@@ -5162,7 +5160,7 @@ eval_method(
51625160
{
51635161
*arg = name;
51645162

5165-
// Truncate the name a the "(". Avoid trying to get another line
5163+
// Truncate the name at the "(". Avoid trying to get another line
51665164
// by making "getline" NULL.
51675165
*paren = NUL;
51685166
char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
@@ -5217,6 +5215,9 @@ eval_method(
52175215
clear_tv(&base);
52185216
vim_free(tofree);
52195217

5218+
if (alias != NULL)
5219+
vim_free(alias);
5220+
52205221
return ret;
52215222
}
52225223

@@ -5434,7 +5435,7 @@ f_slice(typval_T *argvars, typval_T *rettv)
54345435
|| check_for_opt_number_arg(argvars, 2) == FAIL))
54355436
return;
54365437

5437-
if (check_can_index(argvars, TRUE, FALSE) != OK)
5438+
if (check_can_index(&argvars[0], TRUE, FALSE) != OK)
54385439
return;
54395440

54405441
copy_tv(argvars, rettv);

src/evalfunc.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7533,13 +7533,17 @@ indexof_blob(blob_T *b, long startidx, typval_T *expr)
75337533
set_vim_var_type(VV_KEY, VAR_NUMBER);
75347534
set_vim_var_type(VV_VAL, VAR_NUMBER);
75357535

7536+
int called_emsg_start = called_emsg;
75367537
for (idx = startidx; idx < blob_len(b); ++idx)
75377538
{
75387539
set_vim_var_nr(VV_KEY, idx);
75397540
set_vim_var_nr(VV_VAL, blob_get(b, idx));
75407541

75417542
if (indexof_eval_expr(expr))
75427543
return idx;
7544+
7545+
if (called_emsg != called_emsg_start)
7546+
return -1;
75437547
}
75447548

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

75767580
set_vim_var_type(VV_KEY, VAR_NUMBER);
75777581

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

75867591
if (found)
75877592
return idx;
7593+
7594+
if (called_emsg != called_emsg_start)
7595+
return -1;
75887596
}
75897597

75907598
return -1;
@@ -7608,7 +7616,9 @@ f_indexof(typval_T *argvars, typval_T *rettv)
76087616
|| check_for_opt_dict_arg(argvars, 2) == FAIL)
76097617
return;
76107618

7611-
if ((argvars[1].v_type == VAR_STRING && argvars[1].vval.v_string == NULL)
7619+
if ((argvars[1].v_type == VAR_STRING &&
7620+
(argvars[1].vval.v_string == NULL
7621+
|| *argvars[1].vval.v_string == NUL))
76127622
|| (argvars[1].v_type == VAR_FUNC
76137623
&& argvars[1].vval.v_partial == NULL))
76147624
return;

src/testdir/test_blob.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ func Test_blob_assign()
7474
VAR l = [0z12]
7575
VAR m = deepcopy(l)
7676
LET m[0] = 0z34 #" E742 or E741 should not occur.
77+
78+
VAR blob1 = 0z10
79+
LET blob1 += test_null_blob()
80+
call assert_equal(0z10, blob1)
81+
LET blob1 = test_null_blob()
82+
LET blob1 += 0z20
83+
call assert_equal(0z20, blob1)
7784
END
7885
call v9.CheckLegacyAndVim9Success(lines)
7986

@@ -343,6 +350,17 @@ func Test_blob_for_loop()
343350
call assert_equal(5, i)
344351
END
345352
call v9.CheckLegacyAndVim9Success(lines)
353+
354+
" Test for skipping the loop var assignment in a for loop
355+
let lines =<< trim END
356+
VAR blob = 0z998877
357+
VAR c = 0
358+
for _ in blob
359+
LET c += 1
360+
endfor
361+
call assert_equal(3, c)
362+
END
363+
call v9.CheckLegacyAndVim9Success(lines)
346364
endfunc
347365

348366
func Test_blob_concatenate()
@@ -831,6 +849,7 @@ func Test_indexof()
831849
call assert_equal(-1, indexof(test_null_blob(), "v:val == 0xde"))
832850
call assert_equal(-1, indexof(b, test_null_string()))
833851
call assert_equal(-1, indexof(b, test_null_function()))
852+
call assert_equal(-1, indexof(b, ""))
834853

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

847867
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_edit.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,11 @@ func Test_edit_insert_reg()
19521952
let @r = 'sample'
19531953
call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
19541954
call assert_equal('"', g:Line)
1955+
1956+
" Test for inserting an null and an empty list
1957+
call feedkeys("a\<C-R>=test_null_list()\<CR>", "xt")
1958+
call feedkeys("a\<C-R>=[]\<CR>", "xt")
1959+
call assert_equal(['r'], getbufline('', 1, '$'))
19551960
call test_override('ALL', 0)
19561961
close!
19571962
endfunc

src/testdir/test_fold.vim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,6 +1601,34 @@ func Test_foldtext_scriptlocal_func()
16011601
delfunc s:FoldText
16021602
endfunc
16031603

1604+
" Test for setting 'foldtext' from the modeline and executing the expression
1605+
" in a sandbox
1606+
func Test_foldtext_in_modeline()
1607+
func ModelineFoldText()
1608+
call feedkeys('aFoo', 'xt')
1609+
return "folded text"
1610+
endfunc
1611+
let lines =<< trim END
1612+
func T()
1613+
let i = 1
1614+
endfunc
1615+
" vim: foldenable foldtext=ModelineFoldText()
1616+
END
1617+
call writefile(lines, 'Xmodelinefoldtext', 'D')
1618+
1619+
set modeline modelineexpr
1620+
split Xmodelinefoldtext
1621+
1622+
call cursor(1, 1)
1623+
normal! zf3j
1624+
call assert_equal('folded text', foldtextresult(1))
1625+
call assert_equal(lines, getbufline('', 1, '$'))
1626+
1627+
bw!
1628+
set modeline& modelineexpr&
1629+
delfunc ModelineFoldText
1630+
endfunc
1631+
16041632
" Make sure a fold containing a nested fold is split correctly when using
16051633
" foldmethod=indent
16061634
func Test_fold_split()

src/testdir/test_functions.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,6 +4106,8 @@ func Test_slice()
41064106
call assert_equal('', 'ὰ̳β̳́γ̳̂δ̳̃ε̳̄ζ̳̅'->slice(1, -6))
41074107
END
41084108
call v9.CheckLegacyAndVim9Success(lines)
4109+
4110+
call assert_equal(0, slice(v:true, 1))
41094111
endfunc
41104112

41114113
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_listdict.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func Test_list_slice()
6060
assert_equal([1, 2], l[-3 : -1])
6161
END
6262
call v9.CheckDefAndScriptSuccess(lines)
63+
64+
call assert_fails('let l[[]] = 1', 'E730: Using a List as a String')
65+
call assert_fails('let l[1 : []] = [1]', 'E730: Using a List as a String')
6366
endfunc
6467

6568
" List identity
@@ -178,6 +181,19 @@ func Test_list_assign()
178181
END
179182
call v9.CheckScriptFailure(['vim9script'] + lines, 'E688:')
180183
call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')
184+
185+
let lines =<< trim END
186+
VAR l = [2]
187+
LET l += test_null_list()
188+
call assert_equal([2], l)
189+
LET l = test_null_list()
190+
LET l += [1]
191+
call assert_equal([1], l)
192+
END
193+
call v9.CheckLegacyAndVim9Success(lines)
194+
195+
let d = {'abc': [1, 2, 3]}
196+
call assert_fails('let d.abc[0:0z10] = [10, 20]', 'E976: Using a Blob as a String')
181197
endfunc
182198

183199
" test for range assign
@@ -447,6 +463,9 @@ func Test_dict_assign()
447463
n.key = 3
448464
END
449465
call v9.CheckDefFailure(lines, 'E1141:')
466+
467+
let d = {'abc': {}}
468+
call assert_fails("let d.abc[0z10] = 10", 'E976: Using a Blob as a String')
450469
endfunc
451470

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

15091530
" failure cases
15101531
call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')

src/testdir/test_method.vim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,13 @@ func Test_method_syntax()
136136
call assert_fails('eval [1, 2, 3] ->sort ()', 'E274:')
137137
call assert_fails('eval [1, 2, 3]-> sort ()', 'E274:')
138138
call assert_fails('eval [1, 2, 3]-> sort()', 'E274:')
139+
140+
" Test for using a method name containing a curly brace name
141+
let s = 'len'
142+
call assert_equal(4, "xxxx"->str{s}())
143+
144+
" Test for using a method in an interpolated string
145+
call assert_equal('4', $'{"xxxx"->strlen()}')
139146
endfunc
140147

141148
func Test_method_lambda()

src/testdir/test_partial.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,4 +406,18 @@ func Test_compare_partials()
406406
call assert_false(F1 is N1)
407407
endfunc
408408

409+
func Test_partial_method()
410+
func Foo(x, y, z)
411+
return x + y + z
412+
endfunc
413+
let d = {"Fn": function('Foo', [10, 20])}
414+
call assert_fails('echo 30->d.Fn()', 'E1265: Cannot use a partial here')
415+
delfunc Foo
416+
endfunc
417+
418+
func Test_non_callable_type_as_method()
419+
let d = {"Fn": 10}
420+
call assert_fails('echo 30->d.Fn()', 'E1085: Not a callable type: d.Fn')
421+
endfunc
422+
409423
" vim: shiftwidth=2 sts=2 expandtab

src/testdir/test_vim9_assign.vim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,4 +3687,16 @@ def Test_final_var_with_blob_value()
36873687
v9.CheckScriptSuccess(lines)
36883688
enddef
36893689

3690+
" Test for overwriting a script-local function using the s: dictionary
3691+
def Test_override_script_local_func()
3692+
var lines =<< trim END
3693+
vim9script
3694+
def MyFunc()
3695+
enddef
3696+
var d: dict<any> = s:
3697+
d.MyFunc = function('min')
3698+
END
3699+
v9.CheckScriptFailure(lines, 'E705: Variable name conflicts with existing function: MyFunc', 5)
3700+
enddef
3701+
36903702
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker

0 commit comments

Comments
 (0)