robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
fc3f71fb7d
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local eq =t . eq 7local pcall_err =t . pcall_err 8local feed =n . feed 9local api =n . api 10local clear =n . clear 11local source =n . source 12local command =n . command 13local async_meths =n . async_meths 14local NIL =vim . NIL 15 16local screen 17 18before_each ( function () 19clear () 20screen =Screen . new ( 25 , 5 ) 21source ( [[ 22hi Test ctermfg=Red guifg=Red term=bold 23function CustomCompl(...) 24return 'TEST' 25endfunction 26function CustomListCompl(...) 27return ['FOO'] 28endfunction 29 30highlight RBP1 guibg=Red 31highlight RBP2 guibg=Yellow 32highlight RBP3 guibg=Green 33highlight RBP4 guibg=Blue 34let g:NUM_LVLS = 4 35function Redraw() 36redraw! 37return '' 38endfunction 39cnoremap <expr> {REDRAW} Redraw() 40function RainBowParens(cmdline) 41let ret = [] 42let i = 0 43let lvl = 0 44while i < len(a:cmdline) 45if a:cmdline[i] is# '(' 46call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)]) 47let lvl += 1 48elseif a:cmdline[i] is# ')' 49let lvl -= 1 50call add(ret, [i, i + 1, 'RBP' . ((lvl % g:NUM_LVLS) + 1)]) 51endif 52let i += 1 53endwhile 54return ret 55endfunction 56]] ) 57screen : set_default_attr_ids ( { 58EOB ={ bold =true , foreground =Screen . colors . Blue1 } , 59T ={ foreground =Screen . colors . Red } , 60RBP1 ={ background =Screen . colors . Red } , 61RBP2 ={ background =Screen . colors . Yellow } , 62RBP3 ={ background =Screen . colors . Green } , 63RBP4 ={ background =Screen . colors . Blue } , 64SEP ={ bold =true , reverse =true } , 65CONFIRM ={ bold =true , foreground =Screen . colors . SeaGreen4 } , 66} ) 67end ) 68 69describe ( 'input()' , function () 70it ( 'works with multiline prompts' , function () 71feed ( [[:call input("Test\nFoo")<CR>]] ) 72screen : expect ( [[ 73| 74{EOB:~ }| 75{SEP: }| 76Test | 77Foo^ | 78]] ) 79end ) 80it ( 'works with multiline prompts and :echohl' , function () 81feed ( [[:echohl Test | call input("Test\nFoo")<CR>]] ) 82screen : expect ( [[ 83| 84{EOB:~ }| 85{SEP: }| 86{T:Test} | 87{T:Foo}^ | 88]] ) 89command ( 'redraw!' ) 90screen : expect ( [[ 91| 92{EOB:~ }|*3 93{T:Foo}^ | 94]] ) 95end ) 96it ( 'allows unequal numeric arguments when using multiple args' , function () 97command ( 'echohl Test' ) 98feed ( [[:call input(1, 2)<CR>]] ) 99screen : expect ( [[ 100| 101{EOB:~ }|*3 102{T:1}2^ | 103]] ) 104feed ( '<BS>' ) 105screen : expect ( [[ 106| 107{EOB:~ }|*3 108{T:1}^ | 109]] ) 110end ) 111it ( 'allows unequal numeric values when using {opts} dict' , function () 112command ( 'echohl Test' ) 113api . nvim_set_var ( 'opts' , { prompt =1 , default =2 , cancelreturn =3 } ) 114feed ( [[:echo input(opts)<CR>]] ) 115screen : expect ( [[ 116| 117{EOB:~ }|*3 118{T:1}2^ | 119]] ) 120feed ( '<BS>' ) 121screen : expect ( [[ 122| 123{EOB:~ }|*3 124{T:1}^ | 125]] ) 126feed ( '<Esc>' ) 127screen : expect ( [[ 128^ | 129{EOB:~ }|*3 130{T:3} | 131]] ) 132end ) 133it ( 'works with redraw' , function () 134command ( 'echohl Test' ) 135api . nvim_set_var ( 'opts' , { prompt ='Foo>' , default ='Bar' } ) 136feed ( [[:echo inputdialog(opts)<CR>]] ) 137screen : expect ( [[ 138| 139{EOB:~ }|*3 140{T:Foo>}Bar^ | 141]] ) 142command ( 'mode' ) 143screen : expect { 144grid =[[ 145| 146{EOB:~ }|*3 147{T:Foo>}Bar^ | 148]] , 149reset =true , 150} 151feed ( '<BS>' ) 152screen : expect ( [[ 153| 154{EOB:~ }|*3 155{T:Foo>}Ba^ | 156]] ) 157command ( 'mode' ) 158screen : expect { 159grid =[[ 160| 161{EOB:~ }|*3 162{T:Foo>}Ba^ | 163]] , 164reset =true , 165} 166end ) 167it ( 'allows omitting everything with dict argument' , function () 168command ( 'echohl Test' ) 169feed ( [[:call input({})<CR>]] ) 170screen : expect ( [[ 171| 172{EOB:~ }|*3 173^ | 174]] ) 175end ) 176it ( 'supports completion' , function () 177feed ( ':let var = input("", "", "custom,CustomCompl")<CR>' ) 178feed ( '<Tab><CR>' ) 179eq ( 'TEST' , api . nvim_get_var ( 'var' )) 180 181feed ( ':let var = input({"completion": "customlist,CustomListCompl"})<CR>' ) 182feed ( '<Tab><CR>' ) 183eq ( 'FOO' , api . nvim_get_var ( 'var' )) 184end ) 185it ( 'supports cancelreturn' , function () 186feed ( ':let var = input({"cancelreturn": "BAR"})<CR>' ) 187feed ( '<Esc>' ) 188eq ( 'BAR' , api . nvim_get_var ( 'var' )) 189feed ( ':let var = input({"cancelreturn": []})<CR>' ) 190feed ( '<Esc>' ) 191eq ( {} , api . nvim_get_var ( 'var' )) 192feed ( ':let var = input({"cancelreturn": v:false})<CR>' ) 193feed ( '<Esc>' ) 194eq ( false , api . nvim_get_var ( 'var' )) 195feed ( ':let var = input({"cancelreturn": v:null})<CR>' ) 196feed ( '<Esc>' ) 197eq ( NIL , api . nvim_get_var ( 'var' )) 198end ) 199it ( 'supports default string' , function () 200feed ( ':let var = input("", "DEF1")<CR>' ) 201feed ( '<CR>' ) 202eq ( 'DEF1' , api . nvim_get_var ( 'var' )) 203 204feed ( ':let var = input({"default": "DEF2"})<CR>' ) 205feed ( '<CR>' ) 206eq ( 'DEF2' , api . nvim_get_var ( 'var' )) 207end ) 208it ( 'errors out on invalid inputs' , function () 209eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call input([])' )) 210eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call input("", [])' )) 211eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call input("", "", [])' )) 212eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call input({"prompt": []})' )) 213eq ( 214'Vim(call):E730: Using a List as a String' , 215pcall_err ( command , 'call input({"default": []})' ) 216) 217eq ( 218'Vim(call):E730: Using a List as a String' , 219pcall_err ( command , 'call input({"completion": []})' ) 220) 221eq ( 222'Vim(call):E5050: {opts} must be the only argument' , 223pcall_err ( command , 'call input({}, "default")' ) 224) 225eq ( 226'Vim(call):E118: Too many arguments for function: input' , 227pcall_err ( command , 'call input("prompt> ", "default", "file", "extra")' ) 228) 229end ) 230it ( 'supports highlighting' , function () 231command ( 'nnoremap <expr> X input({"highlight": "RainBowParens"})[-1]' ) 232feed ( [[X]] ) 233feed ( '(())' ) 234screen : expect ( [[ 235| 236{EOB:~ }|*3 237{RBP1:(}{RBP2:()}{RBP1:)}^ | 238]] ) 239end ) 240it ( 'is not hidden by :silent' , function () 241feed ( [[:silent call input('Foo: ')<CR>]] ) 242screen : expect ( [[ 243| 244{EOB:~ }| 245{SEP: }| 246Foo: ^ | 247| 248]] ) 249feed ( 'Bar' ) 250screen : expect ( [[ 251| 252{EOB:~ }| 253{SEP: }| 254Foo: Bar^ | 255| 256]] ) 257feed ( '<CR>' ) 258end ) 259end ) 260describe ( 'inputdialog()' , function () 261it ( 'works with multiline prompts' , function () 262feed ( [[:call inputdialog("Test\nFoo")<CR>]] ) 263screen : expect ( [[ 264| 265{EOB:~ }| 266{SEP: }| 267Test | 268Foo^ | 269]] ) 270end ) 271it ( 'works with multiline prompts and :echohl' , function () 272feed ( [[:echohl Test | call inputdialog("Test\nFoo")<CR>]] ) 273screen : expect ( [[ 274| 275{EOB:~ }| 276{SEP: }| 277{T:Test} | 278{T:Foo}^ | 279]] ) 280command ( 'redraw!' ) 281screen : expect ( [[ 282| 283{EOB:~ }|*3 284{T:Foo}^ | 285]] ) 286end ) 287it ( 'allows unequal numeric arguments when using multiple args' , function () 288command ( 'echohl Test' ) 289feed ( [[:call inputdialog(1, 2)<CR>]] ) 290screen : expect ( [[ 291| 292{EOB:~ }|*3 293{T:1}2^ | 294]] ) 295feed ( '<BS>' ) 296screen : expect ( [[ 297| 298{EOB:~ }|*3 299{T:1}^ | 300]] ) 301end ) 302it ( 'allows unequal numeric values when using {opts} dict' , function () 303command ( 'echohl Test' ) 304api . nvim_set_var ( 'opts' , { prompt =1 , default =2 , cancelreturn =3 } ) 305feed ( [[:echo input(opts)<CR>]] ) 306screen : expect ( [[ 307| 308{EOB:~ }|*3 309{T:1}2^ | 310]] ) 311feed ( '<BS>' ) 312screen : expect ( [[ 313| 314{EOB:~ }|*3 315{T:1}^ | 316]] ) 317feed ( '<Esc>' ) 318screen : expect ( [[ 319^ | 320{EOB:~ }|*3 321{T:3} | 322]] ) 323end ) 324it ( 'works with redraw' , function () 325command ( 'echohl Test' ) 326api . nvim_set_var ( 'opts' , { prompt ='Foo>' , default ='Bar' } ) 327feed ( [[:echo input(opts)<CR>]] ) 328screen : expect ( [[ 329| 330{EOB:~ }|*3 331{T:Foo>}Bar^ | 332]] ) 333command ( 'mode' ) 334screen : expect { 335grid =[[ 336| 337{EOB:~ }|*3 338{T:Foo>}Bar^ | 339]] , 340reset =true , 341} 342feed ( '<BS>' ) 343screen : expect ( [[ 344| 345{EOB:~ }|*3 346{T:Foo>}Ba^ | 347]] ) 348command ( 'mode' ) 349screen : expect { 350grid =[[ 351| 352{EOB:~ }|*3 353{T:Foo>}Ba^ | 354]] , 355reset =true , 356} 357end ) 358it ( 'allows omitting everything with dict argument' , function () 359command ( 'echohl Test' ) 360feed ( ':echo inputdialog({})<CR>' ) 361screen : expect ( [[ 362| 363{EOB:~ }|*3 364^ | 365]] ) 366end ) 367it ( 'supports completion' , function () 368feed ( ':let var = inputdialog({"completion": "customlist,CustomListCompl"})<CR>' ) 369feed ( '<Tab><CR>' ) 370eq ( 'FOO' , api . nvim_get_var ( 'var' )) 371end ) 372it ( 'supports cancelreturn' , function () 373feed ( ':let var = inputdialog("", "", "CR1")<CR>' ) 374feed ( '<Esc>' ) 375eq ( 'CR1' , api . nvim_get_var ( 'var' )) 376 377feed ( ':let var = inputdialog({"cancelreturn": "BAR"})<CR>' ) 378feed ( '<Esc>' ) 379eq ( 'BAR' , api . nvim_get_var ( 'var' )) 380end ) 381it ( 'supports default string' , function () 382feed ( ':let var = inputdialog("", "DEF1")<CR>' ) 383feed ( '<CR>' ) 384eq ( 'DEF1' , api . nvim_get_var ( 'var' )) 385 386feed ( ':let var = inputdialog({"default": "DEF2"})<CR>' ) 387feed ( '<CR>' ) 388eq ( 'DEF2' , api . nvim_get_var ( 'var' )) 389end ) 390it ( 'errors out on invalid inputs' , function () 391eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call inputdialog([])' )) 392eq ( 'Vim(call):E730: Using a List as a String' , pcall_err ( command , 'call inputdialog("", [])' )) 393eq ( 394'Vim(call):E730: Using a List as a String' , 395pcall_err ( command , 'call inputdialog("", "", [])' ) 396) 397eq ( 398'Vim(call):E730: Using a List as a String' , 399pcall_err ( command , 'call inputdialog({"prompt": []})' ) 400) 401eq ( 402'Vim(call):E730: Using a List as a String' , 403pcall_err ( command , 'call inputdialog({"default": []})' ) 404) 405eq ( 406'Vim(call):E730: Using a List as a String' , 407pcall_err ( command , 'call inputdialog({"completion": []})' ) 408) 409eq ( 410'Vim(call):E5050: {opts} must be the only argument' , 411pcall_err ( command , 'call inputdialog({}, "default")' ) 412) 413eq ( 414'Vim(call):E118: Too many arguments for function: inputdialog' , 415pcall_err ( command , 'call inputdialog("prompt> ", "default", "file", "extra")' ) 416) 417end ) 418it ( 'supports highlighting' , function () 419command ( 'nnoremap <expr> X inputdialog({"highlight": "RainBowParens"})[-1]' ) 420feed ( [[X]] ) 421feed ( '(())' ) 422screen : expect ( [[ 423| 424{EOB:~ }|*3 425{RBP1:(}{RBP2:()}{RBP1:)}^ | 426]] ) 427end ) 428end ) 429 430describe ( 'inputlist()' , function () 431it ( "only offers the mouse when 'mouse' applies to cmdline mode" , function () 432screen : try_resize ( 75 , 5 ) 433feed ( ':call inputlist(["foo", "bar"])<CR>' ) 434screen : expect ( { any =vim . pesc ( 'Type number and <Enter> (q or empty cancels): ' ) } ) 435feed ( 'q' ) 436command ( 'set mouse=a' ) 437feed ( ':call inputlist(["foo", "bar"])<CR>' ) 438screen : expect ( { any =vim . pesc ( 'Type number and <Enter> or click with the mouse' ) } ) 439feed ( 'q' ) 440end ) 441end ) 442 443describe ( 'confirm()' , function () 444it ( 'works' , function () 445api . nvim_set_option_value ( 'more' , false , {} ) -- Avoid hit-enter prompt 446api . nvim_set_option_value ( 'laststatus' , 2 , {} ) 447-- screen:expect() calls are needed to avoid feeding input too early 448screen : expect ( { any ='%[No Name%]' } ) 449 450async_meths . nvim_command ( [[let a = confirm('Press O to proceed')]] ) 451screen : expect ( { any ='{CONFIRM:.+: }' } ) 452feed ( 'o' ) 453screen : expect ( { any ='%[No Name%]' } ) 454eq ( 1 , api . nvim_get_var ( 'a' )) 455 456async_meths . nvim_command ( [[let a = 'Are you sure?'->confirm("&Yes\n&No")]] ) 457screen : expect ( { any ='{CONFIRM:.+: }' } ) 458feed ( 'y' ) 459screen : expect ( { any ='%[No Name%]' } ) 460eq ( 1 , api . nvim_get_var ( 'a' )) 461 462async_meths . nvim_command ( [[let a = confirm('Are you sure?', "&Yes\n&No")]] ) 463screen : expect ( { any ='{CONFIRM:.+: }' } ) 464feed ( 'n' ) 465screen : expect ( { any ='%[No Name%]' } ) 466eq ( 2 , api . nvim_get_var ( 'a' )) 467 468-- Not possible to match Vim's CTRL-C test here as CTRL-C always sets got_int in Nvim. 469 470-- confirm() should return 0 when pressing ESC. 471async_meths . nvim_command ( [[let a = confirm('Are you sure?', "&Yes\n&No")]] ) 472screen : expect ( { any ='{CONFIRM:.+: }' } ) 473feed ( '<Esc>' ) 474screen : expect ( { any ='%[No Name%]' } ) 475eq ( 0 , api . nvim_get_var ( 'a' )) 476 477-- Default choice is returned when pressing <CR>. 478async_meths . nvim_command ( [[let a = confirm('Are you sure?', "&Yes\n&No")]] ) 479screen : expect ( { any ='{CONFIRM:.+: }' } ) 480feed ( '<CR>' ) 481screen : expect ( { any ='%[No Name%]' } ) 482eq ( 1 , api . nvim_get_var ( 'a' )) 483 484async_meths . nvim_command ( [[let a = confirm('Are you sure?', "&Yes\n&No", 2)]] ) 485screen : expect ( { any ='{CONFIRM:.+: }' } ) 486feed ( '<CR>' ) 487screen : expect ( { any ='%[No Name%]' } ) 488eq ( 2 , api . nvim_get_var ( 'a' )) 489 490async_meths . nvim_command ( [[let a = confirm('Are you sure?', "&Yes\n&No", 0)]] ) 491screen : expect ( { any ='{CONFIRM:.+: }' } ) 492feed ( '<CR>' ) 493screen : expect ( { any ='%[No Name%]' } ) 494eq ( 0 , api . nvim_get_var ( 'a' )) 495 496-- Test with the {type} 4th argument 497for _ , type in ipairs ( { 'Error' , 'Question' , 'Info' , 'Warning' , 'Generic' } ) do 498async_meths . nvim_command ( 499( [[let a = confirm('Are you sure?', "&Yes\n&No", 1, '%s')]] ): format ( type ) 500) 501screen : expect ( { any ='{CONFIRM:.+: }' } ) 502feed ( 'y' ) 503screen : expect ( { any ='%[No Name%]' } ) 504eq ( 1 , api . nvim_get_var ( 'a' )) 505end 506end ) 507 508it ( 'shows dialog even if :silent #8788' , function () 509command ( "autocmd BufNewFile * call confirm('test')" ) 510 511local function check_and_clear ( edit_line ) 512screen : expect ( [[ 513| 514{SEP: }| 515]] .. edit_line .. [[ 516{CONFIRM:test} | 517{CONFIRM:[O]k: }^ | 518]] ) 519feed ( '<cr>' ) 520command ( 'redraw' ) 521command ( 'bdelete!' ) 522end 523 524-- With shortmess-=F 525command ( 'set shortmess-=F' ) 526feed ( ':edit foo<cr>' ) 527check_and_clear ( '"foo" [New] |\n' ) 528 529-- With shortmess+=F 530command ( 'set shortmess+=F' ) 531feed ( ':edit foo<cr>' ) 532check_and_clear ( ':edit foo |\n' ) 533 534-- With :silent 535feed ( ':silent edit foo<cr>' ) 536check_and_clear ( ':silent edit foo |\n' ) 537 538-- With API (via eval/Vimscript) call and shortmess+=F 539feed ( ':call nvim_command("edit x")<cr>' ) 540check_and_clear ( ':call nvim_command("edit |\n' ) 541 542async_meths . nvim_command ( 'edit x' ) 543check_and_clear ( ' |\n' ) 544end ) 545end )