blob: 3b61442577bf04b4619d118ab3b76cda36bbffac [file] [log] [blame]
Bram Moolenaar5a50c222014-04-02 22:17:10 +02001Test for various eval features. vim: set ft=vim :
2
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003NOTE: Do not add more here, use new style test test_eval_stuff.vim
4
Bram Moolenaar5c27fd12015-01-27 14:09:37 +01005Note: system clipboard is saved, changed and restored.
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02006
Bram Moolenaar4ac163a2015-01-27 22:52:15 +01007clipboard contents
8something else
9
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020010STARTTEST
11:so small.vim
Bram Moolenaar5a50c222014-04-02 22:17:10 +020012:set encoding=latin1
13:set noswapfile
14:lang C
15:fun AppendRegContents(reg)
Bram Moolenaar4ac163a2015-01-27 22:52:15 +010016 call AppendRegParts(a:reg, getregtype(a:reg), getreg(a:reg), string(getreg(a:reg, 0, 1)), getreg(a:reg, 1), string(getreg(a:reg, 1, 1)))
17:endfun
18:fun AppendRegParts(reg, type, cont, strcont, cont1, strcont1)
19 call append('$', printf('%s: type %s; value: %s (%s), expr: %s (%s)', a:reg, a:type, a:cont, a:strcont, a:cont1, a:strcont1))
Bram Moolenaar5a50c222014-04-02 22:17:10 +020020endfun
21:command -nargs=? AR :call AppendRegContents(<q-args>)
22:fun SetReg(...)
23 call call('setreg', a:000)
24 call append('$', printf('{{{2 setreg(%s)', string(a:000)[1:-2]))
25 call AppendRegContents(a:1)
26 if a:1 isnot# '='
27 execute "silent normal! Go==\n==\e\"".a:1."P"
28 endif
29endfun
30:fun ErrExe(str)
31 call append('$', 'Executing '.a:str)
32 try
33 execute a:str
34 catch
35 $put =v:exception
36 endtry
37endfun
38:fun Test()
39$put ='{{{1 let tests'
40let @" = 'abc'
41AR "
42let @" = "abc\n"
43AR "
44let @" = "abc\<C-m>"
45AR "
46let @= = '"abc"'
47AR =
48
49$put ='{{{1 Basic setreg tests'
50call SetReg('a', 'abcA', 'c')
51call SetReg('b', 'abcB', 'v')
52call SetReg('c', 'abcC', 'l')
53call SetReg('d', 'abcD', 'V')
54call SetReg('e', 'abcE', 'b')
55call SetReg('f', 'abcF', "\<C-v>")
56call SetReg('g', 'abcG', 'b10')
57call SetReg('h', 'abcH', "\<C-v>10")
58call SetReg('I', 'abcI')
59
60$put ='{{{1 Appending single lines with setreg()'
61call SetReg('A', 'abcAc', 'c')
62call SetReg('A', 'abcAl', 'l')
63call SetReg('A', 'abcAc2','c')
64call SetReg('b', 'abcBc', 'ca')
65call SetReg('b', 'abcBb', 'ba')
66call SetReg('b', 'abcBc2','ca')
67call SetReg('b', 'abcBb2','b50a')
68
69call SetReg('C', 'abcCl', 'l')
70call SetReg('C', 'abcCc', 'c')
71call SetReg('D', 'abcDb', 'b')
72
73call SetReg('E', 'abcEb', 'b')
74call SetReg('E', 'abcEl', 'l')
75call SetReg('F', 'abcFc', 'c')
76
77$put ='{{{1 Appending NL with setreg()'
78call setreg('a', 'abcA2', 'c')
79call setreg('b', 'abcB2', 'v')
80call setreg('c', 'abcC2', 'l')
81call setreg('d', 'abcD2', 'V')
82call setreg('e', 'abcE2', 'b')
83call setreg('f', 'abcF2', "\<C-v>")
84call setreg('g', 'abcG2', 'b10')
85call setreg('h', 'abcH2', "\<C-v>10")
86call setreg('I', 'abcI2')
87
88call SetReg('A', "\n")
89call SetReg('B', "\n", 'c')
90call SetReg('C', "\n")
91call SetReg('D', "\n", 'l')
92call SetReg('E', "\n")
93call SetReg('F', "\n", 'b')
94
95$put ='{{{1 Setting lists with setreg()'
96call SetReg('a', ['abcA3'], 'c')
97call SetReg('b', ['abcB3'], 'l')
98call SetReg('c', ['abcC3'], 'b')
99call SetReg('d', ['abcD3'])
Bram Moolenaar7d647822014-04-05 21:28:56 +0200100call SetReg('e', [1, 2, 'abc', 3])
101call SetReg('f', [1, 2, 3])
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200102
103$put ='{{{1 Appending lists with setreg()'
104call SetReg('A', ['abcA3c'], 'c')
105call SetReg('b', ['abcB3l'], 'la')
106call SetReg('C', ['abcC3b'], 'lb')
107call SetReg('D', ['abcD32'])
108
109call SetReg('A', ['abcA32'])
110call SetReg('B', ['abcB3c'], 'c')
111call SetReg('C', ['abcC3l'], 'l')
112call SetReg('D', ['abcD3b'], 'b')
113
114$put ='{{{1 Appending lists with NL with setreg()'
115call SetReg('A', ["\n", 'abcA3l2'], 'l')
116call SetReg('B', ["\n", 'abcB3c2'], 'c')
117call SetReg('C', ["\n", 'abcC3b2'], 'b')
118call SetReg('D', ["\n", 'abcD3b50'],'b50')
119
120$put ='{{{1 Setting lists with NLs with setreg()'
121call SetReg('a', ['abcA4-0', "\n", "abcA4-2\n", "\nabcA4-3", "abcA4-4\nabcA4-4-2"])
122call SetReg('b', ['abcB4c-0', "\n", "abcB4c-2\n", "\nabcB4c-3", "abcB4c-4\nabcB4c-4-2"], 'c')
123call SetReg('c', ['abcC4l-0', "\n", "abcC4l-2\n", "\nabcC4l-3", "abcC4l-4\nabcC4l-4-2"], 'l')
124call SetReg('d', ['abcD4b-0', "\n", "abcD4b-2\n", "\nabcD4b-3", "abcD4b-4\nabcD4b-4-2"], 'b')
125call SetReg('e', ['abcE4b10-0', "\n", "abcE4b10-2\n", "\nabcE4b10-3", "abcE4b10-4\nabcE4b10-4-2"], 'b10')
126
127$put ='{{{1 Search and expressions'
128call SetReg('/', ['abc/'])
129call SetReg('/', ["abc/\n"])
130call SetReg('=', ['"abc/"'])
131call SetReg('=', ["\"abc/\n\""])
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100132$put ='{{{1 System clipboard'
Bram Moolenaar4ac163a2015-01-27 22:52:15 +0100133if has('clipboard')
Bram Moolenaar5c27fd12015-01-27 14:09:37 +0100134" Save and restore system clipboard.
135" If no connection to X-Server is possible, test should succeed.
Bram Moolenaare08dd4e2015-02-03 16:07:47 +0100136let _clipreg = ['*', getreg('*'), getregtype('*')]
Bram Moolenaar4ac163a2015-01-27 22:52:15 +0100137let _clipopt = &cb
Bram Moolenaare08dd4e2015-02-03 16:07:47 +0100138let &cb='unnamed'
Bram Moolenaarfabaf752017-12-23 17:26:11 +01001397y
Bram Moolenaare08dd4e2015-02-03 16:07:47 +0100140AR *
Bram Moolenaar4ac163a2015-01-27 22:52:15 +0100141tabdo :windo :echo "hi"
Bram Moolenaarfabaf752017-12-23 17:26:11 +01001428y
Bram Moolenaare08dd4e2015-02-03 16:07:47 +0100143AR *
Bram Moolenaar4ac163a2015-01-27 22:52:15 +0100144let &cb=_clipopt
145call call('setreg', _clipreg)
146else
Bram Moolenaare08dd4e2015-02-03 16:07:47 +0100147 call AppendRegParts('*', 'V', "clipboard contents\n", "['clipboard contents']", "clipboard contents\n", "['clipboard contents']")
148 call AppendRegParts('*', 'V', "something else\n", "['something else']", "something else\n", "['something else']")
Bram Moolenaar4ac163a2015-01-27 22:52:15 +0100149endif
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200150$put ='{{{1 Errors'
151call ErrExe('call setreg()')
152call ErrExe('call setreg(1)')
153call ErrExe('call setreg(1, 2, 3, 4)')
154call ErrExe('call setreg([], 2)')
155call ErrExe('call setreg(1, {})')
156call ErrExe('call setreg(1, 2, [])')
Bram Moolenaar7d647822014-04-05 21:28:56 +0200157call ErrExe('call setreg("/", ["1", "2"])')
158call ErrExe('call setreg("=", ["1", "2"])')
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200159call ErrExe('call setreg(1, ["", "", [], ""])')
160endfun
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200161:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200162:call Test()
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200163:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200164:delfunction SetReg
165:delfunction AppendRegContents
166:delfunction ErrExe
167:delfunction Test
168:delcommand AR
169:call garbagecollect(1)
170:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200171:" function name not starting with capital
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200172:try
173:func! g:test()
174:echo "test"
175:endfunc
176:catch
177:$put =v:exception
178:endtry
179:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200180:" function name includes a colon
181:try
182:func! b:test()
183:echo "test"
184:endfunc
185:catch
186:$put =v:exception
187:endtry
188:"
Bram Moolenaar9bdfb002014-04-23 17:43:42 +0200189:" function name folowed by #
190:try
191:func! test2() "#
192:echo "test2"
193:endfunc
194:catch
195:$put =v:exception
196:endtry
197:"
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200198:" function name starting with/without "g:", buffer-local funcref.
Bram Moolenaara4f317d2014-04-24 17:12:33 +0200199:function! g:Foo(n)
200: $put ='called Foo(' . a:n . ')'
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200201:endfunction
202:let b:my_func = function('Foo')
Bram Moolenaara4f317d2014-04-24 17:12:33 +0200203:call b:my_func(1)
204:echo g:Foo(2)
205:echo Foo(3)
Bram Moolenaareccb7fc2014-04-23 20:43:41 +0200206:"
Bram Moolenaar355a95a2014-04-29 14:03:02 +0200207:" script-local function used in Funcref must exist.
208:so test_eval_func.vim
209:"
Bram Moolenaare512c8c2014-04-29 17:41:22 +0200210:" Using $ instead of '$' must give an error
211:try
212: call append($, 'foobar')
213:catch
214:$put =v:exception
215:endtry
216:"
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +0200217:$put ='{{{1 getcurpos/setpos'
Bram Moolenaar493c1782014-05-28 14:34:46 +0200218/^012345678
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02002196l:let sp = getcurpos()
Bram Moolenaar493c1782014-05-28 14:34:46 +02002200:call setpos('.', sp)
221jyl:$put
222:"
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100223:" substring and variable name
224:let str = 'abcdef'
225:let n = 3
226:$put =str[n:]
227:$put =str[:n]
228:$put =str[n:n]
229:unlet n
230:let nn = 3
231:$put =str[nn:]
232:$put =str[:nn]
233:$put =str[nn:nn]
234:unlet nn
235:let b:nn = 4
236:$put =str[b:nn:]
237:$put =str[:b:nn]
238:$put =str[b:nn:b:nn]
239:unlet b:nn
240:"
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200241:/^start:/+1,$wq! test.out
242:" vim: et ts=4 isk-=\: fmr=???,???
243:call getchar()
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +0200244ENDTEST
245
Bram Moolenaar493c1782014-05-28 14:34:46 +0200246012345678
247012345678
248
Bram Moolenaar5a50c222014-04-02 22:17:10 +0200249start: