annotate src/testdir/test_filter_map.vim @ 35115:d401437829ee default tip

Added tag v9.1.0391 for changeset 219a779511feb93e76ff0f74718c609ebad6c7f6
author Christian Brabandt <cb@256bit.org>
date Fri, 03 May 2024 18:30:17 +0200
parents da670b1549b3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1 " Test filter() and map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2
27457
4c16acb2525f patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 26907
diff changeset
3 import './vim9.vim' as v9
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
4
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 " list with expression string
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 func Test_filter_map_list_expr_string()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 call assert_equal([2, 3, 4], filter([1, 2, 3, 4], 'v:val > 1'))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9 call assert_equal([3, 4], filter([1, 2, 3, 4], 'v:key > 1'))
9442
4fe3772969cf commit https://github.com/vim/vim/commit/a06ec8f345eabb66e5b7d7c0192cfebdde63115d
Christian Brabandt <cb@256bit.org>
parents: 9416
diff changeset
10 call assert_equal([], filter([1, 2, 3, 4], 0))
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
13 call assert_equal([2, 4, 6, 8], map([1, 2, 3, 4], 'v:val * 2'))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 call assert_equal([0, 2, 4, 6], map([1, 2, 3, 4], 'v:key * 2'))
9442
4fe3772969cf commit https://github.com/vim/vim/commit/a06ec8f345eabb66e5b7d7c0192cfebdde63115d
Christian Brabandt <cb@256bit.org>
parents: 9416
diff changeset
15 call assert_equal([9, 9, 9, 9], map([1, 2, 3, 4], 9))
21620
46956b6811a1 patch 8.2.1360: stray error for white space after expression
Bram Moolenaar <Bram@vim.org>
parents: 20550
diff changeset
16 call assert_equal([7, 7, 7], map([1, 2, 3], ' 7 '))
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
17
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
18 " foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
19 let list01 = [1, 2, 3, 4]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
20 let list02 = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
21 call assert_equal([1, 2, 3, 4], foreach(list01, 'call add(list02, v:val * 2)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
22 call assert_equal([2, 4, 6, 8], list02)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
23 let list02 = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
24 call assert_equal([1, 2, 3, 4], foreach(list01, 'call add(list02, v:key * 2)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
25 call assert_equal([0, 2, 4, 6], list02)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
26 let list02 = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
27 call assert_equal([1, 2, 3, 4], foreach(list01, 'call add(list02, 9)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
28 call assert_equal([9, 9, 9, 9], list02)
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
29 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
30
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
31 " dict with expression string
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 func Test_filter_map_dict_expr_string()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 let dict = {"foo": 1, "bar": 2, "baz": 3}
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 call assert_equal({"bar": 2, "baz": 3}, filter(copy(dict), 'v:val > 1'))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 call assert_equal({"foo": 1, "baz": 3}, filter(copy(dict), 'v:key > "bar"'))
9442
4fe3772969cf commit https://github.com/vim/vim/commit/a06ec8f345eabb66e5b7d7c0192cfebdde63115d
Christian Brabandt <cb@256bit.org>
parents: 9416
diff changeset
38 call assert_equal({}, filter(copy(dict), 0))
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 call assert_equal({"foo": 2, "bar": 4, "baz": 6}, map(copy(dict), 'v:val * 2'))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 call assert_equal({"foo": "f", "bar": "b", "baz": "b"}, map(copy(dict), 'v:key[0]'))
9442
4fe3772969cf commit https://github.com/vim/vim/commit/a06ec8f345eabb66e5b7d7c0192cfebdde63115d
Christian Brabandt <cb@256bit.org>
parents: 9416
diff changeset
43 call assert_equal({"foo": 9, "bar": 9, "baz": 9}, map(copy(dict), 9))
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
44
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
45 " foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
46 let dict01 = {}
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
47 call assert_equal(dict, foreach(copy(dict), 'let dict01[v:key] = v:val * 2'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
48 call assert_equal({"foo": 2, "bar": 4, "baz": 6}, dict01)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
49 let dict01 = {}
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
50 call assert_equal(dict, foreach(copy(dict), 'let dict01[v:key] = v:key[0]'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
51 call assert_equal({"foo": "f", "bar": "b", "baz": "b"}, dict01)
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
53
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 " list with funcref
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55 func Test_filter_map_list_expr_funcref()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 func! s:filter1(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58 return a:val > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
59 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 call assert_equal([2, 3, 4], filter([1, 2, 3, 4], function('s:filter1')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 func! s:filter2(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
63 return a:index > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
65 call assert_equal([3, 4], filter([1, 2, 3, 4], function('s:filter2')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 func! s:filter3(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 return a:val * 2
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
70 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71 call assert_equal([2, 4, 6, 8], map([1, 2, 3, 4], function('s:filter3')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 func! s:filter4(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 return a:index * 2
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
76 call assert_equal([0, 2, 4, 6], map([1, 2, 3, 4], function('s:filter4')))
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
77
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
78 " foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
79 func! s:foreach1(index, val) abort
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
80 call add(g:test_variable, a:val + 1)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
81 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
82 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
83 let g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
84 call assert_equal([0, 1, 2, 3, 4], foreach(range(5), function('s:foreach1')))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
85 call assert_equal([1, 2, 3, 4, 5], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
86 call remove(g:, 'test_variable')
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
87 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
88
17936
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
89 func Test_filter_map_nested()
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
90 let x = {"x":10}
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
91 let r = map(range(2), 'filter(copy(x), "1")')
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
92 call assert_equal([x, x], r)
17944
745c02392844 patch 8.1.1968: crash when using nested map()
Bram Moolenaar <Bram@vim.org>
parents: 17936
diff changeset
93
745c02392844 patch 8.1.1968: crash when using nested map()
Bram Moolenaar <Bram@vim.org>
parents: 17936
diff changeset
94 let r = map(copy(x), 'filter(copy(x), "1")')
745c02392844 patch 8.1.1968: crash when using nested map()
Bram Moolenaar <Bram@vim.org>
parents: 17936
diff changeset
95 call assert_equal({"x": x}, r)
17936
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
96 endfunc
7f3283683d97 patch 8.1.1964: crash when using nested map() and filter()
Bram Moolenaar <Bram@vim.org>
parents: 15478
diff changeset
97
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
98 " dict with funcref
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
99 func Test_filter_map_dict_expr_funcref()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
100 let dict = {"foo": 1, "bar": 2, "baz": 3}
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
101
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
102 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
103 func! s:filter1(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
104 return a:val > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
105 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
106 call assert_equal({"bar": 2, "baz": 3}, filter(copy(dict), function('s:filter1')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
107
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
108 func! s:filter2(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
109 return a:key > "bar"
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
110 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
111 call assert_equal({"foo": 1, "baz": 3}, filter(copy(dict), function('s:filter2')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
112
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
113 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
114 func! s:filter3(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
115 return a:val * 2
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
116 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
117 call assert_equal({"foo": 2, "bar": 4, "baz": 6}, map(copy(dict), function('s:filter3')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
118
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
119 func! s:filter4(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
120 return a:key[0]
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
121 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
122 call assert_equal({"foo": "f", "bar": "b", "baz": "b"}, map(copy(dict), function('s:filter4')))
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
123
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
124 " foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
125 func! s:foreach1(key, val) abort
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
126 call extend(g:test_variable, {a:key: a:val * 2})
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
127 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
128 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
129 let g:test_variable = {}
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
130 call assert_equal(dict, foreach(copy(dict), function('s:foreach1')))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
131 call assert_equal({"foo": 2, "bar": 4, "baz": 6}, g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
132 call remove(g:, 'test_variable')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
133 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
134
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
135 func Test_map_filter_locked()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
136 let list01 = [1, 2, 3, 4]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
137 lockvar 1 list01
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
138 call assert_fails('call filter(list01, "v:val > 1")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
139 call assert_equal([2, 4, 6, 8], map(list01, 'v:val * 2'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
140 call assert_equal([1, 2, 3, 4], map(list01, 'v:val / 2'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
141 call assert_equal([2, 4, 6, 8], mapnew(list01, 'v:val * 2'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
142 let g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
143 call assert_equal([1, 2, 3, 4], foreach(list01, 'call add(g:test_variable, v:val * 2)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
144 call remove(g:, 'test_variable')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
145 call assert_fails('call filter(list01, "v:val > 1")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
146 unlockvar 1 list01
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
147 lockvar! list01
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
148 call assert_fails('call filter(list01, "v:val > 1")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
149 call assert_fails('call map(list01, "v:val * 2")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
150 call assert_equal([2, 4, 6, 8], mapnew(list01, 'v:val * 2'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
151 let g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
152 call assert_equal([1, 2, 3, 4], foreach(list01, 'call add(g:test_variable, v:val * 2)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
153 call assert_fails('call foreach(list01, "let list01[0] = -1")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
154 call assert_fails('call filter(list01, "v:val > 1")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
155 call remove(g:, 'test_variable')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
156 unlockvar! list01
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
157 endfunc
15478
051937ebaf22 patch 8.1.0747: map() with a bad expression doesn't give an error
Bram Moolenaar <Bram@vim.org>
parents: 9442
diff changeset
158
20109
e82996ad131f patch 8.2.0610: some tests are still old style
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
159 func Test_map_filter_fails()
15478
051937ebaf22 patch 8.1.0747: map() with a bad expression doesn't give an error
Bram Moolenaar <Bram@vim.org>
parents: 9442
diff changeset
160 call assert_fails('call map([1], "42 +")', 'E15:')
051937ebaf22 patch 8.1.0747: map() with a bad expression doesn't give an error
Bram Moolenaar <Bram@vim.org>
parents: 9442
diff changeset
161 call assert_fails('call filter([1], "42 +")', 'E15:')
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
162 call assert_fails('call foreach([1], "let a = }")', 'E15:')
20109
e82996ad131f patch 8.2.0610: some tests are still old style
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
163 call assert_fails("let l = filter([1, 2, 3], '{}')", 'E728:')
e82996ad131f patch 8.2.0610: some tests are still old style
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
164 call assert_fails("let l = filter({'k' : 10}, '{}')", 'E728:')
20128
0b35a7ffceb2 patch 8.2.0619: null dict is not handled like an empty dict
Bram Moolenaar <Bram@vim.org>
parents: 20109
diff changeset
165 call assert_fails("let l = filter([1, 2], {})", 'E731:')
22608
f140b9036aa5 patch 8.2.1852: map() returing zero for NULL list is unexpected
Bram Moolenaar <Bram@vim.org>
parents: 21620
diff changeset
166 call assert_equal(test_null_list(), filter(test_null_list(), 0))
f140b9036aa5 patch 8.2.1852: map() returing zero for NULL list is unexpected
Bram Moolenaar <Bram@vim.org>
parents: 21620
diff changeset
167 call assert_equal(test_null_dict(), filter(test_null_dict(), 0))
f140b9036aa5 patch 8.2.1852: map() returing zero for NULL list is unexpected
Bram Moolenaar <Bram@vim.org>
parents: 21620
diff changeset
168 call assert_equal(test_null_list(), map(test_null_list(), '"> " .. v:val'))
f140b9036aa5 patch 8.2.1852: map() returing zero for NULL list is unexpected
Bram Moolenaar <Bram@vim.org>
parents: 21620
diff changeset
169 call assert_equal(test_null_dict(), map(test_null_dict(), '"> " .. v:val'))
20156
49694eceaa55 patch 8.2.0633: crash when using null partial in filter()
Bram Moolenaar <Bram@vim.org>
parents: 20128
diff changeset
170 call assert_equal([1, 2, 3], filter([1, 2, 3], test_null_function()))
49694eceaa55 patch 8.2.0633: crash when using null partial in filter()
Bram Moolenaar <Bram@vim.org>
parents: 20128
diff changeset
171 call assert_fails("let l = filter([1, 2], function('min'))", 'E118:')
49694eceaa55 patch 8.2.0633: crash when using null partial in filter()
Bram Moolenaar <Bram@vim.org>
parents: 20128
diff changeset
172 call assert_equal([1, 2, 3], filter([1, 2, 3], test_null_partial()))
49694eceaa55 patch 8.2.0633: crash when using null partial in filter()
Bram Moolenaar <Bram@vim.org>
parents: 20128
diff changeset
173 call assert_fails("let l = filter([1, 2], {a, b, c -> 1})", 'E119:')
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
174 call assert_fails('call foreach([1], "xyzzy")', 'E492:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
175 call assert_fails('call foreach([1], "let a = foo")', 'E121:')
15478
051937ebaf22 patch 8.1.0747: map() with a bad expression doesn't give an error
Bram Moolenaar <Bram@vim.org>
parents: 9442
diff changeset
176 endfunc
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
177
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
178 func Test_map_and_modify()
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
179 let l = ["abc"]
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
180 " cannot change the list halfway a map()
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
181 call assert_fails('call map(l, "remove(l, 0)[0]")', 'E741:')
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
182
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
183 let d = #{a: 1, b: 2, c: 3}
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
184 call assert_fails('call map(d, "remove(d, v:key)[0]")', 'E741:')
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
185 call assert_fails('echo map(d, {k,v -> remove(d, k)})', 'E741:')
32170
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
186
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
187 let b = 0z1234
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
188 call assert_fails('call filter(b, "remove(b, 0)")', 'E741:')
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
189 endfunc
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
190
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
191 func Test_filter_and_modify()
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
192 let l = [0]
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
193 " cannot change the list halfway thru filter()
32170
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
194 call assert_fails('call filter(l, "remove(l, 0)")', 'E741:')
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
195
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
196 let d = #{a: 0, b: 0, c: 0}
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
197 call assert_fails('call filter(d, "remove(d, v:key)")', 'E741:')
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
198
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
199 let b = 0z1234
5848a0867b26 patch 9.0.1416: crash when collection is modified when using filter()
Bram Moolenaar <Bram@vim.org>
parents: 27457
diff changeset
200 call assert_fails('call filter(b, "remove(b, 0)")', 'E741:')
19233
04c164c971a3 patch 8.2.0175: crash when removing list element in map()
Bram Moolenaar <Bram@vim.org>
parents: 17944
diff changeset
201 endfunc
20109
e82996ad131f patch 8.2.0610: some tests are still old style
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
202
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
203 func Test_foreach_and_modify()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
204 let l = [0]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
205 " cannot change the list halfway thru foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
206 call assert_fails('call foreach(l, "let a = remove(l, 0)")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
207
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
208 let d = #{a: 0, b: 0, c: 0}
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
209 call assert_fails('call foreach(d, "let a = remove(d, v:key)")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
210
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
211 let b = 0z1234
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
212 call assert_fails('call foreach(b, "let a = remove(b, 0)")', 'E741:')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
213 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
214
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
215 func Test_mapnew_dict()
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
216 let din = #{one: 1, two: 2}
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
217 let dout = mapnew(din, {k, v -> string(v)})
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
218 call assert_equal(#{one: 1, two: 2}, din)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
219 call assert_equal(#{one: '1', two: '2'}, dout)
23058
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
220
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
221 const dconst = #{one: 1, two: 2, three: 3}
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
222 call assert_equal(#{one: 2, two: 3, three: 4}, mapnew(dconst, {_, v -> v + 1}))
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
223 endfunc
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
224
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
225 func Test_mapnew_list()
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
226 let lin = [1, 2, 3]
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
227 let lout = mapnew(lin, {k, v -> string(v)})
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
228 call assert_equal([1, 2, 3], lin)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
229 call assert_equal(['1', '2', '3'], lout)
23058
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
230
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
231 const lconst = [1, 2, 3]
77f181975381 patch 8.2.2075: error for const argument to mapnew()
Bram Moolenaar <Bram@vim.org>
parents: 22844
diff changeset
232 call assert_equal([2, 3, 4], mapnew(lconst, {_, v -> v + 1}))
22844
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
233 endfunc
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
234
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
235 func Test_mapnew_blob()
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
236 let bin = 0z123456
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
237 let bout = mapnew(bin, {k, v -> k == 1 ? 0x99 : v})
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
238 call assert_equal(0z123456, bin)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
239 call assert_equal(0z129956, bout)
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
240 endfunc
36fc73078bce patch 8.2.1969: Vim9: map() may change the list or dict item type
Bram Moolenaar <Bram@vim.org>
parents: 22608
diff changeset
241
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
242 func Test_foreach_blob()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
243 let lines =<< trim END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
244 LET g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
245 call assert_equal(0z0001020304, foreach(0z0001020304, 'call add(g:test_variable, v:val)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
246 call assert_equal([0, 1, 2, 3, 4], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
247 END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
248 call v9.CheckLegacyAndVim9Success(lines)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
249
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
250 func! s:foreach1(index, val) abort
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
251 call add(g:test_variable, a:val)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
252 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
253 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
254 let g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
255 call assert_equal(0z0001020304, foreach(0z0001020304, function('s:foreach1')))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
256 call assert_equal([0, 1, 2, 3, 4], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
257
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
258 let lines =<< trim END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
259 def Foreach1(_, val: any): list<number>
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
260 add(g:test_variable, val)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
261 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
262 enddef
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
263 g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
264 assert_equal(0z0001020304, foreach(0z0001020304, Foreach1))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
265 assert_equal([0, 1, 2, 3, 4], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
266 END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
267 call v9.CheckDefSuccess(lines)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
268
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
269 call remove(g:, 'test_variable')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
270 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
271
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
272 " Test for using map(), filter() and mapnew() with a string
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
273 func Test_filter_map_string()
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
274 " filter()
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
275 let lines =<< trim END
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
276 VAR s = "abc"
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
277 call filter(s, '"b" != v:val')
26907
6f43253463cc patch 8.2.3982: some lines of code not covered by tests
Bram Moolenaar <Bram@vim.org>
parents: 26759
diff changeset
278 call assert_equal('abc', s)
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
279 call assert_equal('ac', filter('abc', '"b" != v:val'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
280 call assert_equal('あいうえお', filter('あxいxうxえxお', '"x" != v:val'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
281 call assert_equal('あa😊💕💕b💕', filter('あxax😊x💕💕b💕x', '"x" != v:val'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
282 call assert_equal('xxxx', filter('あxax😊x💕💕b💕x', '"x" == v:val'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
283 VAR t = "%),:;>?]}’”†‡…‰,‱‼⁇⁈⁉℃℉,、。〉》」,』】〕〗〙〛,!),.:,;?,]}"
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
284 VAR u = "%):;>?]}’”†‡…‰‱‼⁇⁈⁉℃℉、。〉》」』】〕〗〙〛!),.:;?]}"
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
285 call assert_equal(u, filter(t, '"," != v:val'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
286 call assert_equal('', filter('abc', '0'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
287 call assert_equal('ac', filter('abc', LSTART i, x LMIDDLE "b" != x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
288 call assert_equal('あいうえお', filter('あxいxうxえxお', LSTART i, x LMIDDLE "x" != x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
289 call assert_equal('', filter('abc', LSTART i, x LMIDDLE v:false LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
290 call assert_equal('', filter('', "v:val == 'a'"))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
291 call assert_equal('', filter(test_null_string(), "v:val == 'a'"))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
292 END
27457
4c16acb2525f patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 26907
diff changeset
293 call v9.CheckLegacyAndVim9Success(lines)
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
294
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
295 " map()
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
296 let lines =<< trim END
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
297 VAR s = "abc"
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
298 call map(s, 'nr2char(char2nr(v:val) + 2)')
26907
6f43253463cc patch 8.2.3982: some lines of code not covered by tests
Bram Moolenaar <Bram@vim.org>
parents: 26759
diff changeset
299 call assert_equal('abc', s)
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
300 call assert_equal('cde', map('abc', 'nr2char(char2nr(v:val) + 2)'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
301 call assert_equal('[あ][i][う][え][お]', map('あiうえお', '"[" .. v:val .. "]"'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
302 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', map('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
303 call assert_equal('', map('abc', '""'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
304 call assert_equal('cde', map('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
305 call assert_equal('[あ][i][う][え][お]', map('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
306 call assert_equal('', map('abc', LSTART i, x LMIDDLE '' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
307 call assert_equal('', map('', "v:val == 'a'"))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
308 call assert_equal('', map(test_null_string(), "v:val == 'a'"))
26684
2126feddeda6 patch 8.2.3871: list.c contains code for dict and blob
Bram Moolenaar <Bram@vim.org>
parents: 26585
diff changeset
309 call assert_fails('echo map("abc", "10")', 'E928:')
26759
31c23760d590 patch 8.2.3908: cannot use a script-local function for 'foldtext'
Bram Moolenaar <Bram@vim.org>
parents: 26684
diff changeset
310 call assert_fails('echo map("abc", "a10")', 'E121:')
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
311 END
27457
4c16acb2525f patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 26907
diff changeset
312 call v9.CheckLegacyAndVim9Success(lines)
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
313
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
314 " mapnew()
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
315 let lines =<< trim END
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
316 VAR s = "abc"
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
317 call mapnew(s, 'nr2char(char2nr(v:val) + 2)')
26907
6f43253463cc patch 8.2.3982: some lines of code not covered by tests
Bram Moolenaar <Bram@vim.org>
parents: 26759
diff changeset
318 call assert_equal('abc', s)
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
319 call assert_equal('cde', mapnew('abc', 'nr2char(char2nr(v:val) + 2)'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
320 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', '"[" .. v:val .. "]"'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
321 call assert_equal('[あ][a][😊][,][‱][‼][⁇][⁈][⁉][💕][b][💕][c][💕]', mapnew('あa😊,‱‼⁇⁈⁉💕b💕c💕', '"[" .. v:val .. "]"'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
322 call assert_equal('', mapnew('abc', '""'))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
323 call assert_equal('cde', mapnew('abc', LSTART i, x LMIDDLE nr2char(char2nr(x) + 2) LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
324 call assert_equal('[あ][i][う][え][お]', mapnew('あiうえお', LSTART i, x LMIDDLE '[' .. x .. ']' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
325 call assert_equal('', mapnew('abc', LSTART i, x LMIDDLE '' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
326 call assert_equal('', mapnew('', "v:val == 'a'"))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
327 call assert_equal('', mapnew(test_null_string(), "v:val == 'a'"))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
328 END
27457
4c16acb2525f patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 26907
diff changeset
329 call v9.CheckLegacyAndVim9Success(lines)
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
330
34126
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
331 " foreach()
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
332 let lines =<< trim END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
333 VAR s = "abc"
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
334 LET g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
335 call assert_equal(s, foreach(s, 'call add(g:test_variable, v:val)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
336 call assert_equal(['a', 'b', 'c'], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
337 LET g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
338 LET s = 'あiうえお'
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
339 call assert_equal(s, foreach(s, 'call add(g:test_variable, v:val)'))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
340 call assert_equal(['あ', 'i', 'う', 'え', 'お'], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
341 END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
342 call v9.CheckLegacyAndVim9Success(lines)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
343 func! s:foreach1(index, val) abort
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
344 call add(g:test_variable, a:val)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
345 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
346 endfunc
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
347 let g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
348 call assert_equal('abcd', foreach('abcd', function('s:foreach1')))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
349 call assert_equal(['a', 'b', 'c', 'd'], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
350 let lines =<< trim END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
351 def Foreach1(_, val: string): list<number>
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
352 add(g:test_variable, val)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
353 return [ 11, 12, 13, 14 ]
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
354 enddef
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
355 g:test_variable = []
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
356 assert_equal('abcd', foreach('abcd', Foreach1))
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
357 assert_equal(['a', 'b', 'c', 'd'], g:test_variable)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
358 END
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
359 call v9.CheckDefSuccess(lines)
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
360 call remove(g:, 'test_variable')
da670b1549b3 patch 9.1.0027: Vim is missing a foreach() func
Christian Brabandt <cb@256bit.org>
parents: 32170
diff changeset
361
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
362 let lines =<< trim END
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
363 #" map() and filter()
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
364 call assert_equal('[あ][⁈][a][😊][⁉][💕][💕][b][💕]', map(filter('あx⁈ax😊x⁉💕💕b💕x', '"x" != v:val'), '"[" .. v:val .. "]"'))
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
365
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
366 #" patterns-composing(\Z)
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
367 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
368 call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ '\Za' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
369 call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
370 call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
371 call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0x0960) ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
372 call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ '\Za' ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
373 call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xc5) ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
374 call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ '\Z' .. nr2char(0xf6) ? '@' : x LEND))
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
375
26585
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
376 #" patterns-composing(\%C)
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
377 call assert_equal('ॠॠ', filter('ऊॠॡ,ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
378 call assert_equal('àà', filter('càt,càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
379 call assert_equal('ÅÅ', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
380 call assert_equal('öö', filter('Åström,Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
381 call assert_equal('ऊ@ॡ', map('ऊॠॡ', LSTART i, x LMIDDLE x =~ nr2char(0x0960) .. '\%C' ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
382 call assert_equal('c@t', map('càt', LSTART i, x LMIDDLE x =~ 'a' .. '\%C' ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
383 call assert_equal('@ström', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xc5) .. '\%C' ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
384 call assert_equal('Åstr@m', map('Åström', LSTART i, x LMIDDLE x =~ nr2char(0xf6) .. '\%C' ? '@' : x LEND))
0d2a709e2ff0 patch 8.2.3822: leaking memory in map() and filter(), no string in Vim9
Bram Moolenaar <Bram@vim.org>
parents: 26578
diff changeset
385 END
27457
4c16acb2525f patch 8.2.4257: Vim9: finding global function without g: prefix inconsistent
Bram Moolenaar <Bram@vim.org>
parents: 26907
diff changeset
386 call v9.CheckLegacyAndVim9Success(lines)
26578
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
387 endfunc
06693d1afc48 patch 8.2.3818: cannot filter or map characters in a string
Bram Moolenaar <Bram@vim.org>
parents: 23058
diff changeset
388
20109
e82996ad131f patch 8.2.0610: some tests are still old style
Bram Moolenaar <Bram@vim.org>
parents: 19233
diff changeset
389 " vim: shiftwidth=2 sts=2 expandtab