annotate src/testdir/test_method.vim @ 17646:e5397617d6ca v8.1.1820

patch 8.1.1820: using expr->FuncRef() does not work commit https://github.com/vim/vim/commit/761fdf01c6e307c448cec2684f8b315ba6d1f454 Author: Bram Moolenaar <Bram@vim.org> Date: Mon Aug 5 23:10:16 2019 +0200 patch 8.1.1820: using expr->FuncRef() does not work Problem: Using expr->FuncRef() does not work. Solution: Make FuncRef work as a method.
author Bram Moolenaar <Bram@vim.org>
date Mon, 05 Aug 2019 23:15:05 +0200
parents 4c7097a980a5
children 511a03fcdc78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
1 " Tests for ->method()
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
2
17646
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
3 func Test_list_method()
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
4 let l = [1, 2, 3]
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
5 call assert_equal([1, 2, 3, 4], [1, 2, 3]->add(4))
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
6 eval l->assert_equal(l)
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
7 eval l->assert_equal(l, 'wrong')
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
8 eval l->assert_notequal([3, 2, 1])
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
9 eval l->assert_notequal([3, 2, 1], 'wrong')
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
10 call assert_equal(l, l->copy())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
11 call assert_equal(1, l->count(2))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
12 call assert_false(l->empty())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
13 call assert_true([]->empty())
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
14 call assert_equal(579, ['123', '+', '456']->join()->eval())
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
15 call assert_equal([1, 2, 3, 4, 5], [1, 2, 3]->extend([4, 5]))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
16 call assert_equal([1, 3], [1, 2, 3]->filter('v:val != 2'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
17 call assert_equal(2, l->get(1))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
18 call assert_equal(1, l->index(2))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
19 call assert_equal([0, 1, 2, 3], [1, 2, 3]->insert(0))
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
20 call assert_fails('eval l->items()', 'E715:')
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
21 call assert_equal('1 2 3', l->join())
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
22 call assert_fails('eval l->keys()', 'E715:')
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
23 call assert_equal(3, l->len())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
24 call assert_equal([2, 3, 4], [1, 2, 3]->map('v:val + 1'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
25 call assert_equal(3, l->max())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
26 call assert_equal(1, l->min())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
27 call assert_equal(2, [1, 2, 3]->remove(1))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
28 call assert_equal([1, 2, 3, 1, 2, 3], l->repeat(2))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
29 call assert_equal([3, 2, 1], [1, 2, 3]->reverse())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
30 call assert_equal([1, 2, 3, 4], [4, 2, 3, 1]->sort())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
31 call assert_equal('[1, 2, 3]', l->string())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
32 call assert_equal(v:t_list, l->type())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
33 call assert_equal([1, 2, 3], [1, 1, 2, 3, 3]->uniq())
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
34 call assert_fails('eval l->values()', 'E715:')
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
35 endfunc
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
36
17646
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
37 func Test_dict_method()
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
38 let d = #{one: 1, two: 2, three: 3}
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
39
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
40 call assert_equal(d, d->copy())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
41 call assert_equal(1, d->count(2))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
42 call assert_false(d->empty())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
43 call assert_true({}->empty())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
44 call assert_equal(#{one: 1, two: 2, three: 3, four: 4}, d->extend(#{four: 4}))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
45 call assert_equal(#{one: 1, two: 2, three: 3}, d->filter('v:val != 4'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
46 call assert_equal(2, d->get('two'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
47 call assert_fails("let x = d->index(2)", 'E897:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
48 call assert_fails("let x = d->insert(0)", 'E899:')
17624
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
49 call assert_true(d->has_key('two'))
17612
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
50 call assert_equal([['one', 1], ['two', 2], ['three', 3]], d->items())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
51 call assert_fails("let x = d->join()", 'E714:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
52 call assert_equal(['one', 'two', 'three'], d->keys())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
53 call assert_equal(3, d->len())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
54 call assert_equal(#{one: 2, two: 3, three: 4}, d->map('v:val + 1'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
55 call assert_equal(#{one: 1, two: 2, three: 3}, d->map('v:val - 1'))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
56 call assert_equal(3, d->max())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
57 call assert_equal(1, d->min())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
58 call assert_equal(2, d->remove("two"))
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
59 let d.two = 2
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
60 call assert_fails('let x = d->repeat(2)', 'E731:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
61 call assert_fails('let x = d->reverse()', 'E899:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
62 call assert_fails('let x = d->sort()', 'E686:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
63 call assert_equal("{'one': 1, 'two': 2, 'three': 3}", d->string())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
64 call assert_equal(v:t_dict, d->type())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
65 call assert_fails('let x = d->uniq()', 'E686:')
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
66 call assert_equal([1, 2, 3], d->values())
e259d11e2900 patch 8.1.1803: all builtin functions are global
Bram Moolenaar <Bram@vim.org>
parents:
diff changeset
67 endfunc
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
68
17646
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
69 func Test_string_method()
17624
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
70 call assert_equal(['1', '2', '3'], '1 2 3'->split())
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
71 call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)}))
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
72 call assert_equal([65, 66, 67], 'ABC'->str2list())
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
73 call assert_equal(3, 'ABC'->strlen())
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
74 call assert_equal('a^Mb^[c', "a\rb\ec"->strtrans())
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
75 call assert_equal(4, "aあb"->strwidth())
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
76 call assert_equal('axc', 'abc'->substitute('b', 'x', ''))
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
77 endfunc
4c7097a980a5 patch 8.1.1809: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17620
diff changeset
78
17646
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
79 func Test_method_append()
17620
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
80 new
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
81 eval ['one', 'two', 'three']->append(1)
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
82 call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
83
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
84 %del
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
85 let bnr = bufnr('')
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
86 wincmd w
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
87 eval ['one', 'two', 'three']->appendbufline(bnr, 1)
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
88 call assert_equal(['', 'one', 'two', 'three'], getbufline(bnr, 1, '$'))
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
89
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
90 exe 'bwipe! ' .. bnr
072efa9ca875 patch 8.1.1807: more functions can be used as a method
Bram Moolenaar <Bram@vim.org>
parents: 17612
diff changeset
91 endfunc
17646
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
92
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
93 func Test_method_funcref()
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
94 func Concat(one, two, three)
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
95 return a:one .. a:two .. a:three
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
96 endfunc
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
97 let FuncRef = function('Concat')
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
98 eval 'foo'->FuncRef('bar', 'tail')->assert_equal('foobartail')
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
99
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
100 let Partial = function('Concat', ['two'])
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
101 eval 'one'->Partial('three')->assert_equal('onetwothree')
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
102
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
103 delfunc Concat
e5397617d6ca patch 8.1.1820: using expr->FuncRef() does not work
Bram Moolenaar <Bram@vim.org>
parents: 17624
diff changeset
104 endfunc