robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4836ecad1f
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 NIL =vim . NIL 6local clear =n . clear 7local command =n . command 8local eq =t . eq 9local api =n . api 10local matches =t . matches 11local source =n . source 12local pcall_err =t . pcall_err 13local exec_lua =n . exec_lua 14local assert_alive =n . assert_alive 15local feed =n . feed 16local fn =n . fn 17 18describe ( 'nvim_get_commands' , function () 19local cmd_dict ={ 20addr =NIL , 21bang =false , 22bar =false , 23complete =NIL , 24complete_arg =NIL , 25count =NIL , 26definition ='echo "Hello World"' , 27desc ='' , 28name ='Hello' , 29nargs ='1' , 30range =NIL , 31register =false , 32keepscript =false , 33script_id =0 , 34} 35local cmd_dict2 ={ 36addr =NIL , 37bang =false , 38bar =false , 39complete =NIL , 40complete_arg =NIL , 41count =NIL , 42definition ='pwd' , 43desc ='' , 44name ='Pwd' , 45nargs ='?' , 46range =NIL , 47register =false , 48keepscript =false , 49script_id =0 , 50} 51before_each ( clear ) 52 53it ( 'gets empty list if no commands were defined' , function () 54eq ( {} , api . nvim_get_commands ( { builtin =false } )) 55end ) 56 57it ( 'validation' , function () 58eq ( 'builtin=true not implemented' , pcall_err ( api . nvim_get_commands , { builtin =true } )) 59eq ( "Invalid key: 'foo'" , pcall_err ( api . nvim_get_commands , { foo ='blah' } )) 60end ) 61 62it ( 'gets global user-defined commands' , function () 63-- Define a command. 64command ( 'command -nargs=1 Hello echo "Hello World"' ) 65eq ( { Hello =cmd_dict } , api . nvim_get_commands ( { builtin =false } )) 66-- Define another command. 67command ( 'command -nargs=? Pwd pwd' ) 68eq ( { Hello =cmd_dict , Pwd =cmd_dict2 } , api . nvim_get_commands ( { builtin =false } )) 69-- Delete a command. 70command ( 'delcommand Pwd' ) 71eq ( { Hello =cmd_dict } , api . nvim_get_commands ( { builtin =false } )) 72end ) 73 74it ( 'gets buffer-local user-defined commands' , function () 75-- Define a buffer-local command. 76command ( 'command -buffer -nargs=1 Hello echo "Hello World"' ) 77eq ( { Hello =cmd_dict } , api . nvim_buf_get_commands ( 0 , { builtin =false } )) 78-- Define another buffer-local command. 79command ( 'command -buffer -nargs=? Pwd pwd' ) 80eq ( { Hello =cmd_dict , Pwd =cmd_dict2 } , api . nvim_buf_get_commands ( 0 , { builtin =false } )) 81-- Delete a command. 82command ( 'delcommand Pwd' ) 83eq ( { Hello =cmd_dict } , api . nvim_buf_get_commands ( 0 , { builtin =false } )) 84 85-- {builtin=true} always returns empty for buffer-local case. 86eq ( {} , api . nvim_buf_get_commands ( 0 , { builtin =true } )) 87end ) 88 89it ( 'gets various command attributes' , function () 90local cmd0 ={ 91addr ='arguments' , 92bang =false , 93bar =false , 94complete ='dir' , 95complete_arg =NIL , 96count ='10' , 97definition ='pwd <args>' , 98desc ='' , 99name ='TestCmd' , 100nargs ='1' , 101range ='10' , 102register =false , 103keepscript =false , 104script_id =0 , 105} 106local cmd1 ={ 107addr =NIL , 108bang =false , 109bar =false , 110complete ='custom' , 111complete_arg ='ListUsers' , 112count =NIL , 113definition ='!finger <args>' , 114desc ='' , 115name ='Finger' , 116nargs ='+' , 117range =NIL , 118register =false , 119keepscript =false , 120script_id =1 , 121} 122local cmd2 ={ 123addr =NIL , 124bang =true , 125bar =false , 126complete =NIL , 127complete_arg =NIL , 128count =NIL , 129definition ='call \128\253R2_foo(<q-args>)' , 130desc ='' , 131name ='Cmd2' , 132nargs ='*' , 133range =NIL , 134register =false , 135keepscript =false , 136script_id =2 , 137} 138local cmd3 ={ 139addr =NIL , 140bang =false , 141bar =true , 142complete =NIL , 143complete_arg =NIL , 144count =NIL , 145definition ='call \128\253R3_ohyeah()' , 146desc ='' , 147name ='Cmd3' , 148nargs ='0' , 149range =NIL , 150register =false , 151keepscript =false , 152script_id =3 , 153} 154local cmd4 ={ 155addr =NIL , 156bang =false , 157bar =false , 158complete =NIL , 159complete_arg =NIL , 160count =NIL , 161definition ='call \128\253R4_just_great()' , 162desc ='' , 163name ='Cmd4' , 164nargs ='0' , 165range =NIL , 166register =true , 167keepscript =false , 168script_id =4 , 169} 170local previewCmd ={ 171addr =NIL , 172bang =false , 173bar =false , 174complete ='customlist' , 175complete_arg ='s:cpt' , 176count =NIL , 177definition ='' , 178desc ='' , 179name ='PreviewCmd' , 180nargs ='1' , 181range =NIL , 182register =false , 183keepscript =false , 184script_id =5 , 185} 186local previewLuaCmd ={ 187addr =NIL , 188bang =false , 189bar =false , 190-- RPC serializes Lua func as "<Lua N>" (normalized below). 191callback ='<Lua ?>' , 192complete ='<Lua ?>' , 193preview ='<Lua ?>' , 194complete_arg =NIL , 195count =NIL , 196definition ='' , 197desc ='Preview Lua Cmd' , 198name ='PreviewLuaCmd' , 199nargs ='1' , 200range =NIL , 201register =false , 202keepscript =false , 203script_id =- 8 , -- Lua 204} 205local withDesc ={ 206addr =vim . NIL , 207bang =false , 208bar =false , 209complete =vim . NIL , 210complete_arg =vim . NIL , 211count =vim . NIL , 212definition ='echo "hi"' , 213desc ='Says hi' , 214keepscript =false , 215name ='WithDesc' , 216nargs ='0' , 217range =vim . NIL , 218register =false , 219script_id =- 8 , 220} 221 222source ( [[ 223let s:foo = 1 224command -complete=custom,ListUsers -nargs=+ Finger !finger <args> 225]] ) 226eq ( { Finger =cmd1 } , api . nvim_get_commands ( { builtin =false } )) 227command ( 'command -nargs=1 -complete=dir -addr=arguments -count=10 TestCmd pwd <args>' ) 228eq ( { Finger =cmd1 , TestCmd =cmd0 } , api . nvim_get_commands ( { builtin =false } )) 229 230source ( [[ 231function! s:foo() abort 232endfunction 233command -bang -nargs=* Cmd2 call <SID>foo(<q-args>) 234]] ) 235source ( [[ 236function! s:ohyeah() abort 237endfunction 238command -bar -nargs=0 Cmd3 call <SID>ohyeah() 239]] ) 240source ( [[ 241function! s:just_great() abort 242endfunction 243command -register Cmd4 call <SID>just_great() 244]] ) 245source ( [[ 246function! s:cpt() abort 247return 1 248endfunction 249command -nargs=1 -complete=customlist,s:cpt PreviewCmd 250]] ) 251source ( [[ 252lua << EOF 253vim.api.nvim_create_user_command( 254'PreviewLuaCmd', 255function() end, 256{ 257nargs = 1, 258complete = function() return 3 end, 259preview = function() return 4 end, 260desc = 'Preview Lua Cmd' 261} 262) 263vim.api.nvim_create_user_command( 264'WithDesc', 265'echo "hi"', 266{ desc = 'Says hi' } 267) 268EOF 269]] ) 270-- TODO(justinmk): Order is stable but undefined. Sort before return? 271local commands =api . nvim_get_commands ( { builtin =false } ) 272-- Normalize the volatile ref id in the "<Lua N>" funcref hints. 273for _ , k in ipairs ( { 'callback' , 'complete' , 'preview' } ) do 274commands . PreviewLuaCmd [ k ] =( commands . PreviewLuaCmd [ k ]: gsub ( '<Lua %d+.->' , '<Lua ?>' )) 275end 276eq ( { 277Cmd2 =cmd2 , 278Cmd3 =cmd3 , 279Cmd4 =cmd4 , 280Finger =cmd1 , 281TestCmd =cmd0 , 282PreviewCmd =previewCmd , 283PreviewLuaCmd =previewLuaCmd , 284WithDesc =withDesc , 285} , commands ) 286end ) 287 288it ( 'gets callbacks defined as Lua functions' , function () 289exec_lua [[ 290vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts) 291return 3 292end, { 293nargs = 1, 294preview = function() return 4 end, 295complete = function() return 5 end, 296}) 297 298local cmd = vim.api.nvim_get_commands({})["CommandWithLuaCallback"] 299assert(cmd["callback"]() == 3) 300assert(cmd["preview"]() == 4) 301assert(cmd["complete"]() == 5) 302]] 303end ) 304end ) 305 306describe ( 'nvim_create_user_command' , function () 307before_each ( clear ) 308 309it ( 'works with strings' , function () 310api . nvim_create_user_command ( 'SomeCommand' , 'let g:command_fired = <args>' , { nargs =1 } ) 311command ( 'SomeCommand 42' ) 312eq ( 42 , api . nvim_eval ( 'g:command_fired' )) 313end ) 314 315it ( 'does not leak `preview` LuaRef on invalid `cmd`' , function () 316local released =exec_lua ( function () 317local weak =setmetatable ( {} , { __mode ='v' } ) 318for i =1 , 10 do 319local cb =function () end 320weak [ i ] =cb 321pcall ( vim . api . nvim_create_user_command , 'Bogus' .. i , {} , { preview =cb } ) 322end 323collectgarbage ( 'collect' ) 324collectgarbage ( 'collect' ) 325local n =0 326for _ in pairs ( weak ) do 327n =n + 1 328end 329return n 330end ) 331eq ( 0 , released ) 332end ) 333 334it ( 'works with Lua functions' , function () 335exec_lua [[ 336result = {} 337vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts) 338result = opts 339end, { 340nargs = "*", 341bang = true, 342count = 2, 343}) 344]] 345 346eq ( 347{ 348name ='CommandWithLuaCallback' , 349args =[[this\ is a\ test]] , 350fargs ={ 'this ' , 'is' , 'a test' } , 351bang =false , 352line1 =1 , 353line2 =1 , 354mods ='' , 355nargs ='*' , 356smods ={ 357browse =false , 358confirm =false , 359emsg_silent =false , 360filter ={ force =false , pattern ='' } , 361hide =false , 362horizontal =false , 363keepalt =false , 364keepjumps =false , 365keepmarks =false , 366keeppatterns =false , 367lockmarks =false , 368noautocmd =false , 369noswapfile =false , 370sandbox =false , 371silent =false , 372split ='' , 373tab =- 1 , 374unsilent =false , 375verbose =- 1 , 376vertical =false , 377} , 378range =0 , 379count =2 , 380reg ='' , 381} , 382exec_lua [=[ 383vim.api.nvim_command([[CommandWithLuaCallback this\ is a\ test]]) 384return result 385]=] 386) 387 388eq ( 389{ 390name ='CommandWithLuaCallback' , 391args =[[this includes\ a backslash: \\]] , 392fargs ={ 'this' , 'includes a' , 'backslash:' , '\\' } , 393bang =false , 394line1 =1 , 395line2 =1 , 396mods ='' , 397nargs ='*' , 398smods ={ 399browse =false , 400confirm =false , 401emsg_silent =false , 402filter ={ force =false , pattern ='' } , 403hide =false , 404horizontal =false , 405keepalt =false , 406keepjumps =false , 407keepmarks =false , 408keeppatterns =false , 409lockmarks =false , 410noautocmd =false , 411noswapfile =false , 412sandbox =false , 413silent =false , 414split ='' , 415tab =- 1 , 416unsilent =false , 417verbose =- 1 , 418vertical =false , 419} , 420range =0 , 421count =2 , 422reg ='' , 423} , 424exec_lua [=[ 425vim.api.nvim_command([[CommandWithLuaCallback this includes\ a backslash: \\]]) 426return result 427]=] 428) 429 430eq ( 431{ 432name ='CommandWithLuaCallback' , 433args ='a\\b' , 434fargs ={ 'a\\b' } , 435bang =false , 436line1 =1 , 437line2 =1 , 438mods ='' , 439nargs ='*' , 440smods ={ 441browse =false , 442confirm =false , 443emsg_silent =false , 444filter ={ force =false , pattern ='' } , 445hide =false , 446horizontal =false , 447keepalt =false , 448keepjumps =false , 449keepmarks =false , 450keeppatterns =false , 451lockmarks =false , 452noautocmd =false , 453noswapfile =false , 454sandbox =false , 455silent =false , 456split ='' , 457tab =- 1 , 458unsilent =false , 459verbose =- 1 , 460vertical =false , 461} , 462range =0 , 463count =2 , 464reg ='' , 465} , 466exec_lua [=[ 467vim.api.nvim_command('CommandWithLuaCallback a\\b') 468return result 469]=] 470) 471 472eq ( 473{ 474name ='CommandWithLuaCallback' , 475args ='h\tey ' , 476fargs ={ [[h]] , [[ey]] } , 477bang =true , 478line1 =10 , 479line2 =10 , 480mods ='confirm unsilent botright horizontal' , 481nargs ='*' , 482smods ={ 483browse =false , 484confirm =true , 485emsg_silent =false , 486filter ={ force =false , pattern ='' } , 487hide =false , 488horizontal =true , 489keepalt =false , 490keepjumps =false , 491keepmarks =false , 492keeppatterns =false , 493lockmarks =false , 494noautocmd =false , 495noswapfile =false , 496sandbox =false , 497silent =false , 498split ='botright' , 499tab =- 1 , 500unsilent =true , 501verbose =- 1 , 502vertical =false , 503} , 504range =1 , 505count =10 , 506reg ='' , 507} , 508exec_lua [=[ 509vim.api.nvim_command('unsilent horizontal botright confirm 10CommandWithLuaCallback! h\tey ') 510return result 511]=] 512) 513 514eq ( 515{ 516name ='CommandWithLuaCallback' , 517args ='h' , 518fargs ={ 'h' } , 519bang =false , 520line1 =1 , 521line2 =42 , 522mods ='' , 523nargs ='*' , 524smods ={ 525browse =false , 526confirm =false , 527emsg_silent =false , 528filter ={ force =false , pattern ='' } , 529hide =false , 530horizontal =false , 531keepalt =false , 532keepjumps =false , 533keepmarks =false , 534keeppatterns =false , 535lockmarks =false , 536noautocmd =false , 537noswapfile =false , 538sandbox =false , 539silent =false , 540split ='' , 541tab =- 1 , 542unsilent =false , 543verbose =- 1 , 544vertical =false , 545} , 546range =1 , 547count =42 , 548reg ='' , 549} , 550exec_lua [[ 551vim.api.nvim_command('CommandWithLuaCallback 42 h') 552return result 553]] 554) 555 556eq ( 557{ 558name ='CommandWithLuaCallback' , 559args ='' , 560fargs ={} , -- fargs works without args 561bang =false , 562line1 =1 , 563line2 =1 , 564mods ='' , 565nargs ='*' , 566smods ={ 567browse =false , 568confirm =false , 569emsg_silent =false , 570filter ={ force =false , pattern ='' } , 571hide =false , 572horizontal =false , 573keepalt =false , 574keepjumps =false , 575keepmarks =false , 576keeppatterns =false , 577lockmarks =false , 578noautocmd =false , 579noswapfile =false , 580sandbox =false , 581silent =false , 582split ='' , 583tab =- 1 , 584unsilent =false , 585verbose =- 1 , 586vertical =false , 587} , 588range =0 , 589count =2 , 590reg ='' , 591} , 592exec_lua [[ 593vim.api.nvim_command('CommandWithLuaCallback') 594return result 595]] 596) 597 598-- f-args doesn't split when command nargs is 1 or "?" 599exec_lua [[ 600result = {} 601vim.api.nvim_create_user_command('CommandWithOneOrNoArg', function(opts) 602result = opts 603end, { 604nargs = "?", 605bang = true, 606count = 2, 607}) 608]] 609 610eq ( 611{ 612name ='CommandWithOneOrNoArg' , 613args ="hello I'm one argument" , 614fargs ={ "hello I'm one argument" } , -- Doesn't split args 615bang =false , 616line1 =1 , 617line2 =1 , 618mods ='' , 619nargs ='?' , 620smods ={ 621browse =false , 622confirm =false , 623emsg_silent =false , 624filter ={ force =false , pattern ='' } , 625hide =false , 626horizontal =false , 627keepalt =false , 628keepjumps =false , 629keepmarks =false , 630keeppatterns =false , 631lockmarks =false , 632noautocmd =false , 633noswapfile =false , 634sandbox =false , 635silent =false , 636split ='' , 637tab =- 1 , 638unsilent =false , 639verbose =- 1 , 640vertical =false , 641} , 642range =0 , 643count =2 , 644reg ='' , 645} , 646exec_lua [[ 647vim.api.nvim_command('CommandWithOneOrNoArg hello I\'m one argument') 648return result 649]] 650) 651 652-- f-args is an empty table if no args were passed 653eq ( 654{ 655name ='CommandWithOneOrNoArg' , 656args ='' , 657fargs ={} , 658bang =false , 659line1 =1 , 660line2 =1 , 661mods ='' , 662nargs ='?' , 663smods ={ 664browse =false , 665confirm =false , 666emsg_silent =false , 667filter ={ force =false , pattern ='' } , 668hide =false , 669horizontal =false , 670keepalt =false , 671keepjumps =false , 672keepmarks =false , 673keeppatterns =false , 674lockmarks =false , 675noautocmd =false , 676noswapfile =false , 677sandbox =false , 678silent =false , 679split ='' , 680tab =- 1 , 681unsilent =false , 682verbose =- 1 , 683vertical =false , 684} , 685range =0 , 686count =2 , 687reg ='' , 688} , 689exec_lua [[ 690vim.api.nvim_command('CommandWithOneOrNoArg') 691return result 692]] 693) 694 695-- f-args is an empty table when the command nargs=0 696exec_lua [[ 697result = {} 698vim.api.nvim_create_user_command('CommandWithNoArgs', function(opts) 699result = opts 700end, { 701nargs = 0, 702bang = true, 703count = 2, 704register = true, 705}) 706]] 707eq ( 708{ 709name ='CommandWithNoArgs' , 710args ='' , 711fargs ={} , 712bang =false , 713line1 =1 , 714line2 =1 , 715mods ='' , 716nargs ='0' , 717smods ={ 718browse =false , 719confirm =false , 720emsg_silent =false , 721filter ={ force =false , pattern ='' } , 722hide =false , 723horizontal =false , 724keepalt =false , 725keepjumps =false , 726keepmarks =false , 727keeppatterns =false , 728lockmarks =false , 729noautocmd =false , 730noswapfile =false , 731sandbox =false , 732silent =false , 733split ='' , 734tab =- 1 , 735unsilent =false , 736verbose =- 1 , 737vertical =false , 738} , 739range =0 , 740count =2 , 741reg ='' , 742} , 743exec_lua [[ 744vim.cmd('CommandWithNoArgs') 745return result 746]] 747) 748-- register can be specified 749eq ( 750{ 751name ='CommandWithNoArgs' , 752args ='' , 753fargs ={} , 754bang =false , 755line1 =1 , 756line2 =1 , 757mods ='' , 758nargs ='0' , 759smods ={ 760browse =false , 761confirm =false , 762emsg_silent =false , 763filter ={ force =false , pattern ='' } , 764hide =false , 765horizontal =false , 766keepalt =false , 767keepjumps =false , 768keepmarks =false , 769keeppatterns =false , 770lockmarks =false , 771noautocmd =false , 772noswapfile =false , 773sandbox =false , 774silent =false , 775split ='' , 776tab =- 1 , 777unsilent =false , 778verbose =- 1 , 779vertical =false , 780} , 781range =0 , 782count =2 , 783reg ='+' , 784} , 785exec_lua [[ 786vim.cmd('CommandWithNoArgs +') 787return result 788]] 789) 790end ) 791 792it ( 'can define buffer-local commands' , function () 793local bufnr =api . nvim_create_buf ( false , false ) 794api . nvim_buf_create_user_command ( bufnr , 'Hello' , '' , {} ) 795matches ( 'Not an editor command: Hello' , pcall_err ( command , 'Hello' )) 796api . nvim_set_current_buf ( bufnr ) 797command ( 'Hello' ) 798assert_alive () 799eq ( 800'Invalid buffer id: 1234' , 801pcall_err ( api . nvim_buf_create_user_command , 1234 , 'Hello' , '' , {} ) 802) 803assert_alive () 804end ) 805 806it ( 'can use a Lua complete function' , function () 807exec_lua [[ 808vim.api.nvim_create_user_command('Test', '', { 809nargs = "*", 810complete = function(arg, cmdline, pos) 811local options = {"aaa", "bbb", "ccc"} 812local t = {} 813for _, v in ipairs(options) do 814if string.find(v, "^" .. arg) then 815table.insert(t, v) 816end 817end 818return t 819end, 820}) 821]] 822 823feed ( ':Test a<Tab>' ) 824eq ( 'Test aaa' , fn . getcmdline ()) 825feed ( '<C-U>Test b<Tab>' ) 826eq ( 'Test bbb' , fn . getcmdline ()) 827end ) 828 829it ( 'no crash when Lua complete function errors #33447' , function () 830exec_lua ( [[ 831vim.api.nvim_create_user_command('Test','', { 832nargs = 1, 833complete = function() error() end 834}) 835]] ) 836feed ( ':Test <Tab>' ) 837eq ( 'E5108: Lua function: nil' , api . nvim_get_vvar ( 'errmsg' )) 838eq ( 'Test ' , fn . getcmdline ()) 839assert_alive () 840end ) 841 842it ( 'validation' , function () 843matches ( 844"Invalid 'desc'" , 845pcall_err ( 846exec_lua , 847[[ 848vim.api.nvim_create_user_command('Bad', 'echo "hi"', { desc = 123 }) 849]] 850) 851) 852 853eq ( 854"Invalid command name (must start with uppercase): 'test'" , 855pcall_err ( 856exec_lua , 857[[ 858vim.api.nvim_create_user_command('test', 'echo "hi"', {}) 859]] 860) 861) 862eq ( 863"Invalid command name: 't@'" , 864pcall_err ( 865exec_lua , 866[[ 867vim.api.nvim_create_user_command('t@', 'echo "hi"', {}) 868]] 869) 870) 871eq ( 872"Invalid command name: 'T@st'" , 873pcall_err ( 874exec_lua , 875[[ 876vim.api.nvim_create_user_command('T@st', 'echo "hi"', {}) 877]] 878) 879) 880eq ( 881"Invalid command name: 'Test!'" , 882pcall_err ( 883exec_lua , 884[[ 885vim.api.nvim_create_user_command('Test!', 'echo "hi"', {}) 886]] 887) 888) 889eq ( 890"Invalid command name: '💩'" , 891pcall_err ( 892exec_lua , 893[[ 894vim.api.nvim_create_user_command('💩', 'echo "hi"', {}) 895]] 896) 897) 898end ) 899 900it ( 'smods can be used with nvim_cmd' , function () 901exec_lua [[ 902vim.api.nvim_create_user_command('MyEcho', function(opts) 903vim.api.nvim_cmd({ cmd = 'echo', args = { '&verbose' }, mods = opts.smods }, {}) 904end, {}) 905]] 906eq ( '3' , api . nvim_cmd ( { cmd ='MyEcho' , mods ={ verbose =3 } } , { output =true } )) 907 908eq ( 1 , # api . nvim_list_tabpages ()) 909exec_lua [[ 910vim.api.nvim_create_user_command('MySplit', function(opts) 911vim.api.nvim_cmd({ cmd = 'split', mods = opts.smods }, {}) 912end, {}) 913]] 914api . nvim_cmd ( { cmd ='MySplit' } , {} ) 915eq ( 1 , # api . nvim_list_tabpages ()) 916eq ( 2 , # api . nvim_list_wins ()) 917api . nvim_cmd ( { cmd ='MySplit' , mods ={ tab =1 } } , {} ) 918eq ( 2 , # api . nvim_list_tabpages ()) 919eq ( 2 , fn . tabpagenr ()) 920api . nvim_cmd ( { cmd ='MySplit' , mods ={ tab =1 } } , {} ) 921eq ( 3 , # api . nvim_list_tabpages ()) 922eq ( 2 , fn . tabpagenr ()) 923api . nvim_cmd ( { cmd ='MySplit' , mods ={ tab =3 } } , {} ) 924eq ( 4 , # api . nvim_list_tabpages ()) 925eq ( 4 , fn . tabpagenr ()) 926api . nvim_cmd ( { cmd ='MySplit' , mods ={ tab =0 } } , {} ) 927eq ( 5 , # api . nvim_list_tabpages ()) 928eq ( 1 , fn . tabpagenr ()) 929end ) 930 931it ( '"addr" flag allows ranges without explicit "range" flag' , function () 932exec_lua ( [[ 933vim.api.nvim_create_user_command('Test2', 'echo "hello <line1><line2>"', { addr = 'other' }) 934]] ) 935eq ( 'hello 12' , n . exec_capture ( '1,2Test2' )) 936end ) 937end ) 938 939describe ( 'nvim_del_user_command' , function () 940before_each ( clear ) 941 942it ( 'can delete global commands' , function () 943api . nvim_create_user_command ( 'Hello' , 'echo "Hi"' , {} ) 944command ( 'Hello' ) 945api . nvim_del_user_command ( 'Hello' ) 946matches ( 'Not an editor command: Hello' , pcall_err ( command , 'Hello' )) 947end ) 948 949it ( 'can delete buffer-local commands' , function () 950api . nvim_buf_create_user_command ( 0 , 'Hello' , 'echo "Hi"' , {} ) 951command ( 'Hello' ) 952api . nvim_buf_del_user_command ( 0 , 'Hello' ) 953matches ( 'Not an editor command: Hello' , pcall_err ( command , 'Hello' )) 954eq ( 'Invalid command (not found): Hello' , pcall_err ( api . nvim_buf_del_user_command , 0 , 'Hello' )) 955eq ( 'Invalid command (not found): Bye' , pcall_err ( api . nvim_buf_del_user_command , 0 , 'Bye' )) 956eq ( 'Invalid buffer id: 1234' , pcall_err ( api . nvim_buf_del_user_command , 1234 , 'Hello' )) 957assert_alive () 958end ) 959end )