test_buffer.vim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. " Tests for Vim buffer
  2. source check.vim
  3. " Test for the :bunload command with an offset
  4. func Test_bunload_with_offset()
  5. %bwipe!
  6. call writefile(['B1'], 'Xb1', 'D')
  7. call writefile(['B2'], 'Xb2', 'D')
  8. call writefile(['B3'], 'Xb3', 'D')
  9. call writefile(['B4'], 'Xb4', 'D')
  10. " Load four buffers. Unload the second and third buffers and then
  11. " execute .+3bunload to unload the last buffer.
  12. edit Xb1
  13. new Xb2
  14. new Xb3
  15. new Xb4
  16. bunload Xb2
  17. bunload Xb3
  18. exe bufwinnr('Xb1') . 'wincmd w'
  19. .+3bunload
  20. call assert_equal(0, getbufinfo('Xb4')[0].loaded)
  21. call assert_equal('Xb1',
  22. \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
  23. " Load four buffers. Unload the third and fourth buffers. Execute .+3bunload
  24. " and check whether the second buffer is unloaded.
  25. ball
  26. bunload Xb3
  27. bunload Xb4
  28. exe bufwinnr('Xb1') . 'wincmd w'
  29. .+3bunload
  30. call assert_equal(0, getbufinfo('Xb2')[0].loaded)
  31. call assert_equal('Xb1',
  32. \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
  33. " Load four buffers. Unload the second and third buffers and from the last
  34. " buffer execute .-3bunload to unload the first buffer.
  35. ball
  36. bunload Xb2
  37. bunload Xb3
  38. exe bufwinnr('Xb4') . 'wincmd w'
  39. .-3bunload
  40. call assert_equal(0, getbufinfo('Xb1')[0].loaded)
  41. call assert_equal('Xb4',
  42. \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
  43. " Load four buffers. Unload the first and second buffers. Execute .-3bunload
  44. " from the last buffer and check whether the third buffer is unloaded.
  45. ball
  46. bunload Xb1
  47. bunload Xb2
  48. exe bufwinnr('Xb4') . 'wincmd w'
  49. .-3bunload
  50. call assert_equal(0, getbufinfo('Xb3')[0].loaded)
  51. call assert_equal('Xb4',
  52. \ fnamemodify(getbufinfo({'bufloaded' : 1})[0].name, ':t'))
  53. %bwipe!
  54. call assert_fails('1,4bunload', 'E16:')
  55. call assert_fails(',100bunload', 'E16:')
  56. call assert_fails('$bunload', 'E90:')
  57. endfunc
  58. " Test for :buffer, :bnext, :bprevious, :brewind, :blast and :bmodified
  59. " commands
  60. func Test_buflist_browse()
  61. %bwipe!
  62. call assert_fails('buffer 1000', 'E86:')
  63. call writefile(['foo1', 'foo2', 'foo3', 'foo4'], 'Xbrowse1', 'D')
  64. call writefile(['bar1', 'bar2', 'bar3', 'bar4'], 'Xbrowse2', 'D')
  65. call writefile(['baz1', 'baz2', 'baz3', 'baz4'], 'Xbrowse3', 'D')
  66. edit Xbrowse1
  67. let b1 = bufnr()
  68. edit Xbrowse2
  69. let b2 = bufnr()
  70. edit +/baz4 Xbrowse3
  71. let b3 = bufnr()
  72. call assert_fails('buffer ' .. b1 .. ' abc', 'E488:')
  73. call assert_equal(b3, bufnr())
  74. call assert_equal(4, line('.'))
  75. exe 'buffer +/bar2 ' .. b2
  76. call assert_equal(b2, bufnr())
  77. call assert_equal(2, line('.'))
  78. exe 'buffer +/bar1'
  79. call assert_equal(b2, bufnr())
  80. call assert_equal(1, line('.'))
  81. brewind +
  82. call assert_equal(b1, bufnr())
  83. call assert_equal(4, line('.'))
  84. blast +/baz2
  85. call assert_equal(b3, bufnr())
  86. call assert_equal(2, line('.'))
  87. bprevious +/bar4
  88. call assert_equal(b2, bufnr())
  89. call assert_equal(4, line('.'))
  90. bnext +/baz3
  91. call assert_equal(b3, bufnr())
  92. call assert_equal(3, line('.'))
  93. call assert_fails('bmodified', 'E84:')
  94. call setbufvar(b2, '&modified', 1)
  95. exe 'bmodified +/bar3'
  96. call assert_equal(b2, bufnr())
  97. call assert_equal(3, line('.'))
  98. " With no listed buffers in the list, :bnext and :bprev should fail
  99. %bwipe!
  100. set nobuflisted
  101. call assert_fails('bnext', 'E85:')
  102. call assert_fails('bprev', 'E85:')
  103. set buflisted
  104. call assert_fails('sandbox bnext', 'E48:')
  105. %bwipe!
  106. endfunc
  107. " Test for :bnext and :bprev when called from help and non-help buffers.
  108. func Test_bnext_bprev_help()
  109. %bwipe!
  110. e XHelp1 | set bt=help
  111. let b1 = bufnr()
  112. e Xbuf1
  113. let b2 = bufnr()
  114. " There's only one buffer of each type.
  115. b XHelp1
  116. bnext | call assert_equal(b1, bufnr())
  117. bprev | call assert_equal(b1, bufnr())
  118. b Xbuf1
  119. bnext | call assert_equal(b2, bufnr())
  120. bprev | call assert_equal(b2, bufnr())
  121. " Add one more buffer of each type.
  122. e XHelp2 | set bt=help
  123. let b3 = bufnr()
  124. e Xbuf2
  125. let b4 = bufnr()
  126. " Help buffer jumps to help buffer.
  127. b XHelp1
  128. bnext | call assert_equal(b3, bufnr())
  129. bnext | call assert_equal(b1, bufnr())
  130. bprev | call assert_equal(b3, bufnr())
  131. bprev | call assert_equal(b1, bufnr())
  132. " Regular buffer jumps to regular buffer.
  133. b Xbuf1
  134. bnext | call assert_equal(b4, bufnr())
  135. bnext | call assert_equal(b2, bufnr())
  136. bprev | call assert_equal(b4, bufnr())
  137. bprev | call assert_equal(b2, bufnr())
  138. " :brewind and :blast are not affected by the buffer type.
  139. b Xbuf2
  140. brewind | call assert_equal(b1, bufnr())
  141. b XHelp1
  142. blast | call assert_equal(b4, bufnr())
  143. %bwipe!
  144. endfunc
  145. " Test for :bdelete
  146. func Test_bdelete_cmd()
  147. %bwipe!
  148. call assert_fails('bdelete 5', 'E516:')
  149. call assert_fails('1,1bdelete 1 2', 'E488:')
  150. call assert_fails('bdelete \)', 'E55:')
  151. " Deleting an unlisted and unloaded buffer
  152. edit Xbdelfile1
  153. let bnr = bufnr()
  154. set nobuflisted
  155. enew
  156. call assert_fails('bdelete ' .. bnr, 'E516:')
  157. " Deleting more than one buffer
  158. new Xbuf1
  159. new Xbuf2
  160. exe 'bdel ' .. bufnr('Xbuf2') .. ' ' .. bufnr('Xbuf1')
  161. call assert_equal(1, winnr('$'))
  162. call assert_equal(0, getbufinfo('Xbuf1')[0].loaded)
  163. call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
  164. " Deleting more than one buffer and an invalid buffer
  165. new Xbuf1
  166. new Xbuf2
  167. let cmd = "exe 'bdel ' .. bufnr('Xbuf2') .. ' xxx ' .. bufnr('Xbuf1')"
  168. call assert_fails(cmd, 'E94:')
  169. call assert_equal(2, winnr('$'))
  170. call assert_equal(1, getbufinfo('Xbuf1')[0].loaded)
  171. call assert_equal(0, getbufinfo('Xbuf2')[0].loaded)
  172. %bwipe!
  173. endfunc
  174. func Test_buffer_error()
  175. new foo1
  176. new foo2
  177. call assert_fails('buffer foo', 'E93:')
  178. call assert_fails('buffer bar', 'E94:')
  179. call assert_fails('buffer 0', 'E939:')
  180. %bwipe
  181. endfunc
  182. " Test for the status messages displayed when unloading, deleting or wiping
  183. " out buffers
  184. func Test_buffer_statusmsg()
  185. CheckEnglish
  186. set report=1
  187. new Xbuf1
  188. new Xbuf2
  189. let bnr = bufnr()
  190. exe "normal 2\<C-G>"
  191. call assert_match('buf ' .. bnr .. ':', v:statusmsg)
  192. bunload Xbuf1 Xbuf2
  193. call assert_equal('2 buffers unloaded', v:statusmsg)
  194. bdel Xbuf1 Xbuf2
  195. call assert_equal('2 buffers deleted', v:statusmsg)
  196. bwipe Xbuf1 Xbuf2
  197. call assert_equal('2 buffers wiped out', v:statusmsg)
  198. set report&
  199. endfunc
  200. " Test for quitting the 'swapfile exists' dialog with the split buffer
  201. " command.
  202. func Test_buffer_sbuf_cleanup()
  203. call writefile([], 'XsplitCleanup', 'D')
  204. " first open the file in a buffer
  205. new XsplitCleanup
  206. let bnr = bufnr()
  207. close
  208. " create the swap file
  209. call writefile([], '.XsplitCleanup.swp', 'D')
  210. " Remove the catch-all that runtest.vim adds
  211. au! SwapExists
  212. augroup BufTest
  213. au!
  214. autocmd SwapExists XsplitCleanup let v:swapchoice='q'
  215. augroup END
  216. exe 'sbuf ' . bnr
  217. call assert_equal(1, winnr('$'))
  218. call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
  219. " test for :sball
  220. sball
  221. call assert_equal(1, winnr('$'))
  222. call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
  223. %bw!
  224. set shortmess+=F
  225. let v:statusmsg = ''
  226. edit XsplitCleanup
  227. call assert_equal('', v:statusmsg)
  228. call assert_equal(1, winnr('$'))
  229. call assert_equal(0, getbufinfo('XsplitCleanup')[0].loaded)
  230. set shortmess&
  231. augroup BufTest
  232. au!
  233. augroup END
  234. augroup! BufTest
  235. endfunc
  236. " Test for deleting a modified buffer with :confirm
  237. func Test_bdel_with_confirm()
  238. CheckUnix
  239. CheckNotGui
  240. CheckFeature dialog_con
  241. new
  242. call setline(1, 'test')
  243. call assert_fails('bdel', 'E89:')
  244. call feedkeys('c', 'L')
  245. confirm bdel
  246. call assert_equal(2, winnr('$'))
  247. call assert_equal(1, &modified)
  248. call feedkeys('n', 'L')
  249. confirm bdel
  250. call assert_equal(1, winnr('$'))
  251. endfunc
  252. " Test for editing another buffer from a modified buffer with :confirm
  253. func Test_goto_buf_with_confirm()
  254. CheckUnix
  255. CheckNotGui
  256. CheckFeature dialog_con
  257. " When dialog_con_gui is defined, Vim is compiled with GUI support
  258. " and FEAT_BROWSE will be defined, which causes :confirm :b to
  259. " call do_browse(), which will try to use a GUI file browser,
  260. " which aborts if a GUI is not available.
  261. CheckNotFeature dialog_con_gui
  262. new XgotoConf
  263. enew
  264. call setline(1, 'test')
  265. call assert_fails('b XgotoConf', 'E37:')
  266. call feedkeys('c', 'L')
  267. call assert_fails('confirm b XgotoConf', 'E37:')
  268. call assert_true(&modified)
  269. call assert_true(empty(bufname('%')))
  270. call feedkeys('y', 'L')
  271. confirm b XgotoConf
  272. call assert_equal('XgotoConf', bufname('%'))
  273. call assert_equal(['test'], readfile('Untitled'))
  274. e Untitled
  275. call setline(2, 'test2')
  276. call feedkeys('n', 'L')
  277. confirm b XgotoConf
  278. call assert_equal('XgotoConf', bufname('%'))
  279. call assert_equal(['test'], readfile('Untitled'))
  280. call delete('Untitled')
  281. close!
  282. endfunc
  283. " Test for splitting buffer with 'switchbuf'
  284. func Test_buffer_switchbuf()
  285. new Xswitchbuf
  286. wincmd w
  287. set switchbuf=useopen
  288. sbuf Xswitchbuf
  289. call assert_equal(1, winnr())
  290. call assert_equal(2, winnr('$'))
  291. set switchbuf=usetab
  292. tabnew
  293. sbuf Xswitchbuf
  294. call assert_equal(1, tabpagenr())
  295. call assert_equal(2, tabpagenr('$'))
  296. set switchbuf&
  297. %bw
  298. endfunc
  299. " Test for BufAdd autocommand wiping out the buffer
  300. func Test_bufadd_autocmd_bwipe()
  301. %bw!
  302. augroup BufAdd_Wipe
  303. au!
  304. autocmd BufAdd Xbwipe %bw!
  305. augroup END
  306. edit Xbwipe
  307. call assert_equal('', @%)
  308. call assert_equal(0, bufexists('Xbwipe'))
  309. augroup BufAdd_Wipe
  310. au!
  311. augroup END
  312. augroup! BufAdd_Wipe
  313. endfunc
  314. " Test for trying to load a buffer with text locked
  315. " <C-\>e in the command line is used to lock the text
  316. func Test_load_buf_with_text_locked()
  317. new Xlockfile1
  318. edit Xlockfile2
  319. let cmd = ":\<C-\>eexecute(\"normal \<C-O>\")\<CR>\<C-C>"
  320. call assert_fails("call feedkeys(cmd, 'xt')", 'E565:')
  321. %bw!
  322. endfunc
  323. " Test for using CTRL-^ to edit the alternative file keeping the cursor
  324. " position with 'nostartofline'. Also test using the 'buf' command.
  325. func Test_buffer_edit_altfile()
  326. call writefile(repeat(['one two'], 50), 'Xaltfile1', 'D')
  327. call writefile(repeat(['five six'], 50), 'Xaltfile2', 'D')
  328. set nosol
  329. edit Xaltfile1
  330. call cursor(25, 5)
  331. edit Xaltfile2
  332. call cursor(30, 4)
  333. exe "normal \<C-^>"
  334. call assert_equal([0, 25, 5, 0], getpos('.'))
  335. exe "normal \<C-^>"
  336. call assert_equal([0, 30, 4, 0], getpos('.'))
  337. buf Xaltfile1
  338. call assert_equal([0, 25, 5, 0], getpos('.'))
  339. buf Xaltfile2
  340. call assert_equal([0, 30, 4, 0], getpos('.'))
  341. set sol&
  342. endfunc
  343. " Test for running the :sball command with a maximum window count and a
  344. " modified buffer
  345. func Test_sball_with_count()
  346. %bw!
  347. edit Xcountfile1
  348. call setline(1, ['abc'])
  349. new Xcountfile2
  350. new Xcountfile3
  351. new Xcountfile4
  352. 2sball
  353. call assert_equal(bufnr('Xcountfile4'), winbufnr(1))
  354. call assert_equal(bufnr('Xcountfile1'), winbufnr(2))
  355. call assert_equal(0, getbufinfo('Xcountfile2')[0].loaded)
  356. call assert_equal(0, getbufinfo('Xcountfile3')[0].loaded)
  357. %bw!
  358. endfunc
  359. func Test_badd_options()
  360. new SomeNewBuffer
  361. setlocal numberwidth=3
  362. wincmd p
  363. badd +1 SomeNewBuffer
  364. new SomeNewBuffer
  365. call assert_equal(3, &numberwidth)
  366. close
  367. close
  368. bwipe! SomeNewBuffer
  369. endfunc
  370. func Test_balt()
  371. new SomeNewBuffer
  372. balt +3 OtherBuffer
  373. e #
  374. call assert_equal('OtherBuffer', bufname())
  375. endfunc
  376. " Test for buffer match URL(scheme) check
  377. " scheme is alpha and inner hyphen only.
  378. func Test_buffer_scheme()
  379. CheckMSWindows
  380. set noswapfile
  381. set noshellslash
  382. %bwipe!
  383. let bufnames = [
  384. \ #{id: 'ssb0', name: 'test://xyz/foo/ssb0' , match: 1},
  385. \ #{id: 'ssb1', name: 'test+abc://xyz/foo/ssb1', match: 0},
  386. \ #{id: 'ssb2', name: 'test_abc://xyz/foo/ssb2', match: 0},
  387. \ #{id: 'ssb3', name: 'test-abc://xyz/foo/ssb3', match: 1},
  388. \ #{id: 'ssb4', name: '-test://xyz/foo/ssb4' , match: 0},
  389. \ #{id: 'ssb5', name: 'test-://xyz/foo/ssb5' , match: 0},
  390. \]
  391. for buf in bufnames
  392. new `=buf.name`
  393. if buf.match
  394. call assert_equal(buf.name, getbufinfo(buf.id)[0].name)
  395. else
  396. " slashes will have become backslashes
  397. call assert_notequal(buf.name, getbufinfo(buf.id)[0].name)
  398. endif
  399. bwipe
  400. endfor
  401. set shellslash&
  402. set swapfile&
  403. endfunc
  404. " this was using a NULL pointer after failing to use the pattern
  405. func Test_buf_pattern_invalid()
  406. vsplit 0000000
  407. silent! buf [0--]\&\zs*\zs*e
  408. bwipe!
  409. vsplit 00000000000000000000000000
  410. silent! buf [0--]\&\zs*\zs*e
  411. bwipe!
  412. " similar case with different code path
  413. split 0
  414. edit ÿ
  415. silent! buf [0--]\&\zs*\zs*0
  416. bwipe!
  417. endfunc
  418. " Test for the 'maxmem' and 'maxmemtot' options
  419. func Test_buffer_maxmem()
  420. " use 1KB per buffer and 2KB for all the buffers
  421. " set maxmem=1 maxmemtot=2
  422. new
  423. let v:errmsg = ''
  424. " try opening some files
  425. edit test_arglist.vim
  426. call assert_equal('test_arglist.vim', bufname())
  427. edit test_eval_stuff.vim
  428. call assert_equal('test_eval_stuff.vim', bufname())
  429. b test_arglist.vim
  430. call assert_equal('test_arglist.vim', bufname())
  431. b test_eval_stuff.vim
  432. call assert_equal('test_eval_stuff.vim', bufname())
  433. close
  434. call assert_equal('', v:errmsg)
  435. " set maxmem& maxmemtot&
  436. endfunc
  437. " Test for buffer allocation failure
  438. func Test_buflist_alloc_failure()
  439. CheckFunction test_alloc_fail
  440. %bw!
  441. edit XallocFail1
  442. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  443. call assert_fails('edit XallocFail2', 'E342:')
  444. " test for bufadd()
  445. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  446. call assert_fails('call bufadd("Xbuffer")', 'E342:')
  447. " test for setting the arglist
  448. edit XallocFail2
  449. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  450. call assert_fails('next XallocFail3', 'E342:')
  451. " test for setting the alternate buffer name when writing a file
  452. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  453. call assert_fails('write Xother', 'E342:')
  454. call delete('Xother')
  455. " test for creating a buffer using bufnr()
  456. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  457. call assert_fails("call bufnr('Xnewbuf', v:true)", 'E342:')
  458. " test for renaming buffer using :file
  459. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  460. call assert_fails('file Xnewfile', 'E342:')
  461. " test for creating a buffer for a popup window
  462. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  463. call assert_fails('call popup_create("mypop", {})', 'E342:')
  464. if has('terminal')
  465. " test for creating a buffer for a terminal window
  466. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  467. call assert_fails('call term_start(&shell)', 'E342:')
  468. %bw!
  469. endif
  470. " test for loading a new buffer after wiping out all the buffers
  471. edit XallocFail4
  472. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  473. call assert_fails('%bw!', 'E342:')
  474. " test for :checktime loading the buffer
  475. call writefile(['one'], 'XallocFail5', 'D')
  476. if has('unix')
  477. edit XallocFail5
  478. " sleep for some time to make sure the timestamp is different
  479. sleep 200m
  480. call writefile(['two'], 'XallocFail5')
  481. set autoread
  482. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  483. call assert_fails('checktime', 'E342:')
  484. set autoread&
  485. bw!
  486. endif
  487. " test for :vimgrep loading a dummy buffer
  488. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  489. call assert_fails('vimgrep two XallocFail5', 'E342:')
  490. " test for quickfix command loading a buffer
  491. call test_alloc_fail(GetAllocId('newbuf_bvars'), 0, 0)
  492. call assert_fails('cexpr "XallocFail6:10:Line10"', 'E342:')
  493. endfunc
  494. " vim: shiftwidth=2 sts=2 expandtab