2018-02-11 16:05:54 -07:00
|
|
|
" Tests for the List and Dict types
|
2023-08-16 22:41:43 -07:00
|
|
|
scriptencoding utf-8
|
2018-02-11 16:05:54 -07:00
|
|
|
|
2023-05-04 01:12:52 -07:00
|
|
|
source vim9.vim
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
func TearDown()
|
|
|
|
" Run garbage collection after every test
|
|
|
|
call test_garbagecollect_now()
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Tests for List type
|
|
|
|
|
|
|
|
" List creation
|
|
|
|
func Test_list_create()
|
|
|
|
" Creating List directly with different types
|
|
|
|
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
|
|
|
|
call assert_equal("[1, 'as''d', [1, 2, function('strlen')], {'a': 1}]", string(l))
|
|
|
|
call assert_equal({'a' : 1}, l[-1])
|
|
|
|
call assert_equal(1, l[-4])
|
|
|
|
let x = 10
|
|
|
|
try
|
|
|
|
let x = l[-5]
|
|
|
|
catch
|
|
|
|
call assert_match('E684:', v:exception)
|
|
|
|
endtry
|
|
|
|
call assert_equal(10, x)
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" List slices
|
|
|
|
func Test_list_slice()
|
|
|
|
let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
|
|
|
|
call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[:])
|
|
|
|
call assert_equal(['as''d', [1, 2, function('strlen')], {'a': 1}], l[1:])
|
|
|
|
call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2])
|
|
|
|
call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8])
|
|
|
|
call assert_equal([], l[8:-1])
|
2022-10-25 22:41:43 -07:00
|
|
|
call assert_equal([], l[0:-10])
|
2022-10-26 04:53:54 -07:00
|
|
|
" perform an operation on a list slice
|
|
|
|
let l = [1, 2, 3]
|
|
|
|
let l[:1] += [1, 2]
|
|
|
|
let l[2:] -= [1]
|
|
|
|
call assert_equal([2, 4, 2], l)
|
2023-05-04 01:12:52 -07:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [1, 2]
|
|
|
|
call assert_equal([1, 2], l[:])
|
|
|
|
call assert_equal([2], l[-1 : -1])
|
|
|
|
call assert_equal([1, 2], l[-2 : -1])
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
|
|
|
let l = [1, 2]
|
|
|
|
call assert_equal([], l[-3 : -1])
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
var l = [1, 2]
|
|
|
|
assert_equal([1, 2], l[-3 : -1])
|
|
|
|
END
|
|
|
|
call CheckDefAndScriptSuccess(lines)
|
2024-07-29 22:38:13 -07:00
|
|
|
|
|
|
|
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')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" List identity
|
|
|
|
func Test_list_identity()
|
2023-06-11 23:49:53 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
|
|
|
|
VAR ll = l
|
|
|
|
VAR lx = copy(l)
|
|
|
|
call assert_true(l == ll)
|
|
|
|
call assert_false(l isnot ll)
|
|
|
|
call assert_true(l is ll)
|
|
|
|
call assert_true(l == lx)
|
|
|
|
call assert_false(l is lx)
|
|
|
|
call assert_true(l isnot lx)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" removing items with :unlet
|
|
|
|
func Test_list_unlet()
|
2023-06-11 23:49:53 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
|
|
|
|
unlet l[2]
|
|
|
|
call assert_equal([1, 'as''d', {'a': 1}], l)
|
|
|
|
LET l = range(8)
|
|
|
|
unlet l[: 3]
|
|
|
|
unlet l[1 :]
|
|
|
|
call assert_equal([4], l)
|
|
|
|
|
|
|
|
#" removing items out of range: silently skip items that don't exist
|
|
|
|
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[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)
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
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[2:3]
|
|
|
|
call assert_equal([0, 1], l)
|
2023-06-11 23:49:53 -07:00
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [0, 1, 2, 3]
|
|
|
|
unlet l[2 : 1]
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Failure(lines, 'E684:')
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [0, 1, 2, 3]
|
|
|
|
unlet l[-1 : 2]
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Failure(lines, 'E684:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" assignment to a list
|
|
|
|
func Test_list_assign()
|
2023-06-11 23:49:53 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
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])
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
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')
|
2024-07-29 22:38:13 -07:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [2]
|
|
|
|
LET l += v:_null_list
|
|
|
|
call assert_equal([2], l)
|
|
|
|
LET l = v:_null_list
|
|
|
|
LET l += [1]
|
|
|
|
call assert_equal([1], l)
|
|
|
|
END
|
|
|
|
call 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')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" test for range assign
|
|
|
|
func Test_list_range_assign()
|
2023-06-12 05:35:01 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [0]
|
|
|
|
LET l[:] = [1, 2]
|
|
|
|
call assert_equal([1, 2], l)
|
|
|
|
LET l[-4 : -1] = [5, 6]
|
|
|
|
call assert_equal([5, 6], l)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2023-06-12 06:05:36 -07:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
var l = [7]
|
|
|
|
l[:] = ['text']
|
|
|
|
END
|
|
|
|
call CheckDefAndScriptFailure(lines, 'E1012:', 2)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
2024-07-29 20:08:16 -07:00
|
|
|
func Test_list_items()
|
|
|
|
let r = []
|
|
|
|
let l = ['a', 'b', 'c']
|
|
|
|
for [idx, val] in items(l)
|
|
|
|
call extend(r, [[idx, val]])
|
|
|
|
endfor
|
|
|
|
call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
|
|
|
|
|
2024-07-29 20:34:38 -07:00
|
|
|
call assert_fails('call items(3)', 'E1225:')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_string_items()
|
|
|
|
let r = []
|
|
|
|
let s = 'ábツ'
|
|
|
|
for [idx, val] in items(s)
|
|
|
|
call extend(r, [[idx, val]])
|
|
|
|
endfor
|
|
|
|
call assert_equal([[0, 'á'], [1, 'b'], [2, 'ツ']], r)
|
2024-07-29 20:08:16 -07:00
|
|
|
endfunc
|
|
|
|
|
2018-10-04 09:40:59 -07:00
|
|
|
" Test removing items in list
|
|
|
|
func Test_list_func_remove()
|
2023-06-11 23:49:53 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
#" Test removing 1 element
|
|
|
|
VAR l = [1, 2, 3, 4]
|
|
|
|
call assert_equal(1, remove(l, 0))
|
|
|
|
call assert_equal([2, 3, 4], l)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
LET l = [1, 2, 3, 4]
|
|
|
|
call assert_equal(2, remove(l, 1))
|
|
|
|
call assert_equal([1, 3, 4], l)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
LET l = [1, 2, 3, 4]
|
|
|
|
call assert_equal(4, remove(l, -1))
|
|
|
|
call assert_equal([1, 2, 3], l)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
#" Test removing range of element(s)
|
|
|
|
LET l = [1, 2, 3, 4]
|
|
|
|
call assert_equal([3], remove(l, 2, 2))
|
|
|
|
call assert_equal([1, 2, 4], l)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
LET l = [1, 2, 3, 4]
|
|
|
|
call assert_equal([2, 3], remove(l, 1, 2))
|
|
|
|
call assert_equal([1, 4], l)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
LET l = [1, 2, 3, 4]
|
|
|
|
call assert_equal([2, 3], remove(l, -3, -2))
|
|
|
|
call assert_equal([1, 4], l)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-10-04 09:40:59 -07:00
|
|
|
|
|
|
|
" Test invalid cases
|
|
|
|
let l = [1, 2, 3, 4]
|
|
|
|
call assert_fails("call remove(l, 5)", 'E684:')
|
|
|
|
call assert_fails("call remove(l, 1, 5)", 'E684:')
|
|
|
|
call assert_fails("call remove(l, 3, 2)", 'E16:')
|
2020-12-02 11:37:18 -07:00
|
|
|
call assert_fails("call remove(1, 0)", 'E896:')
|
2018-10-04 09:40:59 -07:00
|
|
|
call assert_fails("call remove(l, l)", 'E745:')
|
|
|
|
endfunc
|
|
|
|
|
2022-10-26 04:53:54 -07:00
|
|
|
" List add() function
|
|
|
|
func Test_list_add()
|
2023-06-11 23:49:53 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = []
|
|
|
|
call add(l, 1)
|
|
|
|
call add(l, [2, 3])
|
|
|
|
call add(l, [])
|
|
|
|
call add(l, v:_null_list)
|
|
|
|
call add(l, {'k': 3})
|
|
|
|
call add(l, {})
|
|
|
|
call add(l, v:_null_dict)
|
|
|
|
call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
|
|
|
" weird legacy behavior
|
2022-10-26 04:53:54 -07:00
|
|
|
" call assert_equal(1, add(v:_null_list, 4))
|
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" Tests for Dictionary type
|
|
|
|
|
|
|
|
func Test_dict()
|
|
|
|
" Creating Dictionary directly with different types
|
2023-06-11 23:49:53 -07:00
|
|
|
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('asd', d.1)
|
|
|
|
call assert_equal(['-1', '1', 'b'], sort(keys(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)
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
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))
|
2023-06-11 23:49:53 -07:00
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
let v = []
|
|
|
|
for [key, val] in items(d)
|
|
|
|
call extend(v, [key, val])
|
|
|
|
unlet key val
|
|
|
|
endfor
|
|
|
|
call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v)
|
|
|
|
|
2023-06-11 23:49:53 -07:00
|
|
|
call extend(d, {3: 33, 1: 99})
|
|
|
|
call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737:')
|
2022-09-07 17:12:10 -07:00
|
|
|
|
2022-10-26 04:04:38 -07:00
|
|
|
" duplicate key
|
|
|
|
call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:')
|
|
|
|
" missing comma
|
|
|
|
call assert_fails("let d = {'k' : 10 'k' : 20}", 'E722:')
|
|
|
|
" missing curly brace
|
|
|
|
call assert_fails("let d = {'k' : 10,", 'E723:')
|
|
|
|
" invalid key
|
|
|
|
call assert_fails('let d = #{++ : 10}', 'E15:')
|
|
|
|
" wrong type for key
|
|
|
|
call assert_fails('let d={[] : 10}', 'E730:')
|
|
|
|
" undefined variable as value
|
|
|
|
call assert_fails("let d={'k' : i}", 'E121:')
|
|
|
|
|
2022-09-07 17:12:10 -07:00
|
|
|
" allow key starting with number at the start, not a curly expression
|
|
|
|
call assert_equal({'1foo': 77}, #{1foo: 77})
|
2022-09-07 11:45:22 -07:00
|
|
|
|
|
|
|
" #{expr} is not a curly expression
|
|
|
|
let x = 'x'
|
|
|
|
call assert_equal(#{g: x}, #{g:x})
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Dictionary identity
|
|
|
|
func Test_dict_identity()
|
2023-06-12 00:16:44 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, }
|
|
|
|
VAR dd = d
|
|
|
|
VAR dx = copy(d)
|
|
|
|
call assert_true(d == dd)
|
|
|
|
call assert_false(d isnot dd)
|
|
|
|
call assert_true(d is dd)
|
|
|
|
call assert_true(d == dx)
|
|
|
|
call assert_false(d is dx)
|
|
|
|
call assert_true(d isnot dx)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" removing items with :unlet
|
|
|
|
func Test_dict_unlet()
|
2023-06-12 00:16:44 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}}
|
|
|
|
unlet d.b
|
|
|
|
unlet d[-1]
|
|
|
|
call assert_equal({'1': 99, '3': 33}, d)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
|
|
|
|
func Test_dict_big()
|
|
|
|
let d = {}
|
|
|
|
for i in range(1500)
|
|
|
|
let d[i] = 3000 - i
|
|
|
|
endfor
|
|
|
|
call assert_equal([3000, 2900, 2001, 1600, 1501], [d[0], d[100], d[999], d[1400], d[1499]])
|
|
|
|
let str = ''
|
|
|
|
try
|
|
|
|
let n = d[1500]
|
|
|
|
catch
|
2020-10-30 16:04:23 -07:00
|
|
|
let str = substitute(v:exception, '\v(.{14}).*( "\d{4}").*', '\1\2', '')
|
2018-02-11 16:05:54 -07:00
|
|
|
endtry
|
2020-10-30 16:04:23 -07:00
|
|
|
call assert_equal('Vim(let):E716: "1500"', str)
|
2018-02-11 16:05:54 -07:00
|
|
|
|
2023-09-09 02:47:28 -07:00
|
|
|
" lookup each item
|
2018-02-11 16:05:54 -07:00
|
|
|
for i in range(1500)
|
|
|
|
call assert_equal(3000 - i, d[i])
|
|
|
|
endfor
|
|
|
|
let i += 1
|
|
|
|
|
|
|
|
" delete even items
|
|
|
|
while i >= 2
|
|
|
|
let i -= 2
|
|
|
|
unlet d[i]
|
|
|
|
endwhile
|
|
|
|
call assert_equal('NONE', get(d, 1500 - 100, 'NONE'))
|
|
|
|
call assert_equal(2999, d[1])
|
|
|
|
|
|
|
|
" delete odd items, checking value, one intentionally wrong
|
|
|
|
let d[33] = 999
|
|
|
|
let i = 1
|
|
|
|
while i < 1500
|
|
|
|
if i != 33
|
|
|
|
call assert_equal(3000 - i, d[i])
|
|
|
|
else
|
|
|
|
call assert_equal(999, d[i])
|
|
|
|
endif
|
|
|
|
unlet d[i]
|
|
|
|
let i += 2
|
|
|
|
endwhile
|
|
|
|
call assert_equal({}, d)
|
|
|
|
unlet d
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Dictionary function
|
|
|
|
func Test_dict_func()
|
|
|
|
let d = {}
|
|
|
|
func d.func(a) dict
|
|
|
|
return a:a . len(self.data)
|
|
|
|
endfunc
|
|
|
|
let d.data = [1,2,3]
|
|
|
|
call assert_equal('len: 3', d.func('len: '))
|
|
|
|
let x = d.func('again: ')
|
|
|
|
call assert_equal('again: 3', x)
|
|
|
|
let Fn = d.func
|
|
|
|
call assert_equal('xxx3', Fn('xxx'))
|
|
|
|
endfunc
|
|
|
|
|
2022-11-05 15:35:34 -07:00
|
|
|
func Test_dict_assign()
|
|
|
|
let d = {}
|
|
|
|
let d.1 = 1
|
|
|
|
let d._ = 2
|
|
|
|
call assert_equal({'1': 1, '_': 2}, d)
|
2022-11-05 23:50:09 -07:00
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
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.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:')
|
2024-07-29 22:38:13 -07:00
|
|
|
|
|
|
|
let d = {'abc': {}}
|
|
|
|
call assert_fails("let d.abc[0z10] = 10", 'E976: Using a Blob as a String')
|
2022-11-05 15:35:34 -07:00
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" Function in script-local List or Dict
|
|
|
|
func Test_script_local_dict_func()
|
|
|
|
let g:dict = {}
|
|
|
|
function g:dict.func() dict
|
|
|
|
return 'g:dict.func' . self.foo[1] . self.foo[0]('asdf')
|
|
|
|
endfunc
|
|
|
|
let g:dict.foo = ['-', 2, 3]
|
|
|
|
call insert(g:dict.foo, function('strlen'))
|
|
|
|
call assert_equal('g:dict.func-4', g:dict.func())
|
|
|
|
unlet g:dict
|
|
|
|
endfunc
|
|
|
|
|
2022-10-26 04:04:38 -07:00
|
|
|
" Test removing items in a dictionary
|
2018-10-04 09:40:59 -07:00
|
|
|
func Test_dict_func_remove()
|
2023-06-12 00:16:44 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 'a', 2: 'b', 3: 'c'}
|
|
|
|
call assert_equal('b', remove(d, 2))
|
|
|
|
call assert_equal({1: 'a', 3: 'c'}, d)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 'a', 3: 'c'}
|
|
|
|
call remove(d, 1, 2)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Failure(lines, 'E118:')
|
2018-10-04 09:40:59 -07:00
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 'a', 3: 'c'}
|
|
|
|
call remove(d, 'a')
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Failure(lines, 'E716:')
|
|
|
|
|
2023-12-06 01:49:40 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
let d = {'a-b': 55}
|
|
|
|
echo d.a-b
|
|
|
|
END
|
|
|
|
call CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
vim9script
|
|
|
|
var d = {'a-b': 55}
|
|
|
|
echo d.a-b
|
|
|
|
END
|
|
|
|
call CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
var d = {'a-b': 55}
|
|
|
|
echo d.a-b
|
|
|
|
END
|
|
|
|
call CheckDefFailure(lines, 'E1004: White space required before and after ''-''')
|
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
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>')
|
2018-10-04 09:40:59 -07:00
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" Nasty: remove func from Dict that's being called (works)
|
|
|
|
func Test_dict_func_remove_in_use()
|
|
|
|
let d = {1:1}
|
|
|
|
func d.func(a)
|
|
|
|
return "a:" . a:a
|
|
|
|
endfunc
|
|
|
|
let expected = 'a:' . string(get(d, 'func'))
|
|
|
|
call assert_equal(expected, d.func(string(remove(d, 'func'))))
|
2023-08-16 06:15:18 -07:00
|
|
|
|
|
|
|
" similar, in a way it also works in Vim9
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 1, 2: 'x'}
|
|
|
|
func GetArg(a)
|
|
|
|
return "a:" .. a:a
|
|
|
|
endfunc
|
|
|
|
LET d.func = function('GetArg')
|
|
|
|
VAR expected = 'a:' .. string(get(d, 'func'))
|
|
|
|
call assert_equal(expected, d.func(string(remove(d, 'func'))))
|
|
|
|
END
|
|
|
|
call CheckTransLegacySuccess(lines)
|
|
|
|
call CheckTransVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
2020-10-06 21:03:12 -07:00
|
|
|
func Test_dict_literal_keys()
|
2020-10-06 22:00:11 -07:00
|
|
|
call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},)
|
2020-10-06 21:45:05 -07:00
|
|
|
|
2023-06-12 00:16:44 -07:00
|
|
|
" why *{} cannot be used for a literal dictionary
|
2020-10-06 21:45:05 -07:00
|
|
|
let blue = 'blue'
|
|
|
|
call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
|
2020-10-06 21:03:12 -07:00
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" Nasty: deepcopy() dict that refers to itself (fails when noref used)
|
|
|
|
func Test_dict_deepcopy()
|
2023-08-16 06:17:11 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 1, 2: '2'}
|
|
|
|
VAR l = [4, d, 6]
|
|
|
|
LET d[3] = l
|
|
|
|
VAR dc = deepcopy(d)
|
|
|
|
call deepcopy(d, 1)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Failure(lines, 'E698:')
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {1: 1, 2: '2'}
|
|
|
|
VAR l = [4, d, 6]
|
|
|
|
LET d[3] = l
|
|
|
|
VAR l2 = [0, l, l, 3]
|
|
|
|
LET l[1] = l2
|
|
|
|
VAR l3 = deepcopy(l2)
|
|
|
|
call assert_true(l3[1] is l3[2])
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
2023-08-17 01:22:11 -07:00
|
|
|
call assert_fails("call deepcopy([1, 2], 2)", 'E1212:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Locked variables
|
|
|
|
func Test_list_locked_var()
|
2023-08-16 06:17:11 -07:00
|
|
|
" Not tested with :def function, local vars cannot be locked.
|
|
|
|
let lines =<< trim END
|
|
|
|
VAR expected = [
|
|
|
|
\ [['1000-000', 'ppppppF'],
|
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1000-000', 'ppppppF'],
|
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1100-100', 'ppFppFF'],
|
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1110-110', 'pFFpFFF'],
|
|
|
|
\ ['0010-010', 'pFppFpp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1111-111', 'FFFFFFF'],
|
|
|
|
\ ['0011-011', 'FFpFFpp'],
|
|
|
|
\ ['0000-000', 'ppppppp']]
|
|
|
|
\ ]
|
|
|
|
for depth in range(5)
|
|
|
|
for u in range(3)
|
|
|
|
VAR l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
|
|
|
|
exe "lockvar " .. depth .. " l"
|
|
|
|
if u == 1
|
|
|
|
exe "unlockvar l"
|
|
|
|
elseif u == 2
|
|
|
|
exe "unlockvar " .. depth .. " l"
|
|
|
|
endif
|
|
|
|
VAR ps = islocked("l") .. islocked("l[1]") .. islocked("l[1][1]") .. islocked("l[1][1][0]") .. '-' .. islocked("l[2]") .. islocked("l[2]['6']") .. islocked("l[2]['6'][7]")
|
|
|
|
call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
|
|
|
|
LET ps = ''
|
|
|
|
try
|
|
|
|
LET l[1][1][0] = 99
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l[1][1] = [99]
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l[1] = [99]
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l[2]['6'][7] = 99
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l[2][6] = {99: 99}
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l[2] = {99: 99}
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
LET l = [99]
|
|
|
|
LET ps ..= 'p'
|
|
|
|
catch
|
|
|
|
LET ps ..= 'F'
|
|
|
|
endtry
|
|
|
|
call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
|
|
|
|
unlock! l
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
END
|
|
|
|
call CheckTransLegacySuccess(lines)
|
|
|
|
call CheckTransVim9Success(lines)
|
|
|
|
|
2022-11-04 03:17:26 -07:00
|
|
|
call assert_fails("let x=islocked('a b')", 'E488:')
|
|
|
|
let mylist = [1, 2, 3]
|
|
|
|
call assert_fails("let x = islocked('mylist[1:2]')", 'E786:')
|
|
|
|
let mydict = {'k' : 'v'}
|
|
|
|
call assert_fails("let x = islocked('mydict.a')", 'E716:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Unletting locked variables
|
|
|
|
func Test_list_locked_var_unlet()
|
2023-08-16 06:17:11 -07:00
|
|
|
" Not tested with Vim9: script and local variables cannot be unlocked
|
2018-02-11 16:05:54 -07:00
|
|
|
let expected = [
|
2023-01-18 17:03:13 -07:00
|
|
|
\ [['1000-000', 'ppppppp'],
|
2018-02-11 16:05:54 -07:00
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1000-000', 'ppFppFp'],
|
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1100-100', 'pFFpFFp'],
|
|
|
|
\ ['0000-000', 'ppppppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1110-110', 'FFFFFFp'],
|
|
|
|
\ ['0010-010', 'FppFppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']],
|
|
|
|
\ [['1111-111', 'FFFFFFp'],
|
|
|
|
\ ['0011-011', 'FppFppp'],
|
|
|
|
\ ['0000-000', 'ppppppp']]
|
|
|
|
\ ]
|
|
|
|
|
|
|
|
for depth in range(5)
|
|
|
|
for u in range(3)
|
|
|
|
unlet! l
|
|
|
|
let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
|
|
|
|
exe "lockvar " . depth . " l"
|
|
|
|
if u == 1
|
|
|
|
exe "unlockvar l"
|
|
|
|
elseif u == 2
|
|
|
|
exe "unlockvar " . depth . " l"
|
|
|
|
endif
|
|
|
|
let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
|
2023-01-18 17:03:13 -07:00
|
|
|
call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
|
2018-02-11 16:05:54 -07:00
|
|
|
let ps = ''
|
|
|
|
try
|
|
|
|
unlet l[2]['6'][7]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l[2][6]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l[2]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l[1][1][0]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l[1][1]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l[1]
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
try
|
|
|
|
unlet l
|
|
|
|
let ps .= 'p'
|
|
|
|
catch
|
|
|
|
let ps .= 'F'
|
|
|
|
endtry
|
|
|
|
call assert_equal(expected[depth][u][1], ps)
|
|
|
|
endfor
|
|
|
|
endfor
|
2023-06-12 06:58:40 -07:00
|
|
|
|
|
|
|
" Deleting a list range with locked items works, but changing the items
|
|
|
|
" fails.
|
2022-11-24 19:38:30 -07:00
|
|
|
let l = [1, 2, 3, 4]
|
|
|
|
lockvar l[1:2]
|
2023-06-12 06:58:40 -07:00
|
|
|
call assert_fails('let l[1:2] = [8, 9]', 'E741:')
|
|
|
|
unlet l[1:2]
|
|
|
|
call assert_equal([1, 4], l)
|
2022-11-24 19:38:30 -07:00
|
|
|
unlet l
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Locked variables and :unlet or list / dict functions
|
|
|
|
|
|
|
|
" No :unlet after lock on dict:
|
|
|
|
func Test_dict_lock_unlet()
|
|
|
|
let d = {'a': 99, 'b': 100}
|
|
|
|
lockvar 1 d
|
2023-06-12 00:16:44 -07:00
|
|
|
call assert_fails('unlet d.a', 'E741:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" unlet after lock on dict item
|
|
|
|
func Test_dict_item_lock_unlet()
|
2023-08-16 06:17:11 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'a': 99, 'b': 100}
|
|
|
|
lockvar d.a
|
|
|
|
unlet d.a
|
|
|
|
call assert_equal({'b': 100}, d)
|
|
|
|
END
|
|
|
|
" TODO: make this work in a :def function
|
|
|
|
"call CheckLegacyAndVim9Success(lines)
|
|
|
|
call CheckTransLegacySuccess(lines)
|
|
|
|
call CheckTransVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" filter() after lock on dict item
|
|
|
|
func Test_dict_lock_filter()
|
2023-08-16 06:17:11 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'a': 99, 'b': 100}
|
|
|
|
lockvar d.a
|
|
|
|
call filter(d, 'v:key != "a"')
|
|
|
|
call assert_equal({'b': 100}, d)
|
|
|
|
END
|
|
|
|
" TODO: make this work in a :def function
|
|
|
|
"call CheckLegacyAndVim9Success(lines)
|
|
|
|
call CheckTransLegacySuccess(lines)
|
|
|
|
call CheckTransVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" map() after lock on dict
|
|
|
|
func Test_dict_lock_map()
|
2023-08-16 06:17:11 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'a': 99, 'b': 100}
|
|
|
|
lockvar 1 d
|
|
|
|
call map(d, 'v:val + 200')
|
|
|
|
call assert_equal({'a': 299, 'b': 300}, d)
|
|
|
|
END
|
|
|
|
" This won't work in a :def function
|
|
|
|
call CheckTransLegacySuccess(lines)
|
|
|
|
call CheckTransVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" No extend() after lock on dict item
|
|
|
|
func Test_dict_lock_extend()
|
|
|
|
let d = {'a': 99, 'b': 100}
|
|
|
|
lockvar d.a
|
2023-06-12 00:16:44 -07:00
|
|
|
call assert_fails("call extend(d, {'a' : 123})", 'E741:')
|
2018-02-11 16:05:54 -07:00
|
|
|
call assert_equal({'a': 99, 'b': 100}, d)
|
|
|
|
endfunc
|
|
|
|
|
2021-04-08 15:48:43 -07:00
|
|
|
" Cannot use += with a locked dict
|
|
|
|
func Test_dict_lock_operator()
|
|
|
|
let d = {}
|
|
|
|
lockvar d
|
|
|
|
call assert_fails("let d += {'k' : 10}", 'E741:')
|
|
|
|
unlockvar d
|
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" No remove() of write-protected scope-level variable
|
2022-02-06 14:34:20 -07:00
|
|
|
func Tfunc1(this_is_a_long_parameter_name)
|
2023-06-12 00:16:44 -07:00
|
|
|
call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742:')
|
2022-02-06 14:34:20 -07:00
|
|
|
endfunc
|
2018-02-11 16:05:54 -07:00
|
|
|
func Test_dict_scope_var_remove()
|
2022-02-06 14:34:20 -07:00
|
|
|
call Tfunc1('testval')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" No extend() of write-protected scope-level variable
|
2019-08-21 11:17:09 -07:00
|
|
|
func Test_dict_scope_var_extend()
|
2023-06-12 00:16:44 -07:00
|
|
|
call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
|
2019-08-21 11:17:09 -07:00
|
|
|
endfunc
|
2022-02-06 14:34:20 -07:00
|
|
|
func Tfunc2(this_is_a_long_parameter_name)
|
2023-06-12 00:16:44 -07:00
|
|
|
call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
2019-08-21 11:17:09 -07:00
|
|
|
func Test_dict_scope_var_extend_overwrite()
|
2022-02-06 14:34:20 -07:00
|
|
|
call Tfunc2('testval')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" No :unlet of variable in locked scope
|
|
|
|
func Test_lock_var_unlet()
|
|
|
|
let b:testvar = 123
|
|
|
|
lockvar 1 b:
|
|
|
|
call assert_fails('unlet b:testvar', 'E741:')
|
|
|
|
unlockvar 1 b:
|
|
|
|
unlet! b:testvar
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" No :let += of locked list variable
|
|
|
|
func Test_let_lock_list()
|
|
|
|
let l = ['a', 'b', 3]
|
|
|
|
lockvar 1 l
|
|
|
|
call assert_fails("let l += ['x']", 'E741:')
|
|
|
|
call assert_equal(['a', 'b', 3], l)
|
|
|
|
|
|
|
|
unlet l
|
|
|
|
let l = [1, 2, 3, 4]
|
|
|
|
lockvar! l
|
|
|
|
call assert_equal([1, 2, 3, 4], l)
|
|
|
|
unlockvar l[1]
|
|
|
|
call assert_fails('unlet l[0:1]', 'E741:')
|
|
|
|
call assert_equal([1, 2, 3, 4], l)
|
|
|
|
call assert_fails('unlet l[1:2]', 'E741:')
|
|
|
|
call assert_equal([1, 2, 3, 4], l)
|
|
|
|
unlockvar l[1]
|
|
|
|
call assert_fails('let l[0:1] = [0, 1]', 'E741:')
|
|
|
|
call assert_equal([1, 2, 3, 4], l)
|
|
|
|
call assert_fails('let l[1:2] = [0, 1]', 'E741:')
|
|
|
|
call assert_equal([1, 2, 3, 4], l)
|
|
|
|
unlet l
|
|
|
|
endfunc
|
|
|
|
|
2022-10-25 22:05:33 -07:00
|
|
|
" Locking part of the list
|
|
|
|
func Test_let_lock_list_items()
|
|
|
|
let l = [1, 2, 3, 4]
|
|
|
|
lockvar l[2:]
|
|
|
|
call assert_equal(0, islocked('l[0]'))
|
|
|
|
call assert_equal(1, islocked('l[2]'))
|
|
|
|
call assert_equal(1, islocked('l[3]'))
|
|
|
|
call assert_fails('let l[2] = 10', 'E741:')
|
|
|
|
call assert_fails('let l[3] = 20', 'E741:')
|
|
|
|
unlet l
|
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" lockvar/islocked() triggering script autoloading
|
|
|
|
func Test_lockvar_script_autoload()
|
|
|
|
let old_rtp = &rtp
|
|
|
|
set rtp+=./sautest
|
|
|
|
lockvar g:footest#x
|
|
|
|
unlockvar g:footest#x
|
2021-08-31 14:51:29 -07:00
|
|
|
call assert_equal(-1, 'g:footest#x'->islocked())
|
2018-02-11 16:05:54 -07:00
|
|
|
call assert_equal(0, exists('g:footest#x'))
|
|
|
|
call assert_equal(1, g:footest#x)
|
|
|
|
let &rtp = old_rtp
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" a:000 function argument test
|
|
|
|
func s:arg_list_test(...)
|
|
|
|
call assert_fails('let a:000 = [1, 2]', 'E46:')
|
|
|
|
call assert_fails('let a:000[0] = 9', 'E742:')
|
|
|
|
call assert_fails('let a:000[2] = [9, 10]', 'E742:')
|
|
|
|
call assert_fails('let a:000[3] = {9 : 10}', 'E742:')
|
|
|
|
|
|
|
|
" now the tests that should pass
|
|
|
|
let a:000[2][1] = 9
|
|
|
|
call extend(a:000[2], [5, 6])
|
|
|
|
let a:000[3][5] = 8
|
|
|
|
let a:000[3]['a'] = 12
|
|
|
|
call assert_equal([1, 2, [3, 9, 5, 6], {'a': 12, '5': 8}], a:000)
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_func_arg_list()
|
|
|
|
call s:arg_list_test(1, 2, [3, 4], {5: 6})
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Tests for reverse(), sort(), uniq()
|
|
|
|
func Test_reverse_sort_uniq()
|
2023-06-12 01:15:12 -07:00
|
|
|
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([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)))
|
|
|
|
if has('float')
|
|
|
|
call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(l))
|
|
|
|
call assert_equal([[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'], 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)))
|
|
|
|
|
|
|
|
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'))
|
|
|
|
|
|
|
|
LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
|
2023-08-17 00:42:36 -07:00
|
|
|
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'))
|
2023-06-12 01:15:12 -07:00
|
|
|
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)))
|
|
|
|
endif
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2020-12-02 11:37:18 -07:00
|
|
|
|
2023-08-19 02:57:09 -07:00
|
|
|
call assert_fails('call reverse({})', 'E1252:')
|
2022-11-04 16:24:06 -07:00
|
|
|
call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
|
2023-04-25 06:32:12 -07:00
|
|
|
call assert_fails("call sort([1, 2], function('min'), 1)", "E1206:")
|
2022-10-26 04:04:38 -07:00
|
|
|
call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
|
2022-11-04 16:24:06 -07:00
|
|
|
call assert_fails("call sort([1, 2], function('min'))", "E118:")
|
2023-08-17 00:42:36 -07:00
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
call sort(['a', 'b'], 0)
|
|
|
|
END
|
|
|
|
call CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
call sort(['a', 'b'], 1)
|
|
|
|
END
|
|
|
|
call CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
2023-08-16 22:41:43 -07:00
|
|
|
" reduce a list, blob or string
|
2021-12-06 13:50:29 -07:00
|
|
|
func Test_reduce()
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1))
|
|
|
|
call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1))
|
|
|
|
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('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]))
|
|
|
|
|
|
|
|
VAR l = ['x', 'y', 'z']
|
|
|
|
call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } }))
|
|
|
|
call assert_equal(['x', 'y', 'z'], l)
|
|
|
|
|
|
|
|
call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND))
|
|
|
|
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(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))
|
2023-08-16 22:41:43 -07:00
|
|
|
|
|
|
|
call assert_equal('x,y,z', 'xyz'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal('', ''->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, ''))
|
|
|
|
call assert_equal('あ,い,う,え,お,😊,💕', 'あいうえお😊💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal('😊,あ,い,う,え,お,💕', 'あいうえお💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '😊'))
|
|
|
|
call assert_equal('ऊ,ॠ,ॡ', reduce('ऊॠॡ', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal('c,à,t', reduce('càt', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
|
|
|
|
call assert_equal(',a,b,c', reduce('abc', LSTART acc, val LMIDDLE acc .. ',' .. val LEND, v:_null_string))
|
2023-08-17 14:09:24 -07:00
|
|
|
|
|
|
|
call assert_equal(0x7d, reduce([0x30, 0x25, 0x08, 0x61], 'or'))
|
|
|
|
call assert_equal(0x7d, reduce(0z30250861, 'or'))
|
|
|
|
call assert_equal('β', reduce('ββββ', 'matchstr'))
|
2023-06-12 01:15:12 -07:00
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2021-12-06 13:50:29 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
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 }), {}))
|
2021-12-06 13:50:29 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value')
|
2021-12-06 13:50:29 -07:00
|
|
|
call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
|
2023-08-17 01:22:11 -07:00
|
|
|
call assert_fails("call reduce(v:_null_blob, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
|
2023-08-16 22:41:43 -07:00
|
|
|
call assert_fails("call reduce('', { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
|
|
|
|
call assert_fails("call reduce(v:_null_string, { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
|
2021-12-06 13:50:29 -07:00
|
|
|
|
2023-08-16 22:41:43 -07:00
|
|
|
call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E1098:')
|
|
|
|
call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E1098:')
|
2023-08-17 14:09:24 -07:00
|
|
|
call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E117:')
|
2023-08-17 01:22:11 -07:00
|
|
|
call assert_fails("echo reduce(0z01, { acc, val -> 2 * acc + val }, '')", 'E1210:')
|
2021-12-06 15:43:59 -07:00
|
|
|
|
2023-08-16 22:41:43 -07:00
|
|
|
" call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1252:')
|
|
|
|
" call assert_fails("vim9 reduce({}, (acc, val) => (acc .. val), '')", 'E1252:')
|
|
|
|
" call assert_fails("vim9 reduce(0.1, (acc, val) => (acc .. val), '')", 'E1252:')
|
|
|
|
" call assert_fails("vim9 reduce(function('tr'), (acc, val) => (acc .. val), '')", 'E1252:')
|
2023-08-17 01:22:11 -07:00
|
|
|
call assert_fails("call reduce('', { acc, val -> acc + val }, 1)", 'E1174:')
|
|
|
|
call assert_fails("call reduce('', { acc, val -> acc + val }, {})", 'E1174:')
|
|
|
|
call assert_fails("call reduce('', { acc, val -> acc + val }, 0.1)", 'E1174:')
|
|
|
|
call assert_fails("call reduce('', { acc, val -> acc + val }, function('tr'))", 'E1174:')
|
2023-08-16 23:12:24 -07:00
|
|
|
call assert_fails("call reduce('abc', { a, v -> a10}, '')", 'E121:')
|
2023-08-17 01:22:11 -07:00
|
|
|
call assert_fails("call reduce(0z0102, { a, v -> a10}, 1)", 'E121:')
|
|
|
|
call assert_fails("call reduce([1, 2], { a, v -> a10}, '')", 'E121:')
|
2023-08-16 22:41:43 -07:00
|
|
|
|
2021-12-06 15:43:59 -07:00
|
|
|
let g:lut = [1, 2, 3, 4]
|
|
|
|
func EvilRemove()
|
|
|
|
call remove(g:lut, 1)
|
|
|
|
return 1
|
|
|
|
endfunc
|
|
|
|
call assert_fails("call reduce(g:lut, { acc, val -> EvilRemove() }, 1)", 'E742:')
|
|
|
|
unlet g:lut
|
|
|
|
delfunc EvilRemove
|
2021-12-06 15:51:08 -07:00
|
|
|
|
|
|
|
call assert_equal(42, reduce(v:_null_list, function('add'), 42))
|
|
|
|
call assert_equal(42, reduce(v:_null_blob, function('add'), 42))
|
2023-04-15 17:58:37 -07:00
|
|
|
|
|
|
|
" should not crash
|
|
|
|
" Nvim doesn't have null functions
|
|
|
|
" call assert_fails('echo reduce([1], test_null_function())', 'E1132:')
|
2023-04-15 17:55:42 -07:00
|
|
|
" Nvim doesn't have null partials
|
|
|
|
" call assert_fails('echo reduce([1], test_null_partial())', 'E1132:')
|
2021-12-06 13:50:29 -07:00
|
|
|
endfunc
|
|
|
|
|
2022-10-26 04:53:54 -07:00
|
|
|
" splitting a string to a List using split()
|
2018-02-11 16:05:54 -07:00
|
|
|
func Test_str_split()
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
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\+', 1))
|
|
|
|
call assert_equal(['', '', 'aa', '', 'bb', '', ''], split(' aa bb ', '\W', 1))
|
|
|
|
call assert_equal(['aa', '', 'bb'], split(':aa::bb:', ':', 0))
|
|
|
|
call assert_equal(['', 'aa', '', 'bb', ''], split(':aa::bb:', ':', 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', 1))
|
|
|
|
call assert_equal(['abc'], split('abc', '\\%('))
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
2022-11-04 03:17:26 -07:00
|
|
|
call assert_fails("call split('abc', [])", 'E730:')
|
|
|
|
call assert_fails("call split('abc', 'b', [])", 'E745:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
|
|
|
" compare recursively linked list and dict
|
|
|
|
func Test_listdict_compare()
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [1, 2, 3, '4']
|
|
|
|
VAR d = {'1': 1, '2': l, '3': 3}
|
|
|
|
LET l[1] = d
|
|
|
|
call assert_true(l == l)
|
|
|
|
call assert_true(d == d)
|
|
|
|
call assert_false(l != deepcopy(l))
|
|
|
|
call assert_false(d != deepcopy(d))
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2022-10-25 22:41:43 -07:00
|
|
|
|
|
|
|
" comparison errors
|
|
|
|
call assert_fails('echo [1, 2] =~ {}', 'E691:')
|
|
|
|
call assert_fails('echo [1, 2] =~ [1, 2]', 'E692:')
|
|
|
|
call assert_fails('echo {} =~ 5', 'E735:')
|
|
|
|
call assert_fails('echo {} =~ {}', 'E736:')
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
2024-07-05 00:20:02 -07:00
|
|
|
func Test_recursive_listdict_compare()
|
|
|
|
let l1 = [0, 1]
|
|
|
|
let l1[0] = l1
|
|
|
|
let l2 = [0, 1]
|
|
|
|
let l2[0] = l2
|
|
|
|
call assert_true(l1 == l2)
|
|
|
|
let d1 = {0: 0, 1: 1}
|
|
|
|
let d1[0] = d1
|
|
|
|
let d2 = {0: 0, 1: 1}
|
|
|
|
let d2[0] = d2
|
|
|
|
call assert_true(d1 == d2)
|
|
|
|
endfunc
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" compare complex recursively linked list and dict
|
|
|
|
func Test_listdict_compare_complex()
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = []
|
|
|
|
call add(l, l)
|
|
|
|
VAR dict4 = {"l": l}
|
|
|
|
call add(dict4.l, dict4)
|
|
|
|
VAR lcopy = deepcopy(l)
|
|
|
|
VAR dict4copy = deepcopy(dict4)
|
|
|
|
call assert_true(l == lcopy)
|
|
|
|
call assert_true(dict4 == dict4copy)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
|
|
|
|
2023-04-14 22:31:30 -07:00
|
|
|
" Test for extending lists and dictionaries
|
2018-02-11 16:05:54 -07:00
|
|
|
func Test_listdict_extend()
|
2019-02-25 03:34:55 -07:00
|
|
|
" Test extend() with lists
|
|
|
|
|
2018-02-11 16:05:54 -07:00
|
|
|
" Pass the same List to extend()
|
2023-06-12 01:15:12 -07:00
|
|
|
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], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
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], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET l = [1, 2, 3]
|
|
|
|
call extend(l, [4, 5, 6], 0)
|
|
|
|
call assert_equal([4, 5, 6, 1, 2, 3], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET l = [1, 2, 3]
|
|
|
|
call extend(l, [4, 5, 6], 1)
|
|
|
|
call assert_equal([1, 4, 5, 6, 2, 3], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET l = [1, 2, 3]
|
|
|
|
call extend(l, [4, 5, 6], 3)
|
|
|
|
call assert_equal([1, 2, 3, 4, 5, 6], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET l = [1, 2, 3]
|
|
|
|
call extend(l, [4, 5, 6], -1)
|
|
|
|
call assert_equal([1, 2, 4, 5, 6, 3], l)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET l = [1, 2, 3]
|
|
|
|
call extend(l, [4, 5, 6], -3)
|
|
|
|
call assert_equal([4, 5, 6, 1, 2, 3], l)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
|
|
|
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:')
|
2021-05-24 11:43:47 -07:00
|
|
|
if has('float')
|
|
|
|
call assert_fails("call extend(l, [4, 5, 6], 1.2)", 'E805:')
|
|
|
|
endif
|
2019-02-25 03:34:55 -07:00
|
|
|
|
|
|
|
" Test extend() with dictionaries.
|
2018-02-11 16:05:54 -07:00
|
|
|
|
|
|
|
" Pass the same Dict to extend()
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR d = {'a': {'b': 'B'}, 'x': 9}
|
|
|
|
call extend(d, d)
|
|
|
|
call assert_equal({'a': {'b': 'B'}, 'x': 9}, d)
|
2018-02-11 16:05:54 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
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'}, d)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET d = {'a': 'A', 'b': 9}
|
|
|
|
call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force")
|
|
|
|
call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
2023-06-12 01:15:12 -07:00
|
|
|
LET d = {'a': 'A', 'b': 9}
|
|
|
|
call extend(d, {'b': 0, 'c': 'C'}, "keep")
|
|
|
|
call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2019-02-25 03:34:55 -07:00
|
|
|
|
|
|
|
let d = {'a': 'A', 'b': 'B'}
|
|
|
|
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
|
2023-08-16 22:01:45 -07:00
|
|
|
call assert_fails("call extend(d, {'b': 0}, [])", 'E730:')
|
2019-02-25 03:34:55 -07:00
|
|
|
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
|
2021-05-24 11:43:47 -07:00
|
|
|
if has('float')
|
2023-06-11 21:48:14 -07:00
|
|
|
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:')
|
2021-05-24 11:43:47 -07:00
|
|
|
endif
|
2019-02-25 03:34:55 -07:00
|
|
|
call assert_equal({'a': 'A', 'b': 'B'}, d)
|
|
|
|
|
|
|
|
call assert_fails("call extend([1, 2], 1)", 'E712:')
|
|
|
|
call assert_fails("call extend([1, 2], {})", 'E712:')
|
2021-04-08 19:40:02 -07:00
|
|
|
|
|
|
|
" Extend g: dictionary with an invalid variable name
|
|
|
|
call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
|
|
|
|
|
|
|
|
" Extend a list with itself.
|
2023-06-12 01:15:12 -07:00
|
|
|
let lines =<< trim END
|
|
|
|
VAR l = [1, 5, 7]
|
|
|
|
call extend(l, l, 0)
|
|
|
|
call assert_equal([1, 5, 7, 1, 5, 7], l)
|
|
|
|
LET l = [1, 5, 7]
|
|
|
|
call extend(l, l, 1)
|
|
|
|
call assert_equal([1, 1, 5, 7, 5, 7], l)
|
|
|
|
LET l = [1, 5, 7]
|
|
|
|
call extend(l, l, 2)
|
|
|
|
call assert_equal([1, 5, 1, 5, 7, 7], l)
|
|
|
|
LET l = [1, 5, 7]
|
|
|
|
call extend(l, l, 3)
|
|
|
|
call assert_equal([1, 5, 7, 1, 5, 7], l)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
2018-02-11 16:05:54 -07:00
|
|
|
endfunc
|
2019-08-21 11:17:09 -07:00
|
|
|
|
2023-02-26 19:10:42 -07:00
|
|
|
func Test_listdict_extendnew()
|
|
|
|
" Test extendnew() with lists
|
|
|
|
let l = [1, 2, 3]
|
|
|
|
call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
|
|
|
|
call assert_equal([1, 2, 3], l)
|
2023-02-27 08:27:09 -07:00
|
|
|
lockvar l
|
|
|
|
call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
|
2023-02-26 19:10:42 -07:00
|
|
|
|
2023-02-27 08:27:09 -07:00
|
|
|
" Test extendnew() with dictionaries.
|
2023-02-26 19:10:42 -07:00
|
|
|
let d = {'a': {'b': 'B'}}
|
|
|
|
call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
|
|
|
|
call assert_equal({'a': {'b': 'B'}}, d)
|
2023-02-27 08:27:09 -07:00
|
|
|
lockvar d
|
|
|
|
call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
|
2023-02-26 19:10:42 -07:00
|
|
|
endfunc
|
|
|
|
|
2019-08-21 11:17:09 -07:00
|
|
|
func s:check_scope_dict(x, fixed)
|
|
|
|
func s:gen_cmd(cmd, x)
|
|
|
|
return substitute(a:cmd, '\<x\ze:', a:x, 'g')
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
let cmd = s:gen_cmd('let x:foo = 1', a:x)
|
|
|
|
if a:fixed
|
|
|
|
call assert_fails(cmd, 'E461')
|
|
|
|
else
|
|
|
|
exe cmd
|
|
|
|
exe s:gen_cmd('call assert_equal(1, x:foo)', a:x)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let cmd = s:gen_cmd('let x:["bar"] = 2', a:x)
|
|
|
|
if a:fixed
|
|
|
|
call assert_fails(cmd, 'E461')
|
|
|
|
else
|
|
|
|
exe cmd
|
|
|
|
exe s:gen_cmd('call assert_equal(2, x:bar)', a:x)
|
|
|
|
endif
|
|
|
|
|
|
|
|
let cmd = s:gen_cmd('call extend(x:, {"baz": 3})', a:x)
|
|
|
|
if a:fixed
|
|
|
|
call assert_fails(cmd, 'E742')
|
|
|
|
else
|
|
|
|
exe cmd
|
|
|
|
exe s:gen_cmd('call assert_equal(3, x:baz)', a:x)
|
|
|
|
endif
|
|
|
|
|
|
|
|
if a:fixed
|
|
|
|
if a:x ==# 'a'
|
|
|
|
call assert_fails('unlet a:x', 'E795')
|
|
|
|
call assert_fails('call remove(a:, "x")', 'E742')
|
|
|
|
elseif a:x ==# 'v'
|
|
|
|
call assert_fails('unlet v:count', 'E795')
|
|
|
|
call assert_fails('call remove(v:, "count")', 'E742')
|
|
|
|
endif
|
|
|
|
else
|
|
|
|
exe s:gen_cmd('unlet x:foo', a:x)
|
|
|
|
exe s:gen_cmd('unlet x:bar', a:x)
|
|
|
|
exe s:gen_cmd('call remove(x:, "baz")', a:x)
|
|
|
|
endif
|
|
|
|
|
|
|
|
delfunc s:gen_cmd
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
func Test_scope_dict()
|
|
|
|
" Test for g:
|
|
|
|
call s:check_scope_dict('g', v:false)
|
|
|
|
|
|
|
|
" Test for s:
|
|
|
|
call s:check_scope_dict('s', v:false)
|
|
|
|
|
|
|
|
" Test for l:
|
|
|
|
call s:check_scope_dict('l', v:false)
|
|
|
|
|
|
|
|
" Test for a:
|
|
|
|
call s:check_scope_dict('a', v:true)
|
|
|
|
|
|
|
|
" Test for b:
|
|
|
|
call s:check_scope_dict('b', v:false)
|
|
|
|
|
|
|
|
" Test for w:
|
|
|
|
call s:check_scope_dict('w', v:false)
|
|
|
|
|
|
|
|
" Test for t:
|
|
|
|
call s:check_scope_dict('t', v:false)
|
|
|
|
|
|
|
|
" Test for v:
|
|
|
|
call s:check_scope_dict('v', v:true)
|
|
|
|
endfunc
|
2021-04-08 16:20:50 -07:00
|
|
|
|
2022-10-25 22:05:33 -07:00
|
|
|
" Test for deep nesting of lists (> 100)
|
|
|
|
func Test_deep_nested_list()
|
|
|
|
let deep_list = []
|
|
|
|
let l = deep_list
|
|
|
|
for i in range(102)
|
|
|
|
let newlist = []
|
|
|
|
call add(l, newlist)
|
|
|
|
let l = newlist
|
|
|
|
endfor
|
|
|
|
call add(l, 102)
|
|
|
|
|
|
|
|
call assert_fails('let m = deepcopy(deep_list)', 'E698:')
|
|
|
|
call assert_fails('lockvar 110 deep_list', 'E743:')
|
|
|
|
call assert_fails('unlockvar 110 deep_list', 'E743:')
|
|
|
|
" Nvim implements :echo very differently
|
|
|
|
" call assert_fails('let x = execute("echo deep_list")', 'E724:')
|
|
|
|
call test_garbagecollect_now()
|
|
|
|
unlet deep_list
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
" Test for deep nesting of dicts (> 100)
|
|
|
|
func Test_deep_nested_dict()
|
|
|
|
let deep_dict = {}
|
|
|
|
let d = deep_dict
|
|
|
|
for i in range(102)
|
|
|
|
let newdict = {}
|
|
|
|
let d.k = newdict
|
|
|
|
let d = newdict
|
|
|
|
endfor
|
|
|
|
let d.k = 'v'
|
|
|
|
|
|
|
|
call assert_fails('let m = deepcopy(deep_dict)', 'E698:')
|
|
|
|
call assert_fails('lockvar 110 deep_dict', 'E743:')
|
|
|
|
call assert_fails('unlockvar 110 deep_dict', 'E743:')
|
|
|
|
" Nvim implements :echo very differently
|
|
|
|
" call assert_fails('let x = execute("echo deep_dict")', 'E724:')
|
|
|
|
call test_garbagecollect_now()
|
|
|
|
unlet deep_dict
|
|
|
|
endfunc
|
|
|
|
|
2022-10-25 22:41:43 -07:00
|
|
|
" List and dict indexing tests
|
|
|
|
func Test_listdict_index()
|
2023-06-12 03:10:35 -07:00
|
|
|
call CheckLegacyAndVim9Failure(['echo function("min")[0]'], 'E695:')
|
|
|
|
call CheckLegacyAndVim9Failure(['echo v:true[0]'], 'E909:')
|
|
|
|
call CheckLegacyAndVim9Failure(['echo v:null[0]'], 'E909:')
|
2023-06-12 03:14:58 -07:00
|
|
|
call CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d.'], ['E15:', 'E1127:', 'E15:'])
|
2023-06-12 03:10:35 -07:00
|
|
|
call CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d[1 : 2]'], 'E719:')
|
|
|
|
|
2022-10-25 22:41:43 -07:00
|
|
|
call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
|
2023-06-12 03:14:58 -07:00
|
|
|
call CheckDefAndScriptFailure(['var v = [4, 6][() => 1]'], ['E1012', 'E703:'])
|
|
|
|
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : []]'], ['E730:', 'E1012:', 'E730:'])
|
2023-06-12 03:10:35 -07:00
|
|
|
|
2022-11-04 16:24:06 -07:00
|
|
|
call assert_fails("let v = range(5)[2:{-> 2}(]", ['E15:', 'E116:'])
|
2023-06-12 03:14:58 -07:00
|
|
|
call CheckDefAndScriptFailure(['var v = range(5)[2 : () => 2(]'], 'E15:')
|
|
|
|
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : 3'], ['E111:', 'E1097:', 'E111:'])
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, 10)'], 'E684:')
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, -10)'], 'E684:')
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, [])'], ['E745:', 'E1013:', 'E1210:'])
|
|
|
|
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[i] = 3'], ['E121:', 'E1001:', 'E121:'])
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[1.1] = 4'], ['E805:', 'E1012:', 'E805:'])
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: i] = [4, 5]'], ['E121:', 'E1001:', 'E121:'])
|
|
|
|
call CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: 3.2] = [4, 5]'], ['E805:', 'E1012:', 'E805:'])
|
2024-01-17 21:17:51 -07:00
|
|
|
" call CheckLegacyAndVim9Failure(['VAR t = test_unknown()', 'echo t[0]'], ['E685:', 'E909:', 'E685:'])
|
2022-10-25 22:41:43 -07:00
|
|
|
endfunc
|
|
|
|
|
2021-04-08 16:20:50 -07:00
|
|
|
" Test for a null list
|
|
|
|
func Test_null_list()
|
|
|
|
let l = v:_null_list
|
|
|
|
call assert_equal('', join(l))
|
|
|
|
call assert_equal(0, len(l))
|
|
|
|
call assert_equal(1, empty(l))
|
|
|
|
call assert_fails('let s = join([1, 2], [])', 'E730:')
|
|
|
|
call assert_equal([], split(v:_null_string))
|
|
|
|
call assert_equal([], l[:2])
|
|
|
|
call assert_true([] == l)
|
|
|
|
call assert_equal('[]', string(l))
|
|
|
|
" call assert_equal(0, sort(l))
|
|
|
|
" call assert_equal(0, sort(l))
|
|
|
|
" call assert_equal(0, uniq(l))
|
|
|
|
let k = [] + l
|
|
|
|
call assert_equal([], k)
|
|
|
|
let k = l + []
|
|
|
|
call assert_equal([], k)
|
|
|
|
call assert_equal(0, len(copy(l)))
|
|
|
|
call assert_equal(0, count(l, 5))
|
|
|
|
call assert_equal([], deepcopy(l))
|
|
|
|
call assert_equal(5, get(l, 2, 5))
|
|
|
|
call assert_equal(-1, index(l, 2, 5))
|
|
|
|
" call assert_equal(0, insert(l, 2, -1))
|
|
|
|
call assert_equal(0, min(l))
|
|
|
|
call assert_equal(0, max(l))
|
|
|
|
" call assert_equal(0, remove(l, 0, 2))
|
|
|
|
call assert_equal([], repeat(l, 2))
|
|
|
|
" call assert_equal(0, reverse(l))
|
|
|
|
" call assert_equal(0, sort(l))
|
|
|
|
call assert_equal('[]', string(l))
|
|
|
|
" call assert_equal(0, extend(l, l, 0))
|
|
|
|
lockvar l
|
|
|
|
call assert_equal(1, islocked('l'))
|
|
|
|
unlockvar l
|
|
|
|
endfunc
|
2022-10-25 22:05:33 -07:00
|
|
|
|
2022-10-26 04:04:38 -07:00
|
|
|
" Test for a null dict
|
|
|
|
func Test_null_dict()
|
2022-10-26 04:53:54 -07:00
|
|
|
call assert_equal(v:_null_dict, v:_null_dict)
|
|
|
|
let d = v:_null_dict
|
|
|
|
call assert_equal({}, d)
|
|
|
|
call assert_equal(0, len(d))
|
|
|
|
call assert_equal(1, empty(d))
|
2023-06-12 01:15:12 -07:00
|
|
|
call assert_equal([], items(d))
|
|
|
|
call assert_equal([], keys(d))
|
|
|
|
call assert_equal([], values(d))
|
2022-10-26 04:53:54 -07:00
|
|
|
call assert_false(has_key(d, 'k'))
|
|
|
|
call assert_equal('{}', string(d))
|
2023-04-14 22:31:30 -07:00
|
|
|
call assert_fails('let x = d[10]')
|
2022-10-26 04:53:54 -07:00
|
|
|
call assert_equal({}, {})
|
2023-04-14 22:17:32 -07:00
|
|
|
call assert_equal(0, len(copy(d)))
|
2023-04-14 22:31:30 -07:00
|
|
|
call assert_equal(0, count(d, 'k'))
|
|
|
|
call assert_equal({}, deepcopy(d))
|
|
|
|
call assert_equal(20, get(d, 'k', 20))
|
|
|
|
call assert_equal(0, min(d))
|
|
|
|
call assert_equal(0, max(d))
|
|
|
|
call assert_equal(0, remove(d, 'k'))
|
|
|
|
call assert_equal('{}', string(d))
|
|
|
|
" call assert_equal(0, extend(d, d, 0))
|
|
|
|
lockvar d
|
|
|
|
call assert_equal(1, islocked('d'))
|
|
|
|
unlockvar d
|
2022-10-26 04:04:38 -07:00
|
|
|
endfunc
|
|
|
|
|
2023-02-26 08:13:12 -07:00
|
|
|
" Test for the indexof() function
|
|
|
|
func Test_indexof()
|
|
|
|
let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
|
|
|
|
call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
|
|
|
|
call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
|
|
|
|
call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
|
|
|
|
call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
|
|
|
|
call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
|
|
|
|
|
|
|
|
let l = [#{n: 10}, #{n: 10}, #{n: 20}]
|
|
|
|
call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
|
|
|
|
call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
|
|
|
|
call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
|
|
|
|
call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
|
|
|
|
call assert_equal(0, indexof(l, "v:val.n == 10", v:_null_dict))
|
|
|
|
|
2023-02-26 09:00:01 -07:00
|
|
|
let s = ["a", "b", "c"]
|
|
|
|
call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
|
|
|
|
call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
|
|
|
|
call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
|
|
|
|
|
2023-02-26 08:13:12 -07:00
|
|
|
call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
|
2023-02-26 09:00:01 -07:00
|
|
|
call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
|
2023-02-26 08:13:12 -07:00
|
|
|
call assert_equal(-1, indexof(v:_null_list, {i, v -> v == 'a'}))
|
|
|
|
call assert_equal(-1, indexof(l, v:_null_string))
|
|
|
|
" Nvim doesn't have null functions
|
|
|
|
" call assert_equal(-1, indexof(l, test_null_function()))
|
2024-07-29 22:38:13 -07:00
|
|
|
call assert_equal(-1, indexof(l, ""))
|
|
|
|
call assert_fails('let i = indexof(l, " ")', 'E15:')
|
2023-02-26 08:13:12 -07:00
|
|
|
|
|
|
|
" failure cases
|
|
|
|
call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
|
|
|
|
call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
|
|
|
|
call assert_fails('let i = indexof(l, {})', 'E1256:')
|
|
|
|
call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:')
|
|
|
|
call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
|
|
|
|
|
|
|
|
func TestIdx(k, v)
|
|
|
|
return a:v.n == 20
|
|
|
|
endfunc
|
|
|
|
call assert_equal(2, indexof(l, function("TestIdx")))
|
|
|
|
delfunc TestIdx
|
|
|
|
func TestIdx(k, v)
|
|
|
|
return {}
|
|
|
|
endfunc
|
|
|
|
call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
|
|
|
|
delfunc TestIdx
|
|
|
|
func TestIdx(k, v)
|
|
|
|
throw "IdxError"
|
|
|
|
endfunc
|
|
|
|
call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
|
|
|
|
delfunc TestIdx
|
|
|
|
endfunc
|
|
|
|
|
2024-04-16 15:52:29 -07:00
|
|
|
func Test_extendnew_leak()
|
|
|
|
" This used to leak memory
|
|
|
|
for i in range(100) | silent! call extendnew([], [], []) | endfor
|
|
|
|
for i in range(100) | silent! call extendnew({}, {}, {}) | endfor
|
|
|
|
endfunc
|
|
|
|
|
2024-06-24 20:34:37 -07:00
|
|
|
" Test for comparing deeply nested List/Dict values
|
|
|
|
func Test_deep_nested_listdict_compare()
|
|
|
|
let lines =<< trim END
|
|
|
|
func GetNestedList(sz)
|
|
|
|
let l = []
|
|
|
|
let x = l
|
|
|
|
for i in range(a:sz)
|
|
|
|
let y = [1]
|
|
|
|
call add(x, y)
|
|
|
|
let x = y
|
|
|
|
endfor
|
|
|
|
return l
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
VAR l1 = GetNestedList(1000)
|
|
|
|
VAR l2 = GetNestedList(999)
|
|
|
|
call assert_false(l1 == l2)
|
|
|
|
|
|
|
|
#" after 1000 nested items, the lists are considered to be equal
|
|
|
|
VAR l3 = GetNestedList(1001)
|
|
|
|
VAR l4 = GetNestedList(1002)
|
|
|
|
call assert_true(l3 == l4)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
|
|
|
|
let lines =<< trim END
|
|
|
|
func GetNestedDict(sz)
|
|
|
|
let d = {}
|
|
|
|
let x = d
|
|
|
|
for i in range(a:sz)
|
|
|
|
let y = {}
|
|
|
|
let x['a'] = y
|
|
|
|
let x = y
|
|
|
|
endfor
|
|
|
|
return d
|
|
|
|
endfunc
|
|
|
|
|
|
|
|
VAR d1 = GetNestedDict(1000)
|
|
|
|
VAR d2 = GetNestedDict(999)
|
|
|
|
call assert_false(d1 == d2)
|
|
|
|
|
|
|
|
#" after 1000 nested items, the Dicts are considered to be equal
|
|
|
|
VAR d3 = GetNestedDict(1001)
|
|
|
|
VAR d4 = GetNestedDict(1002)
|
|
|
|
call assert_true(d3 == d4)
|
|
|
|
END
|
|
|
|
call CheckLegacyAndVim9Success(lines)
|
|
|
|
endfunc
|
|
|
|
|
2022-10-25 22:05:33 -07:00
|
|
|
" vim: shiftwidth=2 sts=2 expandtab
|