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_if_linebreak()
72  var lines =<< trim END
73      vim9script
74      if 1 &&
75            2
76            || 3
77        g:res = 42
78      endif
79      assert_equal(42, g:res)
80  END
81  CheckScriptSuccess(lines)
82  unlet g:res
83
84  lines =<< trim END
85      vim9script
86      if 1 &&
87            0
88        g:res = 0
89      elseif 0 ||
90              0
91              || 1
92        g:res = 12
93      endif
94      assert_equal(12, g:res)
95  END
96  CheckScriptSuccess(lines)
97  unlet g:res
98enddef
99
100def Test_while_linebreak()
101  var lines =<< trim END
102      vim9script
103      var nr = 0
104      while nr <
105              10 + 3
106            nr = nr
107                  + 4
108      endwhile
109      assert_equal(16, nr)
110  END
111  CheckScriptSuccess(lines)
112
113  lines =<< trim END
114      vim9script
115      var nr = 0
116      while nr
117            <
118              10
119              +
120              3
121            nr = nr
122                  +
123                  4
124      endwhile
125      assert_equal(16, nr)
126  END
127  CheckScriptSuccess(lines)
128enddef
129
130def Test_for_linebreak()
131  var lines =<< trim END
132      vim9script
133      var nr = 0
134      for x
135            in
136              [1, 2, 3, 4]
137          nr = nr + x
138      endfor
139      assert_equal(10, nr)
140  END
141  CheckScriptSuccess(lines)
142
143  lines =<< trim END
144      vim9script
145      var nr = 0
146      for x
147            in
148              [1, 2,
149                  3, 4
150                  ]
151          nr = nr
152                 +
153                  x
154      endfor
155      assert_equal(10, nr)
156  END
157  CheckScriptSuccess(lines)
158enddef
159
160def Test_method_call_linebreak()
161  var lines =<< trim END
162      vim9script
163      var res = []
164      func RetArg(
165            arg
166            )
167            let s:res = a:arg
168      endfunc
169      [1,
170          2,
171          3]->RetArg()
172      assert_equal([1, 2, 3], res)
173  END
174  CheckScriptSuccess(lines)
175enddef
176
177def Test_dict_member()
178   var test: dict<list<number>> = {'data': [3, 1, 2]}
179   test.data->sort()
180   assert_equal(#{data: [1, 2, 3]}, test)
181   test.data
182      ->reverse()
183   assert_equal(#{data: [3, 2, 1]}, test)
184
185  var lines =<< trim END
186      vim9script
187      var test: dict<list<number>> = {'data': [3, 1, 2]}
188      test.data->sort()
189      assert_equal(#{data: [1, 2, 3]}, test)
190  END
191  CheckScriptSuccess(lines)
192enddef
193
194def Test_bar_after_command()
195  def RedrawAndEcho()
196    var x = 'did redraw'
197    redraw | echo x
198  enddef
199  RedrawAndEcho()
200  assert_match('did redraw', Screenline(&lines))
201
202  def CallAndEcho()
203    var x = 'did redraw'
204    reg_executing() | echo x
205  enddef
206  CallAndEcho()
207  assert_match('did redraw', Screenline(&lines))
208
209  if has('unix')
210    # bar in filter write command does not start new command
211    def WriteToShell()
212      new
213      setline(1, 'some text')
214      w !cat | cat > Xoutfile
215      bwipe!
216    enddef
217    WriteToShell()
218    assert_equal(['some text'], readfile('Xoutfile'))
219    delete('Xoutfile')
220
221    # bar in filter read command does not start new command
222    def ReadFromShell()
223      new
224      r! echo hello there | cat > Xoutfile
225      r !echo again | cat >> Xoutfile
226      bwipe!
227    enddef
228    ReadFromShell()
229    assert_equal(['hello there', 'again'], readfile('Xoutfile'))
230    delete('Xoutfile')
231  endif
232enddef
233
234def Test_filter_is_not_modifier()
235  var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
236  filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
237  assert_equal([#{x: 3, y: 4}], tags)
238enddef
239
240def Test_eval_command()
241  var from = 3
242  var to = 5
243  g:val = 111
244  def Increment(nrs: list<number>)
245    for nr in nrs
246      g:val += nr
247    endfor
248  enddef
249  eval range(from, to)
250        ->Increment()
251  assert_equal(111 + 3 + 4 + 5, g:val)
252  unlet g:val
253enddef
254
255def Test_map_command()
256  var lines =<< trim END
257      nnoremap <F3> :echo 'hit F3 #'<CR>
258      assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
259  END
260  CheckDefSuccess(lines)
261  CheckScriptSuccess(['vim9script'] + lines)
262enddef
263
264def Test_normal_command()
265  new
266  setline(1, 'doesnotexist')
267  var caught = 0
268  try
269    exe "norm! \<C-]>"
270  catch /E433/
271    caught = 2
272  endtry
273  assert_equal(2, caught)
274
275  try
276    exe "norm! 3\<C-]>"
277  catch /E433/
278    caught = 3
279  endtry
280  assert_equal(3, caught)
281  bwipe!
282enddef
283
284def Test_put_command()
285  new
286  @p = 'ppp'
287  put p
288  assert_equal('ppp', getline(2))
289
290  put ='below'
291  assert_equal('below', getline(3))
292  put! ='above'
293  assert_equal('above', getline(3))
294  assert_equal('below', getline(4))
295
296  bwipe!
297enddef
298
299def Test_command_star_range()
300  new
301  setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
302  setpos("'<", [0, 1, 0, 0])
303  setpos("'>", [0, 3, 0, 0])
304  :*s/\(foo\|bar\)/baz/g
305  getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
306
307  bwipe!
308enddef
309
310
311
312" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
313