Mercurial > vim
annotate src/testdir/test55.in @ 11418:162bcd0debd7 v8.0.0593
patch 8.0.0593: duplication of code for adding a list or dict return value
commit https://github.com/vim/vim/commit/45cf6e910c6d162775ca9d470fac4b6db844001f
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Apr 30 20:25:19 2017 +0200
patch 8.0.0593: duplication of code for adding a list or dict return value
Problem: Duplication of code for adding a list or dict return value.
Solution: Add rettv_dict_set() and rettv_list_set(). (Yegappan Lakshmanan)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 30 Apr 2017 20:30:04 +0200 |
parents | e5f2e0f8af10 |
children |
rev | line source |
---|---|
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 :" | |
63 :" removing items with :unlet | |
64 :unlet l[2] | |
65 :$put =string(l) | |
66 :let l = range(8) | |
148 | 67 :try |
119 | 68 :unlet l[:3] |
69 :unlet l[1:] | |
148 | 70 :catch |
71 :$put =v:exception | |
72 :endtry | |
119 | 73 :$put =string(l) |
74 :" | |
75 :unlet d.c | |
76 :unlet d[-1] | |
77 :$put =string(d) | |
78 :" | |
842 | 79 :" removing items out of range: silently skip items that don't exist |
80 let l = [0, 1, 2, 3] | |
81 :unlet l[2:1] | |
82 :$put =string(l) | |
83 let l = [0, 1, 2, 3] | |
84 :unlet l[2:2] | |
85 :$put =string(l) | |
86 let l = [0, 1, 2, 3] | |
87 :unlet l[2:3] | |
88 :$put =string(l) | |
89 let l = [0, 1, 2, 3] | |
90 :unlet l[2:4] | |
91 :$put =string(l) | |
92 let l = [0, 1, 2, 3] | |
93 :unlet l[2:5] | |
94 :$put =string(l) | |
95 let l = [0, 1, 2, 3] | |
96 :unlet l[-1:2] | |
97 :$put =string(l) | |
98 let l = [0, 1, 2, 3] | |
99 :unlet l[-2:2] | |
100 :$put =string(l) | |
101 let l = [0, 1, 2, 3] | |
102 :unlet l[-3:2] | |
103 :$put =string(l) | |
104 let l = [0, 1, 2, 3] | |
105 :unlet l[-4:2] | |
106 :$put =string(l) | |
107 let l = [0, 1, 2, 3] | |
108 :unlet l[-5:2] | |
109 :$put =string(l) | |
110 let l = [0, 1, 2, 3] | |
111 :unlet l[-6:2] | |
112 :$put =string(l) | |
113 :" | |
114 :" assignment to a list | |
115 :let l = [0, 1, 2, 3] | |
116 :let [va, vb] = l[2:3] | |
117 :$put =va | |
118 :$put =vb | |
119 :try | |
120 : let [va, vb] = l | |
121 :catch | |
122 : $put =v:exception[:14] | |
123 :endtry | |
124 :try | |
125 : let [va, vb] = l[1:1] | |
126 :catch | |
127 : $put =v:exception[:14] | |
128 :endtry | |
129 :" | |
123 | 130 :" manipulating a big Dictionary (hashtable.c has a border of 1000 entries) |
119 | 131 :let d = {} |
123 | 132 :for i in range(1500) |
133 : let d[i] = 3000 - i | |
119 | 134 :endfor |
123 | 135 :$put =d[0] . ' ' . d[100] . ' ' . d[999] . ' ' . d[1400] . ' ' . d[1499] |
119 | 136 :try |
123 | 137 : let n = d[1500] |
119 | 138 :catch |
1539 | 139 : $put =substitute(v:exception, '\v(.{14}).*( \d{4}).*', '\1\2', '') |
119 | 140 :endtry |
141 :" lookup each items | |
123 | 142 :for i in range(1500) |
143 : if d[i] != 3000 - i | |
119 | 144 : $put =d[i] |
145 : endif | |
146 :endfor | |
147 : let i += 1 | |
148 :" delete even items | |
149 :while i >= 2 | |
150 : let i -= 2 | |
151 : unlet d[i] | |
152 :endwhile | |
123 | 153 :$put =get(d, 1500 - 100, 'NONE') . ' ' . d[1] |
119 | 154 :" delete odd items, checking value, one intentionally wrong |
155 :let d[33] = 999 | |
156 :let i = 1 | |
123 | 157 :while i < 1500 |
158 : if d[i] != 3000 - i | |
119 | 159 : $put =i . '=' . d[i] |
160 : else | |
161 : unlet d[i] | |
162 : endif | |
163 : let i += 2 | |
164 :endwhile | |
165 :$put =string(d) " must be almost empty now | |
166 :unlet d | |
167 :" | |
168 :" Dictionary function | |
169 :let dict = {} | |
170 :func dict.func(a) dict | |
171 : $put =a:a . len(self.data) | |
172 :endfunc | |
173 :let dict.data = [1,2,3] | |
174 :call dict.func("len: ") | |
123 | 175 :let x = dict.func("again: ") |
8631
e5f2e0f8af10
commit https://github.com/vim/vim/commit/3905e291fe4375ca5c59efa9ffcb01a39c7be3a9
Christian Brabandt <cb@256bit.org>
parents:
8516
diff
changeset
|
176 :let Fn = dict.func |
e5f2e0f8af10
commit https://github.com/vim/vim/commit/3905e291fe4375ca5c59efa9ffcb01a39c7be3a9
Christian Brabandt <cb@256bit.org>
parents:
8516
diff
changeset
|
177 :call Fn('xxx') |
123 | 178 :" |
179 :" Function in script-local List or Dict | |
180 :let g:dict = {} | |
181 :function g:dict.func() dict | |
182 : $put ='g:dict.func'.self.foo[1].self.foo[0]('asdf') | |
183 :endfunc | |
184 :let g:dict.foo = ['-', 2, 3] | |
185 :call insert(g:dict.foo, function('strlen')) | |
186 :call g:dict.func() | |
119 | 187 :" |
188 :" Nasty: remove func from Dict that's being called (works) | |
189 :let d = {1:1} | |
190 :func d.func(a) | |
191 : return "a:". a:a | |
192 :endfunc | |
148 | 193 :$put =d.func(string(remove(d, 'func'))) |
119 | 194 :" |
164 | 195 :" Nasty: deepcopy() dict that refers to itself (fails when noref used) |
119 | 196 :let d = {1:1, 2:2} |
197 :let l = [4, d, 6] | |
198 :let d[3] = l | |
164 | 199 :let dc = deepcopy(d) |
119 | 200 :try |
164 | 201 : let dc = deepcopy(d, 1) |
119 | 202 :catch |
203 : $put =v:exception[:14] | |
204 :endtry | |
164 | 205 :let l2 = [0, l, l, 3] |
206 :let l[1] = l2 | |
207 :let l3 = deepcopy(l2) | |
208 :$put ='same list: ' . (l3[1] is l3[2]) | |
119 | 209 :" |
148 | 210 :" Locked variables |
211 :for depth in range(5) | |
212 : $put ='depth is ' . depth | |
213 : for u in range(3) | |
214 : unlet l | |
215 : let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}] | |
216 : exe "lockvar " . depth . " l" | |
217 : if u == 1 | |
218 : exe "unlockvar l" | |
219 : elseif u == 2 | |
220 : exe "unlockvar " . depth . " l" | |
221 : endif | |
222 : 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]") | |
223 : $put =ps | |
224 : let ps = '' | |
225 : try | |
226 : let l[1][1][0] = 99 | |
227 : let ps .= 'p' | |
228 : catch | |
229 : let ps .= 'F' | |
230 : endtry | |
231 : try | |
232 : let l[1][1] = [99] | |
233 : let ps .= 'p' | |
234 : catch | |
235 : let ps .= 'F' | |
236 : endtry | |
237 : try | |
238 : let l[1] = [99] | |
239 : let ps .= 'p' | |
240 : catch | |
241 : let ps .= 'F' | |
242 : endtry | |
243 : try | |
244 : let l[2]['6'][7] = 99 | |
245 : let ps .= 'p' | |
246 : catch | |
247 : let ps .= 'F' | |
248 : endtry | |
249 : try | |
250 : let l[2][6] = {99: 99} | |
251 : let ps .= 'p' | |
252 : catch | |
253 : let ps .= 'F' | |
254 : endtry | |
255 : try | |
256 : let l[2] = {99: 99} | |
257 : let ps .= 'p' | |
258 : catch | |
259 : let ps .= 'F' | |
260 : endtry | |
261 : try | |
262 : let l = [99] | |
263 : let ps .= 'p' | |
264 : catch | |
265 : let ps .= 'F' | |
266 : endtry | |
267 : $put =ps | |
268 : endfor | |
269 :endfor | |
6751 | 270 :" |
271 :" Unletting locked variables | |
272 :$put ='Unletting:' | |
273 :for depth in range(5) | |
274 : $put ='depth is ' . depth | |
275 : for u in range(3) | |
276 : unlet l | |
277 : let l = [0, [1, [2, 3]], {4: 5, 6: {7: 8}}] | |
278 : exe "lockvar " . depth . " l" | |
279 : if u == 1 | |
280 : exe "unlockvar l" | |
281 : elseif u == 2 | |
282 : exe "unlockvar " . depth . " l" | |
283 : endif | |
284 : 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]") | |
285 : $put =ps | |
286 : let ps = '' | |
287 : try | |
288 : unlet l[2]['6'][7] | |
289 : let ps .= 'p' | |
290 : catch | |
291 : let ps .= 'F' | |
292 : endtry | |
293 : try | |
294 : unlet l[2][6] | |
295 : let ps .= 'p' | |
296 : catch | |
297 : let ps .= 'F' | |
298 : endtry | |
299 : try | |
300 : unlet l[2] | |
301 : let ps .= 'p' | |
302 : catch | |
303 : let ps .= 'F' | |
304 : endtry | |
305 : try | |
306 : unlet l[1][1][0] | |
307 : let ps .= 'p' | |
308 : catch | |
309 : let ps .= 'F' | |
310 : endtry | |
311 : try | |
312 : unlet l[1][1] | |
313 : let ps .= 'p' | |
314 : catch | |
315 : let ps .= 'F' | |
316 : endtry | |
317 : try | |
318 : unlet l[1] | |
319 : let ps .= 'p' | |
320 : catch | |
321 : let ps .= 'F' | |
322 : endtry | |
323 : try | |
324 : unlet l | |
325 : let ps .= 'p' | |
326 : catch | |
327 : let ps .= 'F' | |
328 : endtry | |
329 : $put =ps | |
330 : endfor | |
331 :endfor | |
332 :" | |
333 :" Locked variables and :unlet or list / dict functions | |
334 :$put ='Locks and commands or functions:' | |
335 :" | |
336 :$put ='No :unlet after lock on dict:' | |
337 :unlet! d | |
338 :let d = {'a': 99, 'b': 100} | |
339 :lockvar 1 d | |
340 :try | |
341 : unlet d.a | |
342 : $put ='did :unlet' | |
343 :catch | |
344 : $put =v:exception[:16] | |
345 :endtry | |
346 :$put =string(d) | |
347 :" | |
348 :$put =':unlet after lock on dict item:' | |
349 :unlet! d | |
350 :let d = {'a': 99, 'b': 100} | |
351 :lockvar d.a | |
352 :try | |
353 : unlet d.a | |
354 : $put ='did :unlet' | |
355 :catch | |
356 : $put =v:exception[:16] | |
357 :endtry | |
358 :$put =string(d) | |
359 :" | |
360 :$put ='filter() after lock on dict item:' | |
361 :unlet! d | |
362 :let d = {'a': 99, 'b': 100} | |
363 :lockvar d.a | |
364 :try | |
365 : call filter(d, 'v:key != "a"') | |
366 : $put ='did filter()' | |
367 :catch | |
368 : $put =v:exception[:16] | |
369 :endtry | |
370 :$put =string(d) | |
371 :" | |
372 :$put ='map() after lock on dict:' | |
373 :unlet! d | |
374 :let d = {'a': 99, 'b': 100} | |
375 :lockvar 1 d | |
376 :try | |
377 : call map(d, 'v:val + 200') | |
378 : $put ='did map()' | |
379 :catch | |
380 : $put =v:exception[:16] | |
381 :endtry | |
382 :$put =string(d) | |
383 :" | |
384 :$put ='No extend() after lock on dict item:' | |
385 :unlet! d | |
386 :let d = {'a': 99, 'b': 100} | |
387 :lockvar d.a | |
388 :try | |
389 : $put =string(extend(d, {'a': 123})) | |
390 : $put ='did extend()' | |
391 :catch | |
392 : $put =v:exception[:14] | |
393 :endtry | |
394 :$put =string(d) | |
395 :" | |
396 :$put ='No remove() of write-protected scope-level variable:' | |
397 :fun! Tfunc(this_is_a_loooooooooong_parameter_name) | |
398 : try | |
399 : $put =string(remove(a:, 'this_is_a_loooooooooong_parameter_name')) | |
400 : $put ='did remove()' | |
401 : catch | |
402 : $put =v:exception[:14] | |
403 : endtry | |
404 :endfun | |
405 :call Tfunc('testval') | |
406 :" | |
407 :$put ='No extend() of write-protected scope-level variable:' | |
408 :fun! Tfunc(this_is_a_loooooooooong_parameter_name) | |
409 : try | |
410 : $put =string(extend(a:, {'this_is_a_loooooooooong_parameter_name': 1234})) | |
411 : $put ='did extend()' | |
412 : catch | |
413 : $put =v:exception[:14] | |
414 : endtry | |
415 :endfun | |
416 :call Tfunc('testval') | |
417 :" | |
418 :$put ='No :unlet of variable in locked scope:' | |
419 :let b:testvar = 123 | |
420 :lockvar 1 b: | |
421 :try | |
422 : unlet b:testvar | |
423 : $put ='b:testvar was :unlet: '. (!exists('b:testvar')) | |
424 :catch | |
425 : $put =v:exception[:16] | |
426 :endtry | |
427 :unlockvar 1 b: | |
428 :unlet! b:testvar | |
429 :" | |
6791 | 430 :$put ='No :let += of locked list variable:' |
431 :let l = ['a', 'b', 3] | |
432 :lockvar 1 l | |
433 :try | |
434 : let l += ['x'] | |
435 : $put ='did :let +=' | |
436 :catch | |
437 : $put =v:exception[:14] | |
438 :endtry | |
439 :$put =string(l) | |
440 :" | |
6166 | 441 :unlet l |
442 :let l = [1, 2, 3, 4] | |
443 :lockvar! l | |
444 :$put =string(l) | |
445 :unlockvar l[1] | |
446 :unlet l[0:1] | |
447 :$put =string(l) | |
448 :unlet l[1:2] | |
449 :$put =string(l) | |
450 :unlockvar l[1] | |
451 :let l[0:1] = [0, 1] | |
452 :$put =string(l) | |
453 :let l[1:2] = [0, 1] | |
454 :$put =string(l) | |
455 :unlet l | |
5604 | 456 :" :lockvar/islocked() triggering script autoloading |
457 :set rtp+=./sautest | |
458 :lockvar g:footest#x | |
459 :unlockvar g:footest#x | |
460 :$put ='locked g:footest#x:'.islocked('g:footest#x') | |
461 :$put ='exists g:footest#x:'.exists('g:footest#x') | |
462 :$put ='g:footest#x: '.g:footest#x | |
148 | 463 :" |
464 :" a:000 function argument | |
465 :" first the tests that should fail | |
466 :try | |
467 : let a:000 = [1, 2] | |
468 :catch | |
469 : $put ='caught a:000' | |
470 :endtry | |
471 :try | |
472 : let a:000[0] = 9 | |
473 :catch | |
474 : $put ='caught a:000[0]' | |
475 :endtry | |
476 :try | |
477 : let a:000[2] = [9, 10] | |
478 :catch | |
479 : $put ='caught a:000[2]' | |
480 :endtry | |
481 :try | |
482 : let a:000[3] = {9: 10} | |
483 :catch | |
484 : $put ='caught a:000[3]' | |
485 :endtry | |
486 :" now the tests that should pass | |
487 :try | |
488 : let a:000[2][1] = 9 | |
489 : call extend(a:000[2], [5, 6]) | |
490 : let a:000[3][5] = 8 | |
491 : let a:000[3]['a'] = 12 | |
492 : $put =string(a:000) | |
493 :catch | |
494 : $put ='caught ' . v:exception | |
495 :endtry | |
496 :" | |
5747 | 497 :" reverse(), sort(), uniq() |
498 :let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5] | |
499 :$put =string(uniq(copy(l))) | |
164 | 500 :$put =string(reverse(l)) |
501 :$put =string(reverse(reverse(l))) | |
502 :$put =string(sort(l)) | |
503 :$put =string(reverse(sort(l))) | |
504 :$put =string(sort(reverse(sort(l)))) | |
5747 | 505 :$put =string(uniq(sort(l))) |
6022 | 506 :let l=[7, 9, 'one', 18, 12, 22, 'two', 10.0e-16, -1, 'three', 0xff, 0.22, 'four'] |
6014 | 507 :$put =string(sort(copy(l), 'n')) |
6022 | 508 :let l=[7, 9, 18, 12, 22, 10.0e-16, -1, 0xff, 0, -0, 0.22, 'bar', 'BAR', 'Bar', 'Foo', 'FOO', 'foo', 'FOOBAR', {}, []] |
6001 | 509 :$put =string(sort(copy(l), 1)) |
510 :$put =string(sort(copy(l), 'i')) | |
511 :$put =string(sort(copy(l))) | |
164 | 512 :" |
283 | 513 :" splitting a string to a List |
514 :$put =string(split(' aa bb ')) | |
515 :$put =string(split(' aa bb ', '\W\+', 0)) | |
516 :$put =string(split(' aa bb ', '\W\+', 1)) | |
517 :$put =string(split(' aa bb ', '\W', 1)) | |
518 :$put =string(split(':aa::bb:', ':', 0)) | |
519 :$put =string(split(':aa::bb:', ':', 1)) | |
520 :$put =string(split('aa,,bb, cc,', ',\s*', 1)) | |
294 | 521 :$put =string(split('abc', '\zs')) |
522 :$put =string(split('abc', '\zs', 1)) | |
283 | 523 :" |
1125 | 524 :" compare recursively linked list and dict |
525 :let l = [1, 2, 3, 4] | |
526 :let d = {'1': 1, '2': l, '3': 3} | |
527 :let l[1] = d | |
528 :$put =(l == l) | |
529 :$put =(d == d) | |
530 :$put =(l != deepcopy(l)) | |
531 :$put =(d != deepcopy(d)) | |
2634 | 532 :" |
533 :" compare complex recursively linked list and dict | |
534 :let l = [] | |
535 :call add(l, l) | |
536 :let dict4 = {"l": l} | |
537 :call add(dict4.l, dict4) | |
538 :let lcopy = deepcopy(l) | |
539 :let dict4copy = deepcopy(dict4) | |
540 :$put =(l == lcopy) | |
541 :$put =(dict4 == dict4copy) | |
3508 | 542 :" |
543 :" Pass the same List to extend() | |
544 :let l = [1, 2, 3, 4, 5] | |
545 :call extend(l, l) | |
546 :$put =string(l) | |
547 :" | |
548 :" Pass the same Dict to extend() | |
549 :let d = { 'a': {'b': 'B'}} | |
550 :call extend(d, d) | |
551 :$put =string(d) | |
552 :" | |
553 :" Pass the same Dict to extend() with "error" | |
554 :try | |
555 : call extend(d, d, "error") | |
556 :catch | |
557 : $put =v:exception[:15] . v:exception[-1:-1] | |
558 :endtry | |
559 :$put =string(d) | |
6422 | 560 :" |
561 :" test for range assign | |
562 :let l = [0] | |
563 :let l[:] = [1, 2] | |
564 :$put =string(l) | |
119 | 565 :endfun |
2634 | 566 :" |
148 | 567 :call Test(1, 2, [3, 4], {5: 6}) " This may take a while |
119 | 568 :" |
1405 | 569 :delfunc Test |
570 :unlet dict | |
571 :call garbagecollect(1) | |
572 :" | |
3774 | 573 :" test for patch 7.3.637 |
574 :let a = 'No error caught' | |
575 :try|foldopen|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry | |
576 o=a :" | |
577 :lang C | |
578 :redir => a | |
579 :try|foobar|catch|let a = matchstr(v:exception,'^[^ ]*')|endtry | |
580 :redir END | |
581 o=a :" | |
582 :" | |
119 | 583 :/^start:/,$wq! test.out |
584 ENDTEST | |
585 | |
586 start: |