test_bufline.vim 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. " Tests for setbufline(), getbufline(), appendbufline(), deletebufline()
  2. source shared.vim
  3. source screendump.vim
  4. source check.vim
  5. func Test_setbufline_getbufline()
  6. " similar to Test_set_get_bufline()
  7. new
  8. let b = bufnr('%')
  9. hide
  10. call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
  11. call assert_equal(['foo'], getbufline(b, 1))
  12. call assert_equal('foo', getbufoneline(b, 1))
  13. call assert_equal(['bar'], getbufline(b, '$'))
  14. call assert_equal('bar', getbufoneline(b, '$'))
  15. call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
  16. exe "bd!" b
  17. call assert_equal([], getbufline(b, 1, 2))
  18. split Xtest
  19. call setline(1, ['a', 'b', 'c'])
  20. let b = bufnr('%')
  21. wincmd w
  22. call assert_equal(1, setbufline(b, 5, 'x'))
  23. call assert_equal(1, setbufline(b, 5, ['x']))
  24. call assert_equal(0, setbufline(b, 5, []))
  25. call assert_equal(0, setbufline(b, 5, v:_null_list))
  26. call assert_equal(1, 'x'->setbufline(bufnr('$') + 1, 1))
  27. call assert_equal(1, ['x']->setbufline(bufnr('$') + 1, 1))
  28. call assert_equal(1, []->setbufline(bufnr('$') + 1, 1))
  29. call assert_equal(1, v:_null_list->setbufline(bufnr('$') + 1, 1))
  30. call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
  31. call assert_equal(0, setbufline(b, 4, ['d', 'e']))
  32. call assert_equal(['c'], b->getbufline(3))
  33. call assert_equal('c', b->getbufoneline(3))
  34. call assert_equal(['d'], getbufline(b, 4))
  35. call assert_equal('d', getbufoneline(b, 4))
  36. call assert_equal(['e'], getbufline(b, 5))
  37. call assert_equal('e', getbufoneline(b, 5))
  38. call assert_equal([], getbufline(b, 6))
  39. call assert_equal([], getbufline(b, 2, 1))
  40. if has('job')
  41. call setbufline(b, 2, [function('eval'), #{key: 123}, test_null_job()])
  42. call assert_equal(["function('eval')",
  43. \ "{'key': 123}",
  44. \ "no process"],
  45. \ getbufline(b, 2, 4))
  46. endif
  47. exe "bwipe! " . b
  48. endfunc
  49. func Test_setbufline_getbufline_fold()
  50. split Xtest
  51. setlocal foldmethod=expr foldexpr=0
  52. let b = bufnr('%')
  53. new
  54. call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
  55. call assert_equal(['foo'], getbufline(b, 1))
  56. call assert_equal(['bar'], getbufline(b, 2))
  57. call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
  58. exe "bwipe!" b
  59. bwipe!
  60. endfunc
  61. func Test_setbufline_getbufline_fold_tab()
  62. split Xtest
  63. setlocal foldmethod=expr foldexpr=0
  64. let b = bufnr('%')
  65. tab new
  66. call assert_equal(0, setbufline(b, 1, ['foo', 'bar']))
  67. call assert_equal(['foo'], getbufline(b, 1))
  68. call assert_equal(['bar'], getbufline(b, 2))
  69. call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
  70. exe "bwipe!" b
  71. bwipe!
  72. endfunc
  73. func Test_setline_startup()
  74. let cmd = GetVimCommand('Xscript')
  75. if cmd == ''
  76. return
  77. endif
  78. call writefile(['call setline(1, "Hello")', 'silent w Xtest', 'q!'], 'Xscript', 'D')
  79. call system(cmd)
  80. sleep 50m
  81. call assert_equal(['Hello'], readfile('Xtest'))
  82. call assert_equal(0, setline(1, []))
  83. call assert_equal(0, setline(1, v:_null_list))
  84. call assert_equal(0, setline(5, []))
  85. call assert_equal(0, setline(6, v:_null_list))
  86. call delete('Xtest')
  87. endfunc
  88. func Test_appendbufline()
  89. new
  90. let b = bufnr('%')
  91. hide
  92. new
  93. call setline(1, ['line1', 'line2', 'line3'])
  94. normal! 2gggg
  95. call assert_equal(2, line("''"))
  96. call assert_equal(0, appendbufline(b, 0, ['foo', 'bar']))
  97. call assert_equal(['foo'], getbufline(b, 1))
  98. call assert_equal(['bar'], getbufline(b, 2))
  99. call assert_equal(['foo', 'bar'], getbufline(b, 1, 2))
  100. call assert_equal(0, appendbufline(b, 0, 'baz'))
  101. call assert_equal(['baz', 'foo', 'bar'], getbufline(b, 1, 3))
  102. " appendbufline() in a hidden buffer shouldn't move marks in current window.
  103. call assert_equal(2, line("''"))
  104. bwipe!
  105. exe "bd!" b
  106. call assert_equal([], getbufline(b, 1, 3))
  107. split Xtest
  108. call setline(1, ['a', 'b', 'c'])
  109. let b = bufnr('%')
  110. wincmd w
  111. call assert_equal(1, appendbufline(b, -1, 'x'))
  112. call assert_equal(1, appendbufline(b, -1, ['x']))
  113. call assert_equal(1, appendbufline(b, -1, []))
  114. call assert_equal(1, appendbufline(b, -1, v:_null_list))
  115. call assert_equal(1, appendbufline(b, 4, 'x'))
  116. call assert_equal(1, appendbufline(b, 4, ['x']))
  117. call assert_equal(0, appendbufline(b, 4, []))
  118. call assert_equal(0, appendbufline(b, 4, v:_null_list))
  119. call assert_equal(1, appendbufline(1234, 1, 'x'))
  120. call assert_equal(1, appendbufline(1234, 1, ['x']))
  121. call assert_equal(1, appendbufline(1234, 1, []))
  122. call assert_equal(1, appendbufline(1234, 1, v:_null_list))
  123. call assert_equal(0, appendbufline(b, 1, []))
  124. call assert_equal(0, appendbufline(b, 1, v:_null_list))
  125. call assert_equal(0, appendbufline(b, 3, []))
  126. call assert_equal(0, appendbufline(b, 3, v:_null_list))
  127. call assert_equal(['a', 'b', 'c'], getbufline(b, 1, '$'))
  128. call assert_equal(0, appendbufline(b, 3, ['d', 'e']))
  129. call assert_equal(['c'], getbufline(b, 3))
  130. call assert_equal(['d'], getbufline(b, 4))
  131. call assert_equal(['e'], getbufline(b, 5))
  132. call assert_equal([], getbufline(b, 6))
  133. exe "bwipe! " . b
  134. endfunc
  135. func Test_deletebufline()
  136. new
  137. let b = bufnr('%')
  138. call setline(1, ['aaa', 'bbb', 'ccc'])
  139. hide
  140. new
  141. call setline(1, ['line1', 'line2', 'line3'])
  142. normal! 2gggg
  143. call assert_equal(2, line("''"))
  144. call assert_equal(0, deletebufline(b, 2))
  145. call assert_equal(['aaa', 'ccc'], getbufline(b, 1, 2))
  146. call assert_equal(0, deletebufline(b, 2, 8))
  147. call assert_equal(['aaa'], getbufline(b, 1, 2))
  148. " deletebufline() in a hidden buffer shouldn't move marks in current window.
  149. call assert_equal(2, line("''"))
  150. bwipe!
  151. exe "bd!" b
  152. call assert_equal(1, b->deletebufline(1))
  153. call assert_equal(1, deletebufline(-1, 1))
  154. split Xtest
  155. call setline(1, ['a', 'b', 'c'])
  156. call cursor(line('$'), 1)
  157. let b = bufnr('%')
  158. wincmd w
  159. call assert_equal(1, deletebufline(b, 4))
  160. call assert_equal(0, deletebufline(b, 1))
  161. call assert_equal(['b', 'c'], getbufline(b, 1, 2))
  162. exe "bwipe! " . b
  163. edit XbufOne
  164. let one = bufnr()
  165. call setline(1, ['a', 'b', 'c'])
  166. setlocal nomodifiable
  167. split XbufTwo
  168. let two = bufnr()
  169. call assert_fails('call deletebufline(one, 1)', 'E21:')
  170. call assert_equal(two, bufnr())
  171. bwipe! XbufTwo
  172. bwipe! XbufOne
  173. endfunc
  174. func Test_appendbufline_redraw()
  175. CheckScreendump
  176. let lines =<< trim END
  177. new foo
  178. let winnr = 'foo'->bufwinnr()
  179. let buf = bufnr('foo')
  180. wincmd p
  181. call appendbufline(buf, '$', range(1,200))
  182. exe winnr .. 'wincmd w'
  183. norm! G
  184. wincmd p
  185. call deletebufline(buf, 1, '$')
  186. call appendbufline(buf, '$', 'Hello Vim world...')
  187. END
  188. call writefile(lines, 'XscriptMatchCommon', 'D')
  189. let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 10})
  190. call VerifyScreenDump(buf, 'Test_appendbufline_1', {})
  191. call StopVimInTerminal(buf)
  192. endfunc
  193. func Test_setbufline_select_mode()
  194. new
  195. call setline(1, ['foo', 'bar'])
  196. call feedkeys("j^v2l\<C-G>", 'nx')
  197. let bufnr = bufadd('Xdummy')
  198. call bufload(bufnr)
  199. call setbufline(bufnr, 1, ['abc'])
  200. call feedkeys("x", 'nx')
  201. call assert_equal(['foo', 'x'], getline(1, 2))
  202. exe "bwipe! " .. bufnr
  203. bwipe!
  204. endfunc
  205. func Test_deletebufline_select_mode()
  206. new
  207. call setline(1, ['foo', 'bar'])
  208. call feedkeys("j^v2l\<C-G>", 'nx')
  209. let bufnr = bufadd('Xdummy')
  210. call bufload(bufnr)
  211. call setbufline(bufnr, 1, ['abc', 'def'])
  212. call deletebufline(bufnr, 1)
  213. call feedkeys("x", 'nx')
  214. call assert_equal(['foo', 'x'], getline(1, 2))
  215. exe "bwipe! " .. bufnr
  216. bwipe!
  217. endfunc
  218. func Test_setbufline_startup_nofile()
  219. let before =<< trim [CODE]
  220. set shortmess+=F
  221. file Xresult
  222. set buftype=nofile
  223. call setbufline('', 1, 'success')
  224. [CODE]
  225. let after =<< trim [CODE]
  226. set buftype=
  227. write
  228. quit
  229. [CODE]
  230. if !RunVim(before, after, '--clean')
  231. return
  232. endif
  233. call assert_equal(['success'], readfile('Xresult'))
  234. call delete('Xresult')
  235. endfunc
  236. " Test that setbufline(), appendbufline() and deletebufline() should fail and
  237. " return 1 when "textlock" is active.
  238. func Test_change_bufline_with_textlock()
  239. new
  240. inoremap <buffer> <expr> <F2> setbufline('', 1, '')
  241. call assert_fails("normal a\<F2>", 'E565:')
  242. call assert_equal('1', getline(1))
  243. inoremap <buffer> <expr> <F2> appendbufline('', 1, '')
  244. call assert_fails("normal a\<F2>", 'E565:')
  245. call assert_equal('11', getline(1))
  246. inoremap <buffer> <expr> <F2> deletebufline('', 1)
  247. call assert_fails("normal a\<F2>", 'E565:')
  248. call assert_equal('111', getline(1))
  249. bwipe!
  250. endfunc
  251. " vim: shiftwidth=2 sts=2 expandtab