blob: 82d5f325e47c8a979b214528f4f0947bc217bbc1 [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 Moolenaarfaf86262020-06-27 23:07:36 +020071def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +020072 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +020073 vim9script
74 if 1 &&
Bram Moolenaar2bb26582020-10-03 22:52:39 +020075 true
76 || 1
Bram Moolenaarfaf86262020-06-27 23:07:36 +020077 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()
Bram Moolenaarac564082020-09-27 19:05:33 +0200101 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200102 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200103 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200104 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
Bram Moolenaarac564082020-09-27 19:05:33 +0200115 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200116 while nr
117 <
118 10
119 +
120 3
121 nr = nr
122 +
123 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200124 endwhile
125 assert_equal(16, nr)
126 END
127 CheckScriptSuccess(lines)
128enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200129
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200130def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200131 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200132 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200133 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200134 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
Bram Moolenaarac564082020-09-27 19:05:33 +0200145 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200146 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
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200160def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200161 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200162 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200163 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200164 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
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200177def Test_dict_member()
Bram Moolenaarac564082020-09-27 19:05:33 +0200178 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200179 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
Bram Moolenaarac564082020-09-27 19:05:33 +0200185 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200186 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200187 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200188 test.data->sort()
189 assert_equal(#{data: [1, 2, 3]}, test)
190 END
191 CheckScriptSuccess(lines)
192enddef
193
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200194def Test_bar_after_command()
195 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200196 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200197 redraw | echo x
198 enddef
199 RedrawAndEcho()
200 assert_match('did redraw', Screenline(&lines))
201
Bram Moolenaar788123c2020-07-05 15:32:17 +0200202 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200203 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200204 reg_executing() | echo x
205 enddef
206 CallAndEcho()
207 assert_match('did redraw', Screenline(&lines))
208
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200209 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
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200234def Test_filter_is_not_modifier()
Bram Moolenaarac564082020-09-27 19:05:33 +0200235 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200236 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
237 assert_equal([#{x: 3, y: 4}], tags)
238enddef
239
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200240def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200241 var from = 3
242 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200243 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
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200255def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200256 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200257 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
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200264def Test_normal_command()
265 new
266 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200267 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200268 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
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200284def 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
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200299def 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
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200310
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200311
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200312" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker