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  let filename = 'Xtest'
9  edit `=filename`
10  assert_equal('Xtest', bufname())
11
12  let 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  let 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  let save_rtp = &rtp
38  &rtp = '.'
39
40  let 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_assign_list()
49  let l: list<string> = []
50  l[0] = 'value'
51  assert_equal('value', l[0])
52
53  l[1] = 'asdf'
54  assert_equal('value', l[0])
55  assert_equal('asdf', l[1])
56  assert_equal('asdf', l[-1])
57  assert_equal('value', l[-2])
58
59  let nrl: list<number> = []
60  for i in range(5)
61    nrl[i] = i
62  endfor
63  assert_equal([0, 1, 2, 3, 4], nrl)
64enddef
65
66def Test_assign_dict()
67  let d: dict<string> = {}
68  d['key'] = 'value'
69  assert_equal('value', d['key'])
70
71  d[123] = 'qwerty'
72  assert_equal('qwerty', d[123])
73  assert_equal('qwerty', d['123'])
74
75  let nrd: dict<number> = {}
76  for i in range(3)
77    nrd[i] = i
78  endfor
79  assert_equal({'0': 0, '1': 1, '2': 2}, nrd)
80enddef
81
82def Test_echo_linebreak()
83  let lines =<< trim END
84      vim9script
85      redir @a
86      echo 'one'
87            .. 'two'
88      redir END
89      assert_equal("\nonetwo", @a)
90  END
91  CheckScriptSuccess(lines)
92
93  lines =<< trim END
94      vim9script
95      redir @a
96      echo 11 +
97            77
98            - 22
99      redir END
100      assert_equal("\n66", @a)
101  END
102  CheckScriptSuccess(lines)
103enddef
104
105def Test_if_linebreak()
106  let lines =<< trim END
107      vim9script
108      if 1 &&
109            2
110            || 3
111        g:res = 42
112      endif
113      assert_equal(42, g:res)
114  END
115  CheckScriptSuccess(lines)
116  unlet g:res
117
118  lines =<< trim END
119      vim9script
120      if 1 &&
121            0
122        g:res = 0
123      elseif 0 ||
124              0
125              || 1
126        g:res = 12
127      endif
128      assert_equal(12, g:res)
129  END
130  CheckScriptSuccess(lines)
131  unlet g:res
132enddef
133
134def Test_while_linebreak()
135  let lines =<< trim END
136      vim9script
137      let nr = 0
138      while nr <
139              10 + 3
140            nr = nr
141                  + 4
142      endwhile
143      assert_equal(16, nr)
144  END
145  CheckScriptSuccess(lines)
146
147  lines =<< trim END
148      vim9script
149      let nr = 0
150      while nr
151            <
152              10
153              +
154              3
155            nr = nr
156                  +
157                  4
158      endwhile
159      assert_equal(16, nr)
160  END
161  CheckScriptSuccess(lines)
162enddef
163
164def Test_for_linebreak()
165  let lines =<< trim END
166      vim9script
167      let nr = 0
168      for x
169            in
170              [1, 2, 3, 4]
171          nr = nr + x
172      endfor
173      assert_equal(10, nr)
174  END
175  CheckScriptSuccess(lines)
176
177  lines =<< trim END
178      vim9script
179      let nr = 0
180      for x
181            in
182              [1, 2,
183                  3, 4
184                  ]
185          nr = nr
186                 +
187                  x
188      endfor
189      assert_equal(10, nr)
190  END
191  CheckScriptSuccess(lines)
192enddef
193
194def Test_method_call_linebreak()
195  let lines =<< trim END
196      vim9script
197      let res = []
198      func RetArg(
199            arg
200            )
201            let s:res = a:arg
202      endfunc
203      [1,
204          2,
205          3]->RetArg()
206      assert_equal([1, 2, 3], res)
207  END
208  CheckScriptSuccess(lines)
209enddef
210
211def Test_dict_member()
212   let test: dict<list<number>> = {'data': [3, 1, 2]}
213   test.data->sort()
214   assert_equal(#{data: [1, 2, 3]}, test)
215   test.data
216      ->reverse()
217   assert_equal(#{data: [3, 2, 1]}, test)
218
219  let lines =<< trim END
220      vim9script
221      let test: dict<list<number>> = {'data': [3, 1, 2]}
222      test.data->sort()
223      assert_equal(#{data: [1, 2, 3]}, test)
224  END
225  CheckScriptSuccess(lines)
226enddef
227
228def Test_bar_after_command()
229  def RedrawAndEcho()
230    let x = 'did redraw'
231    redraw | echo x
232  enddef
233  RedrawAndEcho()
234  assert_match('did redraw', Screenline(&lines))
235
236  def CallAndEcho()
237    let x = 'did redraw'
238    reg_executing() | echo x
239  enddef
240  CallAndEcho()
241  assert_match('did redraw', Screenline(&lines))
242
243  if has('unix')
244    # bar in filter write command does not start new command
245    def WriteToShell()
246      new
247      setline(1, 'some text')
248      w !cat | cat > Xoutfile
249      bwipe!
250    enddef
251    WriteToShell()
252    assert_equal(['some text'], readfile('Xoutfile'))
253    delete('Xoutfile')
254
255    # bar in filter read command does not start new command
256    def ReadFromShell()
257      new
258      r! echo hello there | cat > Xoutfile
259      r !echo again | cat >> Xoutfile
260      bwipe!
261    enddef
262    ReadFromShell()
263    assert_equal(['hello there', 'again'], readfile('Xoutfile'))
264    delete('Xoutfile')
265  endif
266enddef
267
268def Test_filter_is_not_modifier()
269  let tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
270  filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
271  assert_equal([#{x: 3, y: 4}], tags)
272enddef
273
274def Test_eval_command()
275  let from = 3
276  let to = 5
277  g:val = 111
278  def Increment(nrs: list<number>)
279    for nr in nrs
280      g:val += nr
281    endfor
282  enddef
283  eval range(from, to)
284        ->Increment()
285  assert_equal(111 + 3 + 4 + 5, g:val)
286  unlet g:val
287enddef
288
289
290" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker
291