blob: 61c47b85f94aa631a3418fdc9193497f5e698701 [file] [log] [blame]
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001" Test commands that are not compiled in a :def function
2
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02003source check.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02004source vim9.vim
Bram Moolenaare9f262b2020-07-05 14:57:51 +02005source view_util.vim
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02006
7def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +02008 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02009 edit `=filename`
10 assert_equal('Xtest', bufname())
11
Bram Moolenaarac564082020-09-27 19:05:33 +020012 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020013 edit Xtest`=filenr`
14 assert_equal('Xtest123', bufname())
15
16 filenr = 77
17 edit `=filename``=filenr`
18 assert_equal('Xtest77', bufname())
19
20 edit X`=filename`xx`=filenr`yy
21 assert_equal('XXtestxx77yy', bufname())
22enddef
23
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020024def Test_hardcopy_wildcards()
25 CheckUnix
26 CheckFeature postscript
27
Bram Moolenaarac564082020-09-27 19:05:33 +020028 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020029 hardcopy > X`=outfile`.ps
30 assert_true(filereadable('Xprint.ps'))
31
32 delete('Xprint.ps')
33enddef
34
35def Test_syn_include_wildcards()
36 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +020037 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020038 &rtp = '.'
39
Bram Moolenaarac564082020-09-27 19:05:33 +020040 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020041 syn include @Group Xthe`=fname`.vim
42 assert_match('Found.* contained found', execute('syn list Found'))
43
44 &rtp = save_rtp
45 delete('Xthemine.vim')
46enddef
47
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020048def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +020049 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020050 vim9script
51 redir @a
52 echo 'one'
53 .. 'two'
54 redir END
55 assert_equal("\nonetwo", @a)
56 END
57 CheckScriptSuccess(lines)
58
59 lines =<< trim END
60 vim9script
61 redir @a
62 echo 11 +
63 77
64 - 22
65 redir END
66 assert_equal("\n66", @a)
67 END
68 CheckScriptSuccess(lines)
69enddef
70
Bram Moolenaar13106602020-10-04 16:06:05 +020071def Test_condition_types()
72 var lines =<< trim END
73 if 'text'
74 endif
75 END
76 CheckDefAndScriptFailure(lines, 'E1030:', 1)
77
78 lines =<< trim END
79 if [1]
80 endif
81 END
82 CheckDefFailure(lines, 'E1012:', 1)
83 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
84
85 lines =<< trim END
86 g:cond = 'text'
87 if g:cond
88 endif
89 END
90 CheckDefExecAndScriptFailure(lines, 'E1030:', 2)
91
92 lines =<< trim END
93 g:cond = 0
94 if g:cond
95 elseif 'text'
96 endif
97 END
98 CheckDefFailure(lines, 'E1012:', 3)
99 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 4)
100
101 lines =<< trim END
102 if g:cond
103 elseif [1]
104 endif
105 END
106 CheckDefFailure(lines, 'E1012:', 2)
107 CheckScriptFailure(['vim9script'] + lines, 'E745:', 3)
108
109 lines =<< trim END
110 g:cond = 'text'
111 if 0
112 elseif g:cond
113 endif
114 END
115 CheckDefExecAndScriptFailure(lines, 'E1030:', 3)
116
117 lines =<< trim END
118 while 'text'
119 endwhile
120 END
121 CheckDefFailure(lines, 'E1012:', 1)
122 CheckScriptFailure(['vim9script'] + lines, 'E1030:', 2)
123
124 lines =<< trim END
125 while [1]
126 endwhile
127 END
128 CheckDefFailure(lines, 'E1012:', 1)
129 CheckScriptFailure(['vim9script'] + lines, 'E745:', 2)
130
131 lines =<< trim END
132 g:cond = 'text'
133 while g:cond
134 endwhile
135 END
136 CheckDefExecAndScriptFailure(lines, 'E1030:', 2)
137enddef
138
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200139def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200140 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200141 vim9script
142 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +0200143 true
144 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200145 g:res = 42
146 endif
147 assert_equal(42, g:res)
148 END
149 CheckScriptSuccess(lines)
150 unlet g:res
151
152 lines =<< trim END
153 vim9script
154 if 1 &&
155 0
156 g:res = 0
157 elseif 0 ||
158 0
159 || 1
160 g:res = 12
161 endif
162 assert_equal(12, g:res)
163 END
164 CheckScriptSuccess(lines)
165 unlet g:res
166enddef
167
168def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200169 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200170 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200171 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200172 while nr <
173 10 + 3
174 nr = nr
175 + 4
176 endwhile
177 assert_equal(16, nr)
178 END
179 CheckScriptSuccess(lines)
180
181 lines =<< trim END
182 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200183 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200184 while nr
185 <
186 10
187 +
188 3
189 nr = nr
190 +
191 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200192 endwhile
193 assert_equal(16, nr)
194 END
195 CheckScriptSuccess(lines)
196enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200197
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200198def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200199 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200200 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200201 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200202 for x
203 in
204 [1, 2, 3, 4]
205 nr = nr + x
206 endfor
207 assert_equal(10, nr)
208 END
209 CheckScriptSuccess(lines)
210
211 lines =<< trim END
212 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200213 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200214 for x
215 in
216 [1, 2,
217 3, 4
218 ]
219 nr = nr
220 +
221 x
222 endfor
223 assert_equal(10, nr)
224 END
225 CheckScriptSuccess(lines)
226enddef
227
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200228def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200229 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200230 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200231 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200232 func RetArg(
233 arg
234 )
235 let s:res = a:arg
236 endfunc
237 [1,
238 2,
239 3]->RetArg()
240 assert_equal([1, 2, 3], res)
241 END
242 CheckScriptSuccess(lines)
243enddef
244
Bram Moolenaar683581e2020-10-22 21:22:58 +0200245def Test_skipped_expr_linebreak()
246 if 0
247 var x = []
248 ->map({ -> 0})
249 endif
250enddef
251
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200252def Test_dict_member()
Bram Moolenaarac564082020-09-27 19:05:33 +0200253 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200254 test.data->sort()
255 assert_equal(#{data: [1, 2, 3]}, test)
256 test.data
257 ->reverse()
258 assert_equal(#{data: [3, 2, 1]}, test)
259
Bram Moolenaarac564082020-09-27 19:05:33 +0200260 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200261 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200262 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200263 test.data->sort()
264 assert_equal(#{data: [1, 2, 3]}, test)
265 END
266 CheckScriptSuccess(lines)
267enddef
268
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200269def Test_bar_after_command()
270 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200271 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200272 redraw | echo x
273 enddef
274 RedrawAndEcho()
275 assert_match('did redraw', Screenline(&lines))
276
Bram Moolenaar788123c2020-07-05 15:32:17 +0200277 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200278 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200279 reg_executing() | echo x
280 enddef
281 CallAndEcho()
282 assert_match('did redraw', Screenline(&lines))
283
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200284 if has('unix')
285 # bar in filter write command does not start new command
286 def WriteToShell()
287 new
288 setline(1, 'some text')
289 w !cat | cat > Xoutfile
290 bwipe!
291 enddef
292 WriteToShell()
293 assert_equal(['some text'], readfile('Xoutfile'))
294 delete('Xoutfile')
295
296 # bar in filter read command does not start new command
297 def ReadFromShell()
298 new
299 r! echo hello there | cat > Xoutfile
300 r !echo again | cat >> Xoutfile
301 bwipe!
302 enddef
303 ReadFromShell()
304 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
305 delete('Xoutfile')
306 endif
307enddef
308
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200309def Test_filter_is_not_modifier()
Bram Moolenaarac564082020-09-27 19:05:33 +0200310 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200311 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
312 assert_equal([#{x: 3, y: 4}], tags)
313enddef
314
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +0100315def Test_filter_is_recognized()
316 var lines =<< trim END
317 final expected = "\nType Name Content\n c \"c piyo"
318 @a = 'hoge'
319 @b = 'fuga'
320 @c = 'piyo'
321
322 assert_equal(execute('filter /piyo/ registers abc'), expected)
323 END
324 CheckDefAndScriptSuccess(lines)
325enddef
326
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200327def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200328 var from = 3
329 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200330 g:val = 111
331 def Increment(nrs: list<number>)
332 for nr in nrs
333 g:val += nr
334 endfor
335 enddef
336 eval range(from, to)
337 ->Increment()
338 assert_equal(111 + 3 + 4 + 5, g:val)
339 unlet g:val
340enddef
341
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200342def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200343 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200344 nnoremap <F3> :echo 'hit F3 #'<CR>
345 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
346 END
347 CheckDefSuccess(lines)
348 CheckScriptSuccess(['vim9script'] + lines)
349enddef
350
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200351def Test_normal_command()
352 new
353 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200354 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200355 try
356 exe "norm! \<C-]>"
357 catch /E433/
358 caught = 2
359 endtry
360 assert_equal(2, caught)
361
362 try
363 exe "norm! 3\<C-]>"
364 catch /E433/
365 caught = 3
366 endtry
367 assert_equal(3, caught)
368 bwipe!
369enddef
370
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200371def Test_put_command()
372 new
373 @p = 'ppp'
374 put p
375 assert_equal('ppp', getline(2))
376
377 put ='below'
378 assert_equal('below', getline(3))
379 put! ='above'
380 assert_equal('above', getline(3))
381 assert_equal('below', getline(4))
382
383 bwipe!
384enddef
385
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200386def Test_command_star_range()
387 new
388 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
389 setpos("'<", [0, 1, 0, 0])
390 setpos("'>", [0, 3, 0, 0])
391 :*s/\(foo\|bar\)/baz/g
392 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
393
394 bwipe!
395enddef
396
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200397def Test_f_args()
398 var lines =<< trim END
399 vim9script
400
401 func SaveCmdArgs(...)
402 let g:args = a:000
403 endfunc
404
405 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
406
407 TestFArgs
408 assert_equal([], g:args)
409
410 TestFArgs one two three
411 assert_equal(['one', 'two', 'three'], g:args)
412 END
413 CheckScriptSuccess(lines)
414enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200415
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +0200416def Test_modifier_silent()
417 echomsg 'last one'
418 silent echomsg "text"
419 redir => g:testmsg
420 :1messages
421 redir END
422 assert_equal("\nlast one", g:testmsg)
423 unlet g:testmsg
424
425 silent! echoerr "error"
426enddef
427
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200428
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200429" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker