robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear , command =n . clear , n . command 6local expect , feed =n . expect , n . feed 7local eq , eval =t . eq , n . eval 8local fn =n . fn 9 10describe ( ':emenu' , function () 11before_each ( function () 12clear () 13command ( 'nnoremenu Test.Test inormal<ESC>' ) 14command ( 'inoremenu Test.Test insert' ) 15command ( 'vnoremenu Test.Test x' ) 16command ( 'cnoremenu Test.Test cmdmode' ) 17 18command ( 'nnoremenu Edit.Paste p' ) 19command ( 'cnoremenu Edit.Paste <C-R>"' ) 20end ) 21 22it ( 'executes correct bindings in normal mode without using API' , function () 23command ( 'emenu Test.Test' ) 24expect ( 'normal' ) 25end ) 26 27it ( 'executes correct bindings in normal mode' , function () 28command ( 'emenu Test.Test' ) 29expect ( 'normal' ) 30end ) 31 32it ( 'executes correct bindings in insert mode' , function () 33feed ( 'i' ) 34command ( 'emenu Test.Test' ) 35expect ( 'insert' ) 36end ) 37 38it ( 'executes correct bindings in visual mode' , function () 39feed ( 'iabcde<ESC>0lvll' ) 40command ( 'emenu Test.Test' ) 41expect ( 'ae' ) 42end ) 43 44it ( 'executes correct bindings in command mode' , function () 45feed ( 'ithis is a sentence<esc>^yiwo<esc>' ) 46 47-- Invoke "Edit.Paste" in normal-mode. 48command ( 'emenu Edit.Paste' ) 49 50-- Invoke "Edit.Paste" and "Test.Test" in command-mode. 51feed ( ':' ) 52command ( 'emenu Edit.Paste' ) 53command ( 'emenu Test.Test' ) 54 55expect ( [[ 56this is a sentence 57this]] ) 58-- Assert that Edit.Paste pasted @" into the commandline. 59eq ( 'thiscmdmode' , eval ( 'getcmdline()' )) 60end ) 61 62it ( 'popup menu in visual mode via <C-o> from insert mode #19473' , function () 63n . exec ( [[ 64aunmenu * 65source $VIMRUNTIME/menu.vim 66]] ) 67feed ( 'itext<C-o>V' ) 68command ( 'emenu PopUp.Cut' ) 69eq ( '' , fn . getline ( 1 )) 70eq ( 'text\n' , fn . getreg ( '"' )) 71eq ( '' , n . api . nvim_get_vvar ( 'errmsg' )) 72end ) 73end ) 74 75local test_menus_cmd =[=[ 76aunmenu * 77 78nnoremenu &Test.Test inormal<ESC> 79inoremenu Test.Test insert 80vnoremenu Test.Test x 81cnoremenu Test.Test cmdmode 82menu Test.Nested.test level1 83menu Test.Nested.Nested2 level2 84 85nnoremenu <script> Export.Script p 86tmenu Export.Script This is the tooltip 87menu ]Export.hidden thisoneshouldbehidden 88 89nnoremenu Edit.Paste p 90cnoremenu Edit.Paste <C-R>" 91]=] 92 93describe ( ':menu listing' , function () 94before_each ( function () 95clear () 96command ( test_menus_cmd ) 97end ) 98 99it ( 'matches by path argument' , function () 100eq ( 101[[ 102--- Menus --- 103500 Edit 104500 Paste 105c* <C-R>"]] , 106n . exec_capture ( 'cmenu Edit' ) 107) 108eq ( 109[[ 110--- Menus --- 111500 &Test 112500 Test 113n* inormal<Esc> 114500 Nested 115500 test 116n level1 117500 Nested2 118n level2]] , 119n . exec_capture ( 'nmenu Test' ) 120) 121eq ( 122[[ 123--- Menus --- 124500 Nested 125500 test 126o level1 127500 Nested2 128o level2]] , 129n . exec_capture ( 'omenu Test.Nested' ) 130) 131eq ( 132[[ 133--- Menus --- 134500 Test 135n* inormal<Esc> 136v* x 137s* x 138i* insert 139c* cmdmode]] , 140n . exec_capture ( 'amenu Test.Test' ) 141) 142end ) 143end ) 144 145describe ( 'menu_get' , function () 146before_each ( function () 147clear () 148command ( test_menus_cmd ) 149end ) 150 151it ( "path='', modes='a'" , function () 152local m =fn . menu_get ( '' , 'a' ) 153-- HINT: To print the expected table and regenerate the tests: 154-- print(require('vim.inspect')(m)) 155local expected ={ 156{ 157shortcut ='T' , 158hidden =0 , 159submenus ={ 160{ 161mappings ={ 162i ={ 163sid =0 , 164noremap =1 , 165enabled =1 , 166rhs ='insert' , 167silent =0 , 168} , 169s ={ 170sid =0 , 171noremap =1 , 172enabled =1 , 173rhs ='x' , 174silent =0 , 175} , 176n ={ 177sid =0 , 178noremap =1 , 179enabled =1 , 180rhs ='inormal<Esc>' , 181silent =0 , 182} , 183v ={ 184sid =0 , 185noremap =1 , 186enabled =1 , 187rhs ='x' , 188silent =0 , 189} , 190c ={ 191sid =0 , 192noremap =1 , 193enabled =1 , 194rhs ='cmdmode' , 195silent =0 , 196} , 197} , 198priority =500 , 199name ='Test' , 200hidden =0 , 201} , 202{ 203priority =500 , 204name ='Nested' , 205submenus ={ 206{ 207mappings ={ 208o ={ 209sid =0 , 210noremap =0 , 211enabled =1 , 212rhs ='level1' , 213silent =0 , 214} , 215v ={ 216sid =0 , 217noremap =0 , 218enabled =1 , 219rhs ='level1' , 220silent =0 , 221} , 222s ={ 223sid =0 , 224noremap =0 , 225enabled =1 , 226rhs ='level1' , 227silent =0 , 228} , 229n ={ 230sid =0 , 231noremap =0 , 232enabled =1 , 233rhs ='level1' , 234silent =0 , 235} , 236} , 237priority =500 , 238name ='test' , 239hidden =0 , 240} , 241{ 242mappings ={ 243o ={ 244sid =0 , 245noremap =0 , 246enabled =1 , 247rhs ='level2' , 248silent =0 , 249} , 250v ={ 251sid =0 , 252noremap =0 , 253enabled =1 , 254rhs ='level2' , 255silent =0 , 256} , 257s ={ 258sid =0 , 259noremap =0 , 260enabled =1 , 261rhs ='level2' , 262silent =0 , 263} , 264n ={ 265sid =0 , 266noremap =0 , 267enabled =1 , 268rhs ='level2' , 269silent =0 , 270} , 271} , 272priority =500 , 273name ='Nested2' , 274hidden =0 , 275} , 276} , 277hidden =0 , 278} , 279} , 280priority =500 , 281name ='Test' , 282} , 283{ 284priority =500 , 285name ='Export' , 286submenus ={ 287{ 288tooltip ='This is the tooltip' , 289hidden =0 , 290name ='Script' , 291priority =500 , 292mappings ={ 293n ={ 294sid =1 , 295noremap =0 , 296enabled =1 , 297rhs ='p' , 298silent =0 , 299} , 300} , 301} , 302} , 303hidden =0 , 304} , 305{ 306priority =500 , 307name ='Edit' , 308submenus ={ 309{ 310mappings ={ 311c ={ 312sid =0 , 313noremap =1 , 314enabled =1 , 315rhs ='<C-R>"' , 316silent =0 , 317} , 318n ={ 319sid =0 , 320noremap =1 , 321enabled =1 , 322rhs ='p' , 323silent =0 , 324} , 325} , 326priority =500 , 327name ='Paste' , 328hidden =0 , 329} , 330} , 331hidden =0 , 332} , 333{ 334priority =500 , 335name =']Export' , 336submenus ={ 337{ 338mappings ={ 339o ={ 340sid =0 , 341noremap =0 , 342enabled =1 , 343rhs ='thisoneshouldbehidden' , 344silent =0 , 345} , 346v ={ 347sid =0 , 348noremap =0 , 349enabled =1 , 350rhs ='thisoneshouldbehidden' , 351silent =0 , 352} , 353s ={ 354sid =0 , 355noremap =0 , 356enabled =1 , 357rhs ='thisoneshouldbehidden' , 358silent =0 , 359} , 360n ={ 361sid =0 , 362noremap =0 , 363enabled =1 , 364rhs ='thisoneshouldbehidden' , 365silent =0 , 366} , 367} , 368priority =500 , 369name ='hidden' , 370hidden =0 , 371} , 372} , 373hidden =1 , 374} , 375} 376eq ( expected , m ) 377end ) 378 379it ( 'matching path, all modes' , function () 380local m =fn . menu_get ( 'Export' , 'a' ) 381local expected ={ 382{ 383hidden =0 , 384name ='Export' , 385priority =500 , 386submenus ={ 387{ 388tooltip ='This is the tooltip' , 389hidden =0 , 390name ='Script' , 391priority =500 , 392mappings ={ 393n ={ 394sid =1 , 395noremap =0 , 396enabled =1 , 397rhs ='p' , 398silent =0 , 399} , 400} , 401} , 402} , 403} , 404} 405eq ( expected , m ) 406end ) 407 408it ( 'no path, matching modes' , function () 409local m =fn . menu_get ( '' , 'i' ) 410local expected ={ 411{ 412shortcut ='T' , 413hidden =0 , 414submenus ={ 415{ 416mappings ={ 417i ={ 418sid =0 , 419noremap =1 , 420enabled =1 , 421rhs ='insert' , 422silent =0 , 423} , 424} , 425priority =500 , 426name ='Test' , 427hidden =0 , 428} , 429} , 430priority =500 , 431name ='Test' , 432} , 433} 434eq ( expected , m ) 435end ) 436 437it ( 'matching path and modes' , function () 438local m =fn . menu_get ( 'Test' , 'i' ) 439local expected ={ 440{ 441shortcut ='T' , 442submenus ={ 443{ 444mappings ={ 445i ={ 446sid =0 , 447noremap =1 , 448enabled =1 , 449rhs ='insert' , 450silent =0 , 451} , 452} , 453priority =500 , 454name ='Test' , 455hidden =0 , 456} , 457} , 458priority =500 , 459name ='Test' , 460hidden =0 , 461} , 462} 463eq ( expected , m ) 464end ) 465end ) 466 467describe ( 'menu_get' , function () 468before_each ( function () 469clear () 470command ( 'aunmenu *' ) 471end ) 472 473it ( 'returns <keycode> representation of special keys' , function () 474command ( 'nnoremenu &Test.Test inormal<ESC>' ) 475command ( 'inoremenu &Test.Test2 <Tab><Esc>' ) 476command ( 'vnoremenu &Test.Test3 yA<C-R>0<Tab>xyz<Esc>' ) 477command ( 'inoremenu &Test.Test4 <c-r>*' ) 478command ( 'inoremenu &Test.Test5 <c-R>+' ) 479command ( 'nnoremenu &Test.Test6 <Nop>' ) 480command ( 'nnoremenu &Test.Test7 <NOP>' ) 481command ( 'nnoremenu &Test.Test8 <NoP>' ) 482command ( 'nnoremenu &Test.Test9 ""' ) 483 484local m =fn . menu_get ( '' ) 485local expected ={ 486{ 487shortcut ='T' , 488hidden =0 , 489submenus ={ 490{ 491priority =500 , 492mappings ={ 493n ={ 494sid =0 , 495noremap =1 , 496enabled =1 , 497rhs ='inormal<Esc>' , 498silent =0 , 499} , 500} , 501name ='Test' , 502hidden =0 , 503} , 504{ 505priority =500 , 506mappings ={ 507i ={ 508sid =0 , 509noremap =1 , 510enabled =1 , 511rhs ='<Tab><Esc>' , 512silent =0 , 513} , 514} , 515name ='Test2' , 516hidden =0 , 517} , 518{ 519priority =500 , 520mappings ={ 521s ={ 522sid =0 , 523noremap =1 , 524enabled =1 , 525rhs ='yA<C-R>0<Tab>xyz<Esc>' , 526silent =0 , 527} , 528v ={ 529sid =0 , 530noremap =1 , 531enabled =1 , 532rhs ='yA<C-R>0<Tab>xyz<Esc>' , 533silent =0 , 534} , 535} , 536name ='Test3' , 537hidden =0 , 538} , 539{ 540priority =500 , 541mappings ={ 542i ={ 543sid =0 , 544noremap =1 , 545enabled =1 , 546rhs ='<C-R>*' , 547silent =0 , 548} , 549} , 550name ='Test4' , 551hidden =0 , 552} , 553{ 554priority =500 , 555mappings ={ 556i ={ 557sid =0 , 558noremap =1 , 559enabled =1 , 560rhs ='<C-R>+' , 561silent =0 , 562} , 563} , 564name ='Test5' , 565hidden =0 , 566} , 567{ 568priority =500 , 569mappings ={ 570n ={ 571sid =0 , 572noremap =1 , 573enabled =1 , 574rhs ='' , 575silent =0 , 576} , 577} , 578name ='Test6' , 579hidden =0 , 580} , 581{ 582priority =500 , 583mappings ={ 584n ={ 585sid =0 , 586noremap =1 , 587enabled =1 , 588rhs ='' , 589silent =0 , 590} , 591} , 592name ='Test7' , 593hidden =0 , 594} , 595{ 596priority =500 , 597mappings ={ 598n ={ 599sid =0 , 600noremap =1 , 601enabled =1 , 602rhs ='' , 603silent =0 , 604} , 605} , 606name ='Test8' , 607hidden =0 , 608} , 609{ 610priority =500 , 611mappings ={ 612n ={ 613sid =0 , 614noremap =1 , 615enabled =1 , 616rhs ='""' , 617silent =0 , 618} , 619} , 620name ='Test9' , 621hidden =0 , 622} , 623} , 624priority =500 , 625name ='Test' , 626} , 627} 628 629eq ( expected , m ) 630end ) 631 632it ( 'works with right-aligned text and spaces' , function () 633command ( 'nnoremenu &Test<Tab>Y.Test<Tab>X\\ x inormal<Alt-j>' ) 634command ( 'nnoremenu &Test\\ 1.Test\\ 2 Wargl' ) 635command ( 'nnoremenu &Test4.Test<Tab>3 i space<Esc>' ) 636 637local m =fn . menu_get ( '' ) 638local expected ={ 639{ 640shortcut ='T' , 641hidden =0 , 642actext ='Y' , 643submenus ={ 644{ 645mappings ={ 646n ={ 647sid =0 , 648noremap =1 , 649enabled =1 , 650rhs ='inormal<Alt-j>' , 651silent =0 , 652} , 653} , 654hidden =0 , 655actext ='X x' , 656priority =500 , 657name ='Test' , 658} , 659} , 660priority =500 , 661name ='Test' , 662} , 663{ 664shortcut ='T' , 665hidden =0 , 666submenus ={ 667{ 668priority =500 , 669mappings ={ 670n ={ 671sid =0 , 672noremap =1 , 673enabled =1 , 674rhs ='Wargl' , 675silent =0 , 676} , 677} , 678name ='Test 2' , 679hidden =0 , 680} , 681} , 682priority =500 , 683name ='Test 1' , 684} , 685{ 686shortcut ='T' , 687hidden =0 , 688submenus ={ 689{ 690mappings ={ 691n ={ 692sid =0 , 693noremap =1 , 694enabled =1 , 695rhs ='i space<Esc>' , 696silent =0 , 697} , 698} , 699hidden =0 , 700actext ='3' , 701priority =500 , 702name ='Test' , 703} , 704} , 705priority =500 , 706name ='Test4' , 707} , 708} 709 710eq ( expected , m ) 711end ) 712 713it ( 'stores terminal-mode mappings in `tl`' , function () 714command ( 'tlnoremenu &Test.Test bar' ) 715local mappings =fn . menu_get ( '' , 'tl' )[ 1 ]. submenus [ 1 ]. mappings 716-- "tl" is the mode designator for terminal mode, "t" is the one for a 717-- tooltip, so a terminal mapping must not land under "t". 718eq ( 'bar' , mappings . tl . rhs ) 719eq ( nil , mappings . t ) 720end ) 721end )