062_tab_pages_spec.lua 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. -- Tests for tab pages
  2. local helpers = require('test.functional.helpers')(after_each)
  3. local feed, insert, source, clear, command, expect, eval, eq =
  4. helpers.feed, helpers.insert, helpers.source, helpers.clear,
  5. helpers.command, helpers.expect, helpers.eval, helpers.eq
  6. local exc_exec = helpers.exc_exec
  7. describe('tab pages', function()
  8. before_each(clear)
  9. it('can be opened and closed', function()
  10. command('tabnew')
  11. eq(2, eval('tabpagenr()'))
  12. command('quit')
  13. eq(1, eval('tabpagenr()'))
  14. end)
  15. it('can be iterated with :tabdo', function()
  16. source([[
  17. 0tabnew
  18. 1tabnew
  19. $tabnew
  20. tabdo call append(line('$'), 'this is tab page ' . tabpagenr())
  21. tabclose! 2
  22. tabrewind
  23. ]])
  24. eq('this is tab page 1', eval("getline('$')"))
  25. command('tablast')
  26. eq('this is tab page 4', eval("getline('$')"))
  27. end)
  28. it('have local variables accasible with settabvar()/gettabvar()', function()
  29. -- Test for settabvar() and gettabvar() functions. Open a new tab page and
  30. -- set 3 variables to a number, string and a list. Verify that the
  31. -- variables are correctly set.
  32. source([[
  33. tabnew
  34. tabfirst
  35. call settabvar(2, 'val_num', 100)
  36. call settabvar(2, 'val_str', 'SetTabVar test')
  37. call settabvar(2, 'val_list', ['red', 'blue', 'green'])
  38. ]])
  39. eq(100, eval('gettabvar(2, "val_num")'))
  40. eq('SetTabVar test', eval('gettabvar(2, "val_str")'))
  41. eq({'red', 'blue', 'green'}, eval('gettabvar(2, "val_list")'))
  42. command('tabnext 2')
  43. eq(100, eval('t:val_num'))
  44. eq('SetTabVar test', eval('t:val_str'))
  45. eq({'red', 'blue', 'green'}, eval('t:val_list'))
  46. end)
  47. it('work together with the drop feature and loaded buffers', function()
  48. -- Test for ":tab drop exist-file" to keep current window.
  49. command('sp test1')
  50. command('tab drop test1')
  51. eq(1, eval('tabpagenr("$")'))
  52. eq(2, eval('winnr("$")'))
  53. eq(1, eval('winnr()'))
  54. end)
  55. it('work together with the drop feature and new files', function()
  56. -- Test for ":tab drop new-file" to keep current window of tabpage 1.
  57. command('split')
  58. command('tab drop newfile')
  59. eq(2, eval('tabpagenr("$")'))
  60. eq(2, eval('tabpagewinnr(1, "$")'))
  61. eq(1, eval('tabpagewinnr(1)'))
  62. end)
  63. it('work together with the drop feature and multi loaded buffers', function()
  64. -- Test for ":tab drop multi-opend-file" to keep current tabpage and
  65. -- window.
  66. command('new test1')
  67. command('tabnew')
  68. command('new test1')
  69. command('tab drop test1')
  70. eq(2, eval('tabpagenr()'))
  71. eq(2, eval('tabpagewinnr(2, "$")'))
  72. eq(1, eval('tabpagewinnr(2)'))
  73. end)
  74. it('can be navigated with :tabmove', function()
  75. command('lang C')
  76. command('for i in range(9) | tabnew | endfor')
  77. feed('1gt')
  78. eq(1, eval('tabpagenr()'))
  79. command('tabmove 5')
  80. eq(5, eval('tabpagenr()'))
  81. command('.tabmove')
  82. eq(5, eval('tabpagenr()'))
  83. command('tabmove -')
  84. eq(4, eval('tabpagenr()'))
  85. command('tabmove +')
  86. eq(5, eval('tabpagenr()'))
  87. command('tabmove -2')
  88. eq(3, eval('tabpagenr()'))
  89. command('tabmove +4')
  90. eq(7, eval('tabpagenr()'))
  91. command('tabmove')
  92. eq(10, eval('tabpagenr()'))
  93. command('0tabmove')
  94. eq(1, eval('tabpagenr()'))
  95. command('$tabmove')
  96. eq(10, eval('tabpagenr()'))
  97. command('tabmove 0')
  98. eq(1, eval('tabpagenr()'))
  99. command('tabmove $')
  100. eq(10, eval('tabpagenr()'))
  101. command('3tabmove')
  102. eq(4, eval('tabpagenr()'))
  103. command('7tabmove 5')
  104. eq(5, eval('tabpagenr()'))
  105. command('let a="No error caught."')
  106. eq('Vim(tabmove):E474: Invalid argument: tabmove foo',
  107. exc_exec('tabmove foo'))
  108. end)
  109. it('can trigger certain autocommands', function()
  110. insert('Results:')
  111. -- Test autocommands.
  112. source([[
  113. tabonly!
  114. let g:r=[]
  115. command -nargs=1 -bar C :call add(g:r, '=== '.<q-args>.' ===')|<args>
  116. function Test()
  117. autocmd TabEnter * :call add(g:r, 'TabEnter')
  118. autocmd WinEnter * :call add(g:r, 'WinEnter')
  119. autocmd BufEnter * :call add(g:r, 'BufEnter')
  120. autocmd TabLeave * :call add(g:r, 'TabLeave')
  121. autocmd WinLeave * :call add(g:r, 'WinLeave')
  122. autocmd BufLeave * :call add(g:r, 'BufLeave')
  123. let t:a='a'
  124. C tab split
  125. let t:a='b'
  126. C tabnew
  127. let t:a='c'
  128. call add(g:r, join(map(range(1, tabpagenr('$')),
  129. \ 'gettabvar(v:val, "a")')))
  130. C call map(range(1, tabpagenr('$')),
  131. \ 'settabvar(v:val, ''a'', v:val*2)')
  132. call add(g:r, join(map(range(1, tabpagenr('$')),
  133. \ 'gettabvar(v:val, "a")')))
  134. let w:a='a'
  135. C vsplit
  136. let w:a='a'
  137. let tabn=tabpagenr()
  138. let winr=range(1, winnr('$'))
  139. C tabnext 1
  140. call add(g:r, join(map(copy(winr),
  141. \ 'gettabwinvar('.tabn.', v:val, "a")')))
  142. C call map(copy(winr),
  143. \ 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)')
  144. call add(g:r, join(map(copy(winr),
  145. \ 'gettabwinvar('.tabn.', v:val, "a")')))
  146. augroup TabDestructive
  147. autocmd TabEnter * :C tabnext 2 | C tabclose 3
  148. augroup END
  149. C tabnext 3
  150. let g:r+=[tabpagenr().'/'.tabpagenr('$')]
  151. autocmd! TabDestructive TabEnter
  152. C tabnew
  153. C tabnext 1
  154. autocmd TabDestructive TabEnter * nested
  155. \ :C tabnext 2 | C tabclose 3
  156. C tabnext 2
  157. let g:r+=[tabpagenr().'/'.tabpagenr('$')]
  158. endfunction
  159. call Test()
  160. $ put =g:r
  161. ]])
  162. -- Assert buffer contents.
  163. expect([[
  164. Results:
  165. === tab split ===
  166. WinLeave
  167. TabLeave
  168. WinEnter
  169. TabEnter
  170. === tabnew ===
  171. WinLeave
  172. TabLeave
  173. WinEnter
  174. TabEnter
  175. BufLeave
  176. BufEnter
  177. a b c
  178. === call map(range(1, tabpagenr('$')), 'settabvar(v:val, ''a'', v:val*2)') ===
  179. 2 4 6
  180. === vsplit ===
  181. WinLeave
  182. WinEnter
  183. === tabnext 1 ===
  184. BufLeave
  185. WinLeave
  186. TabLeave
  187. WinEnter
  188. TabEnter
  189. BufEnter
  190. a a
  191. === call map(copy(winr), 'settabwinvar('.tabn.', v:val, ''a'', v:val*2)') ===
  192. 2 4
  193. === tabnext 3 ===
  194. BufLeave
  195. WinLeave
  196. TabLeave
  197. WinEnter
  198. TabEnter
  199. === tabnext 2 ===
  200. === tabclose 3 ===
  201. 2/2
  202. === tabnew ===
  203. WinLeave
  204. TabLeave
  205. WinEnter
  206. TabEnter
  207. BufLeave
  208. BufEnter
  209. === tabnext 1 ===
  210. BufLeave
  211. WinLeave
  212. TabLeave
  213. WinEnter
  214. TabEnter
  215. BufEnter
  216. === tabnext 2 ===
  217. WinLeave
  218. TabLeave
  219. WinEnter
  220. TabEnter
  221. === tabnext 2 ===
  222. === tabclose 3 ===
  223. 2/2]])
  224. eq(2, eval("tabpagenr('$')"))
  225. end)
  226. end)