Repository: neovim/neovim
File: test/functional/api/command_spec.lua (Lua)

Code before:
    ]]
    api.nvim_cmd({ cmd = 'MySplit' }, {})
    eq(1, #api.nvim_list_tabpages())
    eq(2, #api.nvim_list_wins())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
    eq(2, #api.nvim_list_tabpages())
    eq(2, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
    eq(3, #api.nvim_list_tabpages())
    eq(2, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 3 } }, {})
    eq(4, #api.nvim_list_tabpages())
    eq(4, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 0 } }, {})
    eq(5, #api.nvim_list_tabpages())
    eq(1, fn.tabpagenr())
  end)

  it('"addr" flag allows ranges without explicit "range" flag', function()
    exec_lua([[
      vim.api.nvim_create_user_command('Test2', 'echo "hello <line1><line2>"', { addr = 'other' })
    ]])
    eq('hello 12', n.exec_capture('1,2Test2'))
  end)

  it('does not crash with % in user command having preview and file completion #28851', function()
    exec_lua([[
      local function callback() end
      local function preview()
        return 0
      end

      vim.api.nvim_create_user_command('TestCommand', callback, {
        nargs = '?',
        complete = 'file',
        preview = preview,
      })

      vim.cmd.edit('Xtestscript')
    ]])
    feed(':TestCommand %')
    assert_alive()
  end)
end)

describe('nvim_del_user_command', function()
  before_each(clear)

  it('can delete global commands', function()
    api.nvim_create_user_command('Hello', 'echo "Hi"', {})
    command('Hello')

Review comment (bug):
```suggestion
  it('no crash with % + preview + file completion #28851', function()
```

Code after:
    ]]
    api.nvim_cmd({ cmd = 'MySplit' }, {})
    eq(1, #api.nvim_list_tabpages())
    eq(2, #api.nvim_list_wins())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
    eq(2, #api.nvim_list_tabpages())
    eq(2, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 1 } }, {})
    eq(3, #api.nvim_list_tabpages())
    eq(2, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 3 } }, {})
    eq(4, #api.nvim_list_tabpages())
    eq(4, fn.tabpagenr())
    api.nvim_cmd({ cmd = 'MySplit', mods = { tab = 0 } }, {})
    eq(5, #api.nvim_list_tabpages())
    eq(1, fn.tabpagenr())
  end)

  it('"addr" flag allows ranges without explicit "range" flag', function()
    exec_lua([[
      vim.api.nvim_create_user_command('Test2', 'echo "hello <line1><line2>"', { addr = 'other' })
    ]])
    eq('hello 12', n.exec_capture('1,2Test2'))
  end)
end)

describe('nvim_del_user_command', function()
  before_each(clear)

  it('can delete global commands', function()
    api.nvim_create_user_command('Hello', 'echo "Hi"', {})
    command('Hello')
    api.nvim_del_user_command('Hello')
    matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
  end)

  it('can delete buffer-local commands', function()
    api.nvim_buf_create_user_command(0, 'Hello', 'echo "Hi"', {})
    command('Hello')
    api.nvim_buf_del_user_command(0, 'Hello')
    matches('Not an editor command: Hello', pcall_err(command, 'Hello'))
    eq('Invalid command (not found): Hello', pcall_err(api.nvim_buf_del_user_command, 0, 'Hello'))
    eq('Invalid command (not found): Bye', pcall_err(api.nvim_buf_del_user_command, 0, 'Bye'))
    eq('Invalid buffer id: 1234', pcall_err(api.nvim_buf_del_user_command, 1234, 'Hello'))
    assert_alive()
  end)
end)