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

Code before:
    command('command -nargs=1 -complete=dir -addr=arguments -count=10 TestCmd pwd <args>')
    eq({ Finger = cmd1, TestCmd = cmd0 }, api.nvim_get_commands({ builtin = false }))

    source([[
      function! s:foo() abort
      endfunction
      command -bang -nargs=* Cmd2 call <SID>foo(<q-args>)
    ]])
    source([[
      function! s:ohyeah() abort
      endfunction
      command -bar -nargs=0 Cmd3 call <SID>ohyeah()
    ]])
    source([[
      function! s:just_great() abort
      endfunction
      command -register Cmd4 call <SID>just_great()
    ]])
    -- TODO(justinmk): Order is stable but undefined. Sort before return?
    eq(
      { Cmd2 = cmd2, Cmd3 = cmd3, Cmd4 = cmd4, Finger = cmd1, TestCmd = cmd0 },
      api.nvim_get_commands({ builtin = false })
    )
  end)

  it('works with Lua functions', function()
    exec_lua [[
      vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts)
        return 3
      end, {})

      local cb = vim.api.nvim_get_commands({})["CommandWithLuaCallback"]["callback"]
      assert(cb() == 3)
    ]]
  end)
end)

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

  it('works with strings', function()
    api.nvim_create_user_command('SomeCommand', 'let g:command_fired = <args>', { nargs = 1 })
    command('SomeCommand 42')
    eq(42, api.nvim_eval('g:command_fired'))
  end)

  it('works with Lua functions', function()
    exec_lua [[
      result = {}
      vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts)
        result = opts

Review comment (suggestion):
can test `preview`/ `complete` in here too?

```suggestion
  it('gets callbacks defined as Lua functions', function()
```

Code after:
    ]])
    source([[
      function! s:ohyeah() abort
      endfunction
      command -bar -nargs=0 Cmd3 call <SID>ohyeah()
    ]])
    source([[
      function! s:just_great() abort
      endfunction
      command -register Cmd4 call <SID>just_great()
    ]])
    source([[
      function! s:cpt() abort
        return 1
      endfunction
      command -nargs=1 -complete=customlist,s:cpt CmdWithPreview
    ]])
    source([[
      lua << EOF
      vim.api.nvim_create_user_command(
        'CmdWithPreviewLua',
        function() end,
        {
          nargs = 1,
          complete = function() return 3 end,
          preview = function() return 4 end,
        }
      )
      EOF
    ]])
    -- TODO(justinmk): Order is stable but undefined. Sort before return?
    local commands = api.nvim_get_commands({ builtin = false })
    local cmd_with_preview = commands.CmdWithPreview
    commands.CmdWithPreview = nil
    local cmd_with_preview_lua = commands.CmdWithPreviewLua
    commands.CmdWithPreviewLua = nil
    eq({ Cmd2 = cmd2, Cmd3 = cmd3, Cmd4 = cmd4, Finger = cmd1, TestCmd = cmd0 }, commands)

    eq(cmd_with_preview.complete, 'customlist')
    eq(cmd_with_preview.preview, nil)

    -- user data (NIL), because these are passed through RPC:
    eq(cmd_with_preview_lua.complete, NIL)
    eq(cmd_with_preview_lua.preview, NIL)
  end)

  it('gets callbacks defined as Lua functions', function()
    exec_lua [[
      vim.api.nvim_create_user_command('CommandWithLuaCallback', function(opts)
        return 3
      end, {