blob: 2e59286369b870e4946db2b9e9ec68e033b99114 [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
Bram Moolenaarac564082020-09-27 19:05:33 +02007" TODO: remove later
8let v:disallow_let = 1
9
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020010def Test_edit_wildcards()
Bram Moolenaarac564082020-09-27 19:05:33 +020011 var filename = 'Xtest'
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020012 edit `=filename`
13 assert_equal('Xtest', bufname())
14
Bram Moolenaarac564082020-09-27 19:05:33 +020015 var filenr = 123
Bram Moolenaarcfe435d2020-04-25 20:02:55 +020016 edit Xtest`=filenr`
17 assert_equal('Xtest123', bufname())
18
19 filenr = 77
20 edit `=filename``=filenr`
21 assert_equal('Xtest77', bufname())
22
23 edit X`=filename`xx`=filenr`yy
24 assert_equal('XXtestxx77yy', bufname())
25enddef
26
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020027def Test_hardcopy_wildcards()
28 CheckUnix
29 CheckFeature postscript
30
Bram Moolenaarac564082020-09-27 19:05:33 +020031 var outfile = 'print'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020032 hardcopy > X`=outfile`.ps
33 assert_true(filereadable('Xprint.ps'))
34
35 delete('Xprint.ps')
36enddef
37
38def Test_syn_include_wildcards()
39 writefile(['syn keyword Found found'], 'Xthemine.vim')
Bram Moolenaarac564082020-09-27 19:05:33 +020040 var save_rtp = &rtp
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020041 &rtp = '.'
42
Bram Moolenaarac564082020-09-27 19:05:33 +020043 var fname = 'mine'
Bram Moolenaar6378c4f2020-04-26 13:50:41 +020044 syn include @Group Xthe`=fname`.vim
45 assert_match('Found.* contained found', execute('syn list Found'))
46
47 &rtp = save_rtp
48 delete('Xthemine.vim')
49enddef
50
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020051def Test_echo_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +020052 var lines =<< trim END
Bram Moolenaar7e8967f2020-06-27 21:56:17 +020053 vim9script
54 redir @a
55 echo 'one'
56 .. 'two'
57 redir END
58 assert_equal("\nonetwo", @a)
59 END
60 CheckScriptSuccess(lines)
61
62 lines =<< trim END
63 vim9script
64 redir @a
65 echo 11 +
66 77
67 - 22
68 redir END
69 assert_equal("\n66", @a)
70 END
71 CheckScriptSuccess(lines)
72enddef
73
Bram Moolenaarfaf86262020-06-27 23:07:36 +020074def Test_if_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +020075 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +020076 vim9script
77 if 1 &&
78 2
79 || 3
80 g:res = 42
81 endif
82 assert_equal(42, g:res)
83 END
84 CheckScriptSuccess(lines)
85 unlet g:res
86
87 lines =<< trim END
88 vim9script
89 if 1 &&
90 0
91 g:res = 0
92 elseif 0 ||
93 0
94 || 1
95 g:res = 12
96 endif
97 assert_equal(12, g:res)
98 END
99 CheckScriptSuccess(lines)
100 unlet g:res
101enddef
102
103def Test_while_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200104 var lines =<< trim END
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200105 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200106 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200107 while nr <
108 10 + 3
109 nr = nr
110 + 4
111 endwhile
112 assert_equal(16, nr)
113 END
114 CheckScriptSuccess(lines)
115
116 lines =<< trim END
117 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200118 var nr = 0
Bram Moolenaard5053d02020-06-28 15:51:16 +0200119 while nr
120 <
121 10
122 +
123 3
124 nr = nr
125 +
126 4
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200127 endwhile
128 assert_equal(16, nr)
129 END
130 CheckScriptSuccess(lines)
131enddef
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200132
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200133def Test_for_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200134 var lines =<< trim END
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200135 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200136 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200137 for x
138 in
139 [1, 2, 3, 4]
140 nr = nr + x
141 endfor
142 assert_equal(10, nr)
143 END
144 CheckScriptSuccess(lines)
145
146 lines =<< trim END
147 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200148 var nr = 0
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200149 for x
150 in
151 [1, 2,
152 3, 4
153 ]
154 nr = nr
155 +
156 x
157 endfor
158 assert_equal(10, nr)
159 END
160 CheckScriptSuccess(lines)
161enddef
162
Bram Moolenaard2ef6b32020-07-02 21:11:34 +0200163def Test_method_call_linebreak()
Bram Moolenaarac564082020-09-27 19:05:33 +0200164 var lines =<< trim END
Bram Moolenaar5f195932020-07-01 20:07:14 +0200165 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200166 var res = []
Bram Moolenaar5f195932020-07-01 20:07:14 +0200167 func RetArg(
168 arg
169 )
170 let s:res = a:arg
171 endfunc
172 [1,
173 2,
174 3]->RetArg()
175 assert_equal([1, 2, 3], res)
176 END
177 CheckScriptSuccess(lines)
178enddef
179
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200180def Test_dict_member()
Bram Moolenaarac564082020-09-27 19:05:33 +0200181 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200182 test.data->sort()
183 assert_equal(#{data: [1, 2, 3]}, test)
184 test.data
185 ->reverse()
186 assert_equal(#{data: [3, 2, 1]}, test)
187
Bram Moolenaarac564082020-09-27 19:05:33 +0200188 var lines =<< trim END
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200189 vim9script
Bram Moolenaarac564082020-09-27 19:05:33 +0200190 var test: dict<list<number>> = {'data': [3, 1, 2]}
Bram Moolenaar0a47e092020-07-08 18:30:06 +0200191 test.data->sort()
192 assert_equal(#{data: [1, 2, 3]}, test)
193 END
194 CheckScriptSuccess(lines)
195enddef
196
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200197def Test_bar_after_command()
198 def RedrawAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200199 var x = 'did redraw'
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200200 redraw | echo x
201 enddef
202 RedrawAndEcho()
203 assert_match('did redraw', Screenline(&lines))
204
Bram Moolenaar788123c2020-07-05 15:32:17 +0200205 def CallAndEcho()
Bram Moolenaarac564082020-09-27 19:05:33 +0200206 var x = 'did redraw'
Bram Moolenaar788123c2020-07-05 15:32:17 +0200207 reg_executing() | echo x
208 enddef
209 CallAndEcho()
210 assert_match('did redraw', Screenline(&lines))
211
Bram Moolenaare9f262b2020-07-05 14:57:51 +0200212 if has('unix')
213 # bar in filter write command does not start new command
214 def WriteToShell()
215 new
216 setline(1, 'some text')
217 w !cat | cat > Xoutfile
218 bwipe!
219 enddef
220 WriteToShell()
221 assert_equal(['some text'], readfile('Xoutfile'))
222 delete('Xoutfile')
223
224 # bar in filter read command does not start new command
225 def ReadFromShell()
226 new
227 r! echo hello there | cat > Xoutfile
228 r !echo again | cat >> Xoutfile
229 bwipe!
230 enddef
231 ReadFromShell()
232 assert_equal(['hello there', 'again'], readfile('Xoutfile'))
233 delete('Xoutfile')
234 endif
235enddef
236
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200237def Test_filter_is_not_modifier()
Bram Moolenaarac564082020-09-27 19:05:33 +0200238 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}]
Bram Moolenaarb074e8b2020-07-11 13:40:45 +0200239 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 })
240 assert_equal([#{x: 3, y: 4}], tags)
241enddef
242
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200243def Test_eval_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200244 var from = 3
245 var to = 5
Bram Moolenaar007f9d62020-07-06 23:04:49 +0200246 g:val = 111
247 def Increment(nrs: list<number>)
248 for nr in nrs
249 g:val += nr
250 endfor
251 enddef
252 eval range(from, to)
253 ->Increment()
254 assert_equal(111 + 3 + 4 + 5, g:val)
255 unlet g:val
256enddef
257
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200258def Test_map_command()
Bram Moolenaarac564082020-09-27 19:05:33 +0200259 var lines =<< trim END
Bram Moolenaarb8a92962020-08-20 18:02:47 +0200260 nnoremap <F3> :echo 'hit F3 #'<CR>
261 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n"))
262 END
263 CheckDefSuccess(lines)
264 CheckScriptSuccess(['vim9script'] + lines)
265enddef
266
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200267def Test_normal_command()
268 new
269 setline(1, 'doesnotexist')
Bram Moolenaarac564082020-09-27 19:05:33 +0200270 var caught = 0
Bram Moolenaarb3ea36c2020-08-23 21:46:32 +0200271 try
272 exe "norm! \<C-]>"
273 catch /E433/
274 caught = 2
275 endtry
276 assert_equal(2, caught)
277
278 try
279 exe "norm! 3\<C-]>"
280 catch /E433/
281 caught = 3
282 endtry
283 assert_equal(3, caught)
284 bwipe!
285enddef
286
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200287def Test_put_command()
288 new
289 @p = 'ppp'
290 put p
291 assert_equal('ppp', getline(2))
292
293 put ='below'
294 assert_equal('below', getline(3))
295 put! ='above'
296 assert_equal('above', getline(3))
297 assert_equal('below', getline(4))
298
299 bwipe!
300enddef
301
Bram Moolenaar3bd8de42020-09-14 16:37:34 +0200302def Test_command_star_range()
303 new
304 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar'])
305 setpos("'<", [0, 1, 0, 0])
306 setpos("'>", [0, 3, 0, 0])
307 :*s/\(foo\|bar\)/baz/g
308 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz'])
309
310 bwipe!
311enddef
312
Bram Moolenaarc3516f72020-09-08 22:45:35 +0200313
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200314
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200315" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker