robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
20.7 KiB678 linesraw
1local t = require('test.testutil')
2local n = require('test.functional.testnvim')()
3
4local describe, it, before_each = t.describe, t.it, t.before_each
5local clear = n.clear
6local command = n.command
7local dedent = t.dedent
8local eval = n.eval
9local eq = t.eq
10local feed = n.feed
11local api = n.api
12local exec_capture = n.exec_capture
13
14describe('TabNewEntered', function()
15  describe('au TabNewEntered', function()
16    describe('with * as <afile>', function()
17      it('matches when entering any new tab', function()
18        clear()
19        command('au! TabNewEntered * echom "tabnewentered:".tabpagenr().":".bufnr("")')
20        eq('tabnewentered:2:2', api.nvim_exec('tabnew', true))
21        eq('tabnewentered:3:3', api.nvim_exec('tabnew test.x2', true))
22      end)
23    end)
24    describe('with FILE as <afile>', function()
25      it('matches when opening a new tab for FILE', function()
26        clear()
27        command('au! TabNewEntered Xtest-tabnewentered echom "tabnewentered:match"')
28        eq('tabnewentered:match', api.nvim_exec('tabnew Xtest-tabnewentered', true))
29      end)
30    end)
31    describe('with CTRL-W T', function()
32      it('works when opening a new tab with CTRL-W T', function()
33        clear()
34        command('au! TabNewEntered * echom "entered"')
35        command('tabnew test.x2')
36        command('split')
37        eq('entered', api.nvim_exec('execute "normal \\<C-W>T"', true))
38      end)
39    end)
40    describe('with tab split #4334', function()
41      it('works when create a tab by using tab split command', function()
42        clear()
43        command('au! TabNewEntered * let b:entered = "entered"')
44        command('tab split')
45        eq('entered', api.nvim_exec('echo b:entered', true))
46      end)
47    end)
48  end)
49end)
50
51describe('TabEnter', function()
52  before_each(clear)
53  it('has correct previous tab when entering any new tab', function()
54    command('augroup TEMP')
55    command('au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')')
56    command('augroup END')
57    eq('tabenter:2:1', api.nvim_exec('tabnew', true))
58    eq('tabenter:3:2', api.nvim_exec('tabnew test.x2', true))
59    command('augroup! TEMP')
60  end)
61  it('has correct previous tab when entering any preexisting tab', function()
62    command('tabnew')
63    command('tabnew')
64    command('augroup TEMP')
65    command('au! TabEnter * echom "tabenter:".tabpagenr().":".tabpagenr(\'#\')')
66    command('augroup END')
67    eq('tabenter:1:3', api.nvim_exec('tabnext', true))
68    eq('tabenter:2:1', api.nvim_exec('tabnext', true))
69    command('augroup! TEMP')
70  end)
71end)
72
73describe('tabpage/previous', function()
74  before_each(clear)
75  local function switches_to_previous_after_new_tab_creation_at_end(characters)
76    return function()
77      -- Add three tabs for a total of four
78      command('tabnew')
79      command('tabnew')
80      command('tabnew')
81
82      -- The previous tab is now the third.
83      eq(3, eval("tabpagenr('#')"))
84
85      -- Switch to the previous (third) tab
86      feed(characters)
87
88      eq(
89        dedent([=[
90          Tab page 1
91              [No Name]
92          Tab page 2
93              [No Name]
94          Tab page 3
95          >   [No Name]
96          Tab page 4
97          #   [No Name]]=]),
98        exec_capture('tabs')
99      )
100
101      -- The previous tab is now the fourth.
102      eq(4, eval("tabpagenr('#')"))
103    end
104  end
105  it(
106    'switches to previous via g<Tab> after new tab creation at end',
107    switches_to_previous_after_new_tab_creation_at_end('g<Tab>')
108  )
109  it(
110    'switches to previous via <C-W>g<Tab>. after new tab creation at end',
111    switches_to_previous_after_new_tab_creation_at_end('<C-W>g<Tab>')
112  )
113  it(
114    'switches to previous via <C-Tab>. after new tab creation at end',
115    switches_to_previous_after_new_tab_creation_at_end('<C-Tab>')
116  )
117  it(
118    'switches to previous via :tabn #<CR>. after new tab creation at end',
119    switches_to_previous_after_new_tab_creation_at_end(':tabn #<CR>')
120  )
121
122  local function switches_to_previous_after_new_tab_creation_in_middle(characters)
123    return function()
124      -- Add three tabs for a total of four
125      command('tabnew')
126      command('tabnew')
127      command('tabnew')
128      -- Switch to the second tab
129      command('tabnext 2')
130      -- Add a new tab after the second tab
131      command('tabnew')
132
133      -- The previous tab is now the second.
134      eq(2, eval("tabpagenr('#')"))
135
136      -- Switch to the previous (second) tab
137      feed(characters)
138      eq(
139        dedent([=[
140         Tab page 1
141             [No Name]
142         Tab page 2
143         >   [No Name]
144         Tab page 3
145         #   [No Name]
146         Tab page 4
147             [No Name]
148         Tab page 5
149             [No Name]]=]),
150        exec_capture('tabs')
151      )
152
153      -- The previous tab is now the third.
154      eq(3, eval("tabpagenr('#')"))
155    end
156  end
157  it(
158    'switches to previous via g<Tab> after new tab creation in middle',
159    switches_to_previous_after_new_tab_creation_in_middle('g<Tab>')
160  )
161  it(
162    'switches to previous via <C-W>g<Tab> after new tab creation in middle',
163    switches_to_previous_after_new_tab_creation_in_middle('<C-W>g<Tab>')
164  )
165  it(
166    'switches to previous via <C-Tab> after new tab creation in middle',
167    switches_to_previous_after_new_tab_creation_in_middle('<C-Tab>')
168  )
169  it(
170    'switches to previous via :tabn #<CR> after new tab creation in middle',
171    switches_to_previous_after_new_tab_creation_in_middle(':tabn #<CR>')
172  )
173
174  local function switches_to_previous_after_switching_to_next_tab(characters)
175    return function()
176      -- Add three tabs for a total of four
177      command('tabnew')
178      command('tabnew')
179      command('tabnew')
180      -- Switch to the next (first) tab
181      command('tabnext')
182
183      -- The previous tab is now the fourth.
184      eq(4, eval("tabpagenr('#')"))
185
186      -- Switch to the previous (fourth) tab
187      feed(characters)
188
189      eq(
190        dedent([=[
191         Tab page 1
192         #   [No Name]
193         Tab page 2
194             [No Name]
195         Tab page 3
196             [No Name]
197         Tab page 4
198         >   [No Name]]=]),
199        exec_capture('tabs')
200      )
201
202      -- The previous tab is now the first.
203      eq(1, eval("tabpagenr('#')"))
204    end
205  end
206  it(
207    'switches to previous via g<Tab> after switching to next tab',
208    switches_to_previous_after_switching_to_next_tab('g<Tab>')
209  )
210  it(
211    'switches to previous via <C-W>g<Tab> after switching to next tab',
212    switches_to_previous_after_switching_to_next_tab('<C-W>g<Tab>')
213  )
214  it(
215    'switches to previous via <C-Tab> after switching to next tab',
216    switches_to_previous_after_switching_to_next_tab('<C-Tab>')
217  )
218  it(
219    'switches to previous via :tabn #<CR> after switching to next tab',
220    switches_to_previous_after_switching_to_next_tab(':tabn #<CR>')
221  )
222
223  local function switches_to_previous_after_switching_to_last_tab(characters)
224    return function()
225      -- Add three tabs for a total of four
226      command('tabnew')
227      command('tabnew')
228      command('tabnew')
229      -- Switch to the next (first) tab
230      command('tabnext')
231      -- Switch to the last (fourth) tab.
232      command('tablast')
233
234      -- The previous tab is now the second.
235      eq(1, eval("tabpagenr('#')"))
236
237      -- Switch to the previous (second) tab
238      feed(characters)
239
240      eq(
241        dedent([=[
242         Tab page 1
243         >   [No Name]
244         Tab page 2
245             [No Name]
246         Tab page 3
247             [No Name]
248         Tab page 4
249         #   [No Name]]=]),
250        exec_capture('tabs')
251      )
252
253      -- The previous tab is now the fourth.
254      eq(4, eval("tabpagenr('#')"))
255    end
256  end
257  it(
258    'switches to previous after switching to last tab',
259    switches_to_previous_after_switching_to_last_tab('g<Tab>')
260  )
261  it(
262    'switches to previous after switching to last tab',
263    switches_to_previous_after_switching_to_last_tab('<C-W>g<Tab>')
264  )
265  it(
266    'switches to previous after switching to last tab',
267    switches_to_previous_after_switching_to_last_tab('<C-Tab>')
268  )
269  it(
270    'switches to previous after switching to last tab',
271    switches_to_previous_after_switching_to_last_tab(':tabn #<CR>')
272  )
273
274  local function switches_to_previous_after_switching_to_previous_tab(characters)
275    return function()
276      -- Add three tabs for a total of four
277      command('tabnew')
278      command('tabnew')
279      command('tabnew')
280      -- Switch to the previous (third) tab
281      command('tabprevious')
282
283      -- The previous tab is now the fourth.
284      eq(4, eval("tabpagenr('#')"))
285
286      -- Switch to the previous (fourth) tab
287      feed(characters)
288
289      eq(
290        dedent([=[
291         Tab page 1
292             [No Name]
293         Tab page 2
294             [No Name]
295         Tab page 3
296         #   [No Name]
297         Tab page 4
298         >   [No Name]]=]),
299        exec_capture('tabs')
300      )
301
302      -- The previous tab is now the third.
303      eq(3, eval("tabpagenr('#')"))
304    end
305  end
306  it(
307    'switches to previous via g<Tab> after switching to previous tab',
308    switches_to_previous_after_switching_to_previous_tab('g<Tab>')
309  )
310  it(
311    'switches to previous via <C-W>g<Tab> after switching to previous tab',
312    switches_to_previous_after_switching_to_previous_tab('<C-W>g<Tab>')
313  )
314  it(
315    'switches to previous via <C-Tab> after switching to previous tab',
316    switches_to_previous_after_switching_to_previous_tab('<C-Tab>')
317  )
318  it(
319    'switches to previous via :tabn #<CR> after switching to previous tab',
320    switches_to_previous_after_switching_to_previous_tab(':tabn #<CR>')
321  )
322
323  local function switches_to_previous_after_switching_to_first_tab(characters)
324    return function()
325      -- Add three tabs for a total of four
326      command('tabnew')
327      command('tabnew')
328      command('tabnew')
329      -- Switch to the previous (third) tab
330      command('tabprevious')
331      -- Switch to the first tab
332      command('tabfirst')
333
334      -- The previous tab is now the third.
335      eq(3, eval("tabpagenr('#')"))
336
337      -- Switch to the previous (third) tab
338      feed(characters)
339
340      eq(
341        dedent([=[
342         Tab page 1
343         #   [No Name]
344         Tab page 2
345             [No Name]
346         Tab page 3
347         >   [No Name]
348         Tab page 4
349             [No Name]]=]),
350        exec_capture('tabs')
351      )
352
353      -- The previous tab is now the first.
354      eq(1, eval("tabpagenr('#')"))
355    end
356  end
357  it(
358    'switches to previous via g<Tab> after switching to first tab',
359    switches_to_previous_after_switching_to_first_tab('g<Tab>')
360  )
361  it(
362    'switches to previous via <C-W>g<Tab> after switching to first tab',
363    switches_to_previous_after_switching_to_first_tab('<C-W>g<Tab>')
364  )
365  it(
366    'switches to previous via <C-Tab> after switching to first tab',
367    switches_to_previous_after_switching_to_first_tab('<C-Tab>')
368  )
369  it(
370    'switches to previous via :tabn #<CR> after switching to first tab',
371    switches_to_previous_after_switching_to_first_tab(':tabn #<CR>')
372  )
373
374  local function switches_to_previous_after_numbered_tab_switch(characters)
375    return function()
376      -- Add three tabs for a total of four
377      command('tabnew')
378      command('tabnew')
379      command('tabnew')
380      -- Switch to the second tab
381      command('tabnext 2')
382
383      -- The previous tab is now the fourth.
384      eq(4, eval("tabpagenr('#')"))
385
386      -- Switch to the previous (fourth) tab
387      feed(characters)
388
389      eq(
390        dedent([=[
391         Tab page 1
392             [No Name]
393         Tab page 2
394         #   [No Name]
395         Tab page 3
396             [No Name]
397         Tab page 4
398         >   [No Name]]=]),
399        exec_capture('tabs')
400      )
401
402      -- The previous tab is now the second.
403      eq(2, eval("tabpagenr('#')"))
404    end
405  end
406  it(
407    'switches to previous via g<Tab> after numbered tab switch',
408    switches_to_previous_after_numbered_tab_switch('g<Tab>')
409  )
410  it(
411    'switches to previous via <C-W>g<Tab> after numbered tab switch',
412    switches_to_previous_after_numbered_tab_switch('<C-W>g<Tab>')
413  )
414  it(
415    'switches to previous via <C-Tab> after numbered tab switch',
416    switches_to_previous_after_numbered_tab_switch('<C-Tab>')
417  )
418  it(
419    'switches to previous via :tabn #<CR> after numbered tab switch',
420    switches_to_previous_after_numbered_tab_switch(':tabn #<CR>')
421  )
422
423  local function switches_to_previous_after_switching_to_previous(characters1, characters2)
424    return function()
425      -- Add three tabs for a total of four
426      command('tabnew')
427      command('tabnew')
428      command('tabnew')
429      -- Switch to the second tab
430      command('tabnext 2')
431      -- Switch to the previous (fourth) tab
432      feed(characters1)
433
434      -- The previous tab is now the second.
435      eq(2, eval("tabpagenr('#')"))
436
437      -- Switch to the previous (second) tab
438      feed(characters2)
439
440      eq(
441        dedent([=[
442         Tab page 1
443             [No Name]
444         Tab page 2
445         >   [No Name]
446         Tab page 3
447             [No Name]
448         Tab page 4
449         #   [No Name]]=]),
450        exec_capture('tabs')
451      )
452
453      -- The previous tab is now the fourth.
454      eq(4, eval("tabpagenr('#')"))
455    end
456  end
457  it(
458    'switches to previous via g<Tab> after switching to previous via g<Tab>',
459    switches_to_previous_after_switching_to_previous('g<Tab>', 'g<Tab>')
460  )
461  it(
462    'switches to previous via <C-W>g<Tab> after switching to previous via g<Tab>',
463    switches_to_previous_after_switching_to_previous('g<Tab>', '<C-W>g<Tab>')
464  )
465  it(
466    'switches to previous via <C-Tab> after switching to previous via g<Tab>',
467    switches_to_previous_after_switching_to_previous('g<Tab>', '<C-Tab>')
468  )
469  it(
470    'switches to previous via :tabn #<CR> after switching to previous via g<Tab>',
471    switches_to_previous_after_switching_to_previous('g<Tab>', ':tabn #<CR>')
472  )
473  it(
474    'switches to previous via g<Tab> after switching to previous via <C-W>g<Tab>',
475    switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', 'g<Tab>')
476  )
477  it(
478    'switches to previous via <C-W>g<Tab> after switching to previous via <C-W>g<Tab>',
479    switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-W>g<Tab>')
480  )
481  it(
482    'switches to previous via <C-Tab> after switching to previous via <C-W>g<Tab>',
483    switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', '<C-Tab>')
484  )
485  it(
486    'switches to previous via :tabn #<CR> after switching to previous via <C-W>g<Tab>',
487    switches_to_previous_after_switching_to_previous('<C-W>g<Tab>', ':tabn #<CR>')
488  )
489  it(
490    'switches to previous via g<Tab> after switching to previous via <C-Tab>',
491    switches_to_previous_after_switching_to_previous('<C-Tab>', 'g<Tab>')
492  )
493  it(
494    'switches to previous via <C-W>g<Tab> after switching to previous via <C-Tab>',
495    switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-W>g<Tab>')
496  )
497  it(
498    'switches to previous via <C-Tab> after switching to previous via <C-Tab>',
499    switches_to_previous_after_switching_to_previous('<C-Tab>', '<C-Tab>')
500  )
501  it(
502    'switches to previous via :tabn #<CR> after switching to previous via <C-Tab>',
503    switches_to_previous_after_switching_to_previous('<C-Tab>', ':tabn #<CR>')
504  )
505  it(
506    'switches to previous via g<Tab> after switching to previous via :tabn #<CR>',
507    switches_to_previous_after_switching_to_previous(':tabn #<CR>', 'g<Tab>')
508  )
509  it(
510    'switches to previous via <C-W>g<Tab> after switching to previous via :tabn #<CR>',
511    switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-W>g<Tab>')
512  )
513  it(
514    'switches to previous via <C-Tab> after switching to previous via <C-Tab>',
515    switches_to_previous_after_switching_to_previous(':tabn #<CR>', '<C-Tab>')
516  )
517  it(
518    'switches to previous via :tabn #<CR> after switching to previous via :tabn #<CR>',
519    switches_to_previous_after_switching_to_previous(':tabn #<CR>', ':tabn #<CR>')
520  )
521
522  local function does_not_switch_to_previous_after_closing_current_tab(characters)
523    return function()
524      -- Add three tabs for a total of four
525      command('tabnew')
526      command('tabnew')
527      command('tabnew')
528      -- Close the current (fourth tab)
529      command('wincmd c')
530
531      -- The previous tab is now the "zeroth" -- there isn't one.
532      eq(0, eval("tabpagenr('#')"))
533
534      -- At this point, switching to the "previous" (i.e. fourth) tab would mean
535      -- switching to either a dangling or a null pointer.
536      feed(characters)
537
538      eq(
539        dedent([=[
540         Tab page 1
541             [No Name]
542         Tab page 2
543             [No Name]
544         Tab page 3
545         >   [No Name]]=]),
546        exec_capture('tabs')
547      )
548
549      -- The previous tab is now the "zero".
550      eq(0, eval("tabpagenr('#')"))
551    end
552  end
553  it(
554    'does not switch to previous via g<Tab> after closing current tab',
555    does_not_switch_to_previous_after_closing_current_tab('g<Tab>')
556  )
557  it(
558    'does not switch to previous via <C-W>g<Tab> after closing current tab',
559    does_not_switch_to_previous_after_closing_current_tab('<C-W>g<Tab>')
560  )
561  it(
562    'does not switch to previous via <C-Tab> after closing current tab',
563    does_not_switch_to_previous_after_closing_current_tab('<C-Tab>')
564  )
565  it(
566    'does not switch to previous via :tabn #<CR> after closing current tab',
567    does_not_switch_to_previous_after_closing_current_tab(':tabn #<CR>')
568  )
569
570  local function does_not_switch_to_previous_after_entering_operator_pending(characters)
571    return function()
572      -- Add three tabs for a total of four
573      command('tabnew')
574      command('tabnew')
575      command('tabnew')
576
577      -- The previous tab is now the third.
578      eq(3, eval("tabpagenr('#')"))
579
580      -- Enter operator pending mode.
581      feed('d')
582      eq('no', eval('mode(1)'))
583
584      -- At this point switching to the previous tab should have no effect
585      -- other than leaving operator pending mode.
586      feed(characters)
587
588      -- Attempting to switch tabs returns us to normal mode.
589      eq('n', eval('mode()'))
590
591      -- The current tab is still the fourth.
592      eq(4, eval('tabpagenr()'))
593
594      -- The previous tab is still the third.
595      eq(3, eval("tabpagenr('#')"))
596    end
597  end
598  it(
599    'does not switch to previous via g<Tab> after entering operator pending',
600    does_not_switch_to_previous_after_entering_operator_pending('g<Tab>')
601  )
602  -- NOTE: When in operator pending mode, attempting to switch to previous has
603  --       the following effect:
604  --       - Ctrl-W exits operator pending mode
605  --       - g<Tab> switches to the previous tab
606  --       In other words, the effect of "<C-W>g<Tab>" is to switch to the
607  --       previous tab even from operator pending mode, but only thanks to the
608  --       fact that the suffix after "<C-W>" in "<C-W>g<Tab>" just happens to
609  --       be the same as the normal mode command to switch to the previous tab.
610  -- it('does not switch to previous via <C-W>g<Tab> after entering operator pending',
611  --   does_not_switch_to_previous_after_entering_operator_pending('<C-W>g<Tab>'))
612  it(
613    'does not switch to previous via <C-Tab> after entering operator pending',
614    does_not_switch_to_previous_after_entering_operator_pending('<C-Tab>')
615  )
616  -- NOTE: When in operator pending mode, pressing : leaves operator pending
617  --       mode and enters command mode, so :tabn #<CR> does in fact switch
618  --       tabs.
619  -- it('does not switch to previous via :tabn #<CR> after entering operator pending',
620  --   does_not_switch_to_previous_after_entering_operator_pending(':tabn #<CR>'))
621
622  local function cmdwin_allows_tab_switch(characters)
623    return function()
624      -- Add three tabs for a total of four
625      command('tabnew')
626      command('tabnew')
627      command('tabnew')
628
629      -- The previous tab is now the third.
630      eq(3, eval("tabpagenr('#')"))
631
632      -- Edit : command line in command-line window
633      feed('q:')
634
635      local cmdline_win_id = eval('win_getid()')
636
637      -- Switching to the previous tab now works.
638      feed(characters)
639
640      -- Focus moved out of the cmdwin to the previous (third) tab.
641      t.neq(cmdline_win_id, eval('win_getid()'))
642      eq(3, eval('tabpagenr()'))
643    end
644  end
645
646  it('cmdwin allows tab switch via g<Tab>', cmdwin_allows_tab_switch('g<Tab>'))
647  it('cmdwin allows tab switch via <C-W>g<Tab>', cmdwin_allows_tab_switch('<C-W>g<Tab>'))
648  it('cmdwin allows tab switch via <C-Tab>', cmdwin_allows_tab_switch('<C-Tab>'))
649  it('cmdwin allows tab switch via :tabn #<CR>', cmdwin_allows_tab_switch(':tabn #<CR>'))
650
651  it(':tabs indicates correct prevtab curwin', function()
652    -- Add three tabs for a total of four
653    command('tabnew')
654    command('tabnew')
655    command('split')
656    command('vsplit')
657    feed('<C-w>p')
658    command('tabnew')
659
660    -- The previous tab is now the three.
661    eq(3, eval("tabpagenr('#')"))
662
663    eq(
664      dedent([=[
665         Tab page 1
666             [No Name]
667         Tab page 2
668             [No Name]
669         Tab page 3
670             [No Name]
671         #   [No Name]
672             [No Name]
673         Tab page 4
674         >   [No Name]]=]),
675      exec_capture('tabs')
676    )
677  end)
678end)