blob: fb350a813c048751a08a33857026ecc04070238b [file] [log] [blame]
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001" Tests for the List and Dict types
rbtnn0ccb5842021-12-18 18:33:46 +00002scriptencoding utf-8
Bram Moolenaarfb094e12017-11-05 20:59:28 +01003
Bram Moolenaar62aec932022-01-29 21:45:34 +00004import './vim9.vim' as v9
Bram Moolenaar63cb6562021-07-20 22:21:59 +02005
Bram Moolenaarfb094e12017-11-05 20:59:28 +01006func TearDown()
7 " Run garbage collection after every test
8 call test_garbagecollect_now()
9endfunc
10
11" Tests for List type
12
13" List creation
14func Test_list_create()
15 " Creating List directly with different types
16 let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
17 call assert_equal("[1, 'as''d', [1, 2, function('strlen')], {'a': 1}]", string(l))
18 call assert_equal({'a' : 1}, l[-1])
19 call assert_equal(1, l[-4])
20 let x = 10
21 try
22 let x = l[-5]
23 catch
24 call assert_match('E684:', v:exception)
25 endtry
26 call assert_equal(10, x)
27endfunc
28
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +020029" This was allowed in legacy Vim script
30let s:list_with_spaces = [1 , 2 , 3]
31
Bram Moolenaarfb094e12017-11-05 20:59:28 +010032" List slices
33func Test_list_slice()
34 let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
35 call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[:])
36 call assert_equal(['as''d', [1, 2, function('strlen')], {'a': 1}], l[1:])
37 call assert_equal([1, 'as''d', [1, 2, function('strlen')]], l[:-2])
38 call assert_equal([1, 'as''d', [1, 2, function('strlen')], {'a': 1}], l[0:8])
39 call assert_equal([], l[8:-1])
Bram Moolenaar8b633132020-03-20 18:20:51 +010040 call assert_equal([], l[0:-10])
Bram Moolenaarea04a6e2020-04-23 13:38:02 +020041 " perform an operation on a list slice
42 let l = [1, 2, 3]
43 let l[:1] += [1, 2]
44 let l[2:] -= [1]
45 call assert_equal([2, 4, 2], l)
Bram Moolenaar92f05f22021-08-12 21:12:56 +020046
47 let lines =<< trim END
48 VAR l = [1, 2]
49 call assert_equal([1, 2], l[:])
50 call assert_equal([2], l[-1 : -1])
51 call assert_equal([1, 2], l[-2 : -1])
52 END
Bram Moolenaar62aec932022-01-29 21:45:34 +000053 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar92f05f22021-08-12 21:12:56 +020054
55 let l = [1, 2]
56 call assert_equal([], l[-3 : -1])
57
58 let lines =<< trim END
59 var l = [1, 2]
60 assert_equal([1, 2], l[-3 : -1])
61 END
Bram Moolenaar62aec932022-01-29 21:45:34 +000062 call v9.CheckDefAndScriptSuccess(lines)
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +020063
64 call assert_fails('let l[[]] = 1', 'E730: Using a List as a String')
65 call assert_fails('let l[1 : []] = [1]', 'E730: Using a List as a String')
Bram Moolenaarfb094e12017-11-05 20:59:28 +010066endfunc
67
68" List identity
69func Test_list_identity()
Bram Moolenaar63cb6562021-07-20 22:21:59 +020070 let lines =<< trim END
71 VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
72 VAR ll = l
73 VAR lx = copy(l)
74 call assert_true(l == ll)
75 call assert_false(l isnot ll)
76 call assert_true(l is ll)
77 call assert_true(l == lx)
78 call assert_false(l is lx)
79 call assert_true(l isnot lx)
80 END
Bram Moolenaar62aec932022-01-29 21:45:34 +000081 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +010082endfunc
83
84" removing items with :unlet
85func Test_list_unlet()
Bram Moolenaar63cb6562021-07-20 22:21:59 +020086 let lines =<< trim END
87 VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
88 unlet l[2]
89 call assert_equal([1, 'as''d', {'a': 1}], l)
90 LET l = range(8)
91 unlet l[: 3]
92 unlet l[1 :]
93 call assert_equal([4], l)
Bram Moolenaarfb094e12017-11-05 20:59:28 +010094
Bram Moolenaar63cb6562021-07-20 22:21:59 +020095 #" removing items out of range: silently skip items that don't exist
96 LET l = [0, 1, 2, 3]
97 unlet l[2 : 2]
98 call assert_equal([0, 1, 3], l)
99 LET l = [0, 1, 2, 3]
100 unlet l[2 : 3]
101 call assert_equal([0, 1], l)
102 LET l = [0, 1, 2, 3]
103 unlet l[2 : 4]
104 call assert_equal([0, 1], l)
105 LET l = [0, 1, 2, 3]
106 unlet l[2 : 5]
107 call assert_equal([0, 1], l)
108 LET l = [0, 1, 2, 3]
109 unlet l[-2 : 2]
110 call assert_equal([0, 1, 3], l)
111 LET l = [0, 1, 2, 3]
112 unlet l[-3 : 2]
113 call assert_equal([0, 3], l)
114 LET l = [0, 1, 2, 3]
115 unlet l[-4 : 2]
116 call assert_equal([3], l)
117 LET l = [0, 1, 2, 3]
118 unlet l[-5 : 2]
119 call assert_equal([3], l)
120 LET l = [0, 1, 2, 3]
121 unlet l[-6 : 2]
122 call assert_equal([3], l)
123 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000124 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200125
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100126 let l = [0, 1, 2, 3]
127 unlet l[2:2]
128 call assert_equal([0, 1, 3], l)
129 let l = [0, 1, 2, 3]
130 unlet l[2:3]
131 call assert_equal([0, 1], l)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200132
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200133 let lines =<< trim END
134 VAR l = [0, 1, 2, 3]
135 unlet l[2 : 1]
136 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000137 call v9.CheckLegacyAndVim9Failure(lines, 'E684:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200138
139 let lines =<< trim END
140 VAR l = [0, 1, 2, 3]
141 unlet l[-1 : 2]
142 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000143 call v9.CheckLegacyAndVim9Failure(lines, 'E684:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100144endfunc
145
146" assignment to a list
147func Test_list_assign()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200148 let lines =<< trim END
149 VAR l = [0, 1, 2, 3]
150 VAR va = 0
151 VAR vb = 0
152 LET [va, vb] = l[2 : 3]
153 call assert_equal([2, 3], [va, vb])
154 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000155 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200156
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200157 let lines =<< trim END
158 let l = [0, 1, 2, 3]
159 let [va, vb] = l
160 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000161 call v9.CheckScriptFailure(lines, 'E687:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200162 let lines =<< trim END
163 var l = [0, 1, 2, 3]
164 var va = 0
165 var vb = 0
166 [va, vb] = l
167 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000168 call v9.CheckScriptFailure(['vim9script'] + lines, 'E687:')
169 call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 4')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200170
171 let lines =<< trim END
172 let l = [0, 1, 2, 3]
173 let [va, vb] = l[1:1]
174 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000175 call v9.CheckScriptFailure(lines, 'E688:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200176 let lines =<< trim END
177 var l = [0, 1, 2, 3]
178 var va = 0
179 var vb = 0
180 [va, vb] = l[1 : 1]
181 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000182 call v9.CheckScriptFailure(['vim9script'] + lines, 'E688:')
183 call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +0200184
185 let lines =<< trim END
186 VAR l = [2]
187 LET l += test_null_list()
188 call assert_equal([2], l)
189 LET l = test_null_list()
190 LET l += [1]
191 call assert_equal([1], l)
192 END
193 call v9.CheckLegacyAndVim9Success(lines)
194
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100195 let lines =<< trim END
196 VAR [x, y] = test_null_list()
197 END
198 call v9.CheckLegacyAndVim9Failure(lines, [
199 \ 'E714: List required',
200 \ 'E1093: Expected 2 items but got 0',
201 \ 'E714: List required'])
202
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +0200203 let d = {'abc': [1, 2, 3]}
204 call assert_fails('let d.abc[0:0z10] = [10, 20]', 'E976: Using a Blob as a String')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100205endfunc
206
207" test for range assign
208func Test_list_range_assign()
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200209 let lines =<< trim END
210 VAR l = [0]
211 LET l[:] = [1, 2]
212 call assert_equal([1, 2], l)
213 LET l[-4 : -1] = [5, 6]
214 call assert_equal([5, 6], l)
215 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000216 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200217
218 let lines =<< trim END
219 var l = [7]
220 l[:] = ['text']
221 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000222 call v9.CheckDefAndScriptFailure(lines, 'E1012:', 2)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100223endfunc
224
Bram Moolenaar976f8592022-08-30 14:34:52 +0100225func Test_list_items()
226 let r = []
227 let l = ['a', 'b', 'c']
228 for [idx, val] in items(l)
229 call extend(r, [[idx, val]])
230 endfor
231 call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
232
Bram Moolenaar3e518a82022-08-30 17:45:33 +0100233 call assert_fails('call items(3)', 'E1225:')
234endfunc
235
236func Test_string_items()
237 let r = []
238 let s = 'ábツ'
239 for [idx, val] in items(s)
240 call extend(r, [[idx, val]])
241 endfor
242 call assert_equal([[0, 'á'], [1, 'b'], [2, 'ツ']], r)
Bram Moolenaar976f8592022-08-30 14:34:52 +0100243endfunc
244
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200245" Test removing items in list
246func Test_list_func_remove()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200247 let lines =<< trim END
248 #" Test removing 1 element
249 VAR l = [1, 2, 3, 4]
250 call assert_equal(1, remove(l, 0))
251 call assert_equal([2, 3, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200252
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200253 LET l = [1, 2, 3, 4]
254 call assert_equal(2, remove(l, 1))
255 call assert_equal([1, 3, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200256
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200257 LET l = [1, 2, 3, 4]
258 call assert_equal(4, remove(l, -1))
259 call assert_equal([1, 2, 3], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200260
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200261 #" Test removing range of element(s)
262 LET l = [1, 2, 3, 4]
263 call assert_equal([3], remove(l, 2, 2))
264 call assert_equal([1, 2, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200265
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200266 LET l = [1, 2, 3, 4]
267 call assert_equal([2, 3], remove(l, 1, 2))
268 call assert_equal([1, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200269
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200270 LET l = [1, 2, 3, 4]
271 call assert_equal([2, 3], remove(l, -3, -2))
272 call assert_equal([1, 4], l)
273 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000274 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200275
276 " Test invalid cases
277 let l = [1, 2, 3, 4]
278 call assert_fails("call remove(l, 5)", 'E684:')
279 call assert_fails("call remove(l, 1, 5)", 'E684:')
280 call assert_fails("call remove(l, 3, 2)", 'E16:')
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +0100281 call assert_fails("call remove(1, 0)", 'E896:')
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200282 call assert_fails("call remove(l, l)", 'E745:')
283endfunc
284
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200285" List add() function
286func Test_list_add()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200287 let lines =<< trim END
288 VAR l = []
289 call add(l, 1)
290 call add(l, [2, 3])
291 call add(l, [])
292 call add(l, test_null_list())
293 call add(l, {'k': 3})
294 call add(l, {})
295 call add(l, test_null_dict())
296 call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l)
297 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000298 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200299
300 " weird legacy behavior
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200301 call assert_equal(1, add(test_null_list(), 4))
302endfunc
303
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100304" Tests for Dictionary type
305
306func Test_dict()
307 " Creating Dictionary directly with different types
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200308 let lines =<< trim END
309 VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}, }
310 call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
311 call assert_equal('asd', d.1)
312 call assert_equal(['-1', '1', 'b'], sort(keys(d)))
313 call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d))
314 call extend(d, {3: 33, 1: 99})
315 call extend(d, {'b': 'bbb', 'c': 'ccc'}, "keep")
316 call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d)
317 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000318 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200319
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100320 let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
321 call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200322
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100323 let v = []
324 for [key, val] in items(d)
325 call extend(v, [key, val])
326 unlet key val
327 endfor
328 call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v)
329
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200330 call extend(d, {3: 33, 1: 99})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200331 call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737:')
Bram Moolenaar08f41572020-04-20 16:50:00 +0200332
333 " duplicate key
334 call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:')
335 " missing comma
336 call assert_fails("let d = {'k' : 10 'k' : 20}", 'E722:')
337 " missing curly brace
338 call assert_fails("let d = {'k' : 10,", 'E723:')
339 " invalid key
340 call assert_fails('let d = #{++ : 10}', 'E15:')
341 " wrong type for key
342 call assert_fails('let d={[] : 10}', 'E730:')
343 " undefined variable as value
344 call assert_fails("let d={'k' : i}", 'E121:')
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000345
346 " allow key starting with number at the start, not a curly expression
347 call assert_equal({'1foo': 77}, #{1foo: 77})
ii147c7e1e92022-09-07 19:40:17 +0100348
349 " #{expr} is not a curly expression
350 let x = 'x'
351 call assert_equal(#{g: x}, #{g:x})
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100352endfunc
353
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200354" This was allowed in legacy Vim script
355let s:dict_with_spaces = {'one' : 1 , 'two' : 2 , 'three' : 3}
356let s:dict_with_spaces_lit = #{one : 1 , two : 2 , three : 3}
357
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100358" Dictionary identity
359func Test_dict_identity()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200360 let lines =<< trim END
361 VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, }
362 VAR dd = d
363 VAR dx = copy(d)
364 call assert_true(d == dd)
365 call assert_false(d isnot dd)
366 call assert_true(d is dd)
367 call assert_true(d == dx)
368 call assert_false(d is dx)
369 call assert_true(d isnot dx)
370 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000371 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100372endfunc
373
374" removing items with :unlet
375func Test_dict_unlet()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200376 let lines =<< trim END
377 VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}}
378 unlet d.b
379 unlet d[-1]
380 call assert_equal({'1': 99, '3': 33}, d)
381 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000382 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100383endfunc
384
385" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
386func Test_dict_big()
387 let d = {}
388 for i in range(1500)
389 let d[i] = 3000 - i
390 endfor
391 call assert_equal([3000, 2900, 2001, 1600, 1501], [d[0], d[100], d[999], d[1400], d[1499]])
392 let str = ''
393 try
394 let n = d[1500]
395 catch
Bram Moolenaar6d967122020-10-30 19:06:18 +0100396 let str = substitute(v:exception, '\v(.{14}).*( "\d{4}").*', '\1\2', '')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100397 endtry
Bram Moolenaar6d967122020-10-30 19:06:18 +0100398 call assert_equal('Vim(let):E716: "1500"', str)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100399
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200400 " lookup each item
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100401 for i in range(1500)
402 call assert_equal(3000 - i, d[i])
403 endfor
404 let i += 1
405
406 " delete even items
407 while i >= 2
408 let i -= 2
409 unlet d[i]
410 endwhile
411 call assert_equal('NONE', get(d, 1500 - 100, 'NONE'))
412 call assert_equal(2999, d[1])
413
414 " delete odd items, checking value, one intentionally wrong
415 let d[33] = 999
416 let i = 1
417 while i < 1500
418 if i != 33
419 call assert_equal(3000 - i, d[i])
420 else
421 call assert_equal(999, d[i])
422 endif
423 unlet d[i]
424 let i += 2
425 endwhile
426 call assert_equal({}, d)
427 unlet d
428endfunc
429
430" Dictionary function
431func Test_dict_func()
432 let d = {}
433 func d.func(a) dict
434 return a:a . len(self.data)
435 endfunc
436 let d.data = [1,2,3]
437 call assert_equal('len: 3', d.func('len: '))
438 let x = d.func('again: ')
439 call assert_equal('again: 3', x)
440 let Fn = d.func
441 call assert_equal('xxx3', Fn('xxx'))
442endfunc
443
Bram Moolenaarb13ab992020-07-27 21:43:28 +0200444func Test_dict_assign()
445 let d = {}
446 let d.1 = 1
447 let d._ = 2
448 call assert_equal({'1': 1, '_': 2}, d)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200449
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200450 let lines =<< trim END
451 VAR d = {}
452 LET d.a = 1
453 LET d._ = 2
454 call assert_equal({'a': 1, '_': 2}, d)
455 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000456 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200457
458 let lines =<< trim END
459 let n = 0
460 let n.key = 3
461 END
Ernie Raelf8da3242023-10-11 21:22:12 +0200462 call v9.CheckScriptFailure(lines, 'E1203: Dot not allowed after a number: n.key = 3')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200463 let lines =<< trim END
464 vim9script
465 var n = 0
466 n.key = 3
467 END
Ernie Raelf8da3242023-10-11 21:22:12 +0200468 call v9.CheckScriptFailure(lines, 'E1203: Dot not allowed after a number: n.key = 3')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200469 let lines =<< trim END
470 var n = 0
471 n.key = 3
472 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000473 call v9.CheckDefFailure(lines, 'E1141:')
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +0200474
475 let d = {'abc': {}}
476 call assert_fails("let d.abc[0z10] = 10", 'E976: Using a Blob as a String')
Bram Moolenaarb13ab992020-07-27 21:43:28 +0200477endfunc
478
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100479" Function in script-local List or Dict
480func Test_script_local_dict_func()
481 let g:dict = {}
482 function g:dict.func() dict
483 return 'g:dict.func' . self.foo[1] . self.foo[0]('asdf')
484 endfunc
485 let g:dict.foo = ['-', 2, 3]
486 call insert(g:dict.foo, function('strlen'))
487 call assert_equal('g:dict.func-4', g:dict.func())
488 unlet g:dict
489endfunc
490
Bram Moolenaar08f41572020-04-20 16:50:00 +0200491" Test removing items in a dictionary
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200492func Test_dict_func_remove()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200493 let lines =<< trim END
494 VAR d = {1: 'a', 2: 'b', 3: 'c'}
495 call assert_equal('b', remove(d, 2))
496 call assert_equal({1: 'a', 3: 'c'}, d)
497 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000498 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200499
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200500 let lines =<< trim END
501 VAR d = {1: 'a', 3: 'c'}
502 call remove(d, 1, 2)
503 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000504 call v9.CheckLegacyAndVim9Failure(lines, 'E118:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200505
506 let lines =<< trim END
507 VAR d = {1: 'a', 3: 'c'}
508 call remove(d, 'a')
509 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000510 call v9.CheckLegacyAndVim9Failure(lines, 'E716:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200511
512 let lines =<< trim END
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000513 let d = {'a-b': 55}
514 echo d.a-b
515 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000516 call v9.CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000517
518 let lines =<< trim END
519 vim9script
520 var d = {'a-b': 55}
521 echo d.a-b
522 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000523 call v9.CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000524
525 let lines =<< trim END
526 var d = {'a-b': 55}
527 echo d.a-b
528 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000529 call v9.CheckDefFailure(lines, 'E1004: White space required before and after ''-''')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000530
531 let lines =<< trim END
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200532 let d = {1: 'a', 3: 'c'}
533 call remove(d, [])
534 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000535 call v9.CheckScriptFailure(lines, 'E730:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200536 let lines =<< trim END
537 vim9script
538 var d = {1: 'a', 3: 'c'}
539 call remove(d, [])
540 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000541 call v9.CheckScriptFailure(lines, 'E1220: String or Number required for argument 2')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200542 let lines =<< trim END
543 var d = {1: 'a', 3: 'c'}
544 call remove(d, [])
545 END
Yegappan Lakshmanan66897192023-12-05 15:51:50 +0100546 call v9.CheckDefExecFailure(lines, 'E1013: Argument 2: type mismatch, expected string but got list<any>')
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200547endfunc
548
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100549" Nasty: remove func from Dict that's being called (works)
550func Test_dict_func_remove_in_use()
551 let d = {1:1}
552 func d.func(a)
553 return "a:" . a:a
554 endfunc
555 let expected = 'a:' . string(get(d, 'func'))
556 call assert_equal(expected, d.func(string(remove(d, 'func'))))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200557
558 " similar, in a way it also works in Vim9
559 let lines =<< trim END
560 VAR d = {1: 1, 2: 'x'}
561 func GetArg(a)
562 return "a:" .. a:a
563 endfunc
564 LET d.func = function('GetArg')
565 VAR expected = 'a:' .. string(get(d, 'func'))
566 call assert_equal(expected, d.func(string(remove(d, 'func'))))
567 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000568 call v9.CheckTransLegacySuccess(lines)
569 call v9.CheckTransVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100570endfunc
571
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200572func Test_dict_literal_keys()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200573 call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},)
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200574
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200575 " why *{} cannot be used for a literal dictionary
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200576 let blue = 'blue'
577 call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200578endfunc
579
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100580" Nasty: deepcopy() dict that refers to itself (fails when noref used)
581func Test_dict_deepcopy()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200582 let lines =<< trim END
583 VAR d = {1: 1, 2: '2'}
584 VAR l = [4, d, 6]
585 LET d[3] = l
586 VAR dc = deepcopy(d)
587 call deepcopy(d, 1)
588 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000589 call v9.CheckLegacyAndVim9Failure(lines, 'E698:')
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200590
591 let lines =<< trim END
592 VAR d = {1: 1, 2: '2'}
593 VAR l = [4, d, 6]
594 LET d[3] = l
595 VAR l2 = [0, l, l, 3]
596 LET l[1] = l2
597 VAR l3 = deepcopy(l2)
598 call assert_true(l3[1] is l3[2])
599 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000600 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200601
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100602 call assert_fails("call deepcopy([1, 2], 2)", 'E1212:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100603endfunc
604
605" Locked variables
606func Test_list_locked_var()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200607 " Not tested with :def function, local vars cannot be locked.
608 let lines =<< trim END
609 VAR expected = [
610 \ [['1000-000', 'ppppppF'],
611 \ ['0000-000', 'ppppppp'],
612 \ ['0000-000', 'ppppppp']],
613 \ [['1000-000', 'ppppppF'],
614 \ ['0000-000', 'ppppppp'],
615 \ ['0000-000', 'ppppppp']],
616 \ [['1100-100', 'ppFppFF'],
617 \ ['0000-000', 'ppppppp'],
618 \ ['0000-000', 'ppppppp']],
619 \ [['1110-110', 'pFFpFFF'],
620 \ ['0010-010', 'pFppFpp'],
621 \ ['0000-000', 'ppppppp']],
622 \ [['1111-111', 'FFFFFFF'],
623 \ ['0011-011', 'FFpFFpp'],
624 \ ['0000-000', 'ppppppp']]
625 \ ]
626 for depth in range(5)
627 for u in range(3)
628 VAR l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
629 exe "lockvar " .. depth .. " l"
630 if u == 1
631 exe "unlockvar l"
632 elseif u == 2
633 exe "unlockvar " .. depth .. " l"
634 endif
635 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]")
636 call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
637 LET ps = ''
638 try
639 LET l[1][1][0] = 99
640 LET ps ..= 'p'
641 catch
642 LET ps ..= 'F'
643 endtry
644 try
645 LET l[1][1] = [99]
646 LET ps ..= 'p'
647 catch
648 LET ps ..= 'F'
649 endtry
650 try
651 LET l[1] = [99]
652 LET ps ..= 'p'
653 catch
654 LET ps ..= 'F'
655 endtry
656 try
657 LET l[2]['6'][7] = 99
658 LET ps ..= 'p'
659 catch
660 LET ps ..= 'F'
661 endtry
662 try
663 LET l[2][6] = {99: 99}
664 LET ps ..= 'p'
665 catch
666 LET ps ..= 'F'
667 endtry
668 try
669 LET l[2] = {99: 99}
670 LET ps ..= 'p'
671 catch
672 LET ps ..= 'F'
673 endtry
674 try
675 LET l = [99]
676 LET ps ..= 'p'
677 catch
678 LET ps ..= 'F'
679 endtry
680 call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
681 unlock! l
682 endfor
683 endfor
684 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000685 call v9.CheckTransLegacySuccess(lines)
686 call v9.CheckTransVim9Success(lines)
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200687
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100688 call assert_fails("let x=islocked('a b')", 'E488:')
689 let mylist = [1, 2, 3]
690 call assert_fails("let x = islocked('mylist[1:2]')", 'E786:')
691 let mydict = {'k' : 'v'}
692 call assert_fails("let x = islocked('mydict.a')", 'E716:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100693endfunc
694
695" Unletting locked variables
696func Test_list_locked_var_unlet()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200697 " Not tested with Vim9: script and local variables cannot be unlocked
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100698 let expected = [
Bram Moolenaara187c432020-09-16 21:08:28 +0200699 \ [['1000-000', 'ppppppp'],
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100700 \ ['0000-000', 'ppppppp'],
701 \ ['0000-000', 'ppppppp']],
702 \ [['1000-000', 'ppFppFp'],
703 \ ['0000-000', 'ppppppp'],
704 \ ['0000-000', 'ppppppp']],
705 \ [['1100-100', 'pFFpFFp'],
706 \ ['0000-000', 'ppppppp'],
707 \ ['0000-000', 'ppppppp']],
708 \ [['1110-110', 'FFFFFFp'],
709 \ ['0010-010', 'FppFppp'],
710 \ ['0000-000', 'ppppppp']],
711 \ [['1111-111', 'FFFFFFp'],
712 \ ['0011-011', 'FppFppp'],
713 \ ['0000-000', 'ppppppp']]
714 \ ]
715
716 for depth in range(5)
717 for u in range(3)
718 unlet! l
719 let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
720 exe "lockvar " . depth . " l"
721 if u == 1
722 exe "unlockvar l"
723 elseif u == 2
724 exe "unlockvar " . depth . " l"
725 endif
726 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]")
Bram Moolenaara187c432020-09-16 21:08:28 +0200727 call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100728 let ps = ''
729 try
730 unlet l[2]['6'][7]
731 let ps .= 'p'
732 catch
733 let ps .= 'F'
734 endtry
735 try
736 unlet l[2][6]
737 let ps .= 'p'
738 catch
739 let ps .= 'F'
740 endtry
741 try
742 unlet l[2]
743 let ps .= 'p'
744 catch
745 let ps .= 'F'
746 endtry
747 try
748 unlet l[1][1][0]
749 let ps .= 'p'
750 catch
751 let ps .= 'F'
752 endtry
753 try
754 unlet l[1][1]
755 let ps .= 'p'
756 catch
757 let ps .= 'F'
758 endtry
759 try
760 unlet l[1]
761 let ps .= 'p'
762 catch
763 let ps .= 'F'
764 endtry
765 try
766 unlet l
767 let ps .= 'p'
768 catch
769 let ps .= 'F'
770 endtry
771 call assert_equal(expected[depth][u][1], ps)
772 endfor
773 endfor
Bram Moolenaar422085f2021-12-17 20:36:15 +0000774
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +0000775 " Deleting a list range with locked items works, but changing the items
776 " fails.
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200777 let l = [1, 2, 3, 4]
778 lockvar l[1:2]
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +0000779 call assert_fails('let l[1:2] = [8, 9]', 'E741:')
780 unlet l[1:2]
781 call assert_equal([1, 4], l)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200782 unlet l
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100783endfunc
784
785" Locked variables and :unlet or list / dict functions
786
787" No :unlet after lock on dict:
788func Test_dict_lock_unlet()
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100789 let d = {'a': 99, 'b': 100}
790 lockvar 1 d
Bram Moolenaare2e40752020-09-04 21:18:46 +0200791 call assert_fails('unlet d.a', 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100792endfunc
793
794" unlet after lock on dict item
795func Test_dict_item_lock_unlet()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200796 let lines =<< trim END
797 VAR d = {'a': 99, 'b': 100}
798 lockvar d.a
799 unlet d.a
800 call assert_equal({'b': 100}, d)
801 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000802 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100803endfunc
804
805" filter() after lock on dict item
806func Test_dict_lock_filter()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200807 let lines =<< trim END
808 VAR d = {'a': 99, 'b': 100}
809 lockvar d.a
810 call filter(d, 'v:key != "a"')
811 call assert_equal({'b': 100}, d)
812 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000813 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100814endfunc
815
816" map() after lock on dict
817func Test_dict_lock_map()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200818 let lines =<< trim END
819 VAR d = {'a': 99, 'b': 100}
820 lockvar 1 d
821 call map(d, 'v:val + 200')
822 call assert_equal({'a': 299, 'b': 300}, d)
823 END
824 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000825 call v9.CheckTransLegacySuccess(lines)
826 call v9.CheckTransVim9Success(lines)
Bram Moolenaar71b76852021-12-17 20:15:38 +0000827
828 " For a :def function use a global dict.
829 let lines =<< trim END
830 let g:thedict = {'a': 77, 'b': 88}
831 lockvar 1 g:thedict
832 def Delkey()
833 unlet g:thedict.a
834 enddef
835 call Delkey()
836 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000837 call v9.CheckScriptFailure(lines, 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100838endfunc
839
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000840" Lock one item in a list
841func Test_list_item_lock_map()
842 let lines =<< trim END
843 VAR l = [99, 100, 101]
844 lockvar l[1]
845 call assert_fails("echo map(l, 'v:val + 200')", 'E741:')
846 call assert_equal([299, 100, 101], l)
847 END
848 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000849 call v9.CheckTransLegacySuccess(lines)
850 call v9.CheckTransVim9Success(lines)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000851endfunc
852
853" Lock one item in a dict
854func Test_dict_item_lock_map()
855 let lines =<< trim END
856 VAR d = {'a': 99, 'b': 100, 'c': 101}
857 lockvar d.b
858 call assert_fails("echo map(d, 'v:val + 200')", 'E741:')
859 call assert_equal({'a': 299, 'b': 100, 'c': 101}, d)
860 END
861 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000862 call v9.CheckTransLegacySuccess(lines)
863 call v9.CheckTransVim9Success(lines)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000864endfunc
865
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100866" No extend() after lock on dict item
867func Test_dict_lock_extend()
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100868 let d = {'a': 99, 'b': 100}
869 lockvar d.a
Bram Moolenaare2e40752020-09-04 21:18:46 +0200870 call assert_fails("call extend(d, {'a' : 123})", 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100871 call assert_equal({'a': 99, 'b': 100}, d)
872endfunc
873
Bram Moolenaarf7b398c2020-04-23 15:46:35 +0200874" Cannot use += with a locked dict
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200875func Test_dict_lock_operator()
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200876 let d = {}
877 lockvar d
878 call assert_fails("let d += {'k' : 10}", 'E741:')
879 unlockvar d
880endfunc
881
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100882" No remove() of write-protected scope-level variable
Bram Moolenaar1e115362019-01-09 23:01:02 +0100883func Tfunc1(this_is_a_long_parameter_name)
Bram Moolenaare2e40752020-09-04 21:18:46 +0200884 call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742:')
Bram Moolenaar1e115362019-01-09 23:01:02 +0100885endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100886func Test_dict_scope_var_remove()
Bram Moolenaar1e115362019-01-09 23:01:02 +0100887 call Tfunc1('testval')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100888endfunc
889
890" No extend() of write-protected scope-level variable
Bram Moolenaar31b81602019-02-10 22:14:27 +0100891func Test_dict_scope_var_extend()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200892 call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +0100893endfunc
894
Bram Moolenaar1e115362019-01-09 23:01:02 +0100895func Tfunc2(this_is_a_long_parameter_name)
Bram Moolenaare2e40752020-09-04 21:18:46 +0200896 call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100897endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +0100898func Test_dict_scope_var_extend_overwrite()
Bram Moolenaar1e115362019-01-09 23:01:02 +0100899 call Tfunc2('testval')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100900endfunc
901
902" No :unlet of variable in locked scope
903func Test_lock_var_unlet()
904 let b:testvar = 123
905 lockvar 1 b:
906 call assert_fails('unlet b:testvar', 'E741:')
907 unlockvar 1 b:
908 unlet! b:testvar
909endfunc
910
911" No :let += of locked list variable
912func Test_let_lock_list()
913 let l = ['a', 'b', 3]
914 lockvar 1 l
915 call assert_fails("let l += ['x']", 'E741:')
916 call assert_equal(['a', 'b', 3], l)
917
918 unlet l
919 let l = [1, 2, 3, 4]
920 lockvar! l
921 call assert_equal([1, 2, 3, 4], l)
922 unlockvar l[1]
923 call assert_fails('unlet l[0:1]', 'E741:')
924 call assert_equal([1, 2, 3, 4], l)
925 call assert_fails('unlet l[1:2]', 'E741:')
926 call assert_equal([1, 2, 3, 4], l)
927 unlockvar l[1]
928 call assert_fails('let l[0:1] = [0, 1]', 'E741:')
929 call assert_equal([1, 2, 3, 4], l)
930 call assert_fails('let l[1:2] = [0, 1]', 'E741:')
931 call assert_equal([1, 2, 3, 4], l)
932 unlet l
Bram Moolenaar422085f2021-12-17 20:36:15 +0000933
934 let lines =<< trim END
935 def TryUnletListItem(l: list<any>)
936 unlet l[0]
937 enddef
938 let l = [1, 2, 3, 4]
939 lockvar! l
940 call TryUnletListItem(l)
941 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000942 call v9.CheckScriptFailure(lines, 'E741:')
Bram Moolenaar422085f2021-12-17 20:36:15 +0000943 unlet g:l
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100944endfunc
945
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100946" Locking part of the list
947func Test_let_lock_list_items()
948 let l = [1, 2, 3, 4]
949 lockvar l[2:]
950 call assert_equal(0, islocked('l[0]'))
951 call assert_equal(1, islocked('l[2]'))
952 call assert_equal(1, islocked('l[3]'))
953 call assert_fails('let l[2] = 10', 'E741:')
954 call assert_fails('let l[3] = 20', 'E741:')
955 unlet l
956endfunc
957
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100958" lockvar/islocked() triggering script autoloading
959func Test_lockvar_script_autoload()
960 let old_rtp = &rtp
961 set rtp+=./sautest
962 lockvar g:footest#x
963 unlockvar g:footest#x
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +0200964 call assert_equal(-1, 'g:footest#x'->islocked())
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100965 call assert_equal(0, exists('g:footest#x'))
966 call assert_equal(1, g:footest#x)
967 let &rtp = old_rtp
968endfunc
969
970" a:000 function argument test
971func s:arg_list_test(...)
972 call assert_fails('let a:000 = [1, 2]', 'E46:')
973 call assert_fails('let a:000[0] = 9', 'E742:')
974 call assert_fails('let a:000[2] = [9, 10]', 'E742:')
975 call assert_fails('let a:000[3] = {9 : 10}', 'E742:')
976
977 " now the tests that should pass
978 let a:000[2][1] = 9
979 call extend(a:000[2], [5, 6])
980 let a:000[3][5] = 8
981 let a:000[3]['a'] = 12
982 call assert_equal([1, 2, [3, 9, 5, 6], {'a': 12, '5': 8}], a:000)
983endfunc
984
985func Test_func_arg_list()
986 call s:arg_list_test(1, 2, [3, 4], {5: 6})
987endfunc
988
989" Tests for reverse(), sort(), uniq()
990func Test_reverse_sort_uniq()
Bram Moolenaaref982572021-08-12 19:27:57 +0200991 let lines =<< trim END
992 VAR l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
993 call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l)))
994 call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l))
995 call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(reverse(l)))
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100996 call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(l))
997 call assert_equal([[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'], reverse(sort(l)))
998 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))))
999 call assert_equal(['-0', 'A11', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 4, [0, 1, 2]], uniq(sort(l)))
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001000
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001001 LET l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
1002 call assert_equal([-1, 'one', 'two', 'three', 'four', 1.0e-15, 0.22, 7, 9, 12, 18, 22, 255], sort(copy(l), 'n'))
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001003
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001004 LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
1005 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'))
1006 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'))
1007 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)))
Bram Moolenaaref982572021-08-12 19:27:57 +02001008 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001009 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +01001010
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001011 call assert_fails('call reverse({})', 'E1253:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001012 call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +01001013 call assert_fails("call sort([1, 2], function('min'), 1)", "E1206:")
Bram Moolenaar08f41572020-04-20 16:50:00 +02001014 call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001015 call assert_fails("call sort([1, 2], function('min'))", "E118:")
Bram Moolenaar2007dd42022-02-23 13:17:47 +00001016
1017 let lines =<< trim END
1018 call sort(['a', 'b'], 0)
1019 END
1020 call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
1021
1022 let lines =<< trim END
1023 call sort(['a', 'b'], 1)
1024 END
1025 call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001026endfunc
1027
rbtnn0ccb5842021-12-18 18:33:46 +00001028" reduce a list, blob or string
Bram Moolenaar85629982020-06-01 18:39:20 +02001029func Test_reduce()
Bram Moolenaaref982572021-08-12 19:27:57 +02001030 let lines =<< trim END
1031 call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1))
1032 call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1))
1033 call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
1034 call assert_equal('a x y z', ['x', 'y', 'z']->reduce(LSTART acc, val LMIDDLE acc .. ' ' .. val LEND, 'a'))
1035 call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0]))
Bram Moolenaar85629982020-06-01 18:39:20 +02001036
Bram Moolenaaref982572021-08-12 19:27:57 +02001037 VAR l = ['x', 'y', 'z']
1038 call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } }))
1039 call assert_equal(['x', 'y', 'z'], l)
Bram Moolenaar85629982020-06-01 18:39:20 +02001040
Bram Moolenaaref982572021-08-12 19:27:57 +02001041 call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND))
1042 call assert_equal('x y z', reduce(['x', 'y', 'z'], LSTART acc, val LMIDDLE acc .. ' ' .. val LEND))
1043 call assert_equal(120, range(1, 5)->reduce(LSTART acc, val LMIDDLE acc * val LEND))
1044
Bram Moolenaar52df40e2022-09-28 13:22:59 +01001045 call assert_equal(0, range(1)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1046 call assert_equal(1, range(2)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1047 call assert_equal(3, range(3)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1048 call assert_equal(6, range(4)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1049 call assert_equal(10, range(5)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1050
Bram Moolenaaref982572021-08-12 19:27:57 +02001051 call assert_equal(1, reduce(0z, LSTART acc, val LMIDDLE acc + val LEND, 1))
1052 call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE acc + val LEND, 1))
1053 call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce(LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
1054
1055 call assert_equal(0xff, reduce(0zff, LSTART acc, val LMIDDLE acc + val LEND))
1056 call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE 2 * acc + val LEND))
rbtnn0ccb5842021-12-18 18:33:46 +00001057
1058 call assert_equal('x,y,z', 'xyz'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1059 call assert_equal('', ''->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, ''))
1060 call assert_equal('あ,い,う,え,お,😊,💕', 'あいうえお😊💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1061 call assert_equal('😊,あ,い,う,え,お,💕', 'あいうえお💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '😊'))
1062 call assert_equal('ऊ,ॠ,ॡ', reduce('ऊॠॡ', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1063 call assert_equal('c,à,t', reduce('càt', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1064 call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1065 call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1066 call assert_equal(',a,b,c', reduce('abc', LSTART acc, val LMIDDLE acc .. ',' .. val LEND, test_null_string()))
zeertzjqad0c4422023-08-17 22:15:47 +02001067
1068 call assert_equal(0x7d, reduce([0x30, 0x25, 0x08, 0x61], 'or'))
1069 call assert_equal(0x7d, reduce(0z30250861, 'or'))
1070 call assert_equal('β', reduce('ββββ', 'matchstr'))
Bram Moolenaaref982572021-08-12 19:27:57 +02001071 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001072 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001073
1074 call assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {}))
1075 vim9 assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce((acc, val) => extend(acc, {[val]: 1 }), {}))
1076
Bram Moolenaar85629982020-06-01 18:39:20 +02001077 call assert_fails("call reduce([], { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value')
Bram Moolenaar52df40e2022-09-28 13:22:59 +01001078 call assert_fails("call reduce(range(0), { acc, val -> acc + val })", 'E998: Reduce of an empty List with no initial value')
Bram Moolenaar85629982020-06-01 18:39:20 +02001079 call assert_fails("call reduce(0z, { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001080 call assert_fails("call reduce(test_null_blob(), { acc, val -> acc + val })", 'E998: Reduce of an empty Blob with no initial value')
rbtnn0ccb5842021-12-18 18:33:46 +00001081 call assert_fails("call reduce('', { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
1082 call assert_fails("call reduce(test_null_string(), { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
Bram Moolenaar85629982020-06-01 18:39:20 +02001083
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001084 call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E1253:')
1085 call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E1253:')
zeertzjqad0c4422023-08-17 22:15:47 +02001086 call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E117:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001087 call assert_fails("echo reduce(0z01, { acc, val -> 2 * acc + val }, '')", 'E1210:')
Bram Moolenaarca275a02020-06-24 22:07:46 +02001088
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001089 call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1253:')
1090 call assert_fails("vim9 reduce({}, (acc, val) => (acc .. val), '')", 'E1253:')
1091 call assert_fails("vim9 reduce(0.1, (acc, val) => (acc .. val), '')", 'E1253:')
1092 call assert_fails("vim9 reduce(function('tr'), (acc, val) => (acc .. val), '')", 'E1253:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001093 call assert_fails("call reduce('', { acc, val -> acc + val }, 1)", 'E1174:')
1094 call assert_fails("call reduce('', { acc, val -> acc + val }, {})", 'E1174:')
1095 call assert_fails("call reduce('', { acc, val -> acc + val }, 0.1)", 'E1174:')
1096 call assert_fails("call reduce('', { acc, val -> acc + val }, function('tr'))", 'E1174:')
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00001097 call assert_fails("call reduce('abc', { a, v -> a10}, '')", 'E121:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001098 call assert_fails("call reduce(0z0102, { a, v -> a10}, 1)", 'E121:')
1099 call assert_fails("call reduce([1, 2], { a, v -> a10}, '')", 'E121:')
rbtnn0ccb5842021-12-18 18:33:46 +00001100
Bram Moolenaarca275a02020-06-24 22:07:46 +02001101 let g:lut = [1, 2, 3, 4]
1102 func EvilRemove()
1103 call remove(g:lut, 1)
1104 return 1
1105 endfunc
1106 call assert_fails("call reduce(g:lut, { acc, val -> EvilRemove() }, 1)", 'E742:')
1107 unlet g:lut
1108 delfunc EvilRemove
Bram Moolenaarfda20c42020-06-29 20:09:36 +02001109
1110 call assert_equal(42, reduce(test_null_list(), function('add'), 42))
1111 call assert_equal(42, reduce(test_null_blob(), function('add'), 42))
Bram Moolenaar0d90e722020-11-03 18:20:19 +01001112
1113 " should not crash
1114 call assert_fails('echo reduce([1], test_null_function())', 'E1132:')
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001115 call assert_fails('echo reduce([1], test_null_partial())', 'E1132:')
Bram Moolenaar85629982020-06-01 18:39:20 +02001116endfunc
1117
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001118" splitting a string to a List using split()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001119func Test_str_split()
Bram Moolenaaref982572021-08-12 19:27:57 +02001120 let lines =<< trim END
1121 call assert_equal(['aa', 'bb'], split(' aa bb '))
1122 call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0))
1123 call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1))
1124 call assert_equal(['', '', 'aa', '', 'bb', '', ''], split(' aa bb ', '\W', 1))
1125 call assert_equal(['aa', '', 'bb'], split(':aa::bb:', ':', 0))
1126 call assert_equal(['', 'aa', '', 'bb', ''], split(':aa::bb:', ':', 1))
1127 call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1))
1128 call assert_equal(['a', 'b', 'c'], split('abc', '\zs'))
1129 call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1))
1130 call assert_equal(['abc'], split('abc', '\\%('))
1131 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001132 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001133
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001134 call assert_fails("call split('abc', [])", 'E730:')
1135 call assert_fails("call split('abc', 'b', [])", 'E745:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001136endfunc
1137
1138" compare recursively linked list and dict
1139func Test_listdict_compare()
Bram Moolenaaref982572021-08-12 19:27:57 +02001140 let lines =<< trim END
1141 VAR l = [1, 2, 3, '4']
1142 VAR d = {'1': 1, '2': l, '3': 3}
1143 LET l[1] = d
1144 call assert_true(l == l)
1145 call assert_true(d == d)
1146 call assert_false(l != deepcopy(l))
1147 call assert_false(d != deepcopy(d))
1148 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001149 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar8b633132020-03-20 18:20:51 +01001150
1151 " comparison errors
1152 call assert_fails('echo [1, 2] =~ {}', 'E691:')
1153 call assert_fails('echo [1, 2] =~ [1, 2]', 'E692:')
1154 call assert_fails('echo {} =~ 5', 'E735:')
1155 call assert_fails('echo {} =~ {}', 'E736:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001156endfunc
1157
Yinzuo Jiang7ccd1a22024-07-04 17:20:53 +02001158func Test_recursive_listdict_compare()
1159 let l1 = [0, 1]
1160 let l1[0] = l1
1161 let l2 = [0, 1]
1162 let l2[0] = l2
1163 call assert_true(l1 == l2)
1164 let d1 = {0: 0, 1: 1}
1165 let d1[0] = d1
1166 let d2 = {0: 0, 1: 1}
1167 let d2[0] = d2
1168 call assert_true(d1 == d2)
1169endfunc
1170
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001171 " compare complex recursively linked list and dict
1172func Test_listdict_compare_complex()
Bram Moolenaaref982572021-08-12 19:27:57 +02001173 let lines =<< trim END
1174 VAR l = []
1175 call add(l, l)
1176 VAR dict4 = {"l": l}
1177 call add(dict4.l, dict4)
1178 VAR lcopy = deepcopy(l)
1179 VAR dict4copy = deepcopy(dict4)
1180 call assert_true(l == lcopy)
1181 call assert_true(dict4 == dict4copy)
1182 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001183 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001184endfunc
1185
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001186" Test for extending lists and dictionaries
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001187func Test_listdict_extend()
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001188 " Test extend() with lists
1189
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001190 " Pass the same List to extend()
Bram Moolenaaref982572021-08-12 19:27:57 +02001191 let lines =<< trim END
1192 VAR l = [1, 2, 3]
1193 call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l))
1194 call assert_equal([1, 2, 3, 1, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001195
Bram Moolenaaref982572021-08-12 19:27:57 +02001196 LET l = [1, 2, 3]
1197 call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6]))
1198 call assert_equal([1, 2, 3, 4, 5, 6], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001199
Bram Moolenaaref982572021-08-12 19:27:57 +02001200 LET l = [1, 2, 3]
1201 call extend(l, [4, 5, 6], 0)
1202 call assert_equal([4, 5, 6, 1, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001203
Bram Moolenaaref982572021-08-12 19:27:57 +02001204 LET l = [1, 2, 3]
1205 call extend(l, [4, 5, 6], 1)
1206 call assert_equal([1, 4, 5, 6, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001207
Bram Moolenaaref982572021-08-12 19:27:57 +02001208 LET l = [1, 2, 3]
1209 call extend(l, [4, 5, 6], 3)
1210 call assert_equal([1, 2, 3, 4, 5, 6], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001211
Bram Moolenaaref982572021-08-12 19:27:57 +02001212 LET l = [1, 2, 3]
1213 call extend(l, [4, 5, 6], -1)
1214 call assert_equal([1, 2, 4, 5, 6, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001215
Bram Moolenaaref982572021-08-12 19:27:57 +02001216 LET l = [1, 2, 3]
1217 call extend(l, [4, 5, 6], -3)
1218 call assert_equal([4, 5, 6, 1, 2, 3], l)
1219 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001220 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001221
1222 let l = [1, 2, 3]
1223 call assert_fails("call extend(l, [4, 5, 6], 4)", 'E684:')
1224 call assert_fails("call extend(l, [4, 5, 6], -4)", 'E684:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001225 call assert_fails("call extend(l, [4, 5, 6], 1.2)", 'E805:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001226
1227 " Test extend() with dictionaries.
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001228
1229 " Pass the same Dict to extend()
Bram Moolenaaref982572021-08-12 19:27:57 +02001230 let lines =<< trim END
1231 VAR d = {'a': {'b': 'B'}, 'x': 9}
1232 call extend(d, d)
1233 call assert_equal({'a': {'b': 'B'}, 'x': 9}, d)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001234
Bram Moolenaaref982572021-08-12 19:27:57 +02001235 LET d = {'a': 'A', 'b': 9}
1236 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c': 'C'}))
1237 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001238
Bram Moolenaaref982572021-08-12 19:27:57 +02001239 LET d = {'a': 'A', 'b': 9}
1240 call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force")
1241 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001242
Bram Moolenaaref982572021-08-12 19:27:57 +02001243 LET d = {'a': 'A', 'b': 9}
1244 call extend(d, {'b': 0, 'c': 'C'}, "keep")
1245 call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d)
1246 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001247 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001248
1249 let d = {'a': 'A', 'b': 'B'}
1250 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00001251 call assert_fails("call extend(d, {'b': 0}, [])", 'E730:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001252 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001253 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001254 call assert_equal({'a': 'A', 'b': 'B'}, d)
1255
1256 call assert_fails("call extend([1, 2], 1)", 'E712:')
1257 call assert_fails("call extend([1, 2], {})", 'E712:')
Bram Moolenaar08f41572020-04-20 16:50:00 +02001258
1259 " Extend g: dictionary with an invalid variable name
1260 call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001261
1262 " Extend a list with itself.
Bram Moolenaaref982572021-08-12 19:27:57 +02001263 let lines =<< trim END
1264 VAR l = [1, 5, 7]
1265 call extend(l, l, 0)
1266 call assert_equal([1, 5, 7, 1, 5, 7], l)
1267 LET l = [1, 5, 7]
1268 call extend(l, l, 1)
1269 call assert_equal([1, 1, 5, 7, 5, 7], l)
1270 LET l = [1, 5, 7]
1271 call extend(l, l, 2)
1272 call assert_equal([1, 5, 1, 5, 7, 7], l)
1273 LET l = [1, 5, 7]
1274 call extend(l, l, 3)
1275 call assert_equal([1, 5, 7, 1, 5, 7], l)
1276 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001277 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001278endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +01001279
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001280func Test_listdict_extendnew()
1281 " Test extendnew() with lists
1282 let l = [1, 2, 3]
1283 call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
1284 call assert_equal([1, 2, 3], l)
zeertzjq341f3872023-02-27 14:59:57 +00001285 lockvar l
1286 call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001287
zeertzjq341f3872023-02-27 14:59:57 +00001288 " Test extendnew() with dictionaries.
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001289 let d = {'a': {'b': 'B'}}
1290 call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
1291 call assert_equal({'a': {'b': 'B'}}, d)
zeertzjq341f3872023-02-27 14:59:57 +00001292 lockvar d
1293 call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001294endfunc
1295
Bram Moolenaar31b81602019-02-10 22:14:27 +01001296func s:check_scope_dict(x, fixed)
1297 func s:gen_cmd(cmd, x)
1298 return substitute(a:cmd, '\<x\ze:', a:x, 'g')
1299 endfunc
1300
1301 let cmd = s:gen_cmd('let x:foo = 1', a:x)
1302 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001303 call assert_fails(cmd, 'E461:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001304 else
1305 exe cmd
1306 exe s:gen_cmd('call assert_equal(1, x:foo)', a:x)
1307 endif
1308
1309 let cmd = s:gen_cmd('let x:["bar"] = 2', a:x)
1310 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001311 call assert_fails(cmd, 'E461:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001312 else
1313 exe cmd
1314 exe s:gen_cmd('call assert_equal(2, x:bar)', a:x)
1315 endif
1316
1317 let cmd = s:gen_cmd('call extend(x:, {"baz": 3})', a:x)
1318 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001319 call assert_fails(cmd, 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001320 else
1321 exe cmd
1322 exe s:gen_cmd('call assert_equal(3, x:baz)', a:x)
1323 endif
1324
1325 if a:fixed
1326 if a:x ==# 'a'
Bram Moolenaare2e40752020-09-04 21:18:46 +02001327 call assert_fails('unlet a:x', 'E795:')
1328 call assert_fails('call remove(a:, "x")', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001329 elseif a:x ==# 'v'
Bram Moolenaare2e40752020-09-04 21:18:46 +02001330 call assert_fails('unlet v:count', 'E795:')
1331 call assert_fails('call remove(v:, "count")', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001332 endif
1333 else
1334 exe s:gen_cmd('unlet x:foo', a:x)
1335 exe s:gen_cmd('unlet x:bar', a:x)
1336 exe s:gen_cmd('call remove(x:, "baz")', a:x)
1337 endif
1338
1339 delfunc s:gen_cmd
1340endfunc
1341
1342func Test_scope_dict()
1343 " Test for g:
1344 call s:check_scope_dict('g', v:false)
1345
1346 " Test for s:
1347 call s:check_scope_dict('s', v:false)
1348
1349 " Test for l:
1350 call s:check_scope_dict('l', v:false)
1351
1352 " Test for a:
1353 call s:check_scope_dict('a', v:true)
1354
1355 " Test for b:
1356 call s:check_scope_dict('b', v:false)
1357
1358 " Test for w:
1359 call s:check_scope_dict('w', v:false)
1360
1361 " Test for t:
1362 call s:check_scope_dict('t', v:false)
1363
1364 " Test for v:
1365 call s:check_scope_dict('v', v:true)
1366endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001367
1368" Test for deep nesting of lists (> 100)
1369func Test_deep_nested_list()
1370 let deep_list = []
1371 let l = deep_list
1372 for i in range(102)
1373 let newlist = []
1374 call add(l, newlist)
1375 let l = newlist
1376 endfor
1377 call add(l, 102)
1378
1379 call assert_fails('let m = deepcopy(deep_list)', 'E698:')
1380 call assert_fails('lockvar 110 deep_list', 'E743:')
1381 call assert_fails('unlockvar 110 deep_list', 'E743:')
1382 call assert_fails('let x = execute("echo deep_list")', 'E724:')
1383 call test_garbagecollect_now()
1384 unlet deep_list
1385endfunc
1386
1387" Test for deep nesting of dicts (> 100)
1388func Test_deep_nested_dict()
1389 let deep_dict = {}
1390 let d = deep_dict
1391 for i in range(102)
1392 let newdict = {}
1393 let d.k = newdict
1394 let d = newdict
1395 endfor
1396 let d.k = 'v'
1397
1398 call assert_fails('let m = deepcopy(deep_dict)', 'E698:')
1399 call assert_fails('lockvar 110 deep_dict', 'E743:')
1400 call assert_fails('unlockvar 110 deep_dict', 'E743:')
1401 call assert_fails('let x = execute("echo deep_dict")', 'E724:')
1402 call test_garbagecollect_now()
1403 unlet deep_dict
1404endfunc
1405
Bram Moolenaar8b633132020-03-20 18:20:51 +01001406" List and dict indexing tests
1407func Test_listdict_index()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001408 call v9.CheckLegacyAndVim9Failure(['echo function("min")[0]'], 'E695:')
1409 call v9.CheckLegacyAndVim9Failure(['echo v:true[0]'], 'E909:')
1410 call v9.CheckLegacyAndVim9Failure(['echo v:null[0]'], 'E909:')
1411 call v9.CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d.'], ['E15:', 'E1127:', 'E15:'])
1412 call v9.CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d[1 : 2]'], 'E719:')
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00001413
Bram Moolenaar8b633132020-03-20 18:20:51 +01001414 call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001415 call v9.CheckDefAndScriptFailure(['var v = [4, 6][() => 1]'], ['E1012', 'E703:'])
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00001416
Bram Moolenaar62aec932022-01-29 21:45:34 +00001417 call v9.CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : []]'], ['E730:', 'E1012:', 'E730:'])
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001418
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001419 call assert_fails("let v = range(5)[2:{-> 2}(]", ['E15:', 'E116:'])
Bram Moolenaar62aec932022-01-29 21:45:34 +00001420 call v9.CheckDefAndScriptFailure(['var v = range(5)[2 : () => 2(]'], 'E15:')
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001421
Bram Moolenaar62aec932022-01-29 21:45:34 +00001422 call v9.CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : 3'], ['E111:', 'E1097:', 'E111:'])
1423 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, 10)'], 'E684:')
1424 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, -10)'], 'E684:')
1425 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, [])'], ['E745:', 'E1013:', 'E1210:'])
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001426
Bram Moolenaar62aec932022-01-29 21:45:34 +00001427 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[i] = 3'], ['E121:', 'E1001:', 'E121:'])
1428 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[1.1] = 4'], ['E805:', 'E1012:', 'E805:'])
1429 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: i] = [4, 5]'], ['E121:', 'E1001:', 'E121:'])
1430 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: 3.2] = [4, 5]'], ['E805:', 'E1012:', 'E805:'])
Bram Moolenaar097c5372023-05-24 21:02:24 +01001431 call v9.CheckLegacyAndVim9Failure(['VAR t = test_unknown()', 'echo t[0]'], ['E340:', 'E909:', 'E340:', 'E685:'])
Bram Moolenaar08f41572020-04-20 16:50:00 +02001432endfunc
1433
1434" Test for a null list
1435func Test_null_list()
Bram Moolenaaref982572021-08-12 19:27:57 +02001436 let lines =<< trim END
1437 VAR l = test_null_list()
1438 call assert_equal('', join(test_null_list()))
1439 call assert_equal('', join(l))
1440 call assert_equal(0, len(l))
1441 call assert_equal(1, empty(l))
1442 call assert_equal([], split(test_null_string()))
1443 call assert_equal([], l[ : 2])
1444 call assert_true([] == l)
1445 call assert_equal('[]', string(l))
1446 call assert_equal([], sort(test_null_list()))
1447 call assert_equal([], sort(l))
1448 call assert_equal([], uniq(test_null_list()))
1449 call assert_equal([], uniq(l))
1450 VAR k = [] + l
1451 call assert_equal([], k)
1452 LET k = l + []
1453 call assert_equal([], k)
1454 call assert_equal(0, len(copy(l)))
1455 call assert_equal(0, count(l, 5))
1456 call assert_equal([], deepcopy(l))
1457 call assert_equal(5, get(l, 2, 5))
1458 call assert_equal(-1, index(l, 2, 5))
1459 call assert_equal(0, min(l))
1460 call assert_equal(0, max(l))
1461 call assert_equal(0, remove(test_null_list(), 0, 2))
1462 call assert_equal([], repeat(l, 2))
1463 call assert_equal([], reverse(test_null_list()))
1464 call assert_equal([], reverse(l))
1465 call assert_equal([], sort(test_null_list()))
1466 call assert_equal([], sort(l))
1467 call assert_equal('[]', string(l))
1468 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001469 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001470
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001471 let l = test_null_list()
Bram Moolenaar64ffa9b2020-11-04 12:23:06 +01001472 call assert_equal([], extend(l, l, 0))
Bram Moolenaaref982572021-08-12 19:27:57 +02001473 call assert_equal(0, insert(test_null_list(), 2, -1))
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001474 call assert_fails('let s = join([1, 2], [])', 'E1174:')
Bram Moolenaaref982572021-08-12 19:27:57 +02001475 call assert_fails('call remove(l, 0, 2)', 'E684:')
1476 call assert_fails('call insert(l, 2, -1)', 'E684:')
1477 call assert_fails('call extend(test_null_list(), test_null_list())', 'E1134:')
1478
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001479 lockvar l
1480 call assert_equal(1, islocked('l'))
1481 unlockvar l
Bram Moolenaar08f41572020-04-20 16:50:00 +02001482endfunc
1483
1484" Test for a null dict
1485func Test_null_dict()
Bram Moolenaaref982572021-08-12 19:27:57 +02001486 let lines =<< trim END
1487 call assert_equal(test_null_dict(), test_null_dict())
1488 VAR d = test_null_dict()
1489 call assert_equal({}, d)
1490 call assert_equal(0, len(d))
1491 call assert_equal(1, empty(d))
1492 call assert_equal([], items(test_null_dict()))
1493 call assert_equal([], items(d))
1494 call assert_equal([], keys(test_null_dict()))
1495 call assert_equal([], keys(d))
1496 call assert_equal([], values(test_null_dict()))
1497 call assert_equal([], values(d))
1498 call assert_false(has_key(d, 'k'))
1499 call assert_equal('{}', string(d))
1500 call assert_equal({}, {})
1501 call assert_equal(0, len(copy(d)))
1502 call assert_equal(0, count(d, 'k'))
1503 call assert_equal({}, deepcopy(d))
1504 call assert_equal(20, get(d, 'k', 20))
1505 call assert_equal(0, min(d))
1506 call assert_equal(0, max(d))
1507 call assert_equal(0, remove(test_null_dict(), 'k'))
1508 call assert_equal('{}', string(d))
1509 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001510 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001511
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001512 let d = test_null_dict()
Bram Moolenaar64ffa9b2020-11-04 12:23:06 +01001513 call assert_equal({}, extend(d, d, 'keep'))
Bram Moolenaaref982572021-08-12 19:27:57 +02001514 call assert_fails("call remove(d, 'k')", 'E716:')
1515 call assert_fails('let x = d[10]', 'E716:')
1516 call assert_fails('call extend(test_null_dict(), test_null_dict())', 'E1133:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001517 lockvar d
1518 call assert_equal(1, islocked('d'))
1519 unlockvar d
Bram Moolenaar8b633132020-03-20 18:20:51 +01001520endfunc
1521
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001522" Test for the indexof() function
1523func Test_indexof()
1524 let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
1525 call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
1526 call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
1527 call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
1528 call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
1529 call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
1530
1531 let l = [#{n: 10}, #{n: 10}, #{n: 20}]
1532 call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
1533 call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
1534 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
1535 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
1536 call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
1537
Yegappan Lakshmanan63acae12022-08-14 12:07:11 +01001538 let s = ["a", "b", "c"]
1539 call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
1540 call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
1541 call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
1542
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001543 call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
Yegappan Lakshmanan63acae12022-08-14 12:07:11 +01001544 call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001545 call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
1546 call assert_equal(-1, indexof(l, test_null_string()))
1547 call assert_equal(-1, indexof(l, test_null_function()))
Yegappan Lakshmananfe424d12024-05-17 18:20:43 +02001548 call assert_equal(-1, indexof(l, ""))
1549 call assert_fails('let i = indexof(l, " ")', 'E15:')
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001550
1551 " failure cases
1552 call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
1553 call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
1554 call assert_fails('let i = indexof(l, {})', 'E1256:')
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +01001555 call assert_fails('let i = indexof({}, "v:val == 2")', 'E1528:')
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001556 call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
1557
1558 func TestIdx(k, v)
1559 return a:v.n == 20
1560 endfunc
1561 call assert_equal(2, indexof(l, function("TestIdx")))
1562 delfunc TestIdx
1563 func TestIdx(k, v)
1564 return {}
1565 endfunc
1566 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
1567 delfunc TestIdx
1568 func TestIdx(k, v)
1569 throw "IdxError"
1570 endfunc
1571 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
1572 delfunc TestIdx
1573endfunc
1574
Christian Brabandt29269a72024-04-16 22:44:31 +02001575func Test_extendnew_leak()
1576 " This used to leak memory
1577 for i in range(100) | silent! call extendnew([], [], []) | endfor
1578 for i in range(100) | silent! call extendnew({}, {}, {}) | endfor
1579endfunc
1580
Yegappan Lakshmanan88bbdb02024-06-23 10:00:04 +02001581" Test for comparing deeply nested List/Dict values
1582func Test_deep_nested_listdict_compare()
1583 let lines =<< trim END
1584 def GetNestedList(sz: number): list<any>
1585 var l: list<any> = []
1586 var x: list<any> = l
1587 for i in range(sz)
1588 var y: list<any> = [1]
1589 add(x, y)
1590 x = y
1591 endfor
1592 return l
1593 enddef
1594
1595 VAR l1 = GetNestedList(1000)
1596 VAR l2 = GetNestedList(999)
1597 call assert_false(l1 == l2)
1598
1599 #" after 1000 nested items, the lists are considered to be equal
1600 VAR l3 = GetNestedList(1001)
1601 VAR l4 = GetNestedList(1002)
1602 call assert_true(l3 == l4)
1603 END
1604 call v9.CheckLegacyAndVim9Success(lines)
1605
1606 let lines =<< trim END
1607 def GetNestedDict(sz: number): dict<any>
1608 var d: dict<any> = {}
1609 var x: dict<any> = d
1610 for i in range(sz)
1611 var y: dict<any> = {}
1612 x['a'] = y
1613 x = y
1614 endfor
1615 return d
1616 enddef
1617
1618 VAR d1 = GetNestedDict(1000)
1619 VAR d2 = GetNestedDict(999)
1620 call assert_false(d1 == d2)
1621
1622 #" after 1000 nested items, the Dicts are considered to be equal
1623 VAR d3 = GetNestedDict(1001)
1624 VAR d4 = GetNestedDict(1002)
1625 call assert_true(d3 == d4)
1626 END
1627 call v9.CheckLegacyAndVim9Success(lines)
1628endfunc
1629
Ernie Raelc8e158b2024-07-09 18:39:52 +02001630" Test for using id()
1631def Test_id_with_dict()
zeertzjq8feed3a2024-09-29 10:37:47 +02001632 # demonstrate a way that "id(item)" differs from "string(item)"
Ernie Raelc8e158b2024-07-09 18:39:52 +02001633 var d1 = {one: 1}
1634 var d2 = {one: 1}
1635 var d3 = {one: 1}
1636 var idDict: dict<any>
1637 idDict[id(d1)] = d1
1638 idDict[id(d2)] = d2
1639 idDict[id(d3)] = d3
1640 assert_equal(3, idDict->len())
1641
1642 var stringDict: dict<any>
1643 stringDict[string(d1)] = d1
1644 stringDict[string(d2)] = d2
1645 stringDict[string(d3)] = d3
1646 assert_equal(1, stringDict->len())
1647
1648 assert_equal('', id(3))
1649
1650 assert_equal('', id(null))
1651 assert_equal('', id(null_blob))
1652 assert_equal('', id(null_dict))
1653 assert_equal('', id(null_function))
1654 assert_equal('', id(null_list))
1655 assert_equal('', id(null_partial))
1656 assert_equal('', id(null_string))
1657 assert_equal('', id(null_channel))
1658 assert_equal('', id(null_job))
1659enddef
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001660" vim: shiftwidth=2 sts=2 expandtab