tabpage_spec.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear, eq, ok = n.clear, t.eq, t.ok
  4. local exec = n.exec
  5. local feed = n.feed
  6. local api = n.api
  7. local fn = n.fn
  8. local request = n.request
  9. local NIL = vim.NIL
  10. local pcall_err = t.pcall_err
  11. local command = n.command
  12. describe('api/tabpage', function()
  13. before_each(clear)
  14. describe('list_wins and get_win', function()
  15. it('works', function()
  16. command('tabnew')
  17. command('vsplit')
  18. local tab1, tab2 = unpack(api.nvim_list_tabpages())
  19. local win1, win2, win3 = unpack(api.nvim_list_wins())
  20. eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
  21. eq(win1, api.nvim_tabpage_get_win(tab1))
  22. eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
  23. eq(win2, api.nvim_tabpage_get_win(tab2))
  24. api.nvim_set_current_win(win3)
  25. eq(win3, api.nvim_tabpage_get_win(tab2))
  26. command('tabprev')
  27. eq(win1, api.nvim_tabpage_get_win(tab1))
  28. eq(win3, api.nvim_tabpage_get_win(tab2))
  29. end)
  30. it('validates args', function()
  31. eq('Invalid tabpage id: 23', pcall_err(api.nvim_tabpage_list_wins, 23))
  32. end)
  33. end)
  34. describe('set_win', function()
  35. it('works', function()
  36. command('tabnew')
  37. command('vsplit')
  38. local tab1, tab2 = unpack(api.nvim_list_tabpages())
  39. local win1, win2, win3 = unpack(api.nvim_list_wins())
  40. eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
  41. eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
  42. eq(win2, api.nvim_tabpage_get_win(tab2))
  43. api.nvim_tabpage_set_win(tab2, win3)
  44. eq(win3, api.nvim_tabpage_get_win(tab2))
  45. end)
  46. it('works in non-current tabpages', function()
  47. command('tabnew')
  48. command('vsplit')
  49. local tab1, tab2 = unpack(api.nvim_list_tabpages())
  50. local win1, win2, win3 = unpack(api.nvim_list_wins())
  51. eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
  52. eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
  53. eq(win2, api.nvim_tabpage_get_win(tab2))
  54. eq(win2, api.nvim_get_current_win())
  55. command('tabprev')
  56. eq(tab1, api.nvim_get_current_tabpage())
  57. eq(win2, api.nvim_tabpage_get_win(tab2))
  58. api.nvim_tabpage_set_win(tab2, win3)
  59. eq(win3, api.nvim_tabpage_get_win(tab2))
  60. command('tabnext')
  61. eq(win3, api.nvim_get_current_win())
  62. end)
  63. it('throws an error when the window does not belong to the tabpage', function()
  64. command('tabnew')
  65. command('vsplit')
  66. local tab1, tab2 = unpack(api.nvim_list_tabpages())
  67. local win1, win2, win3 = unpack(api.nvim_list_wins())
  68. eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
  69. eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
  70. eq(win2, api.nvim_get_current_win())
  71. eq(
  72. string.format('Window does not belong to tabpage %d', tab2),
  73. pcall_err(api.nvim_tabpage_set_win, tab2, win1)
  74. )
  75. eq(
  76. string.format('Window does not belong to tabpage %d', tab1),
  77. pcall_err(api.nvim_tabpage_set_win, tab1, win3)
  78. )
  79. end)
  80. it('does not switch window when textlocked or in the cmdwin', function()
  81. local target_win = api.nvim_get_current_win()
  82. feed('q:')
  83. local cur_win = api.nvim_get_current_win()
  84. eq(
  85. 'Vim:E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
  86. pcall_err(api.nvim_tabpage_set_win, 0, target_win)
  87. )
  88. eq(cur_win, api.nvim_get_current_win())
  89. command('quit!')
  90. exec(([[
  91. new
  92. call setline(1, 'foo')
  93. setlocal debug=throw indentexpr=nvim_tabpage_set_win(0,%d)
  94. ]]):format(target_win))
  95. cur_win = api.nvim_get_current_win()
  96. eq(
  97. 'Vim(normal):E5555: API call: Vim:E565: Not allowed to change text or change window',
  98. pcall_err(command, 'normal! ==')
  99. )
  100. eq(cur_win, api.nvim_get_current_win())
  101. end)
  102. end)
  103. describe('{get,set,del}_var', function()
  104. it('works', function()
  105. api.nvim_tabpage_set_var(0, 'lua', { 1, 2, { ['3'] = 1 } })
  106. eq({ 1, 2, { ['3'] = 1 } }, api.nvim_tabpage_get_var(0, 'lua'))
  107. eq({ 1, 2, { ['3'] = 1 } }, api.nvim_eval('t:lua'))
  108. eq(1, fn.exists('t:lua'))
  109. api.nvim_tabpage_del_var(0, 'lua')
  110. eq(0, fn.exists('t:lua'))
  111. eq('Key not found: lua', pcall_err(api.nvim_tabpage_del_var, 0, 'lua'))
  112. api.nvim_tabpage_set_var(0, 'lua', 1)
  113. command('lockvar t:lua')
  114. eq('Key is locked: lua', pcall_err(api.nvim_tabpage_del_var, 0, 'lua'))
  115. eq('Key is locked: lua', pcall_err(api.nvim_tabpage_set_var, 0, 'lua', 1))
  116. end)
  117. it('tabpage_set_var returns the old value', function()
  118. local val1 = { 1, 2, { ['3'] = 1 } }
  119. local val2 = { 4, 7 }
  120. eq(NIL, request('tabpage_set_var', 0, 'lua', val1))
  121. eq(val1, request('tabpage_set_var', 0, 'lua', val2))
  122. end)
  123. it('tabpage_del_var returns the old value', function()
  124. local val1 = { 1, 2, { ['3'] = 1 } }
  125. local val2 = { 4, 7 }
  126. eq(NIL, request('tabpage_set_var', 0, 'lua', val1))
  127. eq(val1, request('tabpage_set_var', 0, 'lua', val2))
  128. eq(val2, request('tabpage_del_var', 0, 'lua'))
  129. end)
  130. end)
  131. describe('get_number', function()
  132. it('works', function()
  133. local tabs = api.nvim_list_tabpages()
  134. eq(1, api.nvim_tabpage_get_number(tabs[1]))
  135. command('tabnew')
  136. local tab1, tab2 = unpack(api.nvim_list_tabpages())
  137. eq(1, api.nvim_tabpage_get_number(tab1))
  138. eq(2, api.nvim_tabpage_get_number(tab2))
  139. command('-tabmove')
  140. eq(2, api.nvim_tabpage_get_number(tab1))
  141. eq(1, api.nvim_tabpage_get_number(tab2))
  142. end)
  143. end)
  144. describe('is_valid', function()
  145. it('works', function()
  146. command('tabnew')
  147. local tab = api.nvim_list_tabpages()[2]
  148. api.nvim_set_current_tabpage(tab)
  149. ok(api.nvim_tabpage_is_valid(tab))
  150. command('tabclose')
  151. ok(not api.nvim_tabpage_is_valid(tab))
  152. end)
  153. end)
  154. end)