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_dict_member() 246 var test: dict<list<number>> = {'data': [3, 1, 2]} 247 test.data->sort() 248 assert_equal(#{data: [1, 2, 3]}, test) 249 test.data 250 ->reverse() 251 assert_equal(#{data: [3, 2, 1]}, test) 252 253 var lines =<< trim END 254 vim9script 255 var test: dict<list<number>> = {'data': [3, 1, 2]} 256 test.data->sort() 257 assert_equal(#{data: [1, 2, 3]}, test) 258 END 259 CheckScriptSuccess(lines) 260enddef 261 262def Test_bar_after_command() 263 def RedrawAndEcho() 264 var x = 'did redraw' 265 redraw | echo x 266 enddef 267 RedrawAndEcho() 268 assert_match('did redraw', Screenline(&lines)) 269 270 def CallAndEcho() 271 var x = 'did redraw' 272 reg_executing() | echo x 273 enddef 274 CallAndEcho() 275 assert_match('did redraw', Screenline(&lines)) 276 277 if has('unix') 278 # bar in filter write command does not start new command 279 def WriteToShell() 280 new 281 setline(1, 'some text') 282 w !cat | cat > Xoutfile 283 bwipe! 284 enddef 285 WriteToShell() 286 assert_equal(['some text'], readfile('Xoutfile')) 287 delete('Xoutfile') 288 289 # bar in filter read command does not start new command 290 def ReadFromShell() 291 new 292 r! echo hello there | cat > Xoutfile 293 r !echo again | cat >> Xoutfile 294 bwipe! 295 enddef 296 ReadFromShell() 297 assert_equal(['hello there', 'again'], readfile('Xoutfile')) 298 delete('Xoutfile') 299 endif 300enddef 301 302def Test_filter_is_not_modifier() 303 var tags = [{'a': 1, 'b': 2}, {'x': 3, 'y': 4}] 304 filter(tags, { _, v -> has_key(v, 'x') ? 1 : 0 }) 305 assert_equal([#{x: 3, y: 4}], tags) 306enddef 307 308def Test_eval_command() 309 var from = 3 310 var to = 5 311 g:val = 111 312 def Increment(nrs: list<number>) 313 for nr in nrs 314 g:val += nr 315 endfor 316 enddef 317 eval range(from, to) 318 ->Increment() 319 assert_equal(111 + 3 + 4 + 5, g:val) 320 unlet g:val 321enddef 322 323def Test_map_command() 324 var lines =<< trim END 325 nnoremap <F3> :echo 'hit F3 #'<CR> 326 assert_equal(":echo 'hit F3 #'<CR>", maparg("<F3>", "n")) 327 END 328 CheckDefSuccess(lines) 329 CheckScriptSuccess(['vim9script'] + lines) 330enddef 331 332def Test_normal_command() 333 new 334 setline(1, 'doesnotexist') 335 var caught = 0 336 try 337 exe "norm! \<C-]>" 338 catch /E433/ 339 caught = 2 340 endtry 341 assert_equal(2, caught) 342 343 try 344 exe "norm! 3\<C-]>" 345 catch /E433/ 346 caught = 3 347 endtry 348 assert_equal(3, caught) 349 bwipe! 350enddef 351 352def Test_put_command() 353 new 354 @p = 'ppp' 355 put p 356 assert_equal('ppp', getline(2)) 357 358 put ='below' 359 assert_equal('below', getline(3)) 360 put! ='above' 361 assert_equal('above', getline(3)) 362 assert_equal('below', getline(4)) 363 364 bwipe! 365enddef 366 367def Test_command_star_range() 368 new 369 setline(1, ['xxx foo xxx', 'xxx bar xxx', 'xxx foo xx bar']) 370 setpos("'<", [0, 1, 0, 0]) 371 setpos("'>", [0, 3, 0, 0]) 372 :*s/\(foo\|bar\)/baz/g 373 getline(1, 3)->assert_equal(['xxx baz xxx', 'xxx baz xxx', 'xxx baz xx baz']) 374 375 bwipe! 376enddef 377 378 379 380" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker 381