robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1" Tests for user defined commands 2 3source check.vim 4source screendump.vim 5source vim9.vim 6 7" Test for <mods> in user defined commands 8function Test_cmdmods () 9let g: mods = '' 10 11command ! -nargs = * MyCmd let g:mods = '<mods>' 12 13MyCmd 14call assert_equal ( '' , g: mods ) 15aboveleft MyCmd 16call assert_equal ( 'aboveleft' , g: mods ) 17abo MyCmd 18call assert_equal ( 'aboveleft' , g: mods ) 19belowright MyCmd 20call assert_equal ( 'belowright' , g: mods ) 21bel MyCmd 22call assert_equal ( 'belowright' , g: mods ) 23botright MyCmd 24call assert_equal ( 'botright' , g: mods ) 25bo MyCmd 26call assert_equal ( 'botright' , g: mods ) 27 browse MyCmd 28call assert_equal ( 'browse' , g: mods ) 29 bro MyCmd 30call assert_equal ( 'browse' , g: mods ) 31confirm MyCmd 32call assert_equal ( 'confirm' , g: mods ) 33conf MyCmd 34call assert_equal ( 'confirm' , g: mods ) 35hide MyCmd 36call assert_equal ( 'hide' , g: mods ) 37hid MyCmd 38call assert_equal ( 'hide' , g: mods ) 39keepalt MyCmd 40call assert_equal ( 'keepalt' , g: mods ) 41keepa MyCmd 42call assert_equal ( 'keepalt' , g: mods ) 43keepjumps MyCmd 44call assert_equal ( 'keepjumps' , g: mods ) 45keepj MyCmd 46call assert_equal ( 'keepjumps' , g: mods ) 47keepmarks MyCmd 48call assert_equal ( 'keepmarks' , g: mods ) 49kee MyCmd 50call assert_equal ( 'keepmarks' , g: mods ) 51keeppatterns MyCmd 52call assert_equal ( 'keeppatterns' , g: mods ) 53keepp MyCmd 54call assert_equal ( 'keeppatterns' , g: mods ) 55leftabove MyCmd " results in :aboveleft 56call assert_equal ( 'aboveleft' , g: mods ) 57lefta MyCmd 58call assert_equal ( 'aboveleft' , g: mods ) 59lockmarks MyCmd 60call assert_equal ( 'lockmarks' , g: mods ) 61loc MyCmd 62call assert_equal ( 'lockmarks' , g: mods ) 63noautocmd MyCmd 64call assert_equal ( 'noautocmd' , g: mods ) 65noa MyCmd 66call assert_equal ( 'noautocmd' , g: mods ) 67noswapfile MyCmd 68call assert_equal ( 'noswapfile' , g: mods ) 69nos MyCmd 70call assert_equal ( 'noswapfile' , g: mods ) 71rightbelow MyCmd " results in :belowright 72call assert_equal ( 'belowright' , g: mods ) 73rightb MyCmd 74call assert_equal ( 'belowright' , g: mods ) 75" sandbox MyCmd 76silent MyCmd 77call assert_equal ( 'silent' , g: mods ) 78sil MyCmd 79call assert_equal ( 'silent' , g: mods ) 80silent ! MyCmd 81call assert_equal ( 'silent!' , g: mods ) 82sil ! MyCmd 83call assert_equal ( 'silent!' , g: mods ) 84tab MyCmd 85call assert_equal ( 'tab' , g: mods ) 860 tab MyCmd 87call assert_equal ( '0tab' , g: mods ) 88tab split 89tab MyCmd 90call assert_equal ( 'tab' , g: mods ) 911 tab MyCmd 92call assert_equal ( '1tab' , g: mods ) 93tabprev 94tab MyCmd 95call assert_equal ( 'tab' , g: mods ) 962 tab MyCmd 97call assert_equal ( '2tab' , g: mods ) 982 tabclose 99topleft MyCmd 100call assert_equal ( 'topleft' , g: mods ) 101to MyCmd 102call assert_equal ( 'topleft' , g: mods ) 103unsilent MyCmd 104call assert_equal ( 'unsilent' , g: mods ) 105uns MyCmd 106call assert_equal ( 'unsilent' , g: mods ) 107verbose MyCmd 108call assert_equal ( 'verbose' , g: mods ) 109verb MyCmd 110call assert_equal ( 'verbose' , g: mods ) 1110 verbose MyCmd 112call assert_equal ( '0verbose' , g: mods ) 1133 verbose MyCmd 114call assert_equal ( '3verbose' , g: mods ) 115999 verbose MyCmd 116call assert_equal ( '999verbose' , g: mods ) 117vertical MyCmd 118call assert_equal ( 'vertical' , g: mods ) 119vert MyCmd 120call assert_equal ( 'vertical' , g: mods ) 121horizontal MyCmd 122call assert_equal ( 'horizontal' , g: mods ) 123hor MyCmd 124call assert_equal ( 'horizontal' , g: mods ) 125 126aboveleft belowright botright browse confirm hide keepalt keepjumps 127\ keepmarks keeppatterns lockmarks noautocmd noswapfile silent 128\ tab topleft unsilent verbose vertical MyCmd 129 130call assert_equal ( 'browse confirm hide keepalt keepjumps ' . 131\ 'keepmarks keeppatterns lockmarks noswapfile unsilent noautocmd ' . 132\ 'silent verbose aboveleft belowright botright tab topleft vertical' , 133\ g: mods ) 134 135let g: mods = '' 136command ! -nargs = * MyQCmd let g:mods .= '<q-mods> ' 137 138vertical MyQCmd 139call assert_equal ( '"vertical" ' , g: mods ) 140 141delcommand MyCmd 142delcommand MyQCmd 143unlet g: mods 144endfunction 145 146func SaveCmdArgs (...) 147let g: args = a: 000 148endfunc 149 150func Test_f_args () 151command -nargs = * TestFArgs call SaveCmdArgs(<f-args>) 152 153TestFArgs 154call assert_equal ([], g: args ) 155 156TestFArgs one two three 157call assert_equal ([ 'one' , 'two' , 'three' ], g: args ) 158 159TestFArgs one\\two three 160call assert_equal ([ 'one\two' , 'three' ], g: args ) 161 162TestFArgs one\ two three 163call assert_equal ([ 'one two' , 'three' ], g: args ) 164 165TestFArgs one\"two three 166call assert_equal ([ 'one\"two' , 'three' ], g: args ) 167 168delcommand TestFArgs 169endfunc 170 171func Test_q_args () 172command -nargs = * TestQArgs call SaveCmdArgs(<q-args>) 173 174TestQArgs 175call assert_equal ([ '' ], g: args ) 176 177TestQArgs one two three 178call assert_equal ([ 'one two three' ], g: args ) 179 180TestQArgs one\\two three 181call assert_equal ([ 'one\\two three' ], g: args ) 182 183TestQArgs one\ two three 184call assert_equal ([ 'one\ two three' ], g: args ) 185 186TestQArgs one\"two three 187call assert_equal ([ 'one\"two three' ], g: args ) 188 189delcommand TestQArgs 190endfunc 191 192func Test_reg_arg () 193command -nargs = * -reg TestRegArg call SaveCmdArgs("<reg>", "<register>") 194 195TestRegArg 196call assert_equal ([ '' , '' ], g: args ) 197 198TestRegArg x 199call assert_equal ([ 'x' , 'x' ], g: args ) 200 201delcommand TestRegArg 202endfunc 203 204func Test_no_arg () 205command -nargs = * TestNoArg call SaveCmdArgs("<args>", "<>", "<x>", "<lt>") 206 207TestNoArg 208call assert_equal ([ '' , '<>' , '<x>' , '<' ], g: args ) 209 210TestNoArg one 211call assert_equal ([ 'one' , '<>' , '<x>' , '<' ], g: args ) 212 213delcommand TestNoArg 214endfunc 215 216func Test_range_arg () 217command -range TestRangeArg call SaveCmdArgs(<range>, <line1>, <line2>) 218new 219call setline ( 1 , range ( 100 )) 220let lnum = line ( '.' ) 221 222TestRangeArg 223call assert_equal ([ 0 , lnum , lnum ], g: args ) 224 22599 TestRangeArg 226call assert_equal ([ 1 , 99 , 99 ], g: args ) 227 22888 , 99 TestRangeArg 229call assert_equal ([ 2 , 88 , 99 ], g: args ) 230 231call assert_fails ( '102TestRangeArg' , 'E16:' ) 232 233bwipe ! 234delcommand TestRangeArg 235endfunc 236 237func Test_Ambiguous () 238command Doit let g:didit = 'yes' 239command Dothat let g:didthat = 'also' 240call assert_fails ( 'Do' , 'E464:' ) 241Doit 242call assert_equal ( 'yes' , g: didit ) 243Dothat 244call assert_equal ( 'also' , g: didthat ) 245unlet g: didit 246unlet g: didthat 247 248delcommand Doit 249Do 250call assert_equal ( 'also' , g: didthat ) 251delcommand Dothat 252 253" Nvim removed the ":Ni!" easter egg in 87e107d92. 254call assert_fails ( "\x4ei\041" , 'E492: Not an editor command: Ni!' ) 255endfunc 256 257func Test_redefine_on_reload () 258call writefile ([ 'command ExistingCommand echo "yes"' ], 'Xcommandexists' ) 259call assert_equal ( 0 , exists ( ':ExistingCommand' )) 260source Xcommandexists 261call assert_equal ( 2 , exists ( ':ExistingCommand' )) 262" Redefining a command when reloading a script is OK. 263source Xcommandexists 264call assert_equal ( 2 , exists ( ':ExistingCommand' )) 265 266" But redefining in another script is not OK. 267call writefile ([ 'command ExistingCommand echo "yes"' ], 'Xcommandexists2' ) 268call assert_fails ( 'source Xcommandexists2' , 'E174:' ) 269call delete ( 'Xcommandexists2' ) 270 271" And defining twice in one script is not OK. 272delcommand ExistingCommand 273call assert_equal ( 0 , exists ( ':ExistingCommand' )) 274call writefile ([ 275\ 'command ExistingCommand echo "yes"' , 276\ 'command ExistingCommand echo "no"' , 277\ ], 'Xcommandexists' ) 278call assert_fails ( 'source Xcommandexists' , 'E174:' ) 279call assert_equal ( 2 , exists ( ':ExistingCommand' )) 280 281call delete ( 'Xcommandexists' ) 282delcommand ExistingCommand 283endfunc 284 285func Test_CmdUndefined () 286call assert_fails ( 'Doit' , 'E492:' ) 287au CmdUndefined Doit : command Doit let g:didit = 'yes' 288Doit 289call assert_equal ( 'yes' , g: didit ) 290delcommand Doit 291 292call assert_fails ( 'Dothat' , 'E492:' ) 293au CmdUndefined * let g: didnot = 'yes' 294call assert_fails ( 'Dothat' , 'E492:' ) 295call assert_equal ( 'yes' , g: didnot ) 296endfunc 297 298func Test_CmdErrors () 299call assert_fails ( 'com! docmd :' , 'E183:' ) 300call assert_fails ( 'com! \<Tab> :' , 'E182:' ) 301call assert_fails ( 'com! _ :' , 'E182:' ) 302call assert_fails ( 'com! - DoCmd :' , 'E175:' ) 303call assert_fails ( 'com! -xxx DoCmd :' , 'E181:' ) 304call assert_fails ( 'com! -addr DoCmd :' , 'E179:' ) 305call assert_fails ( 'com! -addr=asdf DoCmd :' , 'E180:' ) 306call assert_fails ( 'com! -complete DoCmd :' , 'E179:' ) 307call assert_fails ( 'com! -complete=xxx DoCmd :' , 'E180:' ) 308call assert_fails ( 'com! -complete=custom DoCmd :' , 'E467:' ) 309call assert_fails ( 'com! -complete=customlist DoCmd :' , 'E467:' ) 310" call assert_fails('com! -complete=behave,CustomComplete DoCmd :', 'E468:') 311call assert_fails ( 'com! -nargs=x DoCmd :' , 'E176:' ) 312call assert_fails ( 'com! -count=1 -count=2 DoCmd :' , 'E177:' ) 313call assert_fails ( 'com! -count=x DoCmd :' , 'E178:' ) 314call assert_fails ( 'com! -range=x DoCmd :' , 'E178:' ) 315 316call assert_fails ( 'com! -complete=file DoCmd :' , 'E1208:' ) 317call assert_fails ( 'com! -nargs=0 -complete=file DoCmd :' , 'E1208:' ) 318 319let lines =<< trim END 320vim9script 321com! -complete=file DoCmd : 322END 323call CheckScriptFailure ( lines , 'E1208:' , 2 ) 324 325let lines =<< trim END 326vim9script 327com! -nargs=0 -complete=file DoCmd : 328END 329call CheckScriptFailure ( lines , 'E1208:' , 2 ) 330 331com ! -nargs = 0 DoCmd : 332call assert_fails ( 'DoCmd x' , 'E488:' ) 333 334com ! -nargs = 1 DoCmd : 335call assert_fails ( 'DoCmd' , 'E471:' ) 336 337com ! -nargs = _ DoCmd : 338call assert_fails ( 'DoCmd' , 'E471:' ) 339 340com ! -nargs = + DoCmd : 341call assert_fails ( 'DoCmd' , 'E471:' ) 342 343call assert_fails ( 'com DoCmd :' , 'E174:' ) 344comclear 345call assert_fails ( 'delcom DoCmd' , 'E184:' ) 346 347" These used to leak memory 348call assert_fails ( 'com! -complete=custom,CustomComplete _ :' , 'E182:' ) 349call assert_fails ( 'com! -complete=custom,CustomComplete docmd :' , 'E183:' ) 350call assert_fails ( 'com! -complete=custom,CustomComplete -xxx DoCmd :' , 'E181:' ) 351endfunc 352 353func CustomComplete ( A , L , P ) 354return "January\nFebruary\nMars\n" 355endfunc 356 357func CustomCompleteList ( A , L , P ) 358return [ "Monday" , "Tuesday" , "Wednesday" , {}, v: _null_string ] 359endfunc 360 361func CustomCompleteListWithSpaces ( A , L , P ) 362return [ "Monday Here" , "Tuesday There" , "Wednesday OK" , {}, v: _null_string ] 363endfunc 364 365func CustomCompleteListFuzzy ( A , L , P ) 366return [ "Monday Here" , "Tuesday There" , "Wednesday OK" , {}, v: _null_string ] -> matchfuzzy ( a: A ) 367endfunc 368 369func Test_CmdCompletion () 370call feedkeys ( ":com -\<C-A>\<C-B>\"\<CR>" , 'tx' ) 371call assert_equal ( '"com -addr bang bar buffer complete count keepscript nargs range register' , @: ) 372 373call feedkeys ( ":com -nargs=0 -\<C-A>\<C-B>\"\<CR>" , 'tx' ) 374call assert_equal ( '"com -nargs=0 -addr bang bar buffer complete count keepscript nargs range register' , @: ) 375 376call feedkeys ( ":com -nargs=\<C-A>\<C-B>\"\<CR>" , 'tx' ) 377call assert_equal ( '"com -nargs=* + 0 1 ? _' , @: ) 378 379call feedkeys ( ":com -addr=\<C-A>\<C-B>\"\<CR>" , 'tx' ) 380call assert_equal ( '"com -addr=arguments buffers lines loaded_buffers other quickfix tabs windows' , @: ) 381 382call feedkeys ( ":com -complete=co\<C-A>\<C-B>\"\<CR>" , 'tx' ) 383call assert_equal ( '"com -complete=color command compiler' , @: ) 384 385" try completion for unsupported argument values 386call feedkeys ( ":com -newarg=\<Tab>\<C-B>\"\<CR>" , 'tx' ) 387call assert_equal ( "\"com -newarg=\t" , @: ) 388 389" command completion after the name in a user defined command 390call feedkeys ( ":com MyCmd chist\<Tab>\<C-B>\"\<CR>" , 'tx' ) 391call assert_equal ( "\"com MyCmd chistory" , @: ) 392 393" delete the Check commands to avoid them showing up 394call feedkeys ( ":com Check\<C-A>\<C-B>\"\<CR>" , 'tx' ) 395let cmds = substitute ( @: , '"com ' , '' , '' ) -> split () 396for cmd in cmds 397exe 'delcommand ' .. cmd 398endfor 399delcommand MissingFeature 400 401command ! DoCmd1 : 402command ! DoCmd2 : 403call feedkeys ( ":com \<C-A>\<C-B>\"\<CR>" , 'tx' ) 404call assert_equal ( '"com DoCmd1 DoCmd2' , @: ) 405 406call feedkeys ( ":DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 407call assert_equal ( '"DoCmd1 DoCmd2' , @: ) 408 409call feedkeys ( ":delcom DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 410call assert_equal ( '"delcom DoCmd1 DoCmd2' , @: ) 411 412" try argument completion for a command without completion 413call feedkeys ( ":DoCmd1 \<Tab>\<C-B>\"\<CR>" , 'tx' ) 414call assert_equal ( "\"DoCmd1 \t" , @: ) 415 416delcom DoCmd1 417call feedkeys ( ":delcom DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 418call assert_equal ( '"delcom DoCmd2' , @: ) 419 420call feedkeys ( ":com DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 421call assert_equal ( '"com DoCmd2' , @: ) 422 423delcom DoCmd2 424call feedkeys ( ":delcom DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 425call assert_equal ( '"delcom DoC' , @: ) 426 427call feedkeys ( ":com DoC\<C-A>\<C-B>\"\<CR>" , 'tx' ) 428call assert_equal ( '"com DoC' , @: ) 429 430" com! -nargs=1 -complete=behave DoCmd : 431" call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 432" call assert_equal('"DoCmd mswin xterm', @:) 433 434" com! -nargs=_ -complete=behave DoCmd : 435" call feedkeys(":DoCmd \<C-A>\<C-B>\"\<CR>", 'tx') 436" call assert_equal('"DoCmd mswin xterm', @:) 437 438com ! -nargs = 1 -complete = retab DoCmd : 439call feedkeys ( ":DoCmd \<C-A>\<C-B>\"\<CR>" , 'tx' ) 440call assert_equal ( '"DoCmd -indentonly' , @: ) 441 442com ! -nargs = _ -complete=retab DoCmd : 443call feedkeys ( ":DoCmd \<C-A>\<C-B>\"\<CR>" , 'tx' ) 444call assert_equal ( '"DoCmd -indentonly' , @: ) 445 446" Test for file name completion 447com ! -nargs = 1 -complete = file DoCmd : 448call feedkeys ( ":DoCmd READM\<Tab>\<C-B>\"\<CR>" , 'tx' ) 449call assert_equal ( '"DoCmd README.txt' , @: ) 450 451com ! -nargs = _ -complete=file DoCmd : 452call feedkeys ( ":DoCmd READM\<Tab>\<C-B>\"\<CR>" , 'tx' ) 453call assert_equal ( '"DoCmd README.txt' , @: ) 454 455" Test for buffer name completion 456com ! -nargs = 1 -complete = buffer DoCmd : 457let bnum = bufadd ( 'BufForUserCmd' ) 458call setbufvar ( bnum , '&buflisted' , 1 ) 459call feedkeys ( ":DoCmd BufFor\<Tab>\<C-B>\"\<CR>" , 'tx' ) 460call assert_equal ( '"DoCmd BufForUserCmd' , @: ) 461bwipe BufForUserCmd 462call feedkeys ( ":DoCmd BufFor\<Tab>\<C-B>\"\<CR>" , 'tx' ) 463call assert_equal ( '"DoCmd BufFor' , @: ) 464 465com ! -nargs = _ -complete=buffer DoCmd : 466let bnum = bufadd ( 'BufForUserCmd' ) 467call setbufvar ( bnum , '&buflisted' , 1 ) 468call feedkeys ( ":DoCmd BufFor\<Tab>\<C-B>\"\<CR>" , 'tx' ) 469call assert_equal ( '"DoCmd BufForUserCmd' , @: ) 470bwipe BufForUserCmd 471call feedkeys ( ":DoCmd BufFor\<Tab>\<C-B>\"\<CR>" , 'tx' ) 472call assert_equal ( '"DoCmd BufFor' , @: ) 473 474com ! -nargs = * -complete = custom , CustomComplete DoCmd : 475call feedkeys ( ":DoCmd \<C-A>\<C-B>\"\<CR>" , 'tx' ) 476call assert_equal ( '"DoCmd January February Mars' , @: ) 477 478com ! -nargs = ? -complete = customlist , CustomCompleteList DoCmd : 479call feedkeys ( ":DoCmd \<C-A>\<C-B>\"\<CR>" , 'tx' ) 480call assert_equal ( '"DoCmd Monday Tuesday Wednesday' , @: ) 481 482com ! -nargs = _ -complete=customlist, CustomCompleteListWithSpaces DoCmd : 483call feedkeys ( ":DoCmd \<C-A>\<C-B>\"\<CR>" , 'tx' ) 484call assert_equal ( '"DoCmd Monday Here Tuesday There Wednesday OK' , @: ) 485 486com ! -nargs = _ -complete=customlist, CustomCompleteListFuzzy DoCmd : 487call feedkeys ( ":DoCmd mo he\<C-A>\<C-B>\"\<CR>" , 'tx' ) 488call assert_equal ( '"DoCmd Monday Here' , @: ) 489 490com ! -nargs = + -complete = custom , CustomCompleteList DoCmd : 491call assert_fails ( "call feedkeys(':DoCmd \<C-D>', 'tx')" , 'E730:' ) 492 493com ! -nargs = + -complete = customlist , CustomComp DoCmd : 494call assert_fails ( "call feedkeys(':DoCmd \<C-D>', 'tx')" , 'E117:' ) 495 496" custom completion without a function 497com ! -nargs = ? -complete = custom , DoCmd 498call assert_beeps ( "call feedkeys(':DoCmd \t', 'tx')" ) 499 500" custom completion failure with the wrong function 501com ! -nargs = ? -complete = custom , min DoCmd 502call assert_fails ( "call feedkeys(':DoCmd \t', 'tx')" , 'E118:' ) 503 504" custom completion for a pattern with a backslash 505let g: ArgLead = '' 506func ! CustCompl ( A , L , P ) 507let g: ArgLead = a: A 508return [ 'one' , 'two' , 'three' ] 509endfunc 510com ! -nargs = ? -complete = customlist , CustCompl DoCmd 511call feedkeys ( ":DoCmd a\\\t" , 'xt' ) 512call assert_equal ( 'a\' , g: ArgLead ) 513delfunc CustCompl 514 515delcom DoCmd 516endfunc 517 518func CallExecute ( A , L , P ) 519" Drop first '\n' 520return execute ( 'echo "hi"' )[ 1 :] 521endfunc 522 523func Test_use_execute_in_completion () 524command ! -nargs = * -complete = custom , CallExecute DoExec : 525call feedkeys ( ":DoExec \<C-A>\<C-B>\"\<CR>" , 'tx' ) 526call assert_equal ( '"DoExec hi' , @: ) 527delcommand DoExec 528endfunc 529 530func Test_addr_all () 531command ! -addr = lines DoSomething let g:a1 = <line1> | let g:a2 = <line2> 532% DoSomething 533call assert_equal ( 1 , g: a1 ) 534call assert_equal ( line ( '$' ), g: a2 ) 535 536command ! -addr = arguments DoSomething let g:a1 = <line1> | let g:a2 = <line2> 537args one two three 538% DoSomething 539call assert_equal ( 1 , g: a1 ) 540call assert_equal ( 3 , g: a2 ) 541 542command ! -addr = buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 543% DoSomething 544for low in range ( 1 , bufnr ( '$' )) 545if buflisted ( low ) 546break 547endif 548endfor 549call assert_equal ( low , g: a1 ) 550call assert_equal ( bufnr ( '$' ), g: a2 ) 551 552command ! -addr = loaded_buffers DoSomething let g:a1 = <line1> | let g:a2 = <line2> 553% DoSomething 554for low in range ( 1 , bufnr ( '$' )) 555if bufloaded ( low ) 556break 557endif 558endfor 559call assert_equal ( low , g: a1 ) 560for up in range ( bufnr ( '$' ), 1 , -1 ) 561if bufloaded ( up ) 562break 563endif 564endfor 565call assert_equal ( up , g: a2 ) 566 567command ! -addr = windows DoSomething let g:a1 = <line1> | let g:a2 = <line2> 568new 569% DoSomething 570call assert_equal ( 1 , g: a1 ) 571call assert_equal ( winnr ( '$' ), g: a2 ) 572bwipe 573 574command ! -addr = tabs DoSomething let g:a1 = <line1> | let g:a2 = <line2> 575tabnew 576% DoSomething 577call assert_equal ( 1 , g: a1 ) 578call assert_equal ( len ( gettabinfo ()), g: a2 ) 579bwipe 580 581command ! -addr = other DoSomething let g:a1 = <line1> | let g:a2 = <line2> 582DoSomething 583call assert_equal ( line ( '.' ), g: a1 ) 584call assert_equal ( line ( '.' ), g: a2 ) 585% DoSomething 586call assert_equal ( 1 , g: a1 ) 587call assert_equal ( line ( '$' ), g: a2 ) 588 589delcommand DoSomething 590endfunc 591 592func Test_nargs_underscore_fargs () 593" -nargs=_ must behave like -nargs=1 for <f-args>/<q-args>: 594" the whole argument is one token, whitespace is part of it. 595let g: res = [] 596com ! -nargs = 1 DoCmd1 call add(g:res, [<f-args>]) 597com ! -nargs = _ DoCmdU call add( g: res, [<f-args>]) 598DoCmd1 a b c 599DoCmdU a b c 600call assert_equal ([[ 'a b c' ], [ 'a b c' ]], g: res ) 601 602let g: res = [] 603com ! -nargs = _ DoCmdQ call add( g: res, <q-args>) 604DoCmdQ a b c 605call assert_equal ([ 'a b c' ], g: res ) 606 607delcom DoCmd1 608delcom DoCmdU 609delcom DoCmdQ 610unlet g: res 611endfunc 612 613func Test_command_list () 614command ! DoCmd : 615call assert_equal ( "\n Name Args Address Complete Definition" 616\ .. "\n DoCmd 0 :" , 617\ execute ( 'command DoCmd' )) 618 619" Test with various -range= and -count= argument values. 620command ! -range DoCmd : 621call assert_equal ( "\n Name Args Address Complete Definition" 622\ .. "\n DoCmd 0 . :" , 623\ execute ( 'command DoCmd' )) 624command ! -range = % DoCmd : 625call assert_equal ( "\n Name Args Address Complete Definition" 626\ .. "\n DoCmd 0 % :" , 627\ execute ( 'command! DoCmd' )) 628command ! -range = 2 DoCmd : 629call assert_equal ( "\n Name Args Address Complete Definition" 630\ .. "\n DoCmd 0 2 :" , 631\ execute ( 'command DoCmd' )) 632command ! -count = 2 DoCmd : 633call assert_equal ( "\n Name Args Address Complete Definition" 634\ .. "\n DoCmd 0 2c ? :" , 635\ execute ( 'command DoCmd' )) 636 637" Test with various -addr= argument values. 638command ! -addr = lines DoCmd : 639call assert_equal ( "\n Name Args Address Complete Definition" 640\ .. "\n DoCmd 0 . :" , 641\ execute ( 'command DoCmd' )) 642command ! -addr = arguments DoCmd : 643call assert_equal ( "\n Name Args Address Complete Definition" 644\ .. "\n DoCmd 0 . arg :" , 645\ execute ( 'command DoCmd' )) 646command ! -addr = buffers DoCmd : 647call assert_equal ( "\n Name Args Address Complete Definition" 648\ .. "\n DoCmd 0 . buf :" , 649\ execute ( 'command DoCmd' )) 650command ! -addr = loaded_buffers DoCmd : 651call assert_equal ( "\n Name Args Address Complete Definition" 652\ .. "\n DoCmd 0 . load :" , 653\ execute ( 'command DoCmd' )) 654command ! -addr = windows DoCmd : 655call assert_equal ( "\n Name Args Address Complete Definition" 656\ .. "\n DoCmd 0 . win :" , 657\ execute ( 'command DoCmd' )) 658command ! -addr = tabs DoCmd : 659call assert_equal ( "\n Name Args Address Complete Definition" 660\ .. "\n DoCmd 0 . tab :" , 661\ execute ( 'command DoCmd' )) 662command ! -addr = other DoCmd : 663call assert_equal ( "\n Name Args Address Complete Definition" 664\ .. "\n DoCmd 0 . ? :" , 665\ execute ( 'command DoCmd' )) 666 667" Test with various -complete= argument values (non-exhaustive list) 668command ! -nargs = 1 -complete = arglist DoCmd : 669call assert_equal ( "\n Name Args Address Complete Definition" 670\ .. "\n DoCmd 1 arglist :" , 671\ execute ( 'command DoCmd' )) 672command ! -nargs = _ -complete=arglist DoCmd : 673call assert_equal ( "\n Name Args Address Complete Definition" 674\ .. "\n DoCmd _ arglist :" , 675\ execute ( 'command DoCmd' )) 676command ! -nargs = * -complete = augroup DoCmd : 677call assert_equal ( "\n Name Args Address Complete Definition" 678\ .. "\n DoCmd * augroup :" , 679\ execute ( 'command DoCmd' )) 680command ! -nargs = ? -complete = custom , CustomComplete DoCmd : 681call assert_equal ( "\n Name Args Address Complete Definition" 682\ .. "\n DoCmd ? custom :" , 683\ execute ( 'command DoCmd' )) 684command ! -nargs = + -complete = customlist , CustomComplete DoCmd : 685call assert_equal ( "\n Name Args Address Complete Definition" 686\ .. "\n DoCmd + customlist :" , 687\ execute ( 'command DoCmd' )) 688 689" Test with various -narg= argument values. 690command ! -nargs = 0 DoCmd : 691call assert_equal ( "\n Name Args Address Complete Definition" 692\ .. "\n DoCmd 0 :" , 693\ execute ( 'command DoCmd' )) 694command ! -nargs = 1 DoCmd : 695call assert_equal ( "\n Name Args Address Complete Definition" 696\ .. "\n DoCmd 1 :" , 697\ execute ( 'command DoCmd' )) 698command ! -nargs = _ DoCmd : 699call assert_equal ( "\n Name Args Address Complete Definition" 700\ .. "\n DoCmd _ :" , 701\ execute ( 'command DoCmd' )) 702command ! -nargs = * DoCmd : 703call assert_equal ( "\n Name Args Address Complete Definition" 704\ .. "\n DoCmd * :" , 705\ execute ( 'command DoCmd' )) 706command ! -nargs = ? DoCmd : 707call assert_equal ( "\n Name Args Address Complete Definition" 708\ .. "\n DoCmd ? :" , 709\ execute ( 'command DoCmd' )) 710command ! -nargs = + DoCmd : 711call assert_equal ( "\n Name Args Address Complete Definition" 712\ .. "\n DoCmd + :" , 713\ execute ( 'command DoCmd' )) 714 715" Test with other arguments. 716command ! -bang DoCmd : 717call assert_equal ( "\n Name Args Address Complete Definition" 718\ .. "\n! DoCmd 0 :" , 719\ execute ( 'command DoCmd' )) 720command ! -bar DoCmd : 721call assert_equal ( "\n Name Args Address Complete Definition" 722\ .. "\n| DoCmd 0 :" , 723\ execute ( 'command DoCmd' )) 724command ! -register DoCmd : 725call assert_equal ( "\n Name Args Address Complete Definition" 726\ .. "\n\" DoCmd 0 :" , 727\ execute ( 'command DoCmd' )) 728command ! -buffer DoCmd : 729call assert_equal ( "\n Name Args Address Complete Definition" 730\ .. "\nb DoCmd 0 :" 731\ .. "\n\" DoCmd 0 :" , 732\ execute ( 'command DoCmd' )) 733comclear 734 735" Test with many args. 736command ! -bang -bar -register -buffer -nargs = + -complete = environment -addr = windows -count = 3 DoCmd : 737call assert_equal ( "\n Name Args Address Complete Definition" 738\ .. "\n!\"b|DoCmd + 3c win environment :" , 739\ execute ( 'command DoCmd' )) 740comclear 741 742" Test with special characters in command definition. 743command ! DoCmd :<cr><tab><c-d> 744call assert_equal ( "\n Name Args Address Complete Definition" 745\ .. "\n DoCmd 0 :<CR><Tab><C-D>" , 746\ execute ( 'command DoCmd' )) 747 748" Test output in verbose mode. 749command ! DoCmd : 750call assert_match ( "^\n" 751\ .. " Name Args Address Complete Definition\n" 752\ .. " DoCmd 0 :\n" 753\ .. "\tLast set from .*/test_usercommands.vim line \\d\\+$" , 754\ execute ( 'verbose command DoCmd' )) 755 756comclear 757call assert_equal ( "\nNo user-defined commands found" , execute ( ':command Xxx' )) 758call assert_equal ( "\nNo user-defined commands found" , execute ( 'command' )) 759endfunc 760 761" Test for a custom user completion returning the wrong value type 762func Test_usercmd_custom () 763func T1 ( a , c , p ) 764return "a\nb\n" 765endfunc 766command -nargs = * -complete = customlist , T1 TCmd1 767call feedkeys ( ":TCmd1 \<C-A>\<C-B>\"\<CR>" , 'xt' ) 768call assert_equal ( '"TCmd1 ' , @: ) 769delcommand TCmd1 770delfunc T1 771 772func T2 ( a , c , p ) 773return {} 774endfunc 775command -nargs = * -complete = customlist , T2 TCmd2 776call feedkeys ( ":TCmd2 \<C-A>\<C-B>\"\<CR>" , 'xt' ) 777call assert_equal ( '"TCmd2 ' , @: ) 778delcommand TCmd2 779delfunc T2 780endfunc 781 782func Test_delcommand_buffer () 783command Global echo 'global' 784command -buffer OneBuffer echo 'one' 785new 786command -buffer TwoBuffer echo 'two' 787call assert_equal ( 0 , exists ( ':OneBuffer' )) 788call assert_equal ( 2 , exists ( ':Global' )) 789call assert_equal ( 2 , exists ( ':TwoBuffer' )) 790delcommand -buffer TwoBuffer 791call assert_equal ( 0 , exists ( ':TwoBuffer' )) 792call assert_fails ( 'delcommand -buffer Global' , 'E1237:' ) 793call assert_fails ( 'delcommand -buffer OneBuffer' , 'E1237:' ) 794bwipe ! 795call assert_equal ( 2 , exists ( ':OneBuffer' )) 796delcommand -buffer OneBuffer 797call assert_equal ( 0 , exists ( ':OneBuffer' )) 798call assert_fails ( 'delcommand -buffer Global' , 'E1237:' ) 799delcommand Global 800call assert_equal ( 0 , exists ( ':Global' )) 801endfunc 802 803func DefCmd ( name ) 804if len ( a: name ) > 30 805return 806endif 807exe 'command ' .. a: name .. ' call DefCmd("' .. a: name .. 'x")' 808echo a: name 809exe a: name 810endfunc 811 812func Test_recursive_define () 813call DefCmd ( 'Command' ) 814 815let name = 'Command' 816while len ( name ) <= 30 817exe 'delcommand ' .. name 818let name ..= 'x' 819endwhile 820endfunc 821 822" Test for using buffer-local ambiguous user-defined commands 823func Test_buflocal_ambiguous_usercmd () 824new 825command -buffer -nargs = 1 -complete = sign TestCmd1 echo "Hello" 826command -buffer -nargs = 1 -complete = sign TestCmd2 echo "World" 827 828call assert_fails ( "call feedkeys(':TestCmd\<CR>', 'xt')" , 'E464:' ) 829call feedkeys ( ":TestCmd \<Tab>\<C-B>\"\<CR>" , 'xt' ) 830call assert_equal ( '"TestCmd ' , @: ) 831 832delcommand TestCmd1 833delcommand TestCmd2 834bw ! 835endfunc 836 837" Test for using buffer-local user command from cmdwin. 838func Test_buflocal_usercmd_cmdwin () 839throw 'Skipped: Nvim supports cmdwin freedom #40312' 840new 841command -buffer TestCmd edit Test 842" This used to crash Vim 843call assert_fails ( "norm q::TestCmd\<CR>" , 'E11:' ) 844bw ! 845endfunc 846 847" Test for using a multibyte character in a user command 848func Test_multibyte_in_usercmd () 849command SubJapanesePeriodToDot exe "%s/\u3002/./g" 850new 851call setline ( 1 , "Hello\u3002" ) 852SubJapanesePeriodToDot 853call assert_equal ( 'Hello.' , getline ( 1 )) 854bw ! 855delcommand SubJapanesePeriodToDot 856endfunc 857 858" Test for listing user commands. 859func Test_command_list_0 () 860" Check space padding of attribute and name in command list 861set vbs & 862command ! ShortCommand echo "ShortCommand" 863command ! VeryMuchLongerCommand echo "VeryMuchLongerCommand" 864 865redi @"> | com | redi END 866pu 867 868let bl = matchbufline ( bufnr ( '%' ), "^ ShortCommand 0" , 1 , '$' ) 869call assert_false ( bl == []) 870let bl = matchbufline ( bufnr ( '%' ), "^ VeryMuchLongerCommand 0" , 1 , '$' ) 871call assert_false ( bl == []) 872 873bwipe ! 874delcommand ShortCommand 875delcommand VeryMuchLongerCommand 876endfunc 877 878" Test for a block of ":command" or ":autocmd" in a block that is not 879" executed: the lines up to the "}" are skipped, not parsed as commands. 880func Test_command_block_skipped () 881throw 'Skipped: Vim9 script is N/A' 882let lines =<< trim END 883vim9script 884def Something(s: string) 885echo s 886enddef 887if 0 888command! -bar -nargs=? Xfoo { 889Something(<q-args>) 890} 891endif 892if 0 893autocmd BufRead *.xyz { 894Something(<q-args>) 895} 896endif 897while 0 898command! Xbar { 899Something('x') 900} 901endwhile 902g:skipped = 'all three' 903END 904call writefile ( lines , 'Xcmdblock.vim' , 'D' ) 905 906let g: skipped = '' 907source Xcmdblock.vim 908call assert_equal ( 'all three' , g: skipped ) 909call assert_equal ( 0 , exists ( ':Xfoo' )) 910call assert_equal ( 0 , exists ( ':Xbar' )) 911call assert_notmatch ( 'xyz' , execute ( 'autocmd BufRead' )) 912 913unlet g: skipped 914endfunc 915 916" vim: shiftwidth=2 sts=2 expandtab