blob: ad3454d8319bf30d3f9ea022523b0c00d10c968d [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 Moolenaar0a47e092020-07-08 18:30:06 +0200245def Test_dict_member()
Bram Moolenaarac564082020-09-27 19:05:33 +0200246 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200247 test.data->sort()
248 assert_equal(#{data: [1, 2, 3]}, test)
249 test.data
250 ->reverse()
251 assert_equal(#{data: [3, 2, 1]}, test)
252
Bram Moolenaarac564082020-09-27 19:05:33 +0200253 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200254 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200255 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200256 test.data->sort()
257 assert_equal(#{data: [1, 2, 3]}, test)
258 END
259 CheckScriptSuccess(lines)
260enddef
261
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200262def Test_bar_after_command()
263 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200264 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200265 redraw | echo x
266 enddef
267 RedrawAndEcho()
268 assert_match('did redraw', Screenline(&lines))
269
Bram Moolenaar788123c2020-07-05 15:32:17 +0200270 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200271 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200272 reg_executing() | echo x
273 enddef
274 CallAndEcho()
275 assert_match('did redraw', Screenline(&lines))
276
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200277 if has('unix')
278 # bar in filter write command does not start new command
279 def WriteToShell()
280 new
281 setline(1, 'some text')
282 w !cat | cat > Xoutfile
283 bwipe!
284 enddef
285 WriteToShell()
286 assert_equal(['some text'], readfile('Xoutfile'))
287 delete('Xoutfile')
288
289 # bar in filter read command does not start new command
290 def ReadFromShell()
291 new
292 r! echo hello there | cat > Xoutfile
293 r !echo again | cat >> Xoutfile
294 bwipe!
295 enddef
296 ReadFromShell()
297 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
298 delete('Xoutfile')
299 endif
300enddef
301
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200302def Test_filter_is_not_modifier()
Bram Moolenaarac564082020-09-27 19:05:33 +0200303 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200304 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
305 assert_equal([#{x: 3, y: 4}], tags)
306enddef
307
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200308def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200309 var from = 3
310 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200311 g:val = 111
312 def Increment(nrs: list<number>)
313 for nr in nrs
314 g:val += nr
315 endfor
316 enddef
317 eval range(from, to)
318 ->Increment()
319 assert_equal(111 + 3 + 4 + 5, g:val)
320 unlet g:val
321enddef
322
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200323def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200324 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200325 nnoremap <F3> :echo 'hit F3 #'<CR>
326 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
327 END
328 CheckDefSuccess(lines)
329 CheckScriptSuccess(['vim9script'] + lines)
330enddef
331
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200332def Test_normal_command()
333 new
334 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200335 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200336 try
337 exe "norm! \<C-]>"
338 catch /E433/
339 caught = 2
340 endtry
341 assert_equal(2, caught)
342
343 try
344 exe "norm! 3\<C-]>"
345 catch /E433/
346 caught = 3
347 endtry
348 assert_equal(3, caught)
349 bwipe!
350enddef
351
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200352def Test_put_command()
353 new
354 @p = 'ppp'
355 put p
356 assert_equal('ppp', getline(2))
357
358 put ='below'
359 assert_equal('below', getline(3))
360 put! ='above'
361 assert_equal('above', getline(3))
362 assert_equal('below', getline(4))
363
364 bwipe!
365enddef
366
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200367def Test_command_star_range()
368 new
369 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
370 setpos("'<", [0, 1, 0, 0])
371 setpos("'>", [0, 3, 0, 0])
372 :*s/\(foo\|bar\)/baz/g
373 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
374
375 bwipe!
376enddef
377
Bram Moolenaar20d89e02020-10-20 23:11:33 +0200378def Test_f_args()
379 var lines =<< trim END
380 vim9script
381
382 func SaveCmdArgs(...)
383 let g:args = a:000
384 endfunc
385
386 command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
387
388 TestFArgs
389 assert_equal([], g:args)
390
391 TestFArgs one two three
392 assert_equal(['one', 'two', 'three'], g:args)
393 END
394 CheckScriptSuccess(lines)
395enddef
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200396
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200397
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200398" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker