1" Test commands that are not compiled in a :def function
2
3source check.vim
4source vim9.vim
5source view_util.vim
6
7def Test_edit_wildcards()
8  var filename = 'Xtest'
9  edit `=filename`
10  assert_equal('Xtest', bufname())
11
12  var filenr = 123
13  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
24def Test_hardcopy_wildcards()
25  CheckUnix
26  CheckFeature postscript
27
28  var outfile = 'print'
29  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')
37  var save_rtp = &rtp
38  &rtp = '.'
39
40  var fname = 'mine'
41  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
48def Test_echo_linebreak()
49  var lines =<< trim END
50      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
71def 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
139def Test_if_linebreak()
140  var lines =<< trim END
141      vim9script
142      if 1 &&
143            true
144            || 1
145        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()
169  var lines =<< trim END
170      vim9script
171      var nr = 0
172      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
183      var nr = 0
184      while nr
185            <
186              10
187              +
188              3
189            nr = nr
190                  +
191                  4
192      endwhile
193      assert_equal(16, nr)
194  END
195  CheckScriptSuccess(lines)
196enddef
197
198def Test_for_linebreak()
199  var lines =<< trim END
200      vim9script
201      var nr = 0
202      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
213      var nr = 0
214      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
228def Test_method_call_linebreak()
229  var lines =<< trim END
230      vim9script
231      var res = []
232      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
245def Test_skipped_expr_linebreak()
246  if 0
247    var x = []
248               ->map({ -> 0})
249  endif
250enddef
251
252def Test_dict_member()
253   var test: dict<list<number>> = {'data': [3, 1, 2]}
254   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
260  var lines =<< trim END
261      vim9script
262      var test: dict<list<number>> = {'data': [3, 1, 2]}
263      test.data->sort()
264      assert_equal(#{data: [1, 2, 3]}, test)
265  END
266  CheckScriptSuccess(lines)
267enddef
268
269def Test_bar_after_command()
270  def RedrawAndEcho()
271    var x = 'did redraw'
272    redraw | echo x
273  enddef
274  RedrawAndEcho()
275  assert_match('did redraw', Screenline(&lines))
276
277  def CallAndEcho()
278    var x = 'did redraw'
279    reg_executing() | echo x
280  enddef
281  CallAndEcho()
282  assert_match('did redraw', Screenline(&lines))
283
284  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
309def Test_filter_is_not_modifier()
310  var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
311  filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
312  assert_equal([#{x: 3, y: 4}], tags)
313enddef
314
315def Test_eval_command()
316  var from = 3
317  var to = 5
318  g:val = 111
319  def Increment(nrs: list<number>)
320    for nr in nrs
321      g:val += nr
322    endfor
323  enddef
324  eval range(from, to)
325        ->Increment()
326  assert_equal(111 + 3 + 4 + 5, g:val)
327  unlet g:val
328enddef
329
330def Test_map_command()
331  var lines =<< trim END
332      nnoremap <F3> :echo 'hit F3 #'<CR>
333      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
334  END
335  CheckDefSuccess(lines)
336  CheckScriptSuccess(['vim9script'] + lines)
337enddef
338
339def Test_normal_command()
340  new
341  setline(1, 'doesnotexist')
342  var caught = 0
343  try
344    exe "norm! \<C-]>"
345  catch /E433/
346    caught = 2
347  endtry
348  assert_equal(2, caught)
349
350  try
351    exe "norm! 3\<C-]>"
352  catch /E433/
353    caught = 3
354  endtry
355  assert_equal(3, caught)
356  bwipe!
357enddef
358
359def Test_put_command()
360  new
361  @p = 'ppp'
362  put p
363  assert_equal('ppp', getline(2))
364
365  put ='below'
366  assert_equal('below', getline(3))
367  put! ='above'
368  assert_equal('above', getline(3))
369  assert_equal('below', getline(4))
370
371  bwipe!
372enddef
373
374def Test_command_star_range()
375  new
376  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
377  setpos("'<", [0, 1, 0, 0])
378  setpos("'>", [0, 3, 0, 0])
379  :*s/\(foo\|bar\)/baz/g
380  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
381
382  bwipe!
383enddef
384
385def Test_f_args()
386  var lines =<< trim END
387    vim9script
388
389    func SaveCmdArgs(...)
390      let g:args = a:000
391    endfunc
392
393    command -nargs=* TestFArgs call SaveCmdArgs(<f-args>)
394
395    TestFArgs
396    assert_equal([], g:args)
397
398    TestFArgs one two three
399    assert_equal(['one', 'two', 'three'], g:args)
400  END
401  CheckScriptSuccess(lines)
402enddef
403
404def Test_modifier_silent()
405  echomsg 'last one'
406  silent echomsg "text"
407  redir => g:testmsg
408    :1messages
409  redir END
410  assert_equal("\nlast one", g:testmsg)
411  unlet g:testmsg
412
413  silent! echoerr "error"
414enddef
415
416
417" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
418