blob: b9f68f75c7c6cb66ff414d44b467aa666050c9df [file] [log] [blame]
Bram Moolenaar5a50c222014-04-02 22:17:10 +02001Test for various eval features. vim: set ft=vim :
2
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01003Note: system clipboard is saved, changed and restored.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004
5STARTTEST
6:so small.vim
Bram Moolenaar5a50c222014-04-02 22:17:10 +02007:set encoding=latin1
8:set noswapfile
9:lang C
10:fun AppendRegContents(reg)
11 call append('$', printf('%s: type %s; value: %s (%s), expr: %s (%s)', a:reg, getregtype(a:reg), getreg(a:reg), string(getreg(a:reg, 0, 1)), getreg(a:reg, 1), string(getreg(a:reg, 1, 1))))
12endfun
13:command -nargs=? AR :call AppendRegContents(<q-args>)
14:fun SetReg(...)
15 call call('setreg', a:000)
16 call append('$', printf('{{{2 setreg(%s)', string(a:000)[1:-2]))
17 call AppendRegContents(a:1)
18 if a:1 isnot# '='
19 execute "silent normal! Go==\n==\e\"".a:1."P"
20 endif
21endfun
22:fun ErrExe(str)
23 call append('$', 'Executing '.a:str)
24 try
25 execute a:str
26 catch
27 $put =v:exception
28 endtry
29endfun
30:fun Test()
31$put ='{{{1 let tests'
32let @" = 'abc'
33AR "
34let @" = "abc\n"
35AR "
36let @" = "abc\<C-m>"
37AR "
38let @= = '"abc"'
39AR =
40
41$put ='{{{1 Basic setreg tests'
42call SetReg('a', 'abcA', 'c')
43call SetReg('b', 'abcB', 'v')
44call SetReg('c', 'abcC', 'l')
45call SetReg('d', 'abcD', 'V')
46call SetReg('e', 'abcE', 'b')
47call SetReg('f', 'abcF', "\<C-v>")
48call SetReg('g', 'abcG', 'b10')
49call SetReg('h', 'abcH', "\<C-v>10")
50call SetReg('I', 'abcI')
51
52$put ='{{{1 Appending single lines with setreg()'
53call SetReg('A', 'abcAc', 'c')
54call SetReg('A', 'abcAl', 'l')
55call SetReg('A', 'abcAc2','c')
56call SetReg('b', 'abcBc', 'ca')
57call SetReg('b', 'abcBb', 'ba')
58call SetReg('b', 'abcBc2','ca')
59call SetReg('b', 'abcBb2','b50a')
60
61call SetReg('C', 'abcCl', 'l')
62call SetReg('C', 'abcCc', 'c')
63call SetReg('D', 'abcDb', 'b')
64
65call SetReg('E', 'abcEb', 'b')
66call SetReg('E', 'abcEl', 'l')
67call SetReg('F', 'abcFc', 'c')
68
69$put ='{{{1 Appending NL with setreg()'
70call setreg('a', 'abcA2', 'c')
71call setreg('b', 'abcB2', 'v')
72call setreg('c', 'abcC2', 'l')
73call setreg('d', 'abcD2', 'V')
74call setreg('e', 'abcE2', 'b')
75call setreg('f', 'abcF2', "\<C-v>")
76call setreg('g', 'abcG2', 'b10')
77call setreg('h', 'abcH2', "\<C-v>10")
78call setreg('I', 'abcI2')
79
80call SetReg('A', "\n")
81call SetReg('B', "\n", 'c')
82call SetReg('C', "\n")
83call SetReg('D', "\n", 'l')
84call SetReg('E', "\n")
85call SetReg('F', "\n", 'b')
86
87$put ='{{{1 Setting lists with setreg()'
88call SetReg('a', ['abcA3'], 'c')
89call SetReg('b', ['abcB3'], 'l')
90call SetReg('c', ['abcC3'], 'b')
91call SetReg('d', ['abcD3'])
Bram Moolenaar7d647822014-04-05 21:28:56 +020092call SetReg('e', [1, 2, 'abc', 3])
93call SetReg('f', [1, 2, 3])
Bram Moolenaar5a50c222014-04-02 22:17:10 +020094
95$put ='{{{1 Appending lists with setreg()'
96call SetReg('A', ['abcA3c'], 'c')
97call SetReg('b', ['abcB3l'], 'la')
98call SetReg('C', ['abcC3b'], 'lb')
99call SetReg('D', ['abcD32'])
100
101call SetReg('A', ['abcA32'])
102call SetReg('B', ['abcB3c'], 'c')
103call SetReg('C', ['abcC3l'], 'l')
104call SetReg('D', ['abcD3b'], 'b')
105
106$put ='{{{1 Appending lists with NL with setreg()'
107call SetReg('A', ["\n", 'abcA3l2'], 'l')
108call SetReg('B', ["\n", 'abcB3c2'], 'c')
109call SetReg('C', ["\n", 'abcC3b2'], 'b')
110call SetReg('D', ["\n", 'abcD3b50'],'b50')
111
112$put ='{{{1 Setting lists with NLs with setreg()'
113call SetReg('a', ['abcA4-0', "\n", "abcA4-2\n", "\nabcA4-3", "abcA4-4\nabcA4-4-2"])
114call SetReg('b', ['abcB4c-0', "\n", "abcB4c-2\n", "\nabcB4c-3", "abcB4c-4\nabcB4c-4-2"], 'c')
115call SetReg('c', ['abcC4l-0', "\n", "abcC4l-2\n", "\nabcC4l-3", "abcC4l-4\nabcC4l-4-2"], 'l')
116call SetReg('d', ['abcD4b-0', "\n", "abcD4b-2\n", "\nabcD4b-3", "abcD4b-4\nabcD4b-4-2"], 'b')
117call SetReg('e', ['abcE4b10-0', "\n", "abcE4b10-2\n", "\nabcE4b10-3", "abcE4b10-4\nabcE4b10-4-2"], 'b10')
118
119$put ='{{{1 Search and expressions'
120call SetReg('/', ['abc/'])
121call SetReg('/', ["abc/\n"])
122call SetReg('=', ['"abc/"'])
123call SetReg('=', ["\"abc/\n\""])
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100124$put ='{{{1 System clipboard'
125" Save and restore system clipboard.
126" If no connection to X-Server is possible, test should succeed.
127:let _clipreg = ['+', getreg('+'), getregtype('+')]
128:let _clipopt = &cb
129:let &cb='unnamedplus'
130:1y
131:AR +
132:tabdo :windo :echo "hi"
133:3y
134:AR +
135:let &cb=_clipopt
136:call call('setreg', _clipreg)
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200137$put ='{{{1 Errors'
138call ErrExe('call setreg()')
139call ErrExe('call setreg(1)')
140call ErrExe('call setreg(1, 2, 3, 4)')
141call ErrExe('call setreg([], 2)')
142call ErrExe('call setreg(1, {})')
143call ErrExe('call setreg(1, 2, [])')
Bram Moolenaar7d647822014-04-05 21:28:56 +0200144call ErrExe('call setreg("/", ["1", "2"])')
145call ErrExe('call setreg("=", ["1", "2"])')
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200146call ErrExe('call setreg(1, ["", "", [], ""])')
147endfun
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200148:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200149:call Test()
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200150:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200151:delfunction SetReg
152:delfunction AppendRegContents
153:delfunction ErrExe
154:delfunction Test
155:delcommand AR
156:call garbagecollect(1)
157:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200158:" function name not starting with capital
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200159:try
160:func! g:test()
161:echo "test"
162:endfunc
163:catch
164:$put =v:exception
165:endtry
166:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200167:" function name includes a colon
168:try
169:func! b:test()
170:echo "test"
171:endfunc
172:catch
173:$put =v:exception
174:endtry
175:"
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200176:" function name folowed by #
177:try
178:func! test2() "#
179:echo "test2"
180:endfunc
181:catch
182:$put =v:exception
183:endtry
184:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200185:" function name starting with/without "g:", buffer-local funcref.
Bram Moolenaara4f317d2014-04-24 17:12:33 +0200186:function! g:Foo(n)
187: $put ='called Foo(' . a:n . ')'
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200188:endfunction
189:let b:my_func = function('Foo')
Bram Moolenaara4f317d2014-04-24 17:12:33 +0200190:call b:my_func(1)
191:echo g:Foo(2)
192:echo Foo(3)
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200193:"
Bram Moolenaar355a95a2014-04-29 14:03:02 +0200194:" script-local function used in Funcref must exist.
195:so test_eval_func.vim
196:"
Bram Moolenaare512c8c2014-04-29 17:41:22 +0200197:" Using $ instead of '$' must give an error
198:try
199: call append($, 'foobar')
200:catch
201:$put =v:exception
202:endtry
203:"
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200204:$put ='{{{1 getcurpos/setpos'
Bram Moolenaar493c1782014-05-28 14:34:46 +0200205/^012345678
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02002066l:let sp = getcurpos()
Bram Moolenaar493c1782014-05-28 14:34:46 +02002070:call setpos('.', sp)
208jyl:$put
209:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200210:/^start:/+1,$wq! test.out
211:" vim: et ts=4 isk-=\: fmr=???,???
212:call getchar()
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200213ENDTEST
214
Bram Moolenaar493c1782014-05-28 14:34:46 +0200215012345678
216012345678
217
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200218start: