robin/laufey

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

robinmerge: 9a29622b3b00ddb601

master
17.4 KiB490 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3local Screen = require('test.functional.ui.screen')
4
5local describe, it, before_each = t.describe, t.it, t.before_each
6local clear, eq, ok = n.clear, t.eq, t.ok
7local matches = t.matches
8local exec = n.exec
9local api = n.api
10local fn = n.fn
11local request = n.request
12local NIL = vim.NIL
13local pcall_err = t.pcall_err
14local command = n.command
15
16describe('api/tabpage', function()
17  before_each(clear)
18
19  describe('list_wins and get_win', function()
20    it('works', function()
21      command('tabnew')
22      command('vsplit')
23      local tab1, tab2 = unpack(api.nvim_list_tabpages())
24      local win1, win2, win3 = unpack(api.nvim_list_wins())
25      eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
26      eq(win1, api.nvim_tabpage_get_win(tab1))
27      eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
28      eq(win2, api.nvim_tabpage_get_win(tab2))
29      api.nvim_set_current_win(win3)
30      eq(win3, api.nvim_tabpage_get_win(tab2))
31      command('tabprev')
32      eq(win1, api.nvim_tabpage_get_win(tab1))
33      eq(win3, api.nvim_tabpage_get_win(tab2))
34    end)
35
36    it('validates args', function()
37      eq('Invalid tabpage id: 23', pcall_err(api.nvim_tabpage_list_wins, 23))
38    end)
39  end)
40
41  describe('set_win', function()
42    it('works', function()
43      command('tabnew')
44      command('vsplit')
45      local tab1, tab2 = unpack(api.nvim_list_tabpages())
46      local win1, win2, win3 = unpack(api.nvim_list_wins())
47      eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
48      eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
49      eq(win2, api.nvim_tabpage_get_win(tab2))
50      api.nvim_tabpage_set_win(tab2, win3)
51      eq(win3, api.nvim_tabpage_get_win(tab2))
52    end)
53
54    it('works in non-current tabpages', function()
55      command('tabnew')
56      command('vsplit')
57      local tab1, tab2 = unpack(api.nvim_list_tabpages())
58      local win1, win2, win3 = unpack(api.nvim_list_wins())
59      eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
60      eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
61      eq(win2, api.nvim_tabpage_get_win(tab2))
62      eq(win2, api.nvim_get_current_win())
63
64      command('tabprev')
65
66      eq(tab1, api.nvim_get_current_tabpage())
67
68      eq(win2, api.nvim_tabpage_get_win(tab2))
69      api.nvim_tabpage_set_win(tab2, win3)
70      eq(win3, api.nvim_tabpage_get_win(tab2))
71
72      command('tabnext')
73      eq(win3, api.nvim_get_current_win())
74    end)
75
76    it('throws an error when the window does not belong to the tabpage', function()
77      command('tabnew')
78      command('vsplit')
79      local tab1, tab2 = unpack(api.nvim_list_tabpages())
80      local win1, win2, win3 = unpack(api.nvim_list_wins())
81      eq({ win1 }, api.nvim_tabpage_list_wins(tab1))
82      eq({ win2, win3 }, api.nvim_tabpage_list_wins(tab2))
83      eq(win2, api.nvim_get_current_win())
84
85      eq(
86        string.format('Window does not belong to tabpage %d', tab2),
87        pcall_err(api.nvim_tabpage_set_win, tab2, win1)
88      )
89
90      eq(
91        string.format('Window does not belong to tabpage %d', tab1),
92        pcall_err(api.nvim_tabpage_set_win, tab1, win3)
93      )
94    end)
95
96    it('checks textlock restrictions', function()
97      command('autocmd TextYankPost * ++once call nvim_open_tabpage(0, 0, {})')
98      matches('E565:', pcall_err(command, 'yank'))
99      eq(1, fn.tabpagenr('$'))
100
101      exec([[
102        new
103        call setline(1, 'foo')
104        setlocal debug=throw indentexpr=nvim_tabpage_set_win(0,win_getid())
105      ]])
106      local cur_win = api.nvim_get_current_win()
107      matches('E565: Not allowed to change text or change window', pcall_err(command, 'normal! =='))
108      eq(cur_win, api.nvim_get_current_win())
109    end)
110  end)
111
112  describe('{get,set,del}_var', function()
113    it('works', function()
114      api.nvim_tabpage_set_var(0, 'lua', { 1, 2, { ['3'] = 1 } })
115      eq({ 1, 2, { ['3'] = 1 } }, api.nvim_tabpage_get_var(0, 'lua'))
116      eq({ 1, 2, { ['3'] = 1 } }, api.nvim_eval('t:lua'))
117      eq(1, fn.exists('t:lua'))
118      api.nvim_tabpage_del_var(0, 'lua')
119      eq(0, fn.exists('t:lua'))
120      eq('Key not found: lua', pcall_err(api.nvim_tabpage_del_var, 0, 'lua'))
121      api.nvim_tabpage_set_var(0, 'lua', 1)
122      command('lockvar t:lua')
123      eq('Key is locked: lua', pcall_err(api.nvim_tabpage_del_var, 0, 'lua'))
124      eq('Key is locked: lua', pcall_err(api.nvim_tabpage_set_var, 0, 'lua', 1))
125    end)
126
127    it('tabpage_set_var returns the old value', function()
128      local val1 = { 1, 2, { ['3'] = 1 } }
129      local val2 = { 4, 7 }
130      eq(NIL, request('tabpage_set_var', 0, 'lua', val1))
131      eq(val1, request('tabpage_set_var', 0, 'lua', val2))
132    end)
133
134    it('tabpage_del_var returns the old value', function()
135      local val1 = { 1, 2, { ['3'] = 1 } }
136      local val2 = { 4, 7 }
137      eq(NIL, request('tabpage_set_var', 0, 'lua', val1))
138      eq(val1, request('tabpage_set_var', 0, 'lua', val2))
139      eq(val2, request('tabpage_del_var', 0, 'lua'))
140    end)
141  end)
142
143  describe('get_number', function()
144    it('works', function()
145      local tabs = api.nvim_list_tabpages()
146      eq(1, api.nvim_tabpage_get_number(tabs[1]))
147
148      command('tabnew')
149      local tab1, tab2 = unpack(api.nvim_list_tabpages())
150      eq(1, api.nvim_tabpage_get_number(tab1))
151      eq(2, api.nvim_tabpage_get_number(tab2))
152
153      command('-tabmove')
154      eq(2, api.nvim_tabpage_get_number(tab1))
155      eq(1, api.nvim_tabpage_get_number(tab2))
156    end)
157  end)
158
159  describe('is_valid', function()
160    it('works', function()
161      command('tabnew')
162      local tab = api.nvim_list_tabpages()[2]
163      api.nvim_set_current_tabpage(tab)
164      ok(api.nvim_tabpage_is_valid(tab))
165      command('tabclose')
166      ok(not api.nvim_tabpage_is_valid(tab))
167    end)
168  end)
169
170  describe('open_tabpage', function()
171    it('works', function()
172      local tabs = api.nvim_list_tabpages()
173      eq(1, #tabs)
174      local curtab = api.nvim_get_current_tabpage()
175      local tab = api.nvim_open_tabpage(0, false, {})
176      local newtabs = api.nvim_list_tabpages()
177      eq(2, #newtabs)
178      eq(tab, newtabs[2])
179      eq(curtab, api.nvim_get_current_tabpage())
180
181      local tab2 = api.nvim_open_tabpage(0, true, {})
182      local newtabs2 = api.nvim_list_tabpages()
183      eq(3, #newtabs2)
184      eq({
185        tabs[1],
186        tab2, -- new tabs open after the current tab
187        tab,
188      }, newtabs2)
189      eq(tab2, newtabs2[2])
190      eq(tab, newtabs2[3])
191      eq(tab2, api.nvim_get_current_tabpage())
192    end)
193
194    it('respects the `after` option', function()
195      local tab1 = api.nvim_get_current_tabpage()
196      command('tabnew')
197      local tab2 = api.nvim_get_current_tabpage()
198      command('tabnew')
199      local tab3 = api.nvim_get_current_tabpage()
200
201      local newtabs = api.nvim_list_tabpages()
202      eq(3, #newtabs)
203      eq({
204        tab1,
205        tab2,
206        -- new_tab,
207        tab3,
208      }, newtabs)
209
210      local new_tab = api.nvim_open_tabpage(0, false, { after = api.nvim_tabpage_get_number(tab2) })
211      local newtabs2 = api.nvim_list_tabpages()
212      eq(4, #newtabs2)
213      eq({
214        tab1,
215        tab2,
216        new_tab,
217        tab3,
218      }, newtabs2)
219      eq(tab3, api.nvim_get_current_tabpage())
220    end)
221
222    it('respects the `enter` argument', function()
223      local screen = Screen.new(50, 8)
224      eq(1, #api.nvim_list_tabpages())
225      local tab1 = api.nvim_get_current_tabpage()
226
227      local new_tab = api.nvim_open_tabpage(0, false, {})
228      local newtabs = api.nvim_list_tabpages()
229      eq(2, #newtabs)
230      eq({ tab1, new_tab }, newtabs)
231      eq(tab1, api.nvim_get_current_tabpage())
232      -- Tabline redrawn when not entering.
233      screen:expect([[
234        {5: [No Name] }{24: [No Name] }{2:                           }{24:X}|
235        ^                                                  |
236        {1:~                                                 }|*5
237                                                          |
238      ]])
239
240      local new_tab2 = api.nvim_open_tabpage(0, true, {})
241      local newtabs2 = api.nvim_list_tabpages()
242      eq(3, #newtabs2)
243      eq({ tab1, new_tab2, new_tab }, newtabs2)
244      eq(new_tab2, api.nvim_get_current_tabpage())
245      -- Tabline redrawn. (when entering)
246      screen:expect([[
247        {24: [No Name] }{5: [No Name] }{24: [No Name] }{2:                }{24:X}|
248        ^                                                  |
249        {1:~                                                 }|*5
250                                                          |
251      ]])
252
253      api.nvim_open_tabpage(0, false, {})
254      -- Tabline redrawn when not entering, and when there's already one.
255      screen:expect([[
256        {24: [No Name] }{5: [No Name] }{24: [No Name]  [No Name] }{2:     }{24:X}|
257        ^                                                  |
258        {1:~                                                 }|*5
259                                                          |
260      ]])
261    end)
262
263    it('applies autocmds in the context of the new tabpage', function()
264      exec([=[
265        let g:events = []
266        autocmd WinNew * let g:events += [['WinNew', nvim_get_current_tabpage(), win_getid()]]
267        autocmd WinEnter * let g:events += [['WinEnter', nvim_get_current_tabpage(), win_getid()]]
268        autocmd TabNew * let g:events += [['TabNew', nvim_get_current_tabpage(), win_getid()]]
269        autocmd TabEnter * let g:events += [['TabEnter', nvim_get_current_tabpage(), win_getid()]]
270        autocmd BufEnter * let g:events += [['BufEnter', nvim_get_current_tabpage(), win_getid()]]
271        autocmd BufLeave * let g:events += [['BufLeave', nvim_get_current_tabpage(), win_getid()]]
272        autocmd BufWinEnter * let g:events += [['BufWinEnter', nvim_get_current_tabpage(), win_getid()]]
273      ]=])
274
275      local new_tab = api.nvim_open_tabpage(0, true, {})
276      local new_win = api.nvim_tabpage_get_win(new_tab)
277      eq({
278        { 'WinNew', new_tab, new_win },
279        { 'WinEnter', new_tab, new_win },
280        { 'TabNew', new_tab, new_win },
281        { 'TabEnter', new_tab, new_win },
282      }, api.nvim_get_var('events'))
283      eq(new_win, api.nvim_get_current_win())
284
285      api.nvim_set_var('events', {})
286      new_tab = api.nvim_open_tabpage(api.nvim_create_buf(true, true), true, {})
287      new_win = api.nvim_tabpage_get_win(new_tab)
288      eq({
289        { 'WinNew', new_tab, new_win },
290        { 'WinEnter', new_tab, new_win },
291        { 'TabNew', new_tab, new_win },
292        { 'TabEnter', new_tab, new_win },
293        { 'BufLeave', new_tab, new_win },
294        { 'BufEnter', new_tab, new_win },
295        { 'BufWinEnter', new_tab, new_win },
296      }, api.nvim_get_var('events'))
297
298      local curwin = new_win
299      api.nvim_set_var('events', {})
300      new_tab = api.nvim_open_tabpage(0, false, {})
301      new_win = api.nvim_tabpage_get_win(new_tab)
302      eq(
303        { { 'WinNew', new_tab, new_win }, { 'TabNew', new_tab, new_win } },
304        api.nvim_get_var('events')
305      )
306      eq(curwin, api.nvim_get_current_win())
307
308      api.nvim_set_var('events', {})
309      new_tab = api.nvim_open_tabpage(api.nvim_create_buf(true, true), false, {})
310      new_win = api.nvim_tabpage_get_win(new_tab)
311      eq({
312        { 'WinNew', new_tab, new_win },
313        { 'TabNew', new_tab, new_win },
314        { 'BufWinEnter', new_tab, new_win },
315      }, api.nvim_get_var('events'))
316      eq(curwin, api.nvim_get_current_win())
317    end)
318
319    it('handles nasty autocmds', function()
320      command('autocmd WinNewPre * ++once call nvim_open_tabpage(0, 0, {})')
321      matches('E1312:', pcall_err(command, 'split'))
322
323      command('autocmd TabNew * ++once quit')
324      eq('Tabpage was closed immediately', pcall_err(api.nvim_open_tabpage, 0, false, {}))
325      command('autocmd BufEnter * ++once quit')
326      local buf = api.nvim_create_buf(true, true)
327      eq('Tabpage was closed immediately', pcall_err(api.nvim_open_tabpage, buf, true, {}))
328
329      -- No error if autocmds delete target buffer, if new tabpage is still valid to return.
330      command('autocmd BufEnter * ++once buffer # | bwipeout! #')
331      local new_tp = api.nvim_open_tabpage(buf, true, {})
332      eq(false, api.nvim_buf_is_valid(buf))
333      eq(new_tp, api.nvim_get_current_tabpage())
334    end)
335
336    it('handles edge cases for positioning', function()
337      -- Start with 3 tabs
338      local tab1 = api.nvim_get_current_tabpage()
339      command('tabnew')
340      local tab2 = api.nvim_get_current_tabpage()
341      command('tabnew')
342      local tab3 = api.nvim_get_current_tabpage()
343
344      local initial_tabs = api.nvim_list_tabpages()
345      eq(3, #initial_tabs)
346      eq({ tab1, tab2, tab3 }, initial_tabs)
347
348      -- Test after=0: should become first tab
349      local first_tab = api.nvim_open_tabpage(0, false, { after = 0 })
350      local tabs_after_first = api.nvim_list_tabpages()
351      eq(4, #tabs_after_first)
352      eq({ first_tab, tab1, tab2, tab3 }, tabs_after_first)
353
354      -- Test after=-1: should insert after current tab (tab3)
355      local explicit_after_current = api.nvim_open_tabpage(0, false, { after = -1 })
356      local tabs_after_current = api.nvim_list_tabpages()
357      eq(5, #tabs_after_current)
358      eq({ first_tab, tab1, tab2, tab3, explicit_after_current }, tabs_after_current)
359
360      -- Test inserting before a middle tab (before tab2, which is now number 3)
361      local before_middle = api.nvim_open_tabpage(0, false, { after = 2 })
362      local tabs_after_middle = api.nvim_list_tabpages()
363      eq(6, #tabs_after_middle)
364      eq({ first_tab, tab1, before_middle, tab2, tab3, explicit_after_current }, tabs_after_middle)
365
366      eq(tab3, api.nvim_get_current_tabpage())
367
368      -- Test default behavior (after current)
369      local default_after_current = api.nvim_open_tabpage(0, false, {})
370      local final_tabs = api.nvim_list_tabpages()
371      eq(7, #final_tabs)
372      eq({
373        first_tab,
374        tab1,
375        before_middle,
376        tab2,
377        tab3,
378        default_after_current,
379        explicit_after_current,
380      }, final_tabs)
381    end)
382
383    it('handles position beyond last tab', function()
384      -- Create a few tabs first
385      local tab1 = api.nvim_get_current_tabpage()
386      command('tabnew')
387      local tab2 = api.nvim_get_current_tabpage()
388      command('tabnew')
389      local tab3 = api.nvim_get_current_tabpage()
390
391      eq(3, #api.nvim_list_tabpages())
392      eq({ tab1, tab2, tab3 }, api.nvim_list_tabpages())
393
394      -- Test that requesting position beyond last tab still works
395      -- (should place it at the end)
396      local new_tab = api.nvim_open_tabpage(0, false, { after = 10 }) -- Way beyond the last tab
397      local final_tabs = api.nvim_list_tabpages()
398      eq(4, #final_tabs)
399      -- Should append at the end
400      eq({ tab1, tab2, tab3, new_tab }, final_tabs)
401    end)
402
403    it('works with specific buffer', function()
404      local curbuf = api.nvim_get_current_buf()
405      local new_tab = api.nvim_open_tabpage(0, false, {})
406      api.nvim_set_current_tabpage(new_tab)
407      eq(curbuf, api.nvim_get_current_buf())
408
409      local buf = api.nvim_create_buf(false, false)
410      api.nvim_buf_set_lines(buf, 0, -1, false, { 'test content' })
411
412      local original_tab = api.nvim_get_current_tabpage()
413      local original_buf = api.nvim_get_current_buf()
414      new_tab = api.nvim_open_tabpage(buf, true, {}) -- Enter the tab to make testing easier
415
416      -- Check that new tab has the specified buffer
417      eq(new_tab, api.nvim_get_current_tabpage())
418      eq(buf, api.nvim_get_current_buf())
419      eq({ 'test content' }, api.nvim_buf_get_lines(buf, 0, -1, false))
420
421      -- Switch back and check original tab still has original buffer
422      api.nvim_set_current_tabpage(original_tab)
423      eq(original_buf, api.nvim_get_current_buf())
424
425      -- Test invalid buffer
426      eq('Invalid buffer id: 999', pcall_err(api.nvim_open_tabpage, 999, true, {}))
427    end)
428
429    it('handles complex positioning scenarios', function()
430      -- Create 5 tabs total
431      local tabs = { api.nvim_get_current_tabpage() }
432      for i = 2, 5 do
433        command('tabnew')
434        tabs[i] = api.nvim_get_current_tabpage()
435      end
436      eq(5, #api.nvim_list_tabpages())
437
438      -- Go to middle tab (tab 3)
439      api.nvim_set_current_tabpage(tabs[3])
440
441      -- Insert after=0 (after current, which is tab 3)
442      local new_after_current = api.nvim_open_tabpage(0, false, {})
443      local result_tabs = api.nvim_list_tabpages()
444      eq(6, #result_tabs)
445      eq({
446        tabs[1],
447        tabs[2],
448        tabs[3],
449        new_after_current,
450        tabs[4],
451        tabs[5],
452      }, result_tabs)
453
454      -- Insert as number 2 (before tab2, which will become number 3)
455      local new_at_pos2 = api.nvim_open_tabpage(0, false, { after = 1 })
456      local final_result = api.nvim_list_tabpages()
457      eq(7, #final_result)
458      eq({
459        tabs[1],
460        new_at_pos2,
461        tabs[2],
462        tabs[3],
463        new_after_current,
464        tabs[4],
465        tabs[5],
466      }, final_result)
467    end)
468
469    it('preserves tab order when entering new tabs', function()
470      local tab1 = api.nvim_get_current_tabpage()
471      command('tabnew')
472      local tab2 = api.nvim_get_current_tabpage()
473
474      -- Create new tab with enter=true, should insert after current (tab2)
475      local tab3 = api.nvim_open_tabpage(0, true, { after = -1 })
476      local tabs = api.nvim_list_tabpages()
477      eq(3, #tabs)
478      eq({ tab1, tab2, tab3 }, tabs)
479      eq(tab3, api.nvim_get_current_tabpage())
480
481      -- Create another with enter=true and specific position
482      api.nvim_set_current_tabpage(tab1)
483      local tab4 = api.nvim_open_tabpage(0, true, { after = 0 }) -- Should become first tab
484      local final_tabs = api.nvim_list_tabpages()
485      eq(4, #final_tabs)
486      eq({ tab4, tab1, tab2, tab3 }, final_tabs)
487      eq(tab4, api.nvim_get_current_tabpage())
488    end)
489  end)
490end)