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

Code before:
      eq(api.nvim_list_bufs()[1], api.nvim_get_current_buf())
      command('new')
      eq(2, #api.nvim_list_bufs())
      eq(api.nvim_list_bufs()[2], api.nvim_get_current_buf())
      api.nvim_set_current_buf(api.nvim_list_bufs()[1])
      eq(api.nvim_list_bufs()[1], api.nvim_get_current_buf())
    end)
  end)

  describe('nvim_{get,set}_current_win, nvim_list_wins', function()
    it('works', function()
      eq(1, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[1], api.nvim_get_current_win())
      command('vsplit')
      command('split')
      eq(3, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[1], api.nvim_get_current_win())
      api.nvim_set_current_win(api.nvim_list_wins()[2])
      eq(api.nvim_list_wins()[2], api.nvim_get_current_win())
    end)

    it('resets Visual mode when switching to different buffer #37072', function()
      command('new | wincmd w')
      api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' })
      api.nvim_win_set_cursor(0, { 2, 0 })
      api.nvim_feedkeys(api.nvim_replace_termcodes('<C-q>', true, false, true), 'x', false)
      api.nvim_set_current_win(fn.win_getid(fn.winnr('#')))
      eq(true, pcall(command, 'redraw'))
    end)

    it('failure modes', function()
      n.command('split')

      eq('Invalid window id: 9999', pcall_err(api.nvim_set_current_win, 9999))

      -- XXX: force nvim_set_current_win to fail somehow.
      n.command("au WinLeave * throw 'foo'")
      eq('WinLeave Autocommands for "*": foo', pcall_err(api.nvim_set_current_win, 1000))
    end)
  end)

  describe('nvim_{get,set}_current_tabpage, nvim_list_tabpages', function()
    it('works', function()
      eq(1, #api.nvim_list_tabpages())
      eq(api.nvim_list_tabpages()[1], api.nvim_get_current_tabpage())
      command('tabnew')
      eq(2, #api.nvim_list_tabpages())
      eq(2, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[2], api.nvim_get_current_win())
      eq(api.nvim_list_tabpages()[2], api.nvim_get_current_tabpage())
      api.nvim_set_current_win(api.nvim_list_wins()[1])

Review comment (suggestion):
```suggestion
      feed('<C-q>')
      eq({ mode = '\022', blocking = false }, api.nvim_get_mode())
```

Code after:
      eq(api.nvim_list_bufs()[1], api.nvim_get_current_buf())
      command('new')
      eq(2, #api.nvim_list_bufs())
      eq(api.nvim_list_bufs()[2], api.nvim_get_current_buf())
      api.nvim_set_current_buf(api.nvim_list_bufs()[1])
      eq(api.nvim_list_bufs()[1], api.nvim_get_current_buf())
    end)
  end)

  describe('nvim_{get,set}_current_win, nvim_list_wins', function()
    it('works', function()
      eq(1, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[1], api.nvim_get_current_win())
      command('vsplit')
      command('split')
      eq(3, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[1], api.nvim_get_current_win())
      api.nvim_set_current_win(api.nvim_list_wins()[2])
      eq(api.nvim_list_wins()[2], api.nvim_get_current_win())
    end)

    it('resets Visual mode when switching to different buffer #37072', function()
      command('new | wincmd w')
      api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b' })
      api.nvim_win_set_cursor(0, { 2, 0 })
      feed('<C-q>')
      eq({ mode = '\022', blocking = false }, api.nvim_get_mode())
      api.nvim_set_current_win(fn.win_getid(fn.winnr('#')))
      eq(true, pcall(command, 'redraw'))
    end)

    it('failure modes', function()
      n.command('split')

      eq('Invalid window id: 9999', pcall_err(api.nvim_set_current_win, 9999))

      -- XXX: force nvim_set_current_win to fail somehow.
      n.command("au WinLeave * throw 'foo'")
      eq('WinLeave Autocommands for "*": foo', pcall_err(api.nvim_set_current_win, 1000))
    end)
  end)

  describe('nvim_{get,set}_current_tabpage, nvim_list_tabpages', function()
    it('works', function()
      eq(1, #api.nvim_list_tabpages())
      eq(api.nvim_list_tabpages()[1], api.nvim_get_current_tabpage())
      command('tabnew')
      eq(2, #api.nvim_list_tabpages())
      eq(2, #api.nvim_list_wins())
      eq(api.nvim_list_wins()[2], api.nvim_get_current_win())
      eq(api.nvim_list_tabpages()[2], api.nvim_get_current_tabpage())