annotate src/testdir/test_filter_map.vim @ 9442:4fe3772969cf v7.4.2002

commit https://github.com/vim/vim/commit/a06ec8f345eabb66e5b7d7c0192cfebdde63115d Author: Bram Moolenaar <Bram@vim.org> Date: Fri Jul 8 20:11:07 2016 +0200 patch 7.4.2002 Problem: Crash when passing number to filter() or map(). Solution: Convert to a string. (Ozaki Kiichi)
author Christian Brabandt <cb@256bit.org>
date Fri, 08 Jul 2016 20:15:06 +0200
parents cbf052ccb120
children 051937ebaf22
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
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 " list with expression string
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 func Test_filter_map_list_expr_string()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 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
7 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
8 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
9
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 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
12 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
13 call assert_equal([9, 9, 9, 9], map([1, 2, 3, 4], 9))
9416
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
15
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16 " dict with expression string
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 func Test_filter_map_dict_expr_string()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18 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
19
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
20 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
21 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
22 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
23 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
24
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
25 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
26 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
27 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
28 call assert_equal({"foo": 9, "bar": 9, "baz": 9}, map(copy(dict), 9))
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 " list with funcref
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
32 func Test_filter_map_list_expr_funcref()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
33 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
34 func! s:filter1(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
35 return a:val > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
36 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
37 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
38
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
39 func! s:filter2(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
40 return a:index > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
41 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
42 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
43
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
44 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 func! s:filter3(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 return a:val * 2
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
47 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 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
49
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50 func! s:filter4(index, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 return a:index * 2
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 call assert_equal([0, 2, 4, 6], map([1, 2, 3, 4], function('s:filter4')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
54 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 " dict with funcref
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57 func Test_filter_map_dict_expr_funcref()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
58 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
59
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
60 " filter()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
61 func! s:filter1(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
62 return a:val > 1
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
63 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
64 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
65
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
66 func! s:filter2(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
67 return a:key > "bar"
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
68 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
69 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
70
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
71 " map()
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
72 func! s:filter3(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
73 return a:val * 2
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
74 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
75 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
76
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
77 func! s:filter4(key, val) abort
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
78 return a:key[0]
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
79 endfunc
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
80 call assert_equal({"foo": "f", "bar": "b", "baz": "b"}, map(copy(dict), function('s:filter4')))
cbf052ccb120 commit https://github.com/vim/vim/commit/b33c7eb5b813cb631b2b0ca5c4029e1788a09bde
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
81 endfunc