Merge pull request #23996 from zeertzjq/vim-8.2.3191

vim-patch:8.2.{2533,3191,3202,partial:3335}
This commit is contained in:
zeertzjq
2023-06-12 17:50:34 +08:00
committed by GitHub
5 changed files with 422 additions and 257 deletions

View File

@@ -1604,13 +1604,7 @@ char *get_lval(char *const name, typval_T *const rettv, lval_T *const lp, const
lp->ll_dict = NULL; lp->ll_dict = NULL;
lp->ll_list = lp->ll_tv->vval.v_list; lp->ll_list = lp->ll_tv->vval.v_list;
lp->ll_li = tv_list_find(lp->ll_list, (int)lp->ll_n1); lp->ll_li = tv_list_find_index(lp->ll_list, &lp->ll_n1);
if (lp->ll_li == NULL) {
if (lp->ll_n1 < 0) {
lp->ll_n1 = 0;
lp->ll_li = tv_list_find(lp->ll_list, (int)lp->ll_n1);
}
}
if (lp->ll_li == NULL) { if (lp->ll_li == NULL) {
tv_clear(&var2); tv_clear(&var2);
if (!quiet) { if (!quiet) {

View File

@@ -1507,6 +1507,21 @@ const char *tv_list_find_str(list_T *const l, const int n)
return tv_get_string(TV_LIST_ITEM_TV(li)); return tv_get_string(TV_LIST_ITEM_TV(li));
} }
/// Like tv_list_find() but when a negative index is used that is not found use
/// zero and set "idx" to zero. Used for first index of a range.
listitem_T *tv_list_find_index(list_T *const l, long *const idx)
FUNC_ATTR_WARN_UNUSED_RESULT
{
listitem_T *li = tv_list_find(l, (int)(*idx));
if (li == NULL) {
if (*idx < 0) {
*idx = 0;
li = tv_list_find(l, (int)(*idx));
}
}
return li;
}
/// Locate item in a list and return its index /// Locate item in a list and return its index
/// ///
/// @param[in] l List to search. /// @param[in] l List to search.
@@ -3004,11 +3019,12 @@ static void tv_dict_list(typval_T *const tv, typval_T *const rettv, const DictLi
emsg(_(e_dictreq)); emsg(_(e_dictreq));
return; return;
} }
if (tv->vval.v_dict == NULL) {
return;
}
tv_list_alloc_ret(rettv, tv_dict_len(tv->vval.v_dict)); tv_list_alloc_ret(rettv, tv_dict_len(tv->vval.v_dict));
if (tv->vval.v_dict == NULL) {
// NULL dict behaves like an empty dict
return;
}
TV_DICT_ITER(tv->vval.v_dict, di, { TV_DICT_ITER(tv->vval.v_dict, di, {
typval_T tv_item = { .v_lock = VAR_UNLOCKED }; typval_T tv_item = { .v_lock = VAR_UNLOCKED };

View File

@@ -1056,25 +1056,10 @@ static int do_unlet_var(lval_T *lp, char *name_end, exarg_T *eap, int deep FUNC_
lp->ll_name_len))) { lp->ll_name_len))) {
return FAIL; return FAIL;
} else if (lp->ll_range) { } else if (lp->ll_range) {
assert(lp->ll_list != NULL); if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_name_len,
// Delete a range of List items. lp->ll_n1, !lp->ll_empty2, lp->ll_n2) == FAIL) {
listitem_T *const first_li = lp->ll_li; return FAIL;
listitem_T *last_li = first_li;
while (true) {
listitem_T *const li = TV_LIST_ITEM_NEXT(lp->ll_list, lp->ll_li);
if (value_check_lock(TV_LIST_ITEM_TV(lp->ll_li)->v_lock,
lp->ll_name,
lp->ll_name_len)) {
return false;
} }
lp->ll_li = li;
lp->ll_n1++;
if (lp->ll_li == NULL || (!lp->ll_empty2 && lp->ll_n2 < lp->ll_n1)) {
break;
}
last_li = lp->ll_li;
}
tv_list_remove_items(lp->ll_list, first_li, last_li);
} else { } else {
if (lp->ll_list != NULL) { if (lp->ll_list != NULL) {
// unlet a List item. // unlet a List item.
@@ -1107,6 +1092,31 @@ static int do_unlet_var(lval_T *lp, char *name_end, exarg_T *eap, int deep FUNC_
return ret; return ret;
} }
/// Unlet one item or a range of items from a list.
/// Return OK or FAIL.
static int list_unlet_range(list_T *const l, listitem_T *const li_first, const char *const name,
const size_t name_len, const long n1_arg, const bool has_n2,
const long n2)
{
assert(l != NULL);
// Delete a range of List items.
listitem_T *li_last = li_first;
long n1 = n1_arg;
while (true) {
if (value_check_lock(TV_LIST_ITEM_TV(li_last)->v_lock, name, name_len)) {
return FAIL;
}
listitem_T *const li = TV_LIST_ITEM_NEXT(l, li_last);
n1++;
if (li == NULL || (has_n2 && n2 < n1)) {
break;
}
li_last = li;
}
tv_list_remove_items(l, li_first, li_last);
return OK;
}
/// unlet a variable /// unlet a variable
/// ///
/// @param[in] name Variable name to unlet. /// @param[in] name Variable name to unlet.

View File

@@ -60,68 +60,120 @@ endfunc
" List identity " List identity
func Test_list_identity() func Test_list_identity()
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},] let lines =<< trim END
let ll = l VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
let lx = copy(l) VAR ll = l
VAR lx = copy(l)
call assert_true(l == ll) call assert_true(l == ll)
call assert_false(l isnot ll) call assert_false(l isnot ll)
call assert_true(l is ll) call assert_true(l is ll)
call assert_true(l == lx) call assert_true(l == lx)
call assert_false(l is lx) call assert_false(l is lx)
call assert_true(l isnot lx) call assert_true(l isnot lx)
END
call CheckLegacyAndVim9Success(lines)
endfunc endfunc
" removing items with :unlet " removing items with :unlet
func Test_list_unlet() func Test_list_unlet()
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},] let lines =<< trim END
VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
unlet l[2] unlet l[2]
call assert_equal([1, 'as''d', {'a': 1}], l) call assert_equal([1, 'as''d', {'a': 1}], l)
let l = range(8) LET l = range(8)
unlet l[:3] unlet l[: 3]
unlet l[1:] unlet l[1 :]
call assert_equal([4], l) call assert_equal([4], l)
" removing items out of range: silently skip items that don't exist #" removing items out of range: silently skip items that don't exist
let l = [0, 1, 2, 3] LET l = [0, 1, 2, 3]
call assert_fails('unlet l[2:1]', 'E684') unlet l[2 : 2]
call assert_equal([0, 1, 3], l)
LET l = [0, 1, 2, 3]
unlet l[2 : 3]
call assert_equal([0, 1], l)
LET l = [0, 1, 2, 3]
unlet l[2 : 4]
call assert_equal([0, 1], l)
LET l = [0, 1, 2, 3]
unlet l[2 : 5]
call assert_equal([0, 1], l)
LET l = [0, 1, 2, 3]
unlet l[-2 : 2]
call assert_equal([0, 1, 3], l)
LET l = [0, 1, 2, 3]
unlet l[-3 : 2]
call assert_equal([0, 3], l)
LET l = [0, 1, 2, 3]
unlet l[-4 : 2]
call assert_equal([3], l)
LET l = [0, 1, 2, 3]
unlet l[-5 : 2]
call assert_equal([3], l)
LET l = [0, 1, 2, 3]
unlet l[-6 : 2]
call assert_equal([3], l)
END
call CheckLegacyAndVim9Success(lines)
let l = [0, 1, 2, 3] let l = [0, 1, 2, 3]
unlet l[2:2] unlet l[2:2]
call assert_equal([0, 1, 3], l) call assert_equal([0, 1, 3], l)
let l = [0, 1, 2, 3] let l = [0, 1, 2, 3]
unlet l[2:3] unlet l[2:3]
call assert_equal([0, 1], l) call assert_equal([0, 1], l)
let l = [0, 1, 2, 3]
unlet l[2:4] let lines =<< trim END
call assert_equal([0, 1], l) VAR l = [0, 1, 2, 3]
let l = [0, 1, 2, 3] unlet l[2 : 1]
unlet l[2:5] END
call assert_equal([0, 1], l) call CheckLegacyAndVim9Failure(lines, 'E684:')
let l = [0, 1, 2, 3]
call assert_fails('unlet l[-1:2]', 'E684') let lines =<< trim END
let l = [0, 1, 2, 3] VAR l = [0, 1, 2, 3]
unlet l[-2:2] unlet l[-1 : 2]
call assert_equal([0, 1, 3], l) END
let l = [0, 1, 2, 3] call CheckLegacyAndVim9Failure(lines, 'E684:')
unlet l[-3:2]
call assert_equal([0, 3], l)
let l = [0, 1, 2, 3]
unlet l[-4:2]
call assert_equal([3], l)
let l = [0, 1, 2, 3]
unlet l[-5:2]
call assert_equal([3], l)
let l = [0, 1, 2, 3]
unlet l[-6:2]
call assert_equal([3], l)
endfunc endfunc
" assignment to a list " assignment to a list
func Test_list_assign() func Test_list_assign()
let l = [0, 1, 2, 3] let lines =<< trim END
let [va, vb] = l[2:3] VAR l = [0, 1, 2, 3]
VAR va = 0
VAR vb = 0
LET [va, vb] = l[2 : 3]
call assert_equal([2, 3], [va, vb]) call assert_equal([2, 3], [va, vb])
call assert_fails('let [va, vb] = l', 'E687') END
call assert_fails('let [va, vb] = l[1:1]', 'E688') call CheckLegacyAndVim9Success(lines)
let lines =<< trim END
let l = [0, 1, 2, 3]
let [va, vb] = l
END
call CheckScriptFailure(lines, 'E687:')
let lines =<< trim END
var l = [0, 1, 2, 3]
var va = 0
var vb = 0
[va, vb] = l
END
call CheckScriptFailure(['vim9script'] + lines, 'E687:')
call CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 4')
let lines =<< trim END
let l = [0, 1, 2, 3]
let [va, vb] = l[1:1]
END
call CheckScriptFailure(lines, 'E688:')
let lines =<< trim END
var l = [0, 1, 2, 3]
var va = 0
var vb = 0
[va, vb] = l[1 : 1]
END
call CheckScriptFailure(['vim9script'] + lines, 'E688:')
call CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')
endfunc endfunc
" test for range assign " test for range assign
@@ -135,31 +187,34 @@ endfunc
" Test removing items in list " Test removing items in list
func Test_list_func_remove() func Test_list_func_remove()
" Test removing 1 element let lines =<< trim END
let l = [1, 2, 3, 4] #" Test removing 1 element
VAR l = [1, 2, 3, 4]
call assert_equal(1, remove(l, 0)) call assert_equal(1, remove(l, 0))
call assert_equal([2, 3, 4], l) call assert_equal([2, 3, 4], l)
let l = [1, 2, 3, 4] LET l = [1, 2, 3, 4]
call assert_equal(2, remove(l, 1)) call assert_equal(2, remove(l, 1))
call assert_equal([1, 3, 4], l) call assert_equal([1, 3, 4], l)
let l = [1, 2, 3, 4] LET l = [1, 2, 3, 4]
call assert_equal(4, remove(l, -1)) call assert_equal(4, remove(l, -1))
call assert_equal([1, 2, 3], l) call assert_equal([1, 2, 3], l)
" Test removing range of element(s) #" Test removing range of element(s)
let l = [1, 2, 3, 4] LET l = [1, 2, 3, 4]
call assert_equal([3], remove(l, 2, 2)) call assert_equal([3], remove(l, 2, 2))
call assert_equal([1, 2, 4], l) call assert_equal([1, 2, 4], l)
let l = [1, 2, 3, 4] LET l = [1, 2, 3, 4]
call assert_equal([2, 3], remove(l, 1, 2)) call assert_equal([2, 3], remove(l, 1, 2))
call assert_equal([1, 4], l) call assert_equal([1, 4], l)
let l = [1, 2, 3, 4] LET l = [1, 2, 3, 4]
call assert_equal([2, 3], remove(l, -3, -2)) call assert_equal([2, 3], remove(l, -3, -2))
call assert_equal([1, 4], l) call assert_equal([1, 4], l)
END
call CheckLegacyAndVim9Success(lines)
" Test invalid cases " Test invalid cases
let l = [1, 2, 3, 4] let l = [1, 2, 3, 4]
@@ -172,15 +227,20 @@ endfunc
" List add() function " List add() function
func Test_list_add() func Test_list_add()
let l = [] let lines =<< trim END
VAR l = []
call add(l, 1) call add(l, 1)
call add(l, [2, 3]) call add(l, [2, 3])
call add(l, []) call add(l, [])
call add(l, v:_null_list) call add(l, v:_null_list)
call add(l, {'k' : 3}) call add(l, {'k': 3})
call add(l, {}) call add(l, {})
call add(l, v:_null_dict) call add(l, v:_null_dict)
call assert_equal([1, [2, 3], [], [], {'k' : 3}, {}, {}], l) call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l)
END
call CheckLegacyAndVim9Success(lines)
" weird legacy behavior
" call assert_equal(1, add(v:_null_list, 4)) " call assert_equal(1, add(v:_null_list, 4))
endfunc endfunc
@@ -188,11 +248,21 @@ endfunc
func Test_dict() func Test_dict()
" Creating Dictionary directly with different types " Creating Dictionary directly with different types
let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},} let lines =<< trim END
VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}, }
call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d)) call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
call assert_equal('asd', d.1) call assert_equal('asd', d.1)
call assert_equal(['-1', '1', 'b'], sort(keys(d))) call assert_equal(['-1', '1', 'b'], sort(keys(d)))
call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d)) call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d))
call extend(d, {3: 33, 1: 99})
call extend(d, {'b': 'bbb', 'c': 'ccc'}, "keep")
call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d)
END
call CheckLegacyAndVim9Success(lines)
let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
let v = [] let v = []
for [key, val] in items(d) for [key, val] in items(d)
call extend(v, [key, val]) call extend(v, [key, val])
@@ -200,12 +270,8 @@ func Test_dict()
endfor endfor
call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v) call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v)
call extend(d, {3:33, 1:99}) call extend(d, {3: 33, 1: 99})
call extend(d, {'b':'bbb', 'c':'ccc'}, "keep") call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737:')
call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737')
call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d)
call filter(d, 'v:key =~ ''[ac391]''')
call assert_equal({'c': 'ccc', '1': 99, '3': 33, '-1': {'a': 1}}, d)
" duplicate key " duplicate key
call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:') call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:')
@@ -230,23 +296,29 @@ endfunc
" Dictionary identity " Dictionary identity
func Test_dict_identity() func Test_dict_identity()
let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},} let lines =<< trim END
let dd = d VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, }
let dx = copy(d) VAR dd = d
VAR dx = copy(d)
call assert_true(d == dd) call assert_true(d == dd)
call assert_false(d isnot dd) call assert_false(d isnot dd)
call assert_true(d is dd) call assert_true(d is dd)
call assert_true(d == dx) call assert_true(d == dx)
call assert_false(d is dx) call assert_false(d is dx)
call assert_true(d isnot dx) call assert_true(d isnot dx)
END
call CheckLegacyAndVim9Success(lines)
endfunc endfunc
" removing items with :unlet " removing items with :unlet
func Test_dict_unlet() func Test_dict_unlet()
let d = {'b':'bbb', '1': 99, '3': 33, '-1': {'a': 1}} let lines =<< trim END
VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}}
unlet d.b unlet d.b
unlet d[-1] unlet d[-1]
call assert_equal({'1': 99, '3': 33}, d) call assert_equal({'1': 99, '3': 33}, d)
END
call CheckLegacyAndVim9Success(lines)
endfunc endfunc
" manipulating a big Dictionary (hashtable.c has a border of 1000 entries) " manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
@@ -314,8 +386,30 @@ func Test_dict_assign()
let d._ = 2 let d._ = 2
call assert_equal({'1': 1, '_': 2}, d) call assert_equal({'1': 1, '_': 2}, d)
let lines =<< trim END
VAR d = {}
LET d.a = 1
LET d._ = 2
call assert_equal({'a': 1, '_': 2}, d)
END
call CheckLegacyAndVim9Success(lines)
let lines =<< trim END
let n = 0 let n = 0
call assert_fails('let n.key = 3', 'E1203: Dot can only be used on a dictionary: n.key = 3') let n.key = 3
END
call CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
let lines =<< trim END
vim9script
var n = 0
n.key = 3
END
call CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
let lines =<< trim END
var n = 0
n.key = 3
END
call CheckDefFailure(lines, 'E1141:')
endfunc endfunc
" Function in script-local List or Dict " Function in script-local List or Dict
@@ -332,13 +426,41 @@ endfunc
" Test removing items in a dictionary " Test removing items in a dictionary
func Test_dict_func_remove() func Test_dict_func_remove()
let d = {1:'a', 2:'b', 3:'c'} let lines =<< trim END
VAR d = {1: 'a', 2: 'b', 3: 'c'}
call assert_equal('b', remove(d, 2)) call assert_equal('b', remove(d, 2))
call assert_equal({1:'a', 3:'c'}, d) call assert_equal({1: 'a', 3: 'c'}, d)
END
call CheckLegacyAndVim9Success(lines)
call assert_fails("call remove(d, 1, 2)", 'E118:') let lines =<< trim END
call assert_fails("call remove(d, 'a')", 'E716:') VAR d = {1: 'a', 3: 'c'}
call assert_fails("call remove(d, [])", 'E730:') call remove(d, 1, 2)
END
call CheckLegacyAndVim9Failure(lines, 'E118:')
let lines =<< trim END
VAR d = {1: 'a', 3: 'c'}
call remove(d, 'a')
END
call CheckLegacyAndVim9Failure(lines, 'E716:')
let lines =<< trim END
let d = {1: 'a', 3: 'c'}
call remove(d, [])
END
call CheckScriptFailure(lines, 'E730:')
let lines =<< trim END
vim9script
var d = {1: 'a', 3: 'c'}
call remove(d, [])
END
call CheckScriptFailure(lines, 'E1174: String required for argument 2')
let lines =<< trim END
var d = {1: 'a', 3: 'c'}
call remove(d, [])
END
call CheckDefExecFailure(lines, 'E1013: Argument 2: type mismatch, expected string but got list<unknown>')
endfunc endfunc
" Nasty: remove func from Dict that's being called (works) " Nasty: remove func from Dict that's being called (works)
@@ -354,7 +476,7 @@ endfunc
func Test_dict_literal_keys() func Test_dict_literal_keys()
call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},) call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},)
" why *{} cannot be used " why *{} cannot be used for a literal dictionary
let blue = 'blue' let blue = 'blue'
call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue'))) call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
endfunc endfunc
@@ -546,15 +668,13 @@ endfunc
" No :unlet after lock on dict: " No :unlet after lock on dict:
func Test_dict_lock_unlet() func Test_dict_lock_unlet()
unlet! d
let d = {'a': 99, 'b': 100} let d = {'a': 99, 'b': 100}
lockvar 1 d lockvar 1 d
call assert_fails('unlet d.a', 'E741') call assert_fails('unlet d.a', 'E741:')
endfunc endfunc
" unlet after lock on dict item " unlet after lock on dict item
func Test_dict_item_lock_unlet() func Test_dict_item_lock_unlet()
unlet! d
let d = {'a': 99, 'b': 100} let d = {'a': 99, 'b': 100}
lockvar d.a lockvar d.a
unlet d.a unlet d.a
@@ -563,7 +683,6 @@ endfunc
" filter() after lock on dict item " filter() after lock on dict item
func Test_dict_lock_filter() func Test_dict_lock_filter()
unlet! d
let d = {'a': 99, 'b': 100} let d = {'a': 99, 'b': 100}
lockvar d.a lockvar d.a
call filter(d, 'v:key != "a"') call filter(d, 'v:key != "a"')
@@ -572,7 +691,6 @@ endfunc
" map() after lock on dict " map() after lock on dict
func Test_dict_lock_map() func Test_dict_lock_map()
unlet! d
let d = {'a': 99, 'b': 100} let d = {'a': 99, 'b': 100}
lockvar 1 d lockvar 1 d
call map(d, 'v:val + 200') call map(d, 'v:val + 200')
@@ -581,16 +699,14 @@ endfunc
" No extend() after lock on dict item " No extend() after lock on dict item
func Test_dict_lock_extend() func Test_dict_lock_extend()
unlet! d
let d = {'a': 99, 'b': 100} let d = {'a': 99, 'b': 100}
lockvar d.a lockvar d.a
call assert_fails("call extend(d, {'a' : 123})", 'E741') call assert_fails("call extend(d, {'a' : 123})", 'E741:')
call assert_equal({'a': 99, 'b': 100}, d) call assert_equal({'a': 99, 'b': 100}, d)
endfunc endfunc
" Cannot use += with a locked dict " Cannot use += with a locked dict
func Test_dict_lock_operator() func Test_dict_lock_operator()
unlet! d
let d = {} let d = {}
lockvar d lockvar d
call assert_fails("let d += {'k' : 10}", 'E741:') call assert_fails("let d += {'k' : 10}", 'E741:')
@@ -599,7 +715,7 @@ endfunc
" No remove() of write-protected scope-level variable " No remove() of write-protected scope-level variable
func Tfunc1(this_is_a_long_parameter_name) func Tfunc1(this_is_a_long_parameter_name)
call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742') call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742:')
endfunc endfunc
func Test_dict_scope_var_remove() func Test_dict_scope_var_remove()
call Tfunc1('testval') call Tfunc1('testval')
@@ -607,10 +723,10 @@ endfunc
" No extend() of write-protected scope-level variable " No extend() of write-protected scope-level variable
func Test_dict_scope_var_extend() func Test_dict_scope_var_extend()
call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742') call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
endfunc endfunc
func Tfunc2(this_is_a_long_parameter_name) func Tfunc2(this_is_a_long_parameter_name)
call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742') call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
endfunc endfunc
func Test_dict_scope_var_extend_overwrite() func Test_dict_scope_var_extend_overwrite()
call Tfunc2('testval') call Tfunc2('testval')
@@ -692,12 +808,10 @@ func Test_func_arg_list()
call s:arg_list_test(1, 2, [3, 4], {5: 6}) call s:arg_list_test(1, 2, [3, 4], {5: 6})
endfunc endfunc
func Test_dict_item_locked()
endfunc
" Tests for reverse(), sort(), uniq() " Tests for reverse(), sort(), uniq()
func Test_reverse_sort_uniq() func Test_reverse_sort_uniq()
let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5] let lines =<< trim END
VAR l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l))) call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l)))
call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l)) call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l))
call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(reverse(l))) call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(reverse(l)))
@@ -707,14 +821,16 @@ func Test_reverse_sort_uniq()
call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(reverse(sort(l)))) call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(reverse(sort(l))))
call assert_equal(['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]], uniq(sort(l))) call assert_equal(['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]], uniq(sort(l)))
let l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four'] LET l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n')) call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n'))
let l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []] LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 1)) call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 1))
call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i')) call assert_equal(['bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l), 'i'))
call assert_equal(['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l))) call assert_equal(['BAR', 'Bar', 'FOO', 'FOOBAR', 'Foo', 'bar', 'foo', -1, 0, 0, 0.22, 1.0e-15, 12, 18, 22, 255, 7, 9, [], {}], sort(copy(l)))
endif endif
END
call CheckLegacyAndVim9Success(lines)
call assert_fails('call reverse("")', 'E899:') call assert_fails('call reverse("")', 'E899:')
call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:') call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
@@ -725,28 +841,34 @@ endfunc
" reduce a list or a blob " reduce a list or a blob
func Test_reduce() func Test_reduce()
call assert_equal(1, reduce([], { acc, val -> acc + val }, 1)) let lines =<< trim END
call assert_equal(10, reduce([1, 3, 5], { acc, val -> acc + val }, 1)) call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1))
call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], { acc, val -> 2 * acc + val }, 1)) call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1))
call assert_equal('a x y z', ['x', 'y', 'z']->reduce({ acc, val -> acc .. ' ' .. val}, 'a')) call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
call assert_equal(#{ x: 1, y: 1, z: 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {})) call assert_equal('a x y z', ['x', 'y', 'z']->reduce(LSTART acc, val LMIDDLE acc .. ' ' .. val LEND, 'a'))
call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0])) call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0]))
let l = ['x', 'y', 'z'] VAR l = ['x', 'y', 'z']
call assert_equal(42, reduce(l, function('get'), #{ x: #{ y: #{ z: 42 } } })) call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } }))
call assert_equal(['x', 'y', 'z'], l) call assert_equal(['x', 'y', 'z'], l)
call assert_equal(1, reduce([1], { acc, val -> acc + val })) call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND))
call assert_equal('x y z', reduce(['x', 'y', 'z'], { acc, val -> acc .. ' ' .. val })) call assert_equal('x y z', reduce(['x', 'y', 'z'], LSTART acc, val LMIDDLE acc .. ' ' .. val LEND))
call assert_equal(120, range(1, 5)->reduce({ acc, val -> acc * val })) call assert_equal(120, range(1, 5)->reduce(LSTART acc, val LMIDDLE acc * val LEND))
call assert_equal(1, reduce(0z, LSTART acc, val LMIDDLE acc + val LEND, 1))
call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE acc + val LEND, 1))
call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce(LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
call assert_equal(0xff, reduce(0zff, LSTART acc, val LMIDDLE acc + val LEND))
call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE 2 * acc + val LEND))
END
call CheckLegacyAndVim9Success(lines)
call assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {}))
" vim9 assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce((acc, val) => extend(acc, {[val]: 1 }), {}))
call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value') call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value')
call assert_equal(1, reduce(0z, { acc, val -> acc + val }, 1))
call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, { acc, val -> acc + val }, 1))
call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce({ acc, val -> 2 * acc + val }, 1))
call assert_equal(0xff, reduce(0zff, { acc, val -> acc + val }))
call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, { acc, val -> 2 * acc + val }))
call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value') call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E897:') call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E897:')
@@ -776,6 +898,7 @@ endfunc
" splitting a string to a List using split() " splitting a string to a List using split()
func Test_str_split() func Test_str_split()
let lines =<< trim END
call assert_equal(['aa', 'bb'], split(' aa bb ')) call assert_equal(['aa', 'bb'], split(' aa bb '))
call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0)) call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0))
call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1)) call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1))
@@ -785,20 +908,26 @@ func Test_str_split()
call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1)) call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1))
call assert_equal(['a', 'b', 'c'], split('abc', '\zs')) call assert_equal(['a', 'b', 'c'], split('abc', '\zs'))
call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1)) call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1))
call assert_equal(['abc'], split('abc', '\\%('))
END
call CheckLegacyAndVim9Success(lines)
call assert_fails("call split('abc', [])", 'E730:') call assert_fails("call split('abc', [])", 'E730:')
call assert_fails("call split('abc', 'b', [])", 'E745:') call assert_fails("call split('abc', 'b', [])", 'E745:')
call assert_equal(['abc'], split('abc', '\\%('))
endfunc endfunc
" compare recursively linked list and dict " compare recursively linked list and dict
func Test_listdict_compare() func Test_listdict_compare()
let l = [1, 2, 3, 4] let lines =<< trim END
let d = {'1': 1, '2': l, '3': 3} VAR l = [1, 2, 3, '4']
let l[1] = d VAR d = {'1': 1, '2': l, '3': 3}
LET l[1] = d
call assert_true(l == l) call assert_true(l == l)
call assert_true(d == d) call assert_true(d == d)
call assert_false(l != deepcopy(l)) call assert_false(l != deepcopy(l))
call assert_false(d != deepcopy(d)) call assert_false(d != deepcopy(d))
END
call CheckLegacyAndVim9Success(lines)
" comparison errors " comparison errors
call assert_fails('echo [1, 2] =~ {}', 'E691:') call assert_fails('echo [1, 2] =~ {}', 'E691:')
@@ -809,14 +938,17 @@ endfunc
" compare complex recursively linked list and dict " compare complex recursively linked list and dict
func Test_listdict_compare_complex() func Test_listdict_compare_complex()
let l = [] let lines =<< trim END
VAR l = []
call add(l, l) call add(l, l)
let dict4 = {"l": l} VAR dict4 = {"l": l}
call add(dict4.l, dict4) call add(dict4.l, dict4)
let lcopy = deepcopy(l) VAR lcopy = deepcopy(l)
let dict4copy = deepcopy(dict4) VAR dict4copy = deepcopy(dict4)
call assert_true(l == lcopy) call assert_true(l == lcopy)
call assert_true(dict4 == dict4copy) call assert_true(dict4 == dict4copy)
END
call CheckLegacyAndVim9Success(lines)
endfunc endfunc
" Test for extending lists and dictionaries " Test for extending lists and dictionaries
@@ -824,33 +956,36 @@ func Test_listdict_extend()
" Test extend() with lists " Test extend() with lists
" Pass the same List to extend() " Pass the same List to extend()
let l = [1, 2, 3] let lines =<< trim END
VAR l = [1, 2, 3]
call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l)) call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l))
call assert_equal([1, 2, 3, 1, 2, 3], l) call assert_equal([1, 2, 3, 1, 2, 3], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6])) call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6]))
call assert_equal([1, 2, 3, 4, 5, 6], l) call assert_equal([1, 2, 3, 4, 5, 6], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call extend(l, [4, 5, 6], 0) call extend(l, [4, 5, 6], 0)
call assert_equal([4, 5, 6, 1, 2, 3], l) call assert_equal([4, 5, 6, 1, 2, 3], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call extend(l, [4, 5, 6], 1) call extend(l, [4, 5, 6], 1)
call assert_equal([1, 4, 5, 6, 2, 3], l) call assert_equal([1, 4, 5, 6, 2, 3], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call extend(l, [4, 5, 6], 3) call extend(l, [4, 5, 6], 3)
call assert_equal([1, 2, 3, 4, 5, 6], l) call assert_equal([1, 2, 3, 4, 5, 6], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call extend(l, [4, 5, 6], -1) call extend(l, [4, 5, 6], -1)
call assert_equal([1, 2, 4, 5, 6, 3], l) call assert_equal([1, 2, 4, 5, 6, 3], l)
let l = [1, 2, 3] LET l = [1, 2, 3]
call extend(l, [4, 5, 6], -3) call extend(l, [4, 5, 6], -3)
call assert_equal([4, 5, 6, 1, 2, 3], l) call assert_equal([4, 5, 6, 1, 2, 3], l)
END
call CheckLegacyAndVim9Success(lines)
let l = [1, 2, 3] let l = [1, 2, 3]
call assert_fails("call extend(l, [4, 5, 6], 4)", 'E684:') call assert_fails("call extend(l, [4, 5, 6], 4)", 'E684:')
@@ -862,21 +997,24 @@ func Test_listdict_extend()
" Test extend() with dictionaries. " Test extend() with dictionaries.
" Pass the same Dict to extend() " Pass the same Dict to extend()
let d = { 'a': {'b': 'B'}} let lines =<< trim END
VAR d = {'a': {'b': 'B'}, 'x': 9}
call extend(d, d) call extend(d, d)
call assert_equal({'a': {'b': 'B'}}, d) call assert_equal({'a': {'b': 'B'}, 'x': 9}, d)
let d = {'a': 'A', 'b': 'B'} LET d = {'a': 'A', 'b': 9}
call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c':'C'})) call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c': 'C'}))
call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d) call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
let d = {'a': 'A', 'b': 'B'} LET d = {'a': 'A', 'b': 9}
call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force") call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force")
call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d) call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
let d = {'a': 'A', 'b': 'B'} LET d = {'a': 'A', 'b': 9}
call extend(d, {'b': 0, 'c':'C'}, "keep") call extend(d, {'b': 0, 'c': 'C'}, "keep")
call assert_equal({'a': 'A', 'b': 'B', 'c': 'C'}, d) call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d)
END
call CheckLegacyAndVim9Success(lines)
let d = {'a': 'A', 'b': 'B'} let d = {'a': 'A', 'b': 'B'}
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
@@ -893,18 +1031,21 @@ func Test_listdict_extend()
call assert_fails("call extend(g:, {'-!' : 10})", 'E461:') call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
" Extend a list with itself. " Extend a list with itself.
let l = [1, 5, 7] let lines =<< trim END
VAR l = [1, 5, 7]
call extend(l, l, 0) call extend(l, l, 0)
call assert_equal([1, 5, 7, 1, 5, 7], l) call assert_equal([1, 5, 7, 1, 5, 7], l)
let l = [1, 5, 7] LET l = [1, 5, 7]
call extend(l, l, 1) call extend(l, l, 1)
call assert_equal([1, 1, 5, 7, 5, 7], l) call assert_equal([1, 1, 5, 7, 5, 7], l)
let l = [1, 5, 7] LET l = [1, 5, 7]
call extend(l, l, 2) call extend(l, l, 2)
call assert_equal([1, 5, 1, 5, 7, 7], l) call assert_equal([1, 5, 1, 5, 7, 7], l)
let l = [1, 5, 7] LET l = [1, 5, 7]
call extend(l, l, 3) call extend(l, l, 3)
call assert_equal([1, 5, 7, 1, 5, 7], l) call assert_equal([1, 5, 7, 1, 5, 7], l)
END
call CheckLegacyAndVim9Success(lines)
endfunc endfunc
func Test_listdict_extendnew() func Test_listdict_extendnew()
@@ -1103,9 +1244,9 @@ func Test_null_dict()
call assert_equal({}, d) call assert_equal({}, d)
call assert_equal(0, len(d)) call assert_equal(0, len(d))
call assert_equal(1, empty(d)) call assert_equal(1, empty(d))
call assert_equal(0, items(d)) call assert_equal([], items(d))
call assert_equal(0, keys(d)) call assert_equal([], keys(d))
call assert_equal(0, values(d)) call assert_equal([], values(d))
call assert_false(has_key(d, 'k')) call assert_false(has_key(d, 'k'))
call assert_equal('{}', string(d)) call assert_equal('{}', string(d))
call assert_fails('let x = d[10]') call assert_fails('let x = d[10]')

View File

@@ -2,6 +2,10 @@
" Use a different file name for each run. " Use a different file name for each run.
let s:sequence = 1 let s:sequence = 1
func CheckDefFailure(lines, error, lnum = -3)
return
endfunc
func CheckDefExecFailure(lines, error, lnum = -3) func CheckDefExecFailure(lines, error, lnum = -3)
return return
endfunc endfunc