blob: 3ff20973a23c7353c7a9e7067a869af26cc8ac4d [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)
Bram Moolenaarfb094e12017-11-05 20:59:28 +010063endfunc
64
65" List identity
66func Test_list_identity()
Bram Moolenaar63cb6562021-07-20 22:21:59 +020067 let lines =<< trim END
68 VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
69 VAR ll = l
70 VAR lx = copy(l)
71 call assert_true(l == ll)
72 call assert_false(l isnot ll)
73 call assert_true(l is ll)
74 call assert_true(l == lx)
75 call assert_false(l is lx)
76 call assert_true(l isnot lx)
77 END
Bram Moolenaar62aec932022-01-29 21:45:34 +000078 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +010079endfunc
80
81" removing items with :unlet
82func Test_list_unlet()
Bram Moolenaar63cb6562021-07-20 22:21:59 +020083 let lines =<< trim END
84 VAR l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
85 unlet l[2]
86 call assert_equal([1, 'as''d', {'a': 1}], l)
87 LET l = range(8)
88 unlet l[: 3]
89 unlet l[1 :]
90 call assert_equal([4], l)
Bram Moolenaarfb094e12017-11-05 20:59:28 +010091
Bram Moolenaar63cb6562021-07-20 22:21:59 +020092 #" removing items out of range: silently skip items that don't exist
93 LET l = [0, 1, 2, 3]
94 unlet l[2 : 2]
95 call assert_equal([0, 1, 3], l)
96 LET l = [0, 1, 2, 3]
97 unlet l[2 : 3]
98 call assert_equal([0, 1], l)
99 LET l = [0, 1, 2, 3]
100 unlet l[2 : 4]
101 call assert_equal([0, 1], l)
102 LET l = [0, 1, 2, 3]
103 unlet l[2 : 5]
104 call assert_equal([0, 1], l)
105 LET l = [0, 1, 2, 3]
106 unlet l[-2 : 2]
107 call assert_equal([0, 1, 3], l)
108 LET l = [0, 1, 2, 3]
109 unlet l[-3 : 2]
110 call assert_equal([0, 3], l)
111 LET l = [0, 1, 2, 3]
112 unlet l[-4 : 2]
113 call assert_equal([3], l)
114 LET l = [0, 1, 2, 3]
115 unlet l[-5 : 2]
116 call assert_equal([3], l)
117 LET l = [0, 1, 2, 3]
118 unlet l[-6 : 2]
119 call assert_equal([3], l)
120 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000121 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200122
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100123 let l = [0, 1, 2, 3]
124 unlet l[2:2]
125 call assert_equal([0, 1, 3], l)
126 let l = [0, 1, 2, 3]
127 unlet l[2:3]
128 call assert_equal([0, 1], l)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200129
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200130 let lines =<< trim END
131 VAR l = [0, 1, 2, 3]
132 unlet l[2 : 1]
133 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000134 call v9.CheckLegacyAndVim9Failure(lines, 'E684:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200135
136 let lines =<< trim END
137 VAR l = [0, 1, 2, 3]
138 unlet l[-1 : 2]
139 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000140 call v9.CheckLegacyAndVim9Failure(lines, 'E684:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100141endfunc
142
143" assignment to a list
144func Test_list_assign()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200145 let lines =<< trim END
146 VAR l = [0, 1, 2, 3]
147 VAR va = 0
148 VAR vb = 0
149 LET [va, vb] = l[2 : 3]
150 call assert_equal([2, 3], [va, vb])
151 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000152 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200153
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200154 let lines =<< trim END
155 let l = [0, 1, 2, 3]
156 let [va, vb] = l
157 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000158 call v9.CheckScriptFailure(lines, 'E687:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200159 let lines =<< trim END
160 var l = [0, 1, 2, 3]
161 var va = 0
162 var vb = 0
163 [va, vb] = l
164 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000165 call v9.CheckScriptFailure(['vim9script'] + lines, 'E687:')
166 call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 4')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200167
168 let lines =<< trim END
169 let l = [0, 1, 2, 3]
170 let [va, vb] = l[1:1]
171 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000172 call v9.CheckScriptFailure(lines, 'E688:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200173 let lines =<< trim END
174 var l = [0, 1, 2, 3]
175 var va = 0
176 var vb = 0
177 [va, vb] = l[1 : 1]
178 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000179 call v9.CheckScriptFailure(['vim9script'] + lines, 'E688:')
180 call v9.CheckDefExecFailure(lines, 'E1093: Expected 2 items but got 1')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100181endfunc
182
183" test for range assign
184func Test_list_range_assign()
Bram Moolenaar4f0884d2021-08-11 21:49:23 +0200185 let lines =<< trim END
186 VAR l = [0]
187 LET l[:] = [1, 2]
188 call assert_equal([1, 2], l)
189 LET l[-4 : -1] = [5, 6]
190 call assert_equal([5, 6], l)
191 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000192 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar89071cb2021-08-13 18:20:09 +0200193
194 let lines =<< trim END
195 var l = [7]
196 l[:] = ['text']
197 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000198 call v9.CheckDefAndScriptFailure(lines, 'E1012:', 2)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100199endfunc
200
Bram Moolenaar976f8592022-08-30 14:34:52 +0100201func Test_list_items()
202 let r = []
203 let l = ['a', 'b', 'c']
204 for [idx, val] in items(l)
205 call extend(r, [[idx, val]])
206 endfor
207 call assert_equal([[0, 'a'], [1, 'b'], [2, 'c']], r)
208
Bram Moolenaar3e518a82022-08-30 17:45:33 +0100209 call assert_fails('call items(3)', 'E1225:')
210endfunc
211
212func Test_string_items()
213 let r = []
214 let s = 'ábツ'
215 for [idx, val] in items(s)
216 call extend(r, [[idx, val]])
217 endfor
218 call assert_equal([[0, 'á'], [1, 'b'], [2, 'ツ']], r)
Bram Moolenaar976f8592022-08-30 14:34:52 +0100219endfunc
220
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200221" Test removing items in list
222func Test_list_func_remove()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200223 let lines =<< trim END
224 #" Test removing 1 element
225 VAR l = [1, 2, 3, 4]
226 call assert_equal(1, remove(l, 0))
227 call assert_equal([2, 3, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200228
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200229 LET l = [1, 2, 3, 4]
230 call assert_equal(2, remove(l, 1))
231 call assert_equal([1, 3, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200232
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200233 LET l = [1, 2, 3, 4]
234 call assert_equal(4, remove(l, -1))
235 call assert_equal([1, 2, 3], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200236
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200237 #" Test removing range of element(s)
238 LET l = [1, 2, 3, 4]
239 call assert_equal([3], remove(l, 2, 2))
240 call assert_equal([1, 2, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200241
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200242 LET l = [1, 2, 3, 4]
243 call assert_equal([2, 3], remove(l, 1, 2))
244 call assert_equal([1, 4], l)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200245
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200246 LET l = [1, 2, 3, 4]
247 call assert_equal([2, 3], remove(l, -3, -2))
248 call assert_equal([1, 4], l)
249 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000250 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200251
252 " Test invalid cases
253 let l = [1, 2, 3, 4]
254 call assert_fails("call remove(l, 5)", 'E684:')
255 call assert_fails("call remove(l, 1, 5)", 'E684:')
256 call assert_fails("call remove(l, 3, 2)", 'E16:')
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +0100257 call assert_fails("call remove(1, 0)", 'E896:')
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200258 call assert_fails("call remove(l, l)", 'E745:')
259endfunc
260
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200261" List add() function
262func Test_list_add()
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200263 let lines =<< trim END
264 VAR l = []
265 call add(l, 1)
266 call add(l, [2, 3])
267 call add(l, [])
268 call add(l, test_null_list())
269 call add(l, {'k': 3})
270 call add(l, {})
271 call add(l, test_null_dict())
272 call assert_equal([1, [2, 3], [], [], {'k': 3}, {}, {}], l)
273 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000274 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200275
276 " weird legacy behavior
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200277 call assert_equal(1, add(test_null_list(), 4))
278endfunc
279
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100280" Tests for Dictionary type
281
282func Test_dict()
283 " Creating Dictionary directly with different types
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200284 let lines =<< trim END
285 VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}, }
286 call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
287 call assert_equal('asd', d.1)
288 call assert_equal(['-1', '1', 'b'], sort(keys(d)))
289 call assert_equal(['asd', [1, 2, function('strlen')], {'a': 1}], values(d))
290 call extend(d, {3: 33, 1: 99})
291 call extend(d, {'b': 'bbb', 'c': 'ccc'}, "keep")
292 call assert_equal({'c': 'ccc', '1': 99, 'b': [1, 2, function('strlen')], '3': 33, '-1': {'a': 1}}, d)
293 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000294 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200295
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100296 let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
297 call assert_equal("{'1': 'asd', 'b': [1, 2, function('strlen')], '-1': {'a': 1}}", string(d))
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200298
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100299 let v = []
300 for [key, val] in items(d)
301 call extend(v, [key, val])
302 unlet key val
303 endfor
304 call assert_equal(['1','asd','b',[1, 2, function('strlen')],'-1',{'a': 1}], v)
305
Bram Moolenaar63cb6562021-07-20 22:21:59 +0200306 call extend(d, {3: 33, 1: 99})
Bram Moolenaare2e40752020-09-04 21:18:46 +0200307 call assert_fails("call extend(d, {3:333,4:444}, 'error')", 'E737:')
Bram Moolenaar08f41572020-04-20 16:50:00 +0200308
309 " duplicate key
310 call assert_fails("let d = {'k' : 10, 'k' : 20}", 'E721:')
311 " missing comma
312 call assert_fails("let d = {'k' : 10 'k' : 20}", 'E722:')
313 " missing curly brace
314 call assert_fails("let d = {'k' : 10,", 'E723:')
315 " invalid key
316 call assert_fails('let d = #{++ : 10}', 'E15:')
317 " wrong type for key
318 call assert_fails('let d={[] : 10}', 'E730:')
319 " undefined variable as value
320 call assert_fails("let d={'k' : i}", 'E121:')
Bram Moolenaar98cb90e2021-11-30 11:56:22 +0000321
322 " allow key starting with number at the start, not a curly expression
323 call assert_equal({'1foo': 77}, #{1foo: 77})
ii147c7e1e92022-09-07 19:40:17 +0100324
325 " #{expr} is not a curly expression
326 let x = 'x'
327 call assert_equal(#{g: x}, #{g:x})
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100328endfunc
329
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200330" This was allowed in legacy Vim script
331let s:dict_with_spaces = {'one' : 1 , 'two' : 2 , 'three' : 3}
332let s:dict_with_spaces_lit = #{one : 1 , two : 2 , three : 3}
333
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100334" Dictionary identity
335func Test_dict_identity()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200336 let lines =<< trim END
337 VAR d = {'1': 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1}, }
338 VAR dd = d
339 VAR dx = copy(d)
340 call assert_true(d == dd)
341 call assert_false(d isnot dd)
342 call assert_true(d is dd)
343 call assert_true(d == dx)
344 call assert_false(d is dx)
345 call assert_true(d isnot dx)
346 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000347 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100348endfunc
349
350" removing items with :unlet
351func Test_dict_unlet()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200352 let lines =<< trim END
353 VAR d = {'b': 'bbb', '1': 99, '3': 33, '-1': {'a': 1}}
354 unlet d.b
355 unlet d[-1]
356 call assert_equal({'1': 99, '3': 33}, d)
357 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000358 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100359endfunc
360
361" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
362func Test_dict_big()
363 let d = {}
364 for i in range(1500)
365 let d[i] = 3000 - i
366 endfor
367 call assert_equal([3000, 2900, 2001, 1600, 1501], [d[0], d[100], d[999], d[1400], d[1499]])
368 let str = ''
369 try
370 let n = d[1500]
371 catch
Bram Moolenaar6d967122020-10-30 19:06:18 +0100372 let str = substitute(v:exception, '\v(.{14}).*( "\d{4}").*', '\1\2', '')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100373 endtry
Bram Moolenaar6d967122020-10-30 19:06:18 +0100374 call assert_equal('Vim(let):E716: "1500"', str)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100375
376 " lookup each items
377 for i in range(1500)
378 call assert_equal(3000 - i, d[i])
379 endfor
380 let i += 1
381
382 " delete even items
383 while i >= 2
384 let i -= 2
385 unlet d[i]
386 endwhile
387 call assert_equal('NONE', get(d, 1500 - 100, 'NONE'))
388 call assert_equal(2999, d[1])
389
390 " delete odd items, checking value, one intentionally wrong
391 let d[33] = 999
392 let i = 1
393 while i < 1500
394 if i != 33
395 call assert_equal(3000 - i, d[i])
396 else
397 call assert_equal(999, d[i])
398 endif
399 unlet d[i]
400 let i += 2
401 endwhile
402 call assert_equal({}, d)
403 unlet d
404endfunc
405
406" Dictionary function
407func Test_dict_func()
408 let d = {}
409 func d.func(a) dict
410 return a:a . len(self.data)
411 endfunc
412 let d.data = [1,2,3]
413 call assert_equal('len: 3', d.func('len: '))
414 let x = d.func('again: ')
415 call assert_equal('again: 3', x)
416 let Fn = d.func
417 call assert_equal('xxx3', Fn('xxx'))
418endfunc
419
Bram Moolenaarb13ab992020-07-27 21:43:28 +0200420func Test_dict_assign()
421 let d = {}
422 let d.1 = 1
423 let d._ = 2
424 call assert_equal({'1': 1, '_': 2}, d)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +0200425
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200426 let lines =<< trim END
427 VAR d = {}
428 LET d.a = 1
429 LET d._ = 2
430 call assert_equal({'a': 1, '_': 2}, d)
431 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000432 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200433
434 let lines =<< trim END
435 let n = 0
436 let n.key = 3
437 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000438 call v9.CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200439 let lines =<< trim END
440 vim9script
441 var n = 0
442 n.key = 3
443 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000444 call v9.CheckScriptFailure(lines, 'E1203: Dot can only be used on a dictionary: n.key = 3')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200445 let lines =<< trim END
446 var n = 0
447 n.key = 3
448 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000449 call v9.CheckDefFailure(lines, 'E1141:')
Bram Moolenaarb13ab992020-07-27 21:43:28 +0200450endfunc
451
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100452" Function in script-local List or Dict
453func Test_script_local_dict_func()
454 let g:dict = {}
455 function g:dict.func() dict
456 return 'g:dict.func' . self.foo[1] . self.foo[0]('asdf')
457 endfunc
458 let g:dict.foo = ['-', 2, 3]
459 call insert(g:dict.foo, function('strlen'))
460 call assert_equal('g:dict.func-4', g:dict.func())
461 unlet g:dict
462endfunc
463
Bram Moolenaar08f41572020-04-20 16:50:00 +0200464" Test removing items in a dictionary
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200465func Test_dict_func_remove()
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200466 let lines =<< trim END
467 VAR d = {1: 'a', 2: 'b', 3: 'c'}
468 call assert_equal('b', remove(d, 2))
469 call assert_equal({1: 'a', 3: 'c'}, d)
470 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000471 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200472
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200473 let lines =<< trim END
474 VAR d = {1: 'a', 3: 'c'}
475 call remove(d, 1, 2)
476 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000477 call v9.CheckLegacyAndVim9Failure(lines, 'E118:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200478
479 let lines =<< trim END
480 VAR d = {1: 'a', 3: 'c'}
481 call remove(d, 'a')
482 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000483 call v9.CheckLegacyAndVim9Failure(lines, 'E716:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200484
485 let lines =<< trim END
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000486 let d = {'a-b': 55}
487 echo d.a-b
488 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000489 call v9.CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000490
491 let lines =<< trim END
492 vim9script
493 var d = {'a-b': 55}
494 echo d.a-b
495 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000496 call v9.CheckScriptFailure(lines, 'E716: Key not present in Dictionary: "a"')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000497
498 let lines =<< trim END
499 var d = {'a-b': 55}
500 echo d.a-b
501 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000502 call v9.CheckDefFailure(lines, 'E1004: White space required before and after ''-''')
Bram Moolenaar5c1ec432021-11-29 13:44:55 +0000503
504 let lines =<< trim END
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200505 let d = {1: 'a', 3: 'c'}
506 call remove(d, [])
507 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000508 call v9.CheckScriptFailure(lines, 'E730:')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200509 let lines =<< trim END
510 vim9script
511 var d = {1: 'a', 3: 'c'}
512 call remove(d, [])
513 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000514 call v9.CheckScriptFailure(lines, 'E1220: String or Number required for argument 2')
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200515 let lines =<< trim END
516 var d = {1: 'a', 3: 'c'}
517 call remove(d, [])
518 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000519 call v9.CheckDefExecFailure(lines, 'E1013: Argument 2: type mismatch, expected string but got list<unknown>')
Bram Moolenaar2bfddfc2018-09-30 17:16:25 +0200520endfunc
521
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100522" Nasty: remove func from Dict that's being called (works)
523func Test_dict_func_remove_in_use()
524 let d = {1:1}
525 func d.func(a)
526 return "a:" . a:a
527 endfunc
528 let expected = 'a:' . string(get(d, 'func'))
529 call assert_equal(expected, d.func(string(remove(d, 'func'))))
Bram Moolenaar3e9c0b92021-08-12 10:39:10 +0200530
531 " similar, in a way it also works in Vim9
532 let lines =<< trim END
533 VAR d = {1: 1, 2: 'x'}
534 func GetArg(a)
535 return "a:" .. a:a
536 endfunc
537 LET d.func = function('GetArg')
538 VAR expected = 'a:' .. string(get(d, 'func'))
539 call assert_equal(expected, d.func(string(remove(d, 'func'))))
540 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000541 call v9.CheckTransLegacySuccess(lines)
542 call v9.CheckTransVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100543endfunc
544
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200545func Test_dict_literal_keys()
Bram Moolenaar4c6d9042019-07-16 22:04:02 +0200546 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 +0200547
Bram Moolenaar5dd839c2021-07-22 18:48:53 +0200548 " why *{} cannot be used for a literal dictionary
Bram Moolenaarb8be54d2019-07-14 18:22:59 +0200549 let blue = 'blue'
550 call assert_equal('6', trim(execute('echo 2 *{blue: 3}.blue')))
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200551endfunc
552
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100553" Nasty: deepcopy() dict that refers to itself (fails when noref used)
554func Test_dict_deepcopy()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200555 let lines =<< trim END
556 VAR d = {1: 1, 2: '2'}
557 VAR l = [4, d, 6]
558 LET d[3] = l
559 VAR dc = deepcopy(d)
560 call deepcopy(d, 1)
561 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000562 call v9.CheckLegacyAndVim9Failure(lines, 'E698:')
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200563
564 let lines =<< trim END
565 VAR d = {1: 1, 2: '2'}
566 VAR l = [4, d, 6]
567 LET d[3] = l
568 VAR l2 = [0, l, l, 3]
569 LET l[1] = l2
570 VAR l3 = deepcopy(l2)
571 call assert_true(l3[1] is l3[2])
572 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000573 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200574
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +0100575 call assert_fails("call deepcopy([1, 2], 2)", 'E1212:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100576endfunc
577
578" Locked variables
579func Test_list_locked_var()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200580 " Not tested with :def function, local vars cannot be locked.
581 let lines =<< trim END
582 VAR expected = [
583 \ [['1000-000', 'ppppppF'],
584 \ ['0000-000', 'ppppppp'],
585 \ ['0000-000', 'ppppppp']],
586 \ [['1000-000', 'ppppppF'],
587 \ ['0000-000', 'ppppppp'],
588 \ ['0000-000', 'ppppppp']],
589 \ [['1100-100', 'ppFppFF'],
590 \ ['0000-000', 'ppppppp'],
591 \ ['0000-000', 'ppppppp']],
592 \ [['1110-110', 'pFFpFFF'],
593 \ ['0010-010', 'pFppFpp'],
594 \ ['0000-000', 'ppppppp']],
595 \ [['1111-111', 'FFFFFFF'],
596 \ ['0011-011', 'FFpFFpp'],
597 \ ['0000-000', 'ppppppp']]
598 \ ]
599 for depth in range(5)
600 for u in range(3)
601 VAR l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
602 exe "lockvar " .. depth .. " l"
603 if u == 1
604 exe "unlockvar l"
605 elseif u == 2
606 exe "unlockvar " .. depth .. " l"
607 endif
608 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]")
609 call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
610 LET ps = ''
611 try
612 LET l[1][1][0] = 99
613 LET ps ..= 'p'
614 catch
615 LET ps ..= 'F'
616 endtry
617 try
618 LET l[1][1] = [99]
619 LET ps ..= 'p'
620 catch
621 LET ps ..= 'F'
622 endtry
623 try
624 LET l[1] = [99]
625 LET ps ..= 'p'
626 catch
627 LET ps ..= 'F'
628 endtry
629 try
630 LET l[2]['6'][7] = 99
631 LET ps ..= 'p'
632 catch
633 LET ps ..= 'F'
634 endtry
635 try
636 LET l[2][6] = {99: 99}
637 LET ps ..= 'p'
638 catch
639 LET ps ..= 'F'
640 endtry
641 try
642 LET l[2] = {99: 99}
643 LET ps ..= 'p'
644 catch
645 LET ps ..= 'F'
646 endtry
647 try
648 LET l = [99]
649 LET ps ..= 'p'
650 catch
651 LET ps ..= 'F'
652 endtry
653 call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
654 unlock! l
655 endfor
656 endfor
657 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000658 call v9.CheckTransLegacySuccess(lines)
659 call v9.CheckTransVim9Success(lines)
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200660
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100661 call assert_fails("let x=islocked('a b')", 'E488:')
662 let mylist = [1, 2, 3]
663 call assert_fails("let x = islocked('mylist[1:2]')", 'E786:')
664 let mydict = {'k' : 'v'}
665 call assert_fails("let x = islocked('mydict.a')", 'E716:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100666endfunc
667
668" Unletting locked variables
669func Test_list_locked_var_unlet()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200670 " Not tested with Vim9: script and local variables cannot be unlocked
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100671 let expected = [
Bram Moolenaara187c432020-09-16 21:08:28 +0200672 \ [['1000-000', 'ppppppp'],
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100673 \ ['0000-000', 'ppppppp'],
674 \ ['0000-000', 'ppppppp']],
675 \ [['1000-000', 'ppFppFp'],
676 \ ['0000-000', 'ppppppp'],
677 \ ['0000-000', 'ppppppp']],
678 \ [['1100-100', 'pFFpFFp'],
679 \ ['0000-000', 'ppppppp'],
680 \ ['0000-000', 'ppppppp']],
681 \ [['1110-110', 'FFFFFFp'],
682 \ ['0010-010', 'FppFppp'],
683 \ ['0000-000', 'ppppppp']],
684 \ [['1111-111', 'FFFFFFp'],
685 \ ['0011-011', 'FppFppp'],
686 \ ['0000-000', 'ppppppp']]
687 \ ]
688
689 for depth in range(5)
690 for u in range(3)
691 unlet! l
692 let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
693 exe "lockvar " . depth . " l"
694 if u == 1
695 exe "unlockvar l"
696 elseif u == 2
697 exe "unlockvar " . depth . " l"
698 endif
699 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 +0200700 call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100701 let ps = ''
702 try
703 unlet l[2]['6'][7]
704 let ps .= 'p'
705 catch
706 let ps .= 'F'
707 endtry
708 try
709 unlet l[2][6]
710 let ps .= 'p'
711 catch
712 let ps .= 'F'
713 endtry
714 try
715 unlet l[2]
716 let ps .= 'p'
717 catch
718 let ps .= 'F'
719 endtry
720 try
721 unlet l[1][1][0]
722 let ps .= 'p'
723 catch
724 let ps .= 'F'
725 endtry
726 try
727 unlet l[1][1]
728 let ps .= 'p'
729 catch
730 let ps .= 'F'
731 endtry
732 try
733 unlet l[1]
734 let ps .= 'p'
735 catch
736 let ps .= 'F'
737 endtry
738 try
739 unlet l
740 let ps .= 'p'
741 catch
742 let ps .= 'F'
743 endtry
744 call assert_equal(expected[depth][u][1], ps)
745 endfor
746 endfor
Bram Moolenaar422085f2021-12-17 20:36:15 +0000747
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +0000748 " Deleting a list range with locked items works, but changing the items
749 " fails.
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200750 let l = [1, 2, 3, 4]
751 lockvar l[1:2]
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +0000752 call assert_fails('let l[1:2] = [8, 9]', 'E741:')
753 unlet l[1:2]
754 call assert_equal([1, 4], l)
Yegappan Lakshmanan34fcb692021-05-25 20:14:00 +0200755 unlet l
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100756endfunc
757
758" Locked variables and :unlet or list / dict functions
759
760" No :unlet after lock on dict:
761func Test_dict_lock_unlet()
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100762 let d = {'a': 99, 'b': 100}
763 lockvar 1 d
Bram Moolenaare2e40752020-09-04 21:18:46 +0200764 call assert_fails('unlet d.a', 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100765endfunc
766
767" unlet after lock on dict item
768func Test_dict_item_lock_unlet()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200769 let lines =<< trim END
770 VAR d = {'a': 99, 'b': 100}
771 lockvar d.a
772 unlet d.a
773 call assert_equal({'b': 100}, d)
774 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000775 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100776endfunc
777
778" filter() after lock on dict item
779func Test_dict_lock_filter()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200780 let lines =<< trim END
781 VAR d = {'a': 99, 'b': 100}
782 lockvar d.a
783 call filter(d, 'v:key != "a"')
784 call assert_equal({'b': 100}, d)
785 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000786 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100787endfunc
788
789" map() after lock on dict
790func Test_dict_lock_map()
Bram Moolenaarbd77aa92021-08-12 17:06:05 +0200791 let lines =<< trim END
792 VAR d = {'a': 99, 'b': 100}
793 lockvar 1 d
794 call map(d, 'v:val + 200')
795 call assert_equal({'a': 299, 'b': 300}, d)
796 END
797 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000798 call v9.CheckTransLegacySuccess(lines)
799 call v9.CheckTransVim9Success(lines)
Bram Moolenaar71b76852021-12-17 20:15:38 +0000800
801 " For a :def function use a global dict.
802 let lines =<< trim END
803 let g:thedict = {'a': 77, 'b': 88}
804 lockvar 1 g:thedict
805 def Delkey()
806 unlet g:thedict.a
807 enddef
808 call Delkey()
809 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000810 call v9.CheckScriptFailure(lines, 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100811endfunc
812
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000813" Lock one item in a list
814func Test_list_item_lock_map()
815 let lines =<< trim END
816 VAR l = [99, 100, 101]
817 lockvar l[1]
818 call assert_fails("echo map(l, 'v:val + 200')", 'E741:')
819 call assert_equal([299, 100, 101], l)
820 END
821 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000822 call v9.CheckTransLegacySuccess(lines)
823 call v9.CheckTransVim9Success(lines)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000824endfunc
825
826" Lock one item in a dict
827func Test_dict_item_lock_map()
828 let lines =<< trim END
829 VAR d = {'a': 99, 'b': 100, 'c': 101}
830 lockvar d.b
831 call assert_fails("echo map(d, 'v:val + 200')", 'E741:')
832 call assert_equal({'a': 299, 'b': 100, 'c': 101}, d)
833 END
834 " This won't work in a :def function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000835 call v9.CheckTransLegacySuccess(lines)
836 call v9.CheckTransVim9Success(lines)
Yegappan Lakshmananf973eeb2021-12-22 18:19:26 +0000837endfunc
838
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100839" No extend() after lock on dict item
840func Test_dict_lock_extend()
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100841 let d = {'a': 99, 'b': 100}
842 lockvar d.a
Bram Moolenaare2e40752020-09-04 21:18:46 +0200843 call assert_fails("call extend(d, {'a' : 123})", 'E741:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100844 call assert_equal({'a': 99, 'b': 100}, d)
845endfunc
846
Bram Moolenaarf7b398c2020-04-23 15:46:35 +0200847" Cannot use += with a locked dict
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200848func Test_dict_lock_operator()
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200849 let d = {}
850 lockvar d
851 call assert_fails("let d += {'k' : 10}", 'E741:')
852 unlockvar d
853endfunc
854
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100855" No remove() of write-protected scope-level variable
Bram Moolenaar1e115362019-01-09 23:01:02 +0100856func Tfunc1(this_is_a_long_parameter_name)
Bram Moolenaare2e40752020-09-04 21:18:46 +0200857 call assert_fails("call remove(a:, 'this_is_a_long_parameter_name')", 'E742:')
Bram Moolenaar1e115362019-01-09 23:01:02 +0100858endfunc
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100859func Test_dict_scope_var_remove()
Bram Moolenaar1e115362019-01-09 23:01:02 +0100860 call Tfunc1('testval')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100861endfunc
862
863" No extend() of write-protected scope-level variable
Bram Moolenaar31b81602019-02-10 22:14:27 +0100864func Test_dict_scope_var_extend()
Bram Moolenaare2e40752020-09-04 21:18:46 +0200865 call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +0100866endfunc
867
Bram Moolenaar1e115362019-01-09 23:01:02 +0100868func Tfunc2(this_is_a_long_parameter_name)
Bram Moolenaare2e40752020-09-04 21:18:46 +0200869 call assert_fails("call extend(a:, {'this_is_a_long_parameter_name': 1234})", 'E742:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100870endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +0100871func Test_dict_scope_var_extend_overwrite()
Bram Moolenaar1e115362019-01-09 23:01:02 +0100872 call Tfunc2('testval')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100873endfunc
874
875" No :unlet of variable in locked scope
876func Test_lock_var_unlet()
877 let b:testvar = 123
878 lockvar 1 b:
879 call assert_fails('unlet b:testvar', 'E741:')
880 unlockvar 1 b:
881 unlet! b:testvar
882endfunc
883
884" No :let += of locked list variable
885func Test_let_lock_list()
886 let l = ['a', 'b', 3]
887 lockvar 1 l
888 call assert_fails("let l += ['x']", 'E741:')
889 call assert_equal(['a', 'b', 3], l)
890
891 unlet l
892 let l = [1, 2, 3, 4]
893 lockvar! l
894 call assert_equal([1, 2, 3, 4], l)
895 unlockvar l[1]
896 call assert_fails('unlet l[0:1]', 'E741:')
897 call assert_equal([1, 2, 3, 4], l)
898 call assert_fails('unlet l[1:2]', 'E741:')
899 call assert_equal([1, 2, 3, 4], l)
900 unlockvar l[1]
901 call assert_fails('let l[0:1] = [0, 1]', 'E741:')
902 call assert_equal([1, 2, 3, 4], l)
903 call assert_fails('let l[1:2] = [0, 1]', 'E741:')
904 call assert_equal([1, 2, 3, 4], l)
905 unlet l
Bram Moolenaar422085f2021-12-17 20:36:15 +0000906
907 let lines =<< trim END
908 def TryUnletListItem(l: list<any>)
909 unlet l[0]
910 enddef
911 let l = [1, 2, 3, 4]
912 lockvar! l
913 call TryUnletListItem(l)
914 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000915 call v9.CheckScriptFailure(lines, 'E741:')
Bram Moolenaar422085f2021-12-17 20:36:15 +0000916 unlet g:l
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100917endfunc
918
Bram Moolenaar8dfcce32020-03-18 19:32:26 +0100919" Locking part of the list
920func Test_let_lock_list_items()
921 let l = [1, 2, 3, 4]
922 lockvar l[2:]
923 call assert_equal(0, islocked('l[0]'))
924 call assert_equal(1, islocked('l[2]'))
925 call assert_equal(1, islocked('l[3]'))
926 call assert_fails('let l[2] = 10', 'E741:')
927 call assert_fails('let l[3] = 20', 'E741:')
928 unlet l
929endfunc
930
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100931" lockvar/islocked() triggering script autoloading
932func Test_lockvar_script_autoload()
933 let old_rtp = &rtp
934 set rtp+=./sautest
935 lockvar g:footest#x
936 unlockvar g:footest#x
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +0200937 call assert_equal(-1, 'g:footest#x'->islocked())
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100938 call assert_equal(0, exists('g:footest#x'))
939 call assert_equal(1, g:footest#x)
940 let &rtp = old_rtp
941endfunc
942
943" a:000 function argument test
944func s:arg_list_test(...)
945 call assert_fails('let a:000 = [1, 2]', 'E46:')
946 call assert_fails('let a:000[0] = 9', 'E742:')
947 call assert_fails('let a:000[2] = [9, 10]', 'E742:')
948 call assert_fails('let a:000[3] = {9 : 10}', 'E742:')
949
950 " now the tests that should pass
951 let a:000[2][1] = 9
952 call extend(a:000[2], [5, 6])
953 let a:000[3][5] = 8
954 let a:000[3]['a'] = 12
955 call assert_equal([1, 2, [3, 9, 5, 6], {'a': 12, '5': 8}], a:000)
956endfunc
957
958func Test_func_arg_list()
959 call s:arg_list_test(1, 2, [3, 4], {5: 6})
960endfunc
961
962" Tests for reverse(), sort(), uniq()
963func Test_reverse_sort_uniq()
Bram Moolenaaref982572021-08-12 19:27:57 +0200964 let lines =<< trim END
965 VAR l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
966 call assert_equal(['-0', 'A11', 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5], uniq(copy(l)))
967 call assert_equal([1.5, [0, 1, 2], 'x8', [0, 1, 2], 'foo', 'foo6', 'foo', 4, 'xaaa', 2, 2, 'A11', '-0'], reverse(l))
968 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 +0100969 call assert_equal(['-0', 'A11', 'foo', 'foo', 'foo6', 'x8', 'xaaa', 1.5, 2, 2, 4, [0, 1, 2], [0, 1, 2]], sort(l))
970 call assert_equal([[0, 1, 2], [0, 1, 2], 4, 2, 2, 1.5, 'xaaa', 'x8', 'foo6', 'foo', 'foo', 'A11', '-0'], reverse(sort(l)))
971 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))))
972 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 +0100973
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100974 LET l = [7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
975 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 +0100976
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100977 LET l = [7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
978 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'))
979 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'))
980 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 +0200981 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000982 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar0d17f0d2019-01-22 22:20:38 +0100983
Yegappan Lakshmananf9dc2782023-05-11 15:02:56 +0100984 call assert_fails('call reverse({})', 'E1252:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200985 call assert_fails('call uniq([1, 2], {x, y -> []})', 'E745:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100986 call assert_fails("call sort([1, 2], function('min'), 1)", "E1206:")
Bram Moolenaar08f41572020-04-20 16:50:00 +0200987 call assert_fails("call sort([1, 2], function('invalid_func'))", "E700:")
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200988 call assert_fails("call sort([1, 2], function('min'))", "E118:")
Bram Moolenaar2007dd42022-02-23 13:17:47 +0000989
990 let lines =<< trim END
991 call sort(['a', 'b'], 0)
992 END
993 call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
994
995 let lines =<< trim END
996 call sort(['a', 'b'], 1)
997 END
998 call v9.CheckDefAndScriptFailure(lines, 'E1256: String or function required for argument 2')
Bram Moolenaarfb094e12017-11-05 20:59:28 +0100999endfunc
1000
rbtnn0ccb5842021-12-18 18:33:46 +00001001" reduce a list, blob or string
Bram Moolenaar85629982020-06-01 18:39:20 +02001002func Test_reduce()
Bram Moolenaaref982572021-08-12 19:27:57 +02001003 let lines =<< trim END
1004 call assert_equal(1, reduce([], LSTART acc, val LMIDDLE acc + val LEND, 1))
1005 call assert_equal(10, reduce([1, 3, 5], LSTART acc, val LMIDDLE acc + val LEND, 1))
1006 call assert_equal(2 * (2 * ((2 * 1) + 2) + 3) + 4, reduce([2, 3, 4], LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
1007 call assert_equal('a x y z', ['x', 'y', 'z']->reduce(LSTART acc, val LMIDDLE acc .. ' ' .. val LEND, 'a'))
1008 call assert_equal([0, 1, 2, 3], reduce([1, 2, 3], function('add'), [0]))
Bram Moolenaar85629982020-06-01 18:39:20 +02001009
Bram Moolenaaref982572021-08-12 19:27:57 +02001010 VAR l = ['x', 'y', 'z']
1011 call assert_equal(42, reduce(l, function('get'), {'x': {'y': {'z': 42 } } }))
1012 call assert_equal(['x', 'y', 'z'], l)
Bram Moolenaar85629982020-06-01 18:39:20 +02001013
Bram Moolenaaref982572021-08-12 19:27:57 +02001014 call assert_equal(1, reduce([1], LSTART acc, val LMIDDLE acc + val LEND))
1015 call assert_equal('x y z', reduce(['x', 'y', 'z'], LSTART acc, val LMIDDLE acc .. ' ' .. val LEND))
1016 call assert_equal(120, range(1, 5)->reduce(LSTART acc, val LMIDDLE acc * val LEND))
1017
Bram Moolenaar52df40e2022-09-28 13:22:59 +01001018 call assert_equal(0, range(1)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1019 call assert_equal(1, range(2)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1020 call assert_equal(3, range(3)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1021 call assert_equal(6, range(4)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1022 call assert_equal(10, range(5)->reduce(LSTART acc, val LMIDDLE acc + val LEND))
1023
Bram Moolenaaref982572021-08-12 19:27:57 +02001024 call assert_equal(1, reduce(0z, LSTART acc, val LMIDDLE acc + val LEND, 1))
1025 call assert_equal(1 + 0xaf + 0xbf + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE acc + val LEND, 1))
1026 call assert_equal(2 * (2 * 1 + 0xaf) + 0xbf, 0zAFBF->reduce(LSTART acc, val LMIDDLE 2 * acc + val LEND, 1))
1027
1028 call assert_equal(0xff, reduce(0zff, LSTART acc, val LMIDDLE acc + val LEND))
1029 call assert_equal(2 * (2 * 0xaf + 0xbf) + 0xcf, reduce(0zAFBFCF, LSTART acc, val LMIDDLE 2 * acc + val LEND))
rbtnn0ccb5842021-12-18 18:33:46 +00001030
1031 call assert_equal('x,y,z', 'xyz'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1032 call assert_equal('', ''->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, ''))
1033 call assert_equal('あ,い,う,え,お,😊,💕', 'あいうえお😊💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1034 call assert_equal('😊,あ,い,う,え,お,💕', 'あいうえお💕'->reduce(LSTART acc, val LMIDDLE acc .. ',' .. val LEND, '😊'))
1035 call assert_equal('ऊ,ॠ,ॡ', reduce('ऊॠॡ', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1036 call assert_equal('c,à,t', reduce('càt', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1037 call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1038 call assert_equal('Å,s,t,r,ö,m', reduce('Åström', LSTART acc, val LMIDDLE acc .. ',' .. val LEND))
1039 call assert_equal(',a,b,c', reduce('abc', LSTART acc, val LMIDDLE acc .. ',' .. val LEND, test_null_string()))
zeertzjqad0c4422023-08-17 22:15:47 +02001040
1041 call assert_equal(0x7d, reduce([0x30, 0x25, 0x08, 0x61], 'or'))
1042 call assert_equal(0x7d, reduce(0z30250861, 'or'))
1043 call assert_equal('β', reduce('ββββ', 'matchstr'))
Bram Moolenaaref982572021-08-12 19:27:57 +02001044 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001045 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001046
1047 call assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce({ acc, val -> extend(acc, { val: 1 }) }, {}))
1048 vim9 assert_equal({'x': 1, 'y': 1, 'z': 1 }, ['x', 'y', 'z']->reduce((acc, val) => extend(acc, {[val]: 1 }), {}))
1049
Bram Moolenaar85629982020-06-01 18:39:20 +02001050 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 +01001051 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 +02001052 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 +01001053 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 +00001054 call assert_fails("call reduce('', { acc, val -> acc + val })", 'E998: Reduce of an empty String with no initial value')
1055 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 +02001056
rbtnn0ccb5842021-12-18 18:33:46 +00001057 call assert_fails("call reduce({}, { acc, val -> acc + val }, 1)", 'E1098:')
1058 call assert_fails("call reduce(0, { acc, val -> acc + val }, 1)", 'E1098:')
zeertzjqad0c4422023-08-17 22:15:47 +02001059 call assert_fails("call reduce([1, 2], 'Xdoes_not_exist')", 'E117:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001060 call assert_fails("echo reduce(0z01, { acc, val -> 2 * acc + val }, '')", 'E1210:')
Bram Moolenaarca275a02020-06-24 22:07:46 +02001061
rbtnn0ccb5842021-12-18 18:33:46 +00001062 call assert_fails("vim9 reduce(0, (acc, val) => (acc .. val), '')", 'E1252:')
1063 call assert_fails("vim9 reduce({}, (acc, val) => (acc .. val), '')", 'E1252:')
1064 call assert_fails("vim9 reduce(0.1, (acc, val) => (acc .. val), '')", 'E1252:')
1065 call assert_fails("vim9 reduce(function('tr'), (acc, val) => (acc .. val), '')", 'E1252:')
Yegappan Lakshmanan8deb2b32022-09-02 15:15:27 +01001066 call assert_fails("call reduce('', { acc, val -> acc + val }, 1)", 'E1174:')
1067 call assert_fails("call reduce('', { acc, val -> acc + val }, {})", 'E1174:')
1068 call assert_fails("call reduce('', { acc, val -> acc + val }, 0.1)", 'E1174:')
1069 call assert_fails("call reduce('', { acc, val -> acc + val }, function('tr'))", 'E1174:')
Yegappan Lakshmanan389b7212021-12-19 10:35:15 +00001070 call assert_fails("call reduce('abc', { a, v -> a10}, '')", 'E121:')
Yegappan Lakshmanan885de442022-04-23 10:51:14 +01001071 call assert_fails("call reduce(0z0102, { a, v -> a10}, 1)", 'E121:')
1072 call assert_fails("call reduce([1, 2], { a, v -> a10}, '')", 'E121:')
rbtnn0ccb5842021-12-18 18:33:46 +00001073
Bram Moolenaarca275a02020-06-24 22:07:46 +02001074 let g:lut = [1, 2, 3, 4]
1075 func EvilRemove()
1076 call remove(g:lut, 1)
1077 return 1
1078 endfunc
1079 call assert_fails("call reduce(g:lut, { acc, val -> EvilRemove() }, 1)", 'E742:')
1080 unlet g:lut
1081 delfunc EvilRemove
Bram Moolenaarfda20c42020-06-29 20:09:36 +02001082
1083 call assert_equal(42, reduce(test_null_list(), function('add'), 42))
1084 call assert_equal(42, reduce(test_null_blob(), function('add'), 42))
Bram Moolenaar0d90e722020-11-03 18:20:19 +01001085
1086 " should not crash
1087 call assert_fails('echo reduce([1], test_null_function())', 'E1132:')
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02001088 call assert_fails('echo reduce([1], test_null_partial())', 'E1132:')
Bram Moolenaar85629982020-06-01 18:39:20 +02001089endfunc
1090
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001091" splitting a string to a List using split()
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001092func Test_str_split()
Bram Moolenaaref982572021-08-12 19:27:57 +02001093 let lines =<< trim END
1094 call assert_equal(['aa', 'bb'], split(' aa bb '))
1095 call assert_equal(['aa', 'bb'], split(' aa bb ', '\W\+', 0))
1096 call assert_equal(['', 'aa', 'bb', ''], split(' aa bb ', '\W\+', 1))
1097 call assert_equal(['', '', 'aa', '', 'bb', '', ''], split(' aa bb ', '\W', 1))
1098 call assert_equal(['aa', '', 'bb'], split(':aa::bb:', ':', 0))
1099 call assert_equal(['', 'aa', '', 'bb', ''], split(':aa::bb:', ':', 1))
1100 call assert_equal(['aa', '', 'bb', 'cc', ''], split('aa,,bb, cc,', ',\s*', 1))
1101 call assert_equal(['a', 'b', 'c'], split('abc', '\zs'))
1102 call assert_equal(['', 'a', '', 'b', '', 'c', ''], split('abc', '\zs', 1))
1103 call assert_equal(['abc'], split('abc', '\\%('))
1104 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001105 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001106
Bram Moolenaar0e05de42020-03-25 22:23:46 +01001107 call assert_fails("call split('abc', [])", 'E730:')
1108 call assert_fails("call split('abc', 'b', [])", 'E745:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001109endfunc
1110
1111" compare recursively linked list and dict
1112func Test_listdict_compare()
Bram Moolenaaref982572021-08-12 19:27:57 +02001113 let lines =<< trim END
1114 VAR l = [1, 2, 3, '4']
1115 VAR d = {'1': 1, '2': l, '3': 3}
1116 LET l[1] = d
1117 call assert_true(l == l)
1118 call assert_true(d == d)
1119 call assert_false(l != deepcopy(l))
1120 call assert_false(d != deepcopy(d))
1121 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001122 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar8b633132020-03-20 18:20:51 +01001123
1124 " comparison errors
1125 call assert_fails('echo [1, 2] =~ {}', 'E691:')
1126 call assert_fails('echo [1, 2] =~ [1, 2]', 'E692:')
1127 call assert_fails('echo {} =~ 5', 'E735:')
1128 call assert_fails('echo {} =~ {}', 'E736:')
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001129endfunc
1130
1131 " compare complex recursively linked list and dict
1132func Test_listdict_compare_complex()
Bram Moolenaaref982572021-08-12 19:27:57 +02001133 let lines =<< trim END
1134 VAR l = []
1135 call add(l, l)
1136 VAR dict4 = {"l": l}
1137 call add(dict4.l, dict4)
1138 VAR lcopy = deepcopy(l)
1139 VAR dict4copy = deepcopy(dict4)
1140 call assert_true(l == lcopy)
1141 call assert_true(dict4 == dict4copy)
1142 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001143 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001144endfunc
1145
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001146" Test for extending lists and dictionaries
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001147func Test_listdict_extend()
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001148 " Test extend() with lists
1149
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001150 " Pass the same List to extend()
Bram Moolenaaref982572021-08-12 19:27:57 +02001151 let lines =<< trim END
1152 VAR l = [1, 2, 3]
1153 call assert_equal([1, 2, 3, 1, 2, 3], extend(l, l))
1154 call assert_equal([1, 2, 3, 1, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001155
Bram Moolenaaref982572021-08-12 19:27:57 +02001156 LET l = [1, 2, 3]
1157 call assert_equal([1, 2, 3, 4, 5, 6], extend(l, [4, 5, 6]))
1158 call assert_equal([1, 2, 3, 4, 5, 6], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001159
Bram Moolenaaref982572021-08-12 19:27:57 +02001160 LET l = [1, 2, 3]
1161 call extend(l, [4, 5, 6], 0)
1162 call assert_equal([4, 5, 6, 1, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001163
Bram Moolenaaref982572021-08-12 19:27:57 +02001164 LET l = [1, 2, 3]
1165 call extend(l, [4, 5, 6], 1)
1166 call assert_equal([1, 4, 5, 6, 2, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001167
Bram Moolenaaref982572021-08-12 19:27:57 +02001168 LET l = [1, 2, 3]
1169 call extend(l, [4, 5, 6], 3)
1170 call assert_equal([1, 2, 3, 4, 5, 6], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001171
Bram Moolenaaref982572021-08-12 19:27:57 +02001172 LET l = [1, 2, 3]
1173 call extend(l, [4, 5, 6], -1)
1174 call assert_equal([1, 2, 4, 5, 6, 3], l)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001175
Bram Moolenaaref982572021-08-12 19:27:57 +02001176 LET l = [1, 2, 3]
1177 call extend(l, [4, 5, 6], -3)
1178 call assert_equal([4, 5, 6, 1, 2, 3], l)
1179 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001180 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001181
1182 let l = [1, 2, 3]
1183 call assert_fails("call extend(l, [4, 5, 6], 4)", 'E684:')
1184 call assert_fails("call extend(l, [4, 5, 6], -4)", 'E684:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001185 call assert_fails("call extend(l, [4, 5, 6], 1.2)", 'E805:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001186
1187 " Test extend() with dictionaries.
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001188
1189 " Pass the same Dict to extend()
Bram Moolenaaref982572021-08-12 19:27:57 +02001190 let lines =<< trim END
1191 VAR d = {'a': {'b': 'B'}, 'x': 9}
1192 call extend(d, d)
1193 call assert_equal({'a': {'b': 'B'}, 'x': 9}, d)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001194
Bram Moolenaaref982572021-08-12 19:27:57 +02001195 LET d = {'a': 'A', 'b': 9}
1196 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, extend(d, {'b': 0, 'c': 'C'}))
1197 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001198
Bram Moolenaaref982572021-08-12 19:27:57 +02001199 LET d = {'a': 'A', 'b': 9}
1200 call extend(d, {'a': 'A', 'b': 0, 'c': 'C'}, "force")
1201 call assert_equal({'a': 'A', 'b': 0, 'c': 'C'}, d)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001202
Bram Moolenaaref982572021-08-12 19:27:57 +02001203 LET d = {'a': 'A', 'b': 9}
1204 call extend(d, {'b': 0, 'c': 'C'}, "keep")
1205 call assert_equal({'a': 'A', 'b': 9, 'c': 'C'}, d)
1206 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001207 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001208
1209 let d = {'a': 'A', 'b': 'B'}
1210 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
Yegappan Lakshmanan27708e62021-12-26 21:54:43 +00001211 call assert_fails("call extend(d, {'b': 0}, [])", 'E730:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001212 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001213 call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:')
Bram Moolenaar58d63a02019-02-25 05:56:31 +01001214 call assert_equal({'a': 'A', 'b': 'B'}, d)
1215
1216 call assert_fails("call extend([1, 2], 1)", 'E712:')
1217 call assert_fails("call extend([1, 2], {})", 'E712:')
Bram Moolenaar08f41572020-04-20 16:50:00 +02001218
1219 " Extend g: dictionary with an invalid variable name
1220 call assert_fails("call extend(g:, {'-!' : 10})", 'E461:')
Bram Moolenaardcae51f2021-04-08 20:10:10 +02001221
1222 " Extend a list with itself.
Bram Moolenaaref982572021-08-12 19:27:57 +02001223 let lines =<< trim END
1224 VAR l = [1, 5, 7]
1225 call extend(l, l, 0)
1226 call assert_equal([1, 5, 7, 1, 5, 7], l)
1227 LET l = [1, 5, 7]
1228 call extend(l, l, 1)
1229 call assert_equal([1, 1, 5, 7, 5, 7], l)
1230 LET l = [1, 5, 7]
1231 call extend(l, l, 2)
1232 call assert_equal([1, 5, 1, 5, 7, 7], l)
1233 LET l = [1, 5, 7]
1234 call extend(l, l, 3)
1235 call assert_equal([1, 5, 7, 1, 5, 7], l)
1236 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001237 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaarfb094e12017-11-05 20:59:28 +01001238endfunc
Bram Moolenaar31b81602019-02-10 22:14:27 +01001239
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001240func Test_listdict_extendnew()
1241 " Test extendnew() with lists
1242 let l = [1, 2, 3]
1243 call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
1244 call assert_equal([1, 2, 3], l)
zeertzjq341f3872023-02-27 14:59:57 +00001245 lockvar l
1246 call assert_equal([1, 2, 3, 4, 5], extendnew(l, [4, 5]))
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001247
zeertzjq341f3872023-02-27 14:59:57 +00001248 " Test extendnew() with dictionaries.
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001249 let d = {'a': {'b': 'B'}}
1250 call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
1251 call assert_equal({'a': {'b': 'B'}}, d)
zeertzjq341f3872023-02-27 14:59:57 +00001252 lockvar d
1253 call assert_equal({'a': {'b': 'B'}, 'c': 'cc'}, extendnew(d, {'c': 'cc'}))
Bram Moolenaarb0e6b512021-01-12 20:23:40 +01001254endfunc
1255
Bram Moolenaar31b81602019-02-10 22:14:27 +01001256func s:check_scope_dict(x, fixed)
1257 func s:gen_cmd(cmd, x)
1258 return substitute(a:cmd, '\<x\ze:', a:x, 'g')
1259 endfunc
1260
1261 let cmd = s:gen_cmd('let x:foo = 1', a:x)
1262 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001263 call assert_fails(cmd, 'E461:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001264 else
1265 exe cmd
1266 exe s:gen_cmd('call assert_equal(1, x:foo)', a:x)
1267 endif
1268
1269 let cmd = s:gen_cmd('let x:["bar"] = 2', a:x)
1270 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001271 call assert_fails(cmd, 'E461:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001272 else
1273 exe cmd
1274 exe s:gen_cmd('call assert_equal(2, x:bar)', a:x)
1275 endif
1276
1277 let cmd = s:gen_cmd('call extend(x:, {"baz": 3})', a:x)
1278 if a:fixed
Bram Moolenaare2e40752020-09-04 21:18:46 +02001279 call assert_fails(cmd, 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001280 else
1281 exe cmd
1282 exe s:gen_cmd('call assert_equal(3, x:baz)', a:x)
1283 endif
1284
1285 if a:fixed
1286 if a:x ==# 'a'
Bram Moolenaare2e40752020-09-04 21:18:46 +02001287 call assert_fails('unlet a:x', 'E795:')
1288 call assert_fails('call remove(a:, "x")', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001289 elseif a:x ==# 'v'
Bram Moolenaare2e40752020-09-04 21:18:46 +02001290 call assert_fails('unlet v:count', 'E795:')
1291 call assert_fails('call remove(v:, "count")', 'E742:')
Bram Moolenaar31b81602019-02-10 22:14:27 +01001292 endif
1293 else
1294 exe s:gen_cmd('unlet x:foo', a:x)
1295 exe s:gen_cmd('unlet x:bar', a:x)
1296 exe s:gen_cmd('call remove(x:, "baz")', a:x)
1297 endif
1298
1299 delfunc s:gen_cmd
1300endfunc
1301
1302func Test_scope_dict()
1303 " Test for g:
1304 call s:check_scope_dict('g', v:false)
1305
1306 " Test for s:
1307 call s:check_scope_dict('s', v:false)
1308
1309 " Test for l:
1310 call s:check_scope_dict('l', v:false)
1311
1312 " Test for a:
1313 call s:check_scope_dict('a', v:true)
1314
1315 " Test for b:
1316 call s:check_scope_dict('b', v:false)
1317
1318 " Test for w:
1319 call s:check_scope_dict('w', v:false)
1320
1321 " Test for t:
1322 call s:check_scope_dict('t', v:false)
1323
1324 " Test for v:
1325 call s:check_scope_dict('v', v:true)
1326endfunc
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001327
1328" Test for deep nesting of lists (> 100)
1329func Test_deep_nested_list()
1330 let deep_list = []
1331 let l = deep_list
1332 for i in range(102)
1333 let newlist = []
1334 call add(l, newlist)
1335 let l = newlist
1336 endfor
1337 call add(l, 102)
1338
1339 call assert_fails('let m = deepcopy(deep_list)', 'E698:')
1340 call assert_fails('lockvar 110 deep_list', 'E743:')
1341 call assert_fails('unlockvar 110 deep_list', 'E743:')
1342 call assert_fails('let x = execute("echo deep_list")', 'E724:')
1343 call test_garbagecollect_now()
1344 unlet deep_list
1345endfunc
1346
1347" Test for deep nesting of dicts (> 100)
1348func Test_deep_nested_dict()
1349 let deep_dict = {}
1350 let d = deep_dict
1351 for i in range(102)
1352 let newdict = {}
1353 let d.k = newdict
1354 let d = newdict
1355 endfor
1356 let d.k = 'v'
1357
1358 call assert_fails('let m = deepcopy(deep_dict)', 'E698:')
1359 call assert_fails('lockvar 110 deep_dict', 'E743:')
1360 call assert_fails('unlockvar 110 deep_dict', 'E743:')
1361 call assert_fails('let x = execute("echo deep_dict")', 'E724:')
1362 call test_garbagecollect_now()
1363 unlet deep_dict
1364endfunc
1365
Bram Moolenaar8b633132020-03-20 18:20:51 +01001366" List and dict indexing tests
1367func Test_listdict_index()
Bram Moolenaar62aec932022-01-29 21:45:34 +00001368 call v9.CheckLegacyAndVim9Failure(['echo function("min")[0]'], 'E695:')
1369 call v9.CheckLegacyAndVim9Failure(['echo v:true[0]'], 'E909:')
1370 call v9.CheckLegacyAndVim9Failure(['echo v:null[0]'], 'E909:')
1371 call v9.CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d.'], ['E15:', 'E1127:', 'E15:'])
1372 call v9.CheckLegacyAndVim9Failure(['VAR d = {"k": 10}', 'echo d[1 : 2]'], 'E719:')
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00001373
Bram Moolenaar8b633132020-03-20 18:20:51 +01001374 call assert_fails("let v = [4, 6][{-> 1}]", 'E729:')
Bram Moolenaar62aec932022-01-29 21:45:34 +00001375 call v9.CheckDefAndScriptFailure(['var v = [4, 6][() => 1]'], ['E1012', 'E703:'])
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00001376
Bram Moolenaar62aec932022-01-29 21:45:34 +00001377 call v9.CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : []]'], ['E730:', 'E1012:', 'E730:'])
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001378
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001379 call assert_fails("let v = range(5)[2:{-> 2}(]", ['E15:', 'E116:'])
Bram Moolenaar62aec932022-01-29 21:45:34 +00001380 call v9.CheckDefAndScriptFailure(['var v = range(5)[2 : () => 2(]'], 'E15:')
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001381
Bram Moolenaar62aec932022-01-29 21:45:34 +00001382 call v9.CheckLegacyAndVim9Failure(['VAR v = range(5)[2 : 3'], ['E111:', 'E1097:', 'E111:'])
1383 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, 10)'], 'E684:')
1384 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, -10)'], 'E684:')
1385 call v9.CheckLegacyAndVim9Failure(['VAR l = insert([1, 2, 3], 4, [])'], ['E745:', 'E1013:', 'E1210:'])
Bram Moolenaar700e6b12021-12-19 17:27:06 +00001386
Bram Moolenaar62aec932022-01-29 21:45:34 +00001387 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[i] = 3'], ['E121:', 'E1001:', 'E121:'])
1388 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[1.1] = 4'], ['E805:', 'E1012:', 'E805:'])
1389 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: i] = [4, 5]'], ['E121:', 'E1001:', 'E121:'])
1390 call v9.CheckLegacyAndVim9Failure(['VAR l = [1, 2, 3]', 'LET l[: 3.2] = [4, 5]'], ['E805:', 'E1012:', 'E805:'])
Bram Moolenaar097c5372023-05-24 21:02:24 +01001391 call v9.CheckLegacyAndVim9Failure(['VAR t = test_unknown()', 'echo t[0]'], ['E340:', 'E909:', 'E340:', 'E685:'])
Bram Moolenaar08f41572020-04-20 16:50:00 +02001392endfunc
1393
1394" Test for a null list
1395func Test_null_list()
Bram Moolenaaref982572021-08-12 19:27:57 +02001396 let lines =<< trim END
1397 VAR l = test_null_list()
1398 call assert_equal('', join(test_null_list()))
1399 call assert_equal('', join(l))
1400 call assert_equal(0, len(l))
1401 call assert_equal(1, empty(l))
1402 call assert_equal([], split(test_null_string()))
1403 call assert_equal([], l[ : 2])
1404 call assert_true([] == l)
1405 call assert_equal('[]', string(l))
1406 call assert_equal([], sort(test_null_list()))
1407 call assert_equal([], sort(l))
1408 call assert_equal([], uniq(test_null_list()))
1409 call assert_equal([], uniq(l))
1410 VAR k = [] + l
1411 call assert_equal([], k)
1412 LET k = l + []
1413 call assert_equal([], k)
1414 call assert_equal(0, len(copy(l)))
1415 call assert_equal(0, count(l, 5))
1416 call assert_equal([], deepcopy(l))
1417 call assert_equal(5, get(l, 2, 5))
1418 call assert_equal(-1, index(l, 2, 5))
1419 call assert_equal(0, min(l))
1420 call assert_equal(0, max(l))
1421 call assert_equal(0, remove(test_null_list(), 0, 2))
1422 call assert_equal([], repeat(l, 2))
1423 call assert_equal([], reverse(test_null_list()))
1424 call assert_equal([], reverse(l))
1425 call assert_equal([], sort(test_null_list()))
1426 call assert_equal([], sort(l))
1427 call assert_equal('[]', string(l))
1428 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001429 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001430
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001431 let l = test_null_list()
Bram Moolenaar64ffa9b2020-11-04 12:23:06 +01001432 call assert_equal([], extend(l, l, 0))
Bram Moolenaaref982572021-08-12 19:27:57 +02001433 call assert_equal(0, insert(test_null_list(), 2, -1))
1434 call assert_fails('let s = join([1, 2], [])', 'E730:')
1435 call assert_fails('call remove(l, 0, 2)', 'E684:')
1436 call assert_fails('call insert(l, 2, -1)', 'E684:')
1437 call assert_fails('call extend(test_null_list(), test_null_list())', 'E1134:')
1438
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001439 lockvar l
1440 call assert_equal(1, islocked('l'))
1441 unlockvar l
Bram Moolenaar08f41572020-04-20 16:50:00 +02001442endfunc
1443
1444" Test for a null dict
1445func Test_null_dict()
Bram Moolenaaref982572021-08-12 19:27:57 +02001446 let lines =<< trim END
1447 call assert_equal(test_null_dict(), test_null_dict())
1448 VAR d = test_null_dict()
1449 call assert_equal({}, d)
1450 call assert_equal(0, len(d))
1451 call assert_equal(1, empty(d))
1452 call assert_equal([], items(test_null_dict()))
1453 call assert_equal([], items(d))
1454 call assert_equal([], keys(test_null_dict()))
1455 call assert_equal([], keys(d))
1456 call assert_equal([], values(test_null_dict()))
1457 call assert_equal([], values(d))
1458 call assert_false(has_key(d, 'k'))
1459 call assert_equal('{}', string(d))
1460 call assert_equal({}, {})
1461 call assert_equal(0, len(copy(d)))
1462 call assert_equal(0, count(d, 'k'))
1463 call assert_equal({}, deepcopy(d))
1464 call assert_equal(20, get(d, 'k', 20))
1465 call assert_equal(0, min(d))
1466 call assert_equal(0, max(d))
1467 call assert_equal(0, remove(test_null_dict(), 'k'))
1468 call assert_equal('{}', string(d))
1469 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00001470 call v9.CheckLegacyAndVim9Success(lines)
Bram Moolenaaref982572021-08-12 19:27:57 +02001471
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001472 let d = test_null_dict()
Bram Moolenaar64ffa9b2020-11-04 12:23:06 +01001473 call assert_equal({}, extend(d, d, 'keep'))
Bram Moolenaaref982572021-08-12 19:27:57 +02001474 call assert_fails("call remove(d, 'k')", 'E716:')
1475 call assert_fails('let x = d[10]', 'E716:')
1476 call assert_fails('call extend(test_null_dict(), test_null_dict())', 'E1133:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02001477 lockvar d
1478 call assert_equal(1, islocked('d'))
1479 unlockvar d
Bram Moolenaar8b633132020-03-20 18:20:51 +01001480endfunc
1481
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001482" Test for the indexof() function
1483func Test_indexof()
1484 let l = [#{color: 'red'}, #{color: 'blue'}, #{color: 'green'}]
1485 call assert_equal(0, indexof(l, {i, v -> v.color == 'red'}))
1486 call assert_equal(2, indexof(l, {i, v -> v.color == 'green'}))
1487 call assert_equal(-1, indexof(l, {i, v -> v.color == 'grey'}))
1488 call assert_equal(1, indexof(l, "v:val.color == 'blue'"))
1489 call assert_equal(-1, indexof(l, "v:val.color == 'cyan'"))
1490
1491 let l = [#{n: 10}, #{n: 10}, #{n: 20}]
1492 call assert_equal(0, indexof(l, "v:val.n == 10", #{startidx: 0}))
1493 call assert_equal(1, indexof(l, "v:val.n == 10", #{startidx: -2}))
1494 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: 4}))
1495 call assert_equal(-1, indexof(l, "v:val.n == 10", #{startidx: -4}))
1496 call assert_equal(0, indexof(l, "v:val.n == 10", test_null_dict()))
1497
Yegappan Lakshmanan63acae12022-08-14 12:07:11 +01001498 let s = ["a", "b", "c"]
1499 call assert_equal(2, indexof(s, {_, v -> v == 'c'}))
1500 call assert_equal(-1, indexof(s, {_, v -> v == 'd'}))
1501 call assert_equal(-1, indexof(s, {_, v -> "v == 'd'"}))
1502
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001503 call assert_equal(-1, indexof([], {i, v -> v == 'a'}))
Yegappan Lakshmanan63acae12022-08-14 12:07:11 +01001504 call assert_equal(-1, indexof([1, 2, 3], {_, v -> "v == 2"}))
Yegappan Lakshmananb2186552022-08-13 13:09:20 +01001505 call assert_equal(-1, indexof(test_null_list(), {i, v -> v == 'a'}))
1506 call assert_equal(-1, indexof(l, test_null_string()))
1507 call assert_equal(-1, indexof(l, test_null_function()))
1508
1509 " failure cases
1510 call assert_fails('let i = indexof(l, "v:val == ''cyan''")', 'E735:')
1511 call assert_fails('let i = indexof(l, "color == ''cyan''")', 'E121:')
1512 call assert_fails('let i = indexof(l, {})', 'E1256:')
1513 call assert_fails('let i = indexof({}, "v:val == 2")', 'E1226:')
1514 call assert_fails('let i = indexof([], "v:val == 2", [])', 'E1206:')
1515
1516 func TestIdx(k, v)
1517 return a:v.n == 20
1518 endfunc
1519 call assert_equal(2, indexof(l, function("TestIdx")))
1520 delfunc TestIdx
1521 func TestIdx(k, v)
1522 return {}
1523 endfunc
1524 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E728:')
1525 delfunc TestIdx
1526 func TestIdx(k, v)
1527 throw "IdxError"
1528 endfunc
1529 call assert_fails('let i = indexof(l, function("TestIdx"))', 'E605:')
1530 delfunc TestIdx
1531endfunc
1532
Bram Moolenaar8dfcce32020-03-18 19:32:26 +01001533" vim: shiftwidth=2 sts=2 expandtab