119
|
1 Tests for List and Dictionary types. vim: set ft=vim :
|
|
2
|
|
3 STARTTEST
|
|
4 :so small.vim
|
148
|
5 :fun Test(...)
|
4191
|
6 :lang C
|
119
|
7 :" Creating List directly with different types
|
|
8 :let l = [1, 'as''d', [1, 2, function("strlen")], {'a': 1},]
|
|
9 :$put =string(l)
|
|
10 :$put =string(l[-1])
|
|
11 :$put =string(l[-4])
|
|
12 :try
|
|
13 : $put =string(l[-5])
|
|
14 :catch
|
|
15 : $put =v:exception[:14]
|
|
16 :endtry
|
842
|
17 :" List slices
|
|
18 :$put =string(l[:])
|
|
19 :$put =string(l[1:])
|
|
20 :$put =string(l[:-2])
|
|
21 :$put =string(l[0:8])
|
|
22 :$put =string(l[8:-1])
|
119
|
23 :"
|
|
24 :" List identity
|
|
25 :let ll = l
|
|
26 :let lx = copy(l)
|
|
27 :try
|
|
28 : $put =(l == ll) . (l isnot ll) . (l is ll) . (l == lx) . (l is lx) . (l isnot lx)
|
|
29 :catch
|
|
30 : $put =v:exception
|
|
31 :endtry
|
|
32 :"
|
|
33 :" Creating Dictionary directly with different types
|
|
34 :let d = {001: 'asd', 'b': [1, 2, function('strlen')], -1: {'a': 1},}
|
|
35 :$put =string(d) . d.1
|
|
36 :$put =string(sort(keys(d)))
|
148
|
37 :$put =string (values(d))
|
119
|
38 :for [key, val] in items(d)
|
|
39 : $put =key . ':' . string(val)
|
|
40 : unlet key val
|
|
41 :endfor
|
148
|
42 :call extend (d, {3:33, 1:99})
|
119
|
43 :call extend(d, {'b':'bbb', 'c':'ccc'}, "keep")
|
|
44 :try
|
|
45 : call extend(d, {3:333,4:444}, "error")
|
|
46 :catch
|
|
47 : $put =v:exception[:15] . v:exception[-1:-1]
|
|
48 :endtry
|
|
49 :$put =string(d)
|
|
50 :call filter(d, 'v:key =~ ''[ac391]''')
|
|
51 :$put =string(d)
|
|
52 :"
|
|
53 :" Dictionary identity
|
|
54 :let dd = d
|
|
55 :let dx = copy(d)
|
|
56 :try
|
|
57 : $put =(d == dd) . (d isnot dd) . (d is dd) . (d == dx) . (d is dx) . (d isnot dx)
|
|
58 :catch
|
|
59 : $put =v:exception
|
|
60 :endtry
|
|
61 :"
|
|
62 :" Changing var type should fail
|
|
63 :try
|
|
64 : let d = []
|
|
65 :catch
|
|
66 : $put =v:exception[:14] . v:exception[-1:-1]
|
|
67 :endtry
|
|
68 :try
|
|
69 : let l = {}
|
|
70 :catch
|
|
71 : $put =v:exception[:14] . v:exception[-1:-1]
|
|
72 :endtry
|
|
73 :"
|
|
74 :" removing items with :unlet
|
|
75 :unlet l[2]
|
|
76 :$put =string(l)
|
|
77 :let l = range(8)
|
148
|
78 :try
|
119
|
79 :unlet l[:3]
|
|
80 :unlet l[1:]
|
148
|
81 :catch
|
|
82 :$put =v:exception
|
|
83 :endtry
|
119
|
84 :$put =string(l)
|
|
85 :"
|
|
86 :unlet d.c
|
|
87 :unlet d[-1]
|
|
88 :$put =string(d)
|
|
89 :"
|
842
|
90 :" removing items out of range: silently skip items that don't exist
|
|
91 let l = [0, 1, 2, 3]
|
|
92 :unlet l[2:1]
|
|
93 :$put =string(l)
|
|
94 let l = [0, 1, 2, 3]
|
|
95 :unlet l[2:2]
|
|
96 :$put =string(l)
|
|
97 let l = [0, 1, 2, 3]
|
|
98 :unlet l[2:3]
|
|
99 :$put =string(l)
|
|
100 let l = [0, 1, 2, 3]
|
|
101 :unlet l[2:4]
|
|
102 :$put =string(l)
|
|
103 let l = [0, 1, 2, 3]
|
|
104 :unlet l[2:5]
|
|
105 :$put =string(l)
|
|
106 let l = [0, 1, 2, 3]
|
|
107 :unlet l[-1:2]
|
|
108 :$put =string(l)
|
|
109 let l = [0, 1, 2, 3]
|
|
110 :unlet l[-2:2]
|
|
111 :$put =string(l)
|
|
112 let l = [0, 1, 2, 3]
|
|
113 :unlet l[-3:2]
|
|
114 :$put =string(l)
|
|
115 let l = [0, 1, 2, 3]
|
|
116 :unlet l[-4:2]
|
|
117 :$put =string(l)
|
|
118 let l = [0, 1, 2, 3]
|
|
119 :unlet l[-5:2]
|
|
120 :$put =string(l)
|
|
121 let l = [0, 1, 2, 3]
|
|
122 :unlet l[-6:2]
|
|
123 :$put =string(l)
|
|
124 :"
|
|
125 :" assignment to a list
|
|
126 :let l = [0, 1, 2, 3]
|
|
127 :let [va, vb] = l[2:3]
|
|
128 :$put =va
|
|
129 :$put =vb
|
|
130 :try
|
|
131 : let [va, vb] = l
|
|
132 :catch
|
|
133 : $put =v:exception[:14]
|
|
134 :endtry
|
|
135 :try
|
|
136 : let [va, vb] = l[1:1]
|
|
137 :catch
|
|
138 : $put =v:exception[:14]
|
|
139 :endtry
|
|
140 :"
|
123
|
141 :" manipulating a big Dictionary (hashtable.c has a border of 1000 entries)
|
119
|
142 :let d = {}
|
123
|
143 :for i in range(1500)
|
|
144 : let d[i] = 3000 - i
|
119
|
145 :endfor
|
123
|
146 :$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499]
|
119
|
147 :try
|
123
|
148 : let n = d[1500]
|
119
|
149 :catch
|
1539
|
150 : $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '')
|
119
|
151 :endtry
|
|
152 :" lookup each items
|
123
|
153 :for i in range(1500)
|
|
154 : if d[i] != 3000 - i
|
119
|
155 : $put =d[i]
|
|
156 : endif
|
|
157 :endfor
|
|
158 : let i += 1
|
|
159 :" delete even items
|
|
160 :while i >= 2
|
|
161 : let i -= 2
|
|
162 : unlet d[i]
|
|
163 :endwhile
|
123
|
164 :$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1]
|
119
|
165 :" delete odd items, checking value, one intentionally wrong
|
|
166 :let d[33] = 999
|
|
167 :let i = 1
|
123
|
168 :while i < 1500
|
|
169 : if d[i] != 3000 - i
|
119
|
170 : $put =i . '=' . d[i]
|
|
171 : else
|
|
172 : unlet d[i]
|
|
173 : endif
|
|
174 : let i += 2
|
|
175 :endwhile
|
|
176 :$put =string(d) " must be almost empty now
|
|
177 :unlet d
|
|
178 :"
|
|
179 :" Dictionary function
|
|
180 :let dict = {}
|
|
181 :func dict.func(a) dict
|
|
182 : $put =a:a . len(self.data)
|
|
183 :endfunc
|
|
184 :let dict.data = [1,2,3]
|
|
185 :call dict.func("len: ")
|
123
|
186 :let x = dict.func("again: ")
|
119
|
187 :try
|
|
188 : let Fn = dict.func
|
|
189 : call Fn('xxx')
|
|
190 :catch
|
|
191 : $put =v:exception[:15]
|
|
192 :endtry
|
123
|
193 :"
|
|
194 :" Function in script-local List or Dict
|
|
195 :let g:dict = {}
|
|
196 :function g:dict.func() dict
|
|
197 : $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf')
|
|
198 :endfunc
|
|
199 :let g:dict.foo = ['-', 2, 3]
|
|
200 :call insert(g:dict.foo, function('strlen'))
|
|
201 :call g:dict.func()
|
119
|
202 :"
|
|
203 :" Nasty: remove func from Dict that's being called (works)
|
|
204 :let d = {1:1}
|
|
205 :func d.func(a)
|
|
206 : return "a:". a:a
|
|
207 :endfunc
|
148
|
208 :$put =d.func(string(remove(d, 'func')))
|
119
|
209 :"
|
164
|
210 :" Nasty: deepcopy() dict that refers to itself (fails when noref used)
|
119
|
211 :let d = {1:1, 2:2}
|
|
212 :let l = [4, d, 6]
|
|
213 :let d[3] = l
|
164
|
214 :let dc = deepcopy(d)
|
119
|
215 :try
|
164
|
216 : let dc = deepcopy(d, 1)
|
119
|
217 :catch
|
|
218 : $put =v:exception[:14]
|
|
219 :endtry
|
164
|
220 :let l2 = [0, l, l, 3]
|
|
221 :let l[1] = l2
|
|
222 :let l3 = deepcopy(l2)
|
|
223 :$put ='same list: ' . (l3[1] is l3[2])
|
119
|
224 :"
|
148
|
225 :" Locked variables
|
|
226 :for depth in range(5)
|
|
227 : $put ='depth is ' . depth
|
|
228 : for u in range(3)
|
|
229 : unlet l
|
|
230 : let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}]
|
|
231 : exe "lockvar " . depth . " l"
|
|
232 : if u == 1
|
|
233 : exe "unlockvar l"
|
|
234 : elseif u == 2
|
|
235 : exe "unlockvar " . depth . " l"
|
|
236 : endif
|
|
237 : 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]")
|
|
238 : $put =ps
|
|
239 : let ps = ''
|
|
240 : try
|
|
241 : let l[1][1][0] = 99
|
|
242 : let ps .= 'p'
|
|
243 : catch
|
|
244 : let ps .= 'F'
|
|
245 : endtry
|
|
246 : try
|
|
247 : let l[1][1] = [99]
|
|
248 : let ps .= 'p'
|
|
249 : catch
|
|
250 : let ps .= 'F'
|
|
251 : endtry
|
|
252 : try
|
|
253 : let l[1] = [99]
|
|
254 : let ps .= 'p'
|
|
255 : catch
|
|
256 : let ps .= 'F'
|
|
257 : endtry
|
|
258 : try
|
|
259 : let l[2]['6'][7] = 99
|
|
260 : let ps .= 'p'
|
|
261 : catch
|
|
262 : let ps .= 'F'
|
|
263 : endtry
|
|
264 : try
|
|
265 : let l[2][6] = {99: 99}
|
|
266 : let ps .= 'p'
|
|
267 : catch
|
|
268 : let ps .= 'F'
|
|
269 : endtry
|
|
270 : try
|
|
271 : let l[2] = {99: 99}
|
|
272 : let ps .= 'p'
|
|
273 : catch
|
|
274 : let ps .= 'F'
|
|
275 : endtry
|
|
276 : try
|
|
277 : let l = [99]
|
|
278 : let ps .= 'p'
|
|
279 : catch
|
|
280 : let ps .= 'F'
|
|
281 : endtry
|
|
282 : $put =ps
|
|
283 : endfor
|
|
284 :endfor
|
6166
|
285 :unlet l
|
|
286 :let l = [1, 2, 3, 4]
|
|
287 :lockvar! l
|
|
288 :$put =string(l)
|
|
289 :unlockvar l[1]
|
|
290 :unlet l[0:1]
|
|
291 :$put =string(l)
|
|
292 :unlet l[1:2]
|
|
293 :$put =string(l)
|
|
294 :unlockvar l[1]
|
|
295 :let l[0:1] = [0, 1]
|
|
296 :$put =string(l)
|
|
297 :let l[1:2] = [0, 1]
|
|
298 :$put =string(l)
|
|
299 :unlet l
|
5604
|
300 :" :lockvar/islocked() triggering script autoloading
|
|
301 :set rtp+=./sautest
|
|
302 :lockvar g:footest#x
|
|
303 :unlockvar g:footest#x
|
|
304 :$put ='locked g:footest#x:'.islocked('g:footest#x')
|
|
305 :$put ='exists g:footest#x:'.exists('g:footest#x')
|
|
306 :$put ='g:footest#x: '.g:footest#x
|
148
|
307 :"
|
|
308 :" a:000 function argument
|
|
309 :" first the tests that should fail
|
|
310 :try
|
|
311 : let a:000 = [1, 2]
|
|
312 :catch
|
|
313 : $put ='caught a:000'
|
|
314 :endtry
|
|
315 :try
|
|
316 : let a:000[0] = 9
|
|
317 :catch
|
|
318 : $put ='caught a:000[0]'
|
|
319 :endtry
|
|
320 :try
|
|
321 : let a:000[2] = [9, 10]
|
|
322 :catch
|
|
323 : $put ='caught a:000[2]'
|
|
324 :endtry
|
|
325 :try
|
|
326 : let a:000[3] = {9: 10}
|
|
327 :catch
|
|
328 : $put ='caught a:000[3]'
|
|
329 :endtry
|
|
330 :" now the tests that should pass
|
|
331 :try
|
|
332 : let a:000[2][1] = 9
|
|
333 : call extend(a:000[2], [5, 6])
|
|
334 : let a:000[3][5] = 8
|
|
335 : let a:000[3]['a'] = 12
|
|
336 : $put =string(a:000)
|
|
337 :catch
|
|
338 : $put ='caught ' . v:exception
|
|
339 :endtry
|
|
340 :"
|
5747
|
341 :" reverse(), sort(), uniq()
|
|
342 :let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
|
|
343 :$put =string(uniq(copy(l)))
|
164
|
344 :$put =string(reverse(l))
|
|
345 :$put =string(reverse(reverse(l)))
|
|
346 :$put =string(sort(l))
|
|
347 :$put =string(reverse(sort(l)))
|
|
348 :$put =string(sort(reverse(sort(l))))
|
5747
|
349 :$put =string(uniq(sort(l)))
|
6022
|
350 :let l=[7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four']
|
6014
|
351 :$put =string(sort(copy(l), 'n'))
|
6022
|
352 :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []]
|
6001
|
353 :$put =string(sort(copy(l), 1))
|
|
354 :$put =string(sort(copy(l), 'i'))
|
|
355 :$put =string(sort(copy(l)))
|
164
|
356 :"
|
283
|
357 :" splitting a string to a List
|
|
358 :$put =string(split(' aa bb '))
|
|
359 :$put =string(split(' aa bb ', '\W\+', 0))
|
|
360 :$put =string(split(' aa bb ', '\W\+', 1))
|
|
361 :$put =string(split(' aa bb ', '\W', 1))
|
|
362 :$put =string(split(':aa::bb:', ':', 0))
|
|
363 :$put =string(split(':aa::bb:', ':', 1))
|
|
364 :$put =string(split('aa,,bb, cc,', ',\s*', 1))
|
294
|
365 :$put =string(split('abc', '\zs'))
|
|
366 :$put =string(split('abc', '\zs', 1))
|
283
|
367 :"
|
1125
|
368 :" compare recursively linked list and dict
|
|
369 :let l = [1, 2, 3, 4]
|
|
370 :let d = {'1': 1, '2': l, '3': 3}
|
|
371 :let l[1] = d
|
|
372 :$put =(l == l)
|
|
373 :$put =(d == d)
|
|
374 :$put =(l != deepcopy(l))
|
|
375 :$put =(d != deepcopy(d))
|
2634
|
376 :"
|
|
377 :" compare complex recursively linked list and dict
|
|
378 :let l = []
|
|
379 :call add(l, l)
|
|
380 :let dict4 = {"l": l}
|
|
381 :call add(dict4.l, dict4)
|
|
382 :let lcopy = deepcopy(l)
|
|
383 :let dict4copy = deepcopy(dict4)
|
|
384 :$put =(l == lcopy)
|
|
385 :$put =(dict4 == dict4copy)
|
3508
|
386 :"
|
|
387 :" Pass the same List to extend()
|
|
388 :let l = [1, 2, 3, 4, 5]
|
|
389 :call extend(l, l)
|
|
390 :$put =string(l)
|
|
391 :"
|
|
392 :" Pass the same Dict to extend()
|
|
393 :let d = { 'a': {'b': 'B'}}
|
|
394 :call extend(d, d)
|
|
395 :$put =string(d)
|
|
396 :"
|
|
397 :" Pass the same Dict to extend() with "error"
|
|
398 :try
|
|
399 : call extend(d, d, "error")
|
|
400 :catch
|
|
401 : $put =v:exception[:15] . v:exception[-1:-1]
|
|
402 :endtry
|
|
403 :$put =string(d)
|
119
|
404 :endfun
|
2634
|
405 :"
|
148
|
406 :call Test(1, 2, [3, 4], {5: 6}) " This may take a while
|
119
|
407 :"
|
1405
|
408 :delfunc Test
|
|
409 :unlet dict
|
|
410 :call garbagecollect(1)
|
|
411 :"
|
3774
|
412 :" test for patch 7.3.637
|
|
413 :let a = 'No error caught'
|
|
414 :try|foldopen|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
|
|
415 o=a
:"
|
|
416 :lang C
|
|
417 :redir => a
|
|
418 :try|foobar|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry
|
|
419 :redir END
|
|
420 o=a
:"
|
|
421 :"
|
119
|
422 :/^start:/,$wq! test.out
|
|
423 ENDTEST
|
|
424
|
|
425 start:
|