test_tabpage.vim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. " Tests for tabpage
  2. source screendump.vim
  3. source check.vim
  4. function Test_tabpage()
  5. CheckFeature quickfix
  6. bw!
  7. " Simple test for opening and closing a tab page
  8. tabnew
  9. call assert_equal(2, tabpagenr())
  10. quit
  11. " Open three tab pages and use ":tabdo"
  12. 0tabnew
  13. 1tabnew
  14. $tabnew
  15. %del
  16. tabdo call append(line('$'), tabpagenr())
  17. tabclose! 2
  18. tabrewind
  19. let line1 = getline('$')
  20. undo
  21. q
  22. tablast
  23. let line2 = getline('$')
  24. q!
  25. call append(line('$'), line1)
  26. call append(line('$'), line2)
  27. unlet line1 line2
  28. call assert_equal(['', '3', '1', '4'], getline(1, '$'))
  29. "
  30. " Test for settabvar() and gettabvar() functions. Open a new tab page and
  31. " set 3 variables to a number, string and a list. Verify that the variables
  32. " are correctly set.
  33. tabnew
  34. tabfirst
  35. call settabvar(2, 'val_num', 100)
  36. eval 'SetTabVar test'->settabvar(2, 'val_str')
  37. call settabvar(2, 'val_list', ['red', 'blue', 'green'])
  38. "
  39. call assert_true(gettabvar(2, 'val_num') == 100 && gettabvar(2, 'val_str') == 'SetTabVar test' && gettabvar(2, 'val_list') == ['red', 'blue', 'green'])
  40. tabnext 2
  41. call assert_true(t:val_num == 100 && t:val_str == 'SetTabVar test' && t:val_list == ['red', 'blue', 'green'])
  42. tabclose
  43. " Test for ":tab drop exist-file" to keep current window.
  44. sp test1
  45. tab drop test1
  46. call assert_true(tabpagenr('$') == 1 && winnr('$') == 2 && winnr() == 1)
  47. close
  48. "
  49. "
  50. " Test for ":tab drop new-file" to keep current window of tabpage 1.
  51. split
  52. tab drop newfile
  53. call assert_true(tabpagenr('$') == 2 && tabpagewinnr(1, '$') == 2 && tabpagewinnr(1) == 1)
  54. tabclose
  55. q
  56. "
  57. "
  58. " Test for ":tab drop multi-opened-file" to keep current tabpage and window.
  59. new test1
  60. tabnew
  61. new test1
  62. tab drop test1
  63. call assert_true(tabpagenr() == 2 && tabpagewinnr(2, '$') == 2 && tabpagewinnr(2) == 1)
  64. tabclose
  65. q
  66. "
  67. "
  68. " Test for ":tab drop vertical-split-window" to jump test1 buffer
  69. tabedit test1
  70. vnew
  71. tabfirst
  72. tab drop test1
  73. call assert_equal([2, 2, 2, 2], [tabpagenr('$'), tabpagenr(), tabpagewinnr(2, '$'), tabpagewinnr(2)])
  74. 1tabonly
  75. "
  76. "
  77. for i in range(9) | tabnew | endfor
  78. normal! 1gt
  79. call assert_equal(1, tabpagenr())
  80. tabmove 5
  81. call assert_equal(5, tabpagenr())
  82. .tabmove
  83. call assert_equal(5, tabpagenr())
  84. tabmove -
  85. call assert_equal(4, tabpagenr())
  86. tabmove +
  87. call assert_equal(5, tabpagenr())
  88. tabmove -2
  89. call assert_equal(3, tabpagenr())
  90. tabmove +4
  91. call assert_equal(7, tabpagenr())
  92. tabmove
  93. call assert_equal(10, tabpagenr())
  94. 0tabmove
  95. call assert_equal(1, tabpagenr())
  96. $tabmove
  97. call assert_equal(10, tabpagenr())
  98. tabmove 0
  99. call assert_equal(1, tabpagenr())
  100. tabmove $
  101. call assert_equal(10, tabpagenr())
  102. 3tabmove
  103. call assert_equal(4, tabpagenr())
  104. 7tabmove 5
  105. call assert_equal(5, tabpagenr())
  106. -tabmove
  107. call assert_equal(4, tabpagenr())
  108. +tabmove
  109. call assert_equal(5, tabpagenr())
  110. -2tabmove
  111. call assert_equal(3, tabpagenr())
  112. +3tabmove
  113. call assert_equal(6, tabpagenr())
  114. silent -tabmove
  115. call assert_equal(5, tabpagenr())
  116. silent -2 tabmove
  117. call assert_equal(3, tabpagenr())
  118. silent -2 tabmove
  119. call assert_equal(1, tabpagenr())
  120. norm! 2gt
  121. call assert_equal(2, tabpagenr())
  122. " The following are a no-op
  123. tabmove 2
  124. call assert_equal(2, tabpagenr())
  125. 2tabmove
  126. call assert_equal(2, tabpagenr())
  127. tabmove 1
  128. call assert_equal(2, tabpagenr())
  129. 1tabmove
  130. call assert_equal(2, tabpagenr())
  131. call assert_fails('let t = tabpagenr("@")', 'E15:')
  132. call assert_equal(0, tabpagewinnr(-1))
  133. call assert_fails("99tabmove", 'E16:')
  134. call assert_fails("+99tabmove", 'E16:')
  135. call assert_fails("-99tabmove", 'E16:')
  136. call assert_fails("tabmove foo", 'E475:')
  137. call assert_fails("tabmove 99", 'E475:')
  138. call assert_fails("tabmove +99", 'E475:')
  139. call assert_fails("tabmove -99", 'E475:')
  140. call assert_fails("tabmove -3+", 'E475:')
  141. call assert_fails("tabmove $3", 'E475:')
  142. call assert_fails("%tabonly", 'E16:')
  143. 1tabonly!
  144. tabmove 1
  145. call assert_equal(1, tabpagenr())
  146. tabnew
  147. call assert_fails("-2tabmove", 'E16:')
  148. tabonly!
  149. endfunc
  150. func Test_tabpage_drop()
  151. edit f1
  152. tab split f2
  153. tab split f3
  154. normal! gt
  155. call assert_equal(1, tabpagenr())
  156. tab drop f4
  157. call assert_equal(1, tabpagenr('#'))
  158. tab drop f3
  159. call assert_equal(4, tabpagenr())
  160. call assert_equal(2, tabpagenr('#'))
  161. bwipe!
  162. bwipe!
  163. bwipe!
  164. bwipe!
  165. call assert_equal(1, tabpagenr('$'))
  166. call assert_equal(1, winnr('$'))
  167. call assert_equal('', bufname(''))
  168. call writefile(['L1', 'L2'], 'Xdropfile', 'D')
  169. " Test for ':tab drop single-file': reuse current buffer
  170. let expected_nr = bufnr()
  171. tab drop Xdropfile
  172. call assert_equal(1, tabpagenr('$'))
  173. call assert_equal(expected_nr, bufnr())
  174. call assert_equal('L2', getline(2))
  175. bwipe!
  176. " Test for ':tab drop single-file': not reuse modified buffer
  177. set modified
  178. let expected_nr = bufnr() + 1
  179. tab drop Xdropfile
  180. call assert_equal(2, tabpagenr())
  181. call assert_equal(2, tabpagenr('$'))
  182. call assert_equal(expected_nr, bufnr())
  183. call assert_equal('L2', getline(2))
  184. bwipe!
  185. " Test for ':tab drop single-file': multiple tabs already exist
  186. tab split f2
  187. tab split f3
  188. let expected_nr = bufnr() + 1
  189. tab drop Xdropfile
  190. call assert_equal(4, tabpagenr())
  191. call assert_equal(4, tabpagenr('$'))
  192. call assert_equal(expected_nr, bufnr())
  193. call assert_equal('L2', getline(2))
  194. %bwipe!
  195. " Test for ':tab drop multi-files': reuse current buffer
  196. let expected_nr = bufnr()
  197. tab drop Xdropfile f1 f2 f3
  198. call assert_equal(1, tabpagenr())
  199. call assert_equal(4, tabpagenr('$'))
  200. call assert_equal(expected_nr, bufnr())
  201. call assert_equal('L2', getline(2))
  202. %bwipe!
  203. " Test for ':tab drop multi-files': not reuse modified buffer
  204. set modified
  205. let expected_nr = bufnr() + 1
  206. tab drop Xdropfile f1 f2 f3
  207. call assert_equal(2, tabpagenr())
  208. call assert_equal(5, tabpagenr('$'))
  209. call assert_equal(expected_nr, bufnr())
  210. call assert_equal('L2', getline(2))
  211. %bwipe!
  212. " Test for ':tab drop multi-files': multiple tabs already exist
  213. tab split f2
  214. tab split f3
  215. let expected_nr = bufnr() + 1
  216. tab drop a b c
  217. call assert_equal(4, tabpagenr())
  218. call assert_equal(6, tabpagenr('$'))
  219. call assert_equal(expected_nr, bufnr())
  220. let expected_nr = bufnr() + 3
  221. tab drop Xdropfile f1 f2 f3
  222. call assert_equal(5, tabpagenr())
  223. call assert_equal(8, tabpagenr('$'))
  224. call assert_equal(expected_nr, bufnr())
  225. call assert_equal('L2', getline(2))
  226. %bwipe!
  227. endfunc
  228. " Test autocommands
  229. function Test_tabpage_with_autocmd()
  230. command -nargs=1 -bar C :call add(s:li, '=== ' . <q-args> . ' ===')|<args>
  231. augroup TestTabpageGroup
  232. au!
  233. autocmd TabEnter * call add(s:li, 'TabEnter')
  234. autocmd WinEnter * call add(s:li, 'WinEnter')
  235. autocmd BufEnter * call add(s:li, 'BufEnter')
  236. autocmd TabLeave * call add(s:li, 'TabLeave')
  237. autocmd WinLeave * call add(s:li, 'WinLeave')
  238. autocmd BufLeave * call add(s:li, 'BufLeave')
  239. augroup END
  240. let s:li = []
  241. let t:a='a'
  242. C tab split
  243. call assert_equal(['=== tab split ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter'], s:li)
  244. let s:li = []
  245. let t:a='b'
  246. C tabnew
  247. call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
  248. let t:a='c'
  249. let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
  250. call assert_equal(['a', 'b', 'c'], s:li)
  251. let s:li = []
  252. C call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)')
  253. call assert_equal(["=== call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ==="], s:li)
  254. let s:li = split(join(map(range(1, tabpagenr('$')), 'gettabvar(v:val, "a")')) , '\s\+')
  255. call assert_equal(['2', '4', '6'], s:li)
  256. let s:li = []
  257. let w:a='a'
  258. C vsplit
  259. call assert_equal(['=== vsplit ===', 'WinLeave', 'WinEnter'], s:li)
  260. let s:li = []
  261. let w:a='a'
  262. let tabn=tabpagenr()
  263. let winr=range(1, winnr('$'))
  264. C tabnext 1
  265. call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
  266. let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
  267. call assert_equal(['a', 'a'], s:li)
  268. let s:li = []
  269. C call map(copy(winr), '(v:val*2)->settabwinvar(' .. tabn .. ', v:val, ''a'')')
  270. let s:li = split(join(map(copy(winr), 'gettabwinvar('.tabn.', v:val, "a")')), '\s\+')
  271. call assert_equal(['2', '4'], s:li)
  272. augroup TabDestructive
  273. autocmd TabEnter * :C tabnext 2 | C tabclose 3
  274. augroup END
  275. let s:li = []
  276. C tabnext 3
  277. call assert_equal(['=== tabnext 3 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
  278. call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
  279. autocmd! TabDestructive TabEnter
  280. let s:li = []
  281. C tabnew
  282. call assert_equal(['=== tabnew ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufLeave', 'BufEnter'], s:li)
  283. let s:li = []
  284. C tabnext 1
  285. call assert_equal(['=== tabnext 1 ===', 'BufLeave', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter'], s:li)
  286. autocmd TabDestructive TabEnter * nested :C tabnext 2 | C tabclose 3
  287. let s:li = []
  288. call assert_equal(3, tabpagenr('$'))
  289. C tabnext 2
  290. call assert_equal(2, tabpagenr('$'))
  291. call assert_equal(['=== tabnext 2 ===', 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', '=== tabnext 2 ===', '=== tabclose 3 ==='], s:li)
  292. call assert_equal(['2/2'], [tabpagenr().'/'.tabpagenr('$')])
  293. delcommand C
  294. autocmd! TabDestructive
  295. augroup! TabDestructive
  296. autocmd! TestTabpageGroup
  297. augroup! TestTabpageGroup
  298. 1tabonly!
  299. endfunction
  300. " Test autocommands on tab drop
  301. function Test_tabpage_with_autocmd_tab_drop()
  302. augroup TestTabpageGroup
  303. au!
  304. autocmd TabEnter * call add(s:li, 'TabEnter')
  305. autocmd WinEnter * call add(s:li, 'WinEnter')
  306. autocmd BufEnter * call add(s:li, 'BufEnter')
  307. autocmd TabLeave * call add(s:li, 'TabLeave')
  308. autocmd WinLeave * call add(s:li, 'WinLeave')
  309. autocmd BufLeave * call add(s:li, 'BufLeave')
  310. augroup END
  311. let s:li = []
  312. tab drop test1
  313. call assert_equal(['BufEnter'], s:li)
  314. let s:li = []
  315. tab drop test2 test3
  316. call assert_equal([
  317. \ 'TabLeave', 'TabEnter', 'TabLeave', 'TabEnter',
  318. \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter',
  319. \ 'TabLeave', 'WinEnter', 'TabEnter', 'BufEnter', 'BufEnter'], s:li)
  320. autocmd! TestTabpageGroup
  321. augroup! TestTabpageGroup
  322. 1tabonly!
  323. endfunction
  324. function Test_tabpage_with_tab_modifier()
  325. CheckFeature quickfix
  326. for n in range(4)
  327. tabedit
  328. endfor
  329. function s:check_tab(pre_nr, cmd, post_nr)
  330. exec 'tabnext ' . a:pre_nr
  331. exec a:cmd
  332. call assert_equal(a:post_nr, tabpagenr())
  333. call assert_equal('help', &buftype)
  334. helpclose
  335. endfunc
  336. call s:check_tab(1, 'tab help', 2)
  337. call s:check_tab(1, '3tab help', 4)
  338. call s:check_tab(1, '.tab help', 2)
  339. call s:check_tab(1, '.+1tab help', 3)
  340. call s:check_tab(1, '0tab help', 1)
  341. call s:check_tab(2, '+tab help', 4)
  342. call s:check_tab(2, '+2tab help', 5)
  343. call s:check_tab(4, '-tab help', 4)
  344. call s:check_tab(4, '-2tab help', 3)
  345. call s:check_tab(3, '$tab help', 6)
  346. call assert_fails('99tab help', 'E16:')
  347. call assert_fails('+99tab help', 'E16:')
  348. call assert_fails('-99tab help', 'E16:')
  349. delfunction s:check_tab
  350. 1tabonly!
  351. endfunction
  352. function Check_tab_count(pre_nr, cmd, post_nr)
  353. exec 'tabnext' a:pre_nr
  354. normal! G
  355. exec a:cmd
  356. call assert_equal(a:post_nr, tabpagenr(), a:cmd)
  357. endfunc
  358. " Test for [count] of tabnext
  359. function Test_tabpage_with_tabnext()
  360. for n in range(4)
  361. tabedit
  362. call setline(1, ['', '', '3'])
  363. endfor
  364. call Check_tab_count(1, 'tabnext', 2)
  365. call Check_tab_count(1, '3tabnext', 3)
  366. call Check_tab_count(1, '.tabnext', 1)
  367. call Check_tab_count(1, '.+1tabnext', 2)
  368. call Check_tab_count(2, '+tabnext', 3)
  369. call Check_tab_count(2, '+2tabnext', 4)
  370. call Check_tab_count(4, '-tabnext', 3)
  371. call Check_tab_count(4, '-2tabnext', 2)
  372. call Check_tab_count(3, '$tabnext', 5)
  373. call assert_fails('0tabnext', 'E16:')
  374. call assert_fails('99tabnext', 'E16:')
  375. call assert_fails('+99tabnext', 'E16:')
  376. call assert_fails('-99tabnext', 'E16:')
  377. call Check_tab_count(1, 'tabnext 3', 3)
  378. call Check_tab_count(2, 'tabnext +', 3)
  379. call Check_tab_count(2, 'tabnext +2', 4)
  380. call Check_tab_count(4, 'tabnext -', 3)
  381. call Check_tab_count(4, 'tabnext -2', 2)
  382. call Check_tab_count(3, 'tabnext $', 5)
  383. call assert_fails('tabnext 0', 'E475:')
  384. call assert_fails('tabnext .', 'E475:')
  385. call assert_fails('tabnext -+', 'E475:')
  386. call assert_fails('tabnext +2-', 'E475:')
  387. call assert_fails('tabnext $3', 'E475:')
  388. call assert_fails('tabnext 99', 'E475:')
  389. call assert_fails('tabnext +99', 'E475:')
  390. call assert_fails('tabnext -99', 'E475:')
  391. 1tabonly!
  392. endfunction
  393. " Test for [count] of tabprevious
  394. function Test_tabpage_with_tabprevious()
  395. for n in range(5)
  396. tabedit
  397. call setline(1, ['', '', '3'])
  398. endfor
  399. for cmd in ['tabNext', 'tabprevious']
  400. call Check_tab_count(6, cmd, 5)
  401. call Check_tab_count(6, '3' . cmd, 3)
  402. call Check_tab_count(6, '8' . cmd, 4)
  403. call Check_tab_count(6, cmd . ' 3', 3)
  404. call Check_tab_count(6, cmd . ' 8', 4)
  405. for n in range(2)
  406. for c in ['0', '.+3', '+', '+2' , '-', '-2' , '$', '+99', '-99']
  407. if n == 0 " pre count
  408. let entire_cmd = c . cmd
  409. let err_code = 'E16:'
  410. else
  411. let entire_cmd = cmd . ' ' . c
  412. let err_code = 'E475:'
  413. endif
  414. call assert_fails(entire_cmd, err_code)
  415. endfor
  416. endfor
  417. endfor
  418. 1tabonly!
  419. endfunction
  420. function s:reconstruct_tabpage_for_test(nr)
  421. let n = (a:nr > 2) ? a:nr - 2 : 1
  422. 1tabonly!
  423. 0tabedit n0
  424. for n in range(1, n)
  425. exec '$tabedit n' . n
  426. if n == 1
  427. call setline(1, ['', '', '3'])
  428. endif
  429. endfor
  430. endfunc
  431. func Test_tabpage_ctrl_pgup_pgdown()
  432. enew!
  433. tabnew tab1
  434. tabnew tab2
  435. call assert_equal(3, tabpagenr())
  436. exe "norm! \<C-PageUp>"
  437. call assert_equal(2, tabpagenr())
  438. exe "norm! \<C-PageDown>"
  439. call assert_equal(3, tabpagenr())
  440. " Check wrapping at last or first page.
  441. exe "norm! \<C-PageDown>"
  442. call assert_equal(1, tabpagenr())
  443. exe "norm! \<C-PageUp>"
  444. call assert_equal(3, tabpagenr())
  445. " With a count, <C-PageUp> and <C-PageDown> are not symmetrical somehow:
  446. " - {count}<C-PageUp> goes {count} pages downward (relative count)
  447. " - {count}<C-PageDown> goes to page number {count} (absolute count)
  448. exe "norm! 2\<C-PageUp>"
  449. call assert_equal(1, tabpagenr())
  450. exe "norm! 2\<C-PageDown>"
  451. call assert_equal(2, tabpagenr())
  452. 1tabonly!
  453. endfunc
  454. " Test for [count] of tabclose
  455. function Test_tabpage_with_tabclose()
  456. " pre count
  457. call s:reconstruct_tabpage_for_test(6)
  458. call Check_tab_count(3, 'tabclose!', 3)
  459. call Check_tab_count(1, '3tabclose', 1)
  460. call Check_tab_count(4, '4tabclose', 3)
  461. call Check_tab_count(3, '1tabclose', 2)
  462. call Check_tab_count(2, 'tabclose', 1)
  463. call assert_equal(1, tabpagenr('$'))
  464. call assert_equal('', bufname(''))
  465. call s:reconstruct_tabpage_for_test(6)
  466. call Check_tab_count(2, '$tabclose', 2)
  467. call Check_tab_count(4, '.tabclose', 4)
  468. call Check_tab_count(3, '.+tabclose', 3)
  469. call Check_tab_count(3, '.-2tabclose', 2)
  470. call Check_tab_count(1, '.+1tabclose!', 1)
  471. call assert_equal(1, tabpagenr('$'))
  472. call assert_equal('', bufname(''))
  473. " post count
  474. call s:reconstruct_tabpage_for_test(6)
  475. call Check_tab_count(3, 'tabclose!', 3)
  476. call Check_tab_count(1, 'tabclose 3', 1)
  477. call Check_tab_count(4, 'tabclose 4', 3)
  478. call Check_tab_count(3, 'tabclose 1', 2)
  479. call Check_tab_count(2, 'tabclose', 1)
  480. call assert_equal(1, tabpagenr('$'))
  481. call assert_equal('', bufname(''))
  482. call s:reconstruct_tabpage_for_test(6)
  483. call Check_tab_count(2, 'tabclose $', 2)
  484. call Check_tab_count(4, 'tabclose', 4)
  485. call Check_tab_count(3, 'tabclose +', 3)
  486. call Check_tab_count(3, 'tabclose -2', 2)
  487. call Check_tab_count(1, 'tabclose! +1', 1)
  488. call assert_equal(1, tabpagenr('$'))
  489. call assert_equal('', bufname(''))
  490. call s:reconstruct_tabpage_for_test(6)
  491. for n in range(2)
  492. for c in ['0', '$3', '99', '+99', '-99']
  493. if n == 0 " pre count
  494. let entire_cmd = c . 'tabclose'
  495. let err_code = 'E16:'
  496. else
  497. let entire_cmd = 'tabclose ' . c
  498. let err_code = 'E475:'
  499. endif
  500. call assert_fails(entire_cmd, err_code)
  501. call assert_equal(6, tabpagenr('$'))
  502. endfor
  503. endfor
  504. call assert_fails('3tabclose', 'E37:')
  505. call assert_fails('tabclose 3', 'E37:')
  506. call assert_fails('tabclose -+', 'E475:')
  507. call assert_fails('tabclose +2-', 'E475:')
  508. call assert_equal(6, tabpagenr('$'))
  509. 1tabonly!
  510. endfunction
  511. " Test for [count] of tabonly
  512. function Test_tabpage_with_tabonly()
  513. " Test for the normal behavior (pre count only)
  514. let tc = [ [4, '.', '!'], [2, '.+', ''], [3, '.-2', '!'], [1, '.+1', '!'] ]
  515. for c in tc
  516. call s:reconstruct_tabpage_for_test(6)
  517. let entire_cmd = c[1] . 'tabonly' . c[2]
  518. call Check_tab_count(c[0], entire_cmd, 1)
  519. call assert_equal(1, tabpagenr('$'))
  520. endfor
  521. " Test for the normal behavior
  522. let tc2 = [ [3, '', ''], [1, '3', ''], [4, '4', '!'], [3, '1', '!'],
  523. \ [2, '', '!'],
  524. \ [2, '$', '!'], [3, '+', '!'], [3, '-2', '!'], [3, '+1', '!']
  525. \ ]
  526. for n in range(2)
  527. for c in tc2
  528. call s:reconstruct_tabpage_for_test(6)
  529. if n == 0 " pre count
  530. let entire_cmd = c[1] . 'tabonly' . c[2]
  531. else
  532. let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
  533. endif
  534. call Check_tab_count(c[0], entire_cmd, 1)
  535. call assert_equal(1, tabpagenr('$'))
  536. endfor
  537. endfor
  538. " Test for the error behavior
  539. for n in range(2)
  540. for c in ['0', '$3', '99', '+99', '-99']
  541. call s:reconstruct_tabpage_for_test(6)
  542. if n == 0 " pre count
  543. let entire_cmd = c . 'tabonly'
  544. let err_code = 'E16:'
  545. else
  546. let entire_cmd = 'tabonly ' . c
  547. let err_code = 'E475:'
  548. endif
  549. call assert_fails(entire_cmd, err_code)
  550. call assert_equal(6, tabpagenr('$'))
  551. endfor
  552. endfor
  553. " Test for the error behavior (post count only)
  554. for c in tc
  555. call s:reconstruct_tabpage_for_test(6)
  556. let entire_cmd = 'tabonly' . c[2] . ' ' . c[1]
  557. let err_code = 'E475:'
  558. call assert_fails(entire_cmd, err_code)
  559. call assert_equal(6, tabpagenr('$'))
  560. endfor
  561. call assert_fails('tabonly -+', 'E475:')
  562. call assert_fails('tabonly +2-', 'E475:')
  563. call assert_equal(6, tabpagenr('$'))
  564. 1tabonly!
  565. new
  566. only!
  567. endfunction
  568. func Test_tabnext_on_buf_unload1()
  569. " This once caused a crash
  570. new
  571. tabedit
  572. tabfirst
  573. au BufUnload <buffer> tabnext
  574. q
  575. while tabpagenr('$') > 1
  576. bwipe!
  577. endwhile
  578. endfunc
  579. func Test_tabnext_on_buf_unload2()
  580. " This once caused a crash
  581. tabedit
  582. autocmd BufUnload <buffer> tabnext
  583. file x
  584. edit y
  585. while tabpagenr('$') > 1
  586. bwipe!
  587. endwhile
  588. endfunc
  589. func Test_close_on_quitpre()
  590. " This once caused a crash
  591. edit Xtest
  592. new
  593. only
  594. set bufhidden=delete
  595. au QuitPre <buffer> close
  596. tabnew tab1
  597. tabnew tab2
  598. 1tabn
  599. q!
  600. call assert_equal(1, tabpagenr())
  601. call assert_equal(2, tabpagenr('$'))
  602. " clean up
  603. while tabpagenr('$') > 1
  604. bwipe!
  605. endwhile
  606. buf Xtest
  607. endfunc
  608. func Test_tabs()
  609. enew!
  610. tabnew tab1
  611. norm ixxx
  612. let a=split(execute(':tabs'), "\n")
  613. call assert_equal(['Tab page 1',
  614. \ '# [No Name]',
  615. \ 'Tab page 2',
  616. \ '> + tab1'], a)
  617. 1tabonly!
  618. bw!
  619. endfunc
  620. func Test_tabpage_cmdheight()
  621. CheckRunVimInTerminal
  622. call writefile([
  623. \ 'set laststatus=2',
  624. \ 'set cmdheight=2',
  625. \ 'tabnew',
  626. \ 'set cmdheight=3',
  627. \ 'tabnext',
  628. \ 'redraw!',
  629. \ 'echo "hello\nthere"',
  630. \ 'tabnext',
  631. \ 'redraw',
  632. \ ], 'XTest_tabpage_cmdheight')
  633. " Check that cursor line is concealed
  634. let buf = RunVimInTerminal('-S XTest_tabpage_cmdheight', {'statusoff': 3})
  635. call VerifyScreenDump(buf, 'Test_tabpage_cmdheight', {})
  636. call StopVimInTerminal(buf)
  637. call delete('XTest_tabpage_cmdheight')
  638. endfunc
  639. " Test for closing the tab page from a command window
  640. func Test_tabpage_close_cmdwin()
  641. tabnew
  642. call feedkeys("q/:tabclose\<CR>\<Esc>", 'xt')
  643. call assert_equal(2, tabpagenr('$'))
  644. call feedkeys("q/:tabonly\<CR>\<Esc>", 'xt')
  645. call assert_equal(2, tabpagenr('$'))
  646. tabonly
  647. endfunc
  648. " Pressing <C-PageUp> in insert mode should go to the previous tab page
  649. " and <C-PageDown> should go to the next tab page
  650. func Test_tabpage_Ctrl_Pageup()
  651. tabnew
  652. call feedkeys("i\<C-PageUp>", 'xt')
  653. call assert_equal(1, tabpagenr())
  654. call feedkeys("i\<C-PageDown>", 'xt')
  655. call assert_equal(2, tabpagenr())
  656. %bw!
  657. endfunc
  658. " Return the terminal key code for selecting a tab page from the tabline. This
  659. " sequence contains the following codes: a CSI (0x9b), KS_TABLINE (0xf0),
  660. " KS_FILLER (0x58) and then the tab page number.
  661. func TabLineSelectPageCode(tabnr)
  662. return "\x9b\xf0\x58" .. nr2char(a:tabnr)
  663. endfunc
  664. " Return the terminal key code for opening a new tabpage from the tabpage
  665. " menu. This sequence consists of the following codes: a CSI (0x9b),
  666. " KS_TABMENU (0xef), KS_FILLER (0x58), the tab page number and
  667. " TABLINE_MENU_NEW (2).
  668. func TabMenuNewItemCode(tabnr)
  669. return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(2)
  670. endfunc
  671. " Return the terminal key code for closing a tabpage from the tabpage menu.
  672. " This sequence consists of the following codes: a CSI (0x9b), KS_TABMENU
  673. " (0xef), KS_FILLER (0x58), the tab page number and TABLINE_MENU_CLOSE (1).
  674. func TabMenuCloseItemCode(tabnr)
  675. return "\x9b\xef\x58" .. nr2char(a:tabnr) .. nr2char(1)
  676. endfunc
  677. " Test for using the tabpage menu from the insert and normal modes
  678. func Test_tabline_tabmenu()
  679. " only works in GUI
  680. CheckGui
  681. %bw!
  682. tabnew
  683. tabnew
  684. call assert_equal(3, tabpagenr())
  685. " go to tab page 2 in normal mode
  686. call feedkeys(TabLineSelectPageCode(2), "Lx!")
  687. call assert_equal(2, tabpagenr())
  688. " close tab page 3 in normal mode
  689. call feedkeys(TabMenuCloseItemCode(3), "Lx!")
  690. call assert_equal(2, tabpagenr('$'))
  691. call assert_equal(2, tabpagenr())
  692. " open new tab page before tab page 1 in normal mode
  693. call feedkeys(TabMenuNewItemCode(1), "Lx!")
  694. call assert_equal(1, tabpagenr())
  695. call assert_equal(3, tabpagenr('$'))
  696. " go to tab page 2 in operator-pending mode (should beep)
  697. call assert_beeps('call feedkeys("c" .. TabLineSelectPageCode(2), "Lx!")')
  698. call assert_equal(2, tabpagenr())
  699. call assert_equal(3, tabpagenr('$'))
  700. " open new tab page before tab page 1 in operator-pending mode (should beep)
  701. call assert_beeps('call feedkeys("c" .. TabMenuNewItemCode(1), "Lx!")')
  702. call assert_equal(1, tabpagenr())
  703. call assert_equal(4, tabpagenr('$'))
  704. " open new tab page after tab page 3 in normal mode
  705. call feedkeys(TabMenuNewItemCode(4), "Lx!")
  706. call assert_equal(4, tabpagenr())
  707. call assert_equal(5, tabpagenr('$'))
  708. " go to tab page 2 in insert mode
  709. call feedkeys("i" .. TabLineSelectPageCode(2) .. "\<C-C>", "Lx!")
  710. call assert_equal(2, tabpagenr())
  711. " close tab page 2 in insert mode
  712. call feedkeys("i" .. TabMenuCloseItemCode(2) .. "\<C-C>", "Lx!")
  713. call assert_equal(4, tabpagenr('$'))
  714. " open new tab page before tab page 3 in insert mode
  715. call feedkeys("i" .. TabMenuNewItemCode(3) .. "\<C-C>", "Lx!")
  716. call assert_equal(3, tabpagenr())
  717. call assert_equal(5, tabpagenr('$'))
  718. " open new tab page after tab page 4 in insert mode
  719. call feedkeys("i" .. TabMenuNewItemCode(5) .. "\<C-C>", "Lx!")
  720. call assert_equal(5, tabpagenr())
  721. call assert_equal(6, tabpagenr('$'))
  722. %bw!
  723. endfunc
  724. " Test for changing the current tab page from an autocmd when closing a tab
  725. " page.
  726. func Test_tabpage_switchtab_on_close()
  727. only
  728. tabnew
  729. tabnew
  730. " Test for BufLeave
  731. augroup T1
  732. au!
  733. au BufLeave * tabfirst
  734. augroup END
  735. tabclose
  736. call assert_equal(1, tabpagenr())
  737. augroup T1
  738. au!
  739. augroup END
  740. " Test for WinLeave
  741. $tabnew
  742. augroup T1
  743. au!
  744. au WinLeave * tabfirst
  745. augroup END
  746. tabclose
  747. call assert_equal(1, tabpagenr())
  748. augroup T1
  749. au!
  750. augroup END
  751. " Test for TabLeave
  752. $tabnew
  753. augroup T1
  754. au!
  755. au TabLeave * tabfirst
  756. augroup END
  757. tabclose
  758. call assert_equal(1, tabpagenr())
  759. augroup T1
  760. au!
  761. augroup END
  762. augroup! T1
  763. tabonly
  764. endfunc
  765. " Test for closing the destination tabpage when jumping from one to another.
  766. func Test_tabpage_close_on_switch()
  767. tabnew
  768. tabnew
  769. edit Xfile
  770. augroup T2
  771. au!
  772. au BufLeave Xfile 1tabclose
  773. augroup END
  774. tabfirst
  775. call assert_equal(2, tabpagenr())
  776. call assert_equal('Xfile', @%)
  777. augroup T2
  778. au!
  779. augroup END
  780. augroup! T2
  781. %bw!
  782. endfunc
  783. " Test for jumping to last accessed tabpage
  784. func Test_lastused_tabpage()
  785. tabonly!
  786. call assert_equal(0, tabpagenr('#'))
  787. call assert_beeps('call feedkeys("g\<Tab>", "xt")')
  788. call assert_beeps('call feedkeys("\<C-Tab>", "xt")')
  789. call assert_beeps('call feedkeys("\<C-W>g\<Tab>", "xt")')
  790. call assert_fails('tabnext #', 'E475:')
  791. " open four tab pages
  792. tabnew
  793. tabnew
  794. tabnew
  795. 2tabnext
  796. " Test for g<Tab>
  797. call assert_equal(4, tabpagenr('#'))
  798. call feedkeys("g\<Tab>", "xt")
  799. call assert_equal(4, tabpagenr())
  800. call assert_equal(2, tabpagenr('#'))
  801. " Test for <C-Tab>
  802. call feedkeys("\<C-Tab>", "xt")
  803. call assert_equal(2, tabpagenr())
  804. call assert_equal(4, tabpagenr('#'))
  805. " Test for <C-W>g<Tab>
  806. call feedkeys("\<C-W>g\<Tab>", "xt")
  807. call assert_equal(4, tabpagenr())
  808. call assert_equal(2, tabpagenr('#'))
  809. " Test for :tabnext #
  810. tabnext #
  811. call assert_equal(2, tabpagenr())
  812. call assert_equal(4, tabpagenr('#'))
  813. " Try to jump to a closed tab page
  814. tabclose #
  815. call assert_equal(0, tabpagenr('#'))
  816. call feedkeys("g\<Tab>", "xt")
  817. call assert_equal(2, tabpagenr())
  818. call feedkeys("\<C-Tab>", "xt")
  819. call assert_equal(2, tabpagenr())
  820. call feedkeys("\<C-W>g\<Tab>", "xt")
  821. call assert_equal(2, tabpagenr())
  822. call assert_fails('tabnext #', 'E475:')
  823. call assert_equal(2, tabpagenr())
  824. " Test for :tabonly #
  825. let wnum = win_getid()
  826. $tabnew
  827. tabonly #
  828. call assert_equal(wnum, win_getid())
  829. call assert_equal(1, tabpagenr('$'))
  830. " Test for :tabmove #
  831. tabnew
  832. let wnum = win_getid()
  833. tabnew
  834. tabnew
  835. tabnext 2
  836. tabmove #
  837. call assert_equal(4, tabpagenr())
  838. call assert_equal(wnum, win_getid())
  839. tabonly!
  840. endfunc
  841. " Test for tabpage allocation failure
  842. func Test_tabpage_alloc_failure()
  843. CheckFunction test_alloc_fail
  844. call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
  845. call assert_fails('tabnew', 'E342:')
  846. call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
  847. edit Xfile1
  848. call assert_fails('tabedit Xfile2', 'E342:')
  849. call assert_equal(1, winnr('$'))
  850. call assert_equal(1, tabpagenr('$'))
  851. call assert_equal('Xfile1', @%)
  852. new
  853. call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
  854. call assert_fails('wincmd T', 'E342:')
  855. bw!
  856. call test_alloc_fail(GetAllocId('newtabpage_tvars'), 0, 0)
  857. call assert_fails('tab split', 'E342:')
  858. call assert_equal(2, winnr('$'))
  859. call assert_equal(1, tabpagenr('$'))
  860. endfunc
  861. func Test_tabpage_tabclose()
  862. " Default behaviour, move to the right.
  863. call s:reconstruct_tabpage_for_test(6)
  864. norm! 4gt
  865. setl tcl=
  866. tabclose
  867. call assert_equal("n3", bufname())
  868. " Move to the left.
  869. call s:reconstruct_tabpage_for_test(6)
  870. norm! 4gt
  871. setl tcl=left
  872. tabclose
  873. call assert_equal("n1", bufname())
  874. " Move to the last used tab page.
  875. call s:reconstruct_tabpage_for_test(6)
  876. norm! 5gt
  877. norm! 2gt
  878. setl tcl=uselast
  879. tabclose
  880. call assert_equal("n3", bufname())
  881. " Same, but the last used tab page is invalid. Move to the right.
  882. call s:reconstruct_tabpage_for_test(6)
  883. norm! 5gt
  884. norm! 3gt
  885. setl tcl=uselast
  886. tabclose 5
  887. tabclose!
  888. call assert_equal("n2", bufname())
  889. " Same, but the last used tab page is invalid. Move to the left.
  890. call s:reconstruct_tabpage_for_test(6)
  891. norm! 5gt
  892. norm! 3gt
  893. setl tcl=uselast,left
  894. tabclose 5
  895. tabclose!
  896. call assert_equal("n0", bufname())
  897. " Move left when moving right is not possible.
  898. call s:reconstruct_tabpage_for_test(6)
  899. setl tcl=
  900. norm! 6gt
  901. tabclose
  902. call assert_equal("n3", bufname())
  903. " Move right when moving left is not possible.
  904. call s:reconstruct_tabpage_for_test(6)
  905. setl tcl=left
  906. norm! 1gt
  907. tabclose
  908. call assert_equal("n0", bufname())
  909. setl tcl&
  910. endfunc
  911. " this was giving ml_get errors
  912. func Test_tabpage_last_line()
  913. enew
  914. call setline(1, repeat(['a'], &lines + 5))
  915. $
  916. tabnew
  917. call setline(1, repeat(['b'], &lines + 20))
  918. $
  919. tabNext
  920. call assert_equal('a', getline('.'))
  921. bwipe!
  922. bwipe!
  923. endfunc
  924. " this was causing an endless loop
  925. func Test_tabpage_drop_tabmove()
  926. augroup TestTabpageTabmove
  927. au!
  928. autocmd! TabEnter * :if tabpagenr() > 1 | tabmove - | endif
  929. augroup end
  930. $tab drop XTab_99.log
  931. $tab drop XTab_98.log
  932. $tab drop XTab_97.log
  933. autocmd! TestTabpageTabmove
  934. augroup! TestTabpageTabmove
  935. " clean up
  936. bwipe!
  937. bwipe!
  938. bwipe!
  939. endfunc
  940. " Test that settabvar() shouldn't change the last accessed tabpage.
  941. func Test_lastused_tabpage_settabvar()
  942. tabonly!
  943. tabnew
  944. tabnew
  945. tabnew
  946. call assert_equal(3, tabpagenr('#'))
  947. call settabvar(2, 'myvar', 'tabval')
  948. call assert_equal('tabval', gettabvar(2, 'myvar'))
  949. call assert_equal(3, tabpagenr('#'))
  950. bwipe!
  951. bwipe!
  952. bwipe!
  953. endfunc
  954. " vim: shiftwidth=2 sts=2 expandtab