robin/laufey

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

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.7 KiB129 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, eq = n.clear, t.eq
6local command = n.command
7local eval = n.eval
8local exec = n.exec
9local exec_capture = n.exec_capture
10
11describe('TabClosed', function()
12  before_each(clear)
13
14  describe('au TabClosed', function()
15    describe('with * as <afile>', function()
16      it('matches when closing any tab', function()
17        command(
18          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
19        )
20        repeat
21          command('tabnew')
22        until eval('tabpagenr()') == 6 -- current tab is now 6
23        eq('tabclosed:6:6:5', exec_capture('tabclose')) -- close last 6, current tab is now 5
24        eq('tabclosed:5:5:4', exec_capture('close')) -- close last window on tab, closes tab
25        eq('tabclosed:2:2:3', exec_capture('2tabclose')) -- close tab 2, current tab is now 3
26        eq('tabclosed:1:1:2\ntabclosed:1:1:1', exec_capture('tabonly')) -- close tabs 1 and 2
27      end)
28
29      it('is triggered when closing a window via bdelete from another tab', function()
30        command(
31          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
32        )
33        command('1tabedit Xtestfile')
34        command('1tabedit Xtestfile')
35        command('normal! 1gt')
36        eq({ 1, 3 }, eval('[tabpagenr(), tabpagenr("$")]'))
37        eq('tabclosed:2:2:1\ntabclosed:2:2:1', exec_capture('bdelete Xtestfile'))
38        eq({ 1, 1 }, eval('[tabpagenr(), tabpagenr("$")]'))
39      end)
40
41      it('is triggered when closing a window via bdelete from current tab', function()
42        command(
43          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
44        )
45        command('file Xtestfile1')
46        command('1tabedit Xtestfile2')
47        command('1tabedit Xtestfile2')
48
49        -- Only one tab is closed, and the alternate file is used for the other.
50        eq({ 2, 3 }, eval('[tabpagenr(), tabpagenr("$")]'))
51        eq('tabclosed:2:2:2', exec_capture('bdelete Xtestfile2'))
52        eq('Xtestfile1', eval('bufname("")'))
53      end)
54
55      it('triggers after tab page is properly freed', function()
56        exec([[
57          let s:tp = nvim_get_current_tabpage()
58          let g:buf = bufnr()
59
60          setlocal bufhidden=wipe
61          tabnew
62          au TabClosed * ++once let g:tp_valid = nvim_tabpage_is_valid(s:tp)
63                             \| let g:curbuf = bufnr()
64                             \| let g:abuf = expand('<abuf>')
65
66          call nvim_buf_delete(g:buf, #{force: 1})
67        ]])
68        eq(false, eval('g:tp_valid'))
69        eq(false, eval('nvim_buf_is_valid(g:buf)'))
70        eq(eval('g:curbuf'), tonumber(eval('g:abuf'))) -- Falls back to curbuf.
71
72        exec([[
73          tabnew
74          let g:buf = bufnr()
75          let s:win = win_getid()
76
77          tabfirst
78          au TabClosed * ++once let g:abuf = expand('<abuf>')
79
80          call nvim_win_close(s:win, 1)
81        ]])
82        eq(true, eval('nvim_buf_is_valid(g:buf)'))
83        eq(eval('g:buf'), tonumber(eval('g:abuf')))
84
85        exec([[
86          tabnew
87          let s:win = win_getid()
88
89          tabfirst
90          let g:buf = nvim_create_buf(1, 1)
91          au BufHidden * ++once call nvim_win_set_buf(s:win, g:buf)
92          au TabClosed * ++once let g:abuf = expand('<abuf>')
93
94          call nvim_win_close(s:win, 1)
95        ]])
96        -- BufHidden switched buffers at the last moment; TabClosed's <abuf> should show that.
97        eq(eval('g:buf'), tonumber(eval('g:abuf')))
98      end)
99    end)
100
101    describe('with NR as <afile>', function()
102      it('matches when closing a tab whose index is NR', function()
103        command(
104          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
105        )
106        command('au! TabClosed 2 echom "tabclosed:match"')
107        repeat
108          command('tabnew')
109        until eval('tabpagenr()') == 7 -- current tab is now 7
110        -- sanity check, we shouldn't match on tabs with numbers other than 2
111        eq('tabclosed:7:7:6', exec_capture('tabclose'))
112        -- close tab page 2, current tab is now 5
113        eq('tabclosed:2:2:5\ntabclosed:match', exec_capture('2tabclose'))
114      end)
115    end)
116
117    describe('with close', function()
118      it('is triggered', function()
119        command(
120          'au! TabClosed * echom "tabclosed:".expand("<afile>").":".expand("<amatch>").":".tabpagenr()'
121        )
122        command('tabedit Xtestfile')
123        eq({ 2, 2 }, eval('[tabpagenr(), tabpagenr("$")]'))
124        eq('tabclosed:2:2:1', exec_capture('close'))
125        eq({ 1, 1 }, eval('[tabpagenr(), tabpagenr("$")]'))
126      end)
127    end)
128  end)
129end)