robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #41065 from justinmk/optionfunc4836ecad1f

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