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