test_put.vim 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. " Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc.
  2. source check.vim
  3. source screendump.vim
  4. func Test_put_block()
  5. new
  6. call feedkeys("i\<C-V>u2500\<CR>x\<ESC>", 'x')
  7. call feedkeys("\<C-V>y", 'x')
  8. call feedkeys("gg0p", 'x')
  9. call assert_equal("\u2500x", getline(1))
  10. bwipe!
  11. endfunc
  12. func Test_put_block_unicode()
  13. new
  14. call setreg('a', "À\nÀÀ\naaaaaaaaaaaa", "\<C-V>")
  15. call setline(1, [' 1', ' 2', ' 3'])
  16. exe "norm! \<C-V>jj\"ap"
  17. let expected = ['À 1', 'ÀÀ 2', 'aaaaaaaaaaaa3']
  18. call assert_equal(expected, getline(1, 3))
  19. bw!
  20. endfunc
  21. func Test_put_char_block()
  22. new
  23. call setline(1, ['Line 1', 'Line 2'])
  24. f Xfile_put
  25. " visually select both lines and put the cursor at the top of the visual
  26. " selection and then put the buffer name over it
  27. exe "norm! G0\<c-v>ke\"%p"
  28. call assert_equal(['Xfile_put 1', 'Xfile_put 2'], getline(1,2))
  29. bw!
  30. endfunc
  31. func Test_put_char_block2()
  32. new
  33. call setreg('a', ' one ', 'v')
  34. call setline(1, ['Line 1', '', 'Line 3', ''])
  35. " visually select the first 3 lines and put register a over it
  36. exe "norm! ggl\<c-v>2j2l\"ap"
  37. call assert_equal(['L one 1', '', 'L one 3', ''], getline(1, 4))
  38. " clean up
  39. bw!
  40. endfunc
  41. func Test_put_lines()
  42. new
  43. let a = [ getreg('a'), getregtype('a') ]
  44. call setline(1, ['Line 1', 'Line2', 'Line 3', ''])
  45. exe 'norm! gg"add"AddG""p'
  46. call assert_equal(['Line 3', '', 'Line 1', 'Line2'], getline(1, '$'))
  47. " clean up
  48. bw!
  49. eval a[0]->setreg('a', a[1])
  50. endfunc
  51. func Test_put_expr()
  52. new
  53. call setline(1, repeat(['A'], 6))
  54. exec "1norm! \"=line('.')\<cr>p"
  55. norm! j0.
  56. norm! j0.
  57. exec "4norm! \"=\<cr>P"
  58. norm! j0.
  59. norm! j0.
  60. call assert_equal(['A1','A2','A3','4A','5A','6A'], getline(1, '$'))
  61. bw!
  62. endfunc
  63. func Test_put_fails_when_nomodifiable()
  64. new
  65. setlocal nomodifiable
  66. normal! yy
  67. call assert_fails(':put', 'E21')
  68. call assert_fails(':put!', 'E21')
  69. call assert_fails(':normal! p', 'E21')
  70. call assert_fails(':normal! gp', 'E21')
  71. call assert_fails(':normal! P', 'E21')
  72. call assert_fails(':normal! gP', 'E21')
  73. if has('mouse')
  74. set mouse=n
  75. call assert_fails('execute "normal! \<MiddleMouse>"', 'E21')
  76. set mouse&
  77. endif
  78. bwipeout!
  79. endfunc
  80. " A bug was discovered where the Normal mode put commands (e.g., "p") would
  81. " output duplicate error messages when invoked in a non-modifiable buffer.
  82. func Test_put_p_errmsg_nodup()
  83. new
  84. setlocal nomodifiable
  85. normal! yy
  86. func Capture_p_error()
  87. redir => s:p_err
  88. normal! p
  89. redir END
  90. endfunc
  91. silent! call Capture_p_error()
  92. " Error message output within a function should be three lines (the function
  93. " name, the line number, and the error message).
  94. call assert_equal(3, count(s:p_err, "\n"))
  95. delfunction Capture_p_error
  96. bwipeout!
  97. endfunc
  98. func Test_put_p_indent_visual()
  99. new
  100. call setline(1, ['select this text', 'select that text'])
  101. " yank "that" from the second line
  102. normal 2Gwvey
  103. " select "this" in the first line and put
  104. normal k0wve[p
  105. call assert_equal('select that text', getline(1))
  106. call assert_equal('select that text', getline(2))
  107. bwipe!
  108. endfunc
  109. " Test for deleting all the contents of a buffer with a put
  110. func Test_put_visual_delete_all_lines()
  111. new
  112. call setline(1, ['one', 'two', 'three'])
  113. let @r = ''
  114. normal! VG"rgp
  115. call assert_equal(1, line('$'))
  116. close!
  117. endfunc
  118. func Test_gp_with_count_leaves_cursor_at_end()
  119. new
  120. call setline(1, '<---->')
  121. call setreg('@', "foo\nbar", 'c')
  122. normal 1G3|3gp
  123. call assert_equal([0, 4, 4, 0], getpos("."))
  124. call assert_equal(['<--foo', 'barfoo', 'barfoo', 'bar-->'], getline(1, '$'))
  125. call assert_equal([0, 4, 3, 0], getpos("']"))
  126. bwipe!
  127. endfunc
  128. func Test_p_with_count_leaves_mark_at_end()
  129. new
  130. call setline(1, '<---->')
  131. call setreg('@', "start\nend", 'c')
  132. normal 1G3|3p
  133. call assert_equal([0, 1, 4, 0], getpos("."))
  134. call assert_equal(['<--start', 'endstart', 'endstart', 'end-->'], getline(1, '$'))
  135. call assert_equal([0, 4, 3, 0], getpos("']"))
  136. bwipe!
  137. endfunc
  138. func Test_very_large_count()
  139. new
  140. " total put-length (21474837 * 100) brings 32 bit int overflow
  141. let @" = repeat('x', 100)
  142. call assert_fails('norm 21474837p', 'E1240:')
  143. bwipe!
  144. endfunc
  145. func Test_very_large_count_64bit()
  146. new
  147. let @" = repeat('x', 100)
  148. call assert_fails('norm 999999999p', 'E1240:')
  149. bwipe!
  150. endfunc
  151. func Test_very_large_count_block()
  152. new
  153. " total put-length (21474837 * 100) brings 32 bit int overflow
  154. call setline(1, repeat('x', 100))
  155. exe "norm \<C-V>99ly"
  156. call assert_fails('norm 21474837p', 'E1240:')
  157. bwipe!
  158. endfunc
  159. func Test_very_large_count_block_64bit()
  160. new
  161. call setline(1, repeat('x', 100))
  162. exe "norm \<C-V>$y"
  163. call assert_fails('norm 999999999p', 'E1240:')
  164. bwipe!
  165. endfunc
  166. func Test_put_above_first_line()
  167. new
  168. let @" = 'text'
  169. silent! normal 0o00
  170. 0put
  171. call assert_equal('text', getline(1))
  172. bwipe!
  173. endfunc
  174. func Test_multibyte_op_end_mark()
  175. new
  176. call setline(1, 'тест')
  177. normal viwdp
  178. call assert_equal([0, 1, 7, 0], getpos("'>"))
  179. call assert_equal([0, 1, 7, 0], getpos("']"))
  180. normal Vyp
  181. call assert_equal([0, 1, v:maxcol, 0], getpos("'>"))
  182. call assert_equal([0, 2, 7, 0], getpos("']"))
  183. bwipe!
  184. endfunc
  185. " this was putting a mark before the start of a line
  186. func Test_put_empty_register()
  187. new
  188. norm yy
  189. norm [Pi00ggv)s0
  190. sil! norm [P
  191. bwipe!
  192. endfunc
  193. " this was putting the end mark after the end of the line
  194. func Test_put_visual_mode()
  195. edit! SomeNewBuffer
  196. set selection=exclusive
  197. exe "norm o\t"
  198. m0
  199. sil! norm  p p
  200. bwipe!
  201. set selection&
  202. endfunc
  203. func Test_put_visual_block_mode()
  204. enew
  205. exe "norm 0R\<CR>\<C-C>V"
  206. sil exe "norm \<C-V>c \<MiddleDrag>"
  207. set ve=all
  208. sil norm vz=p
  209. bwipe!
  210. set ve=
  211. endfunc
  212. func Test_put_other_window()
  213. CheckRunVimInTerminal
  214. let lines =<< trim END
  215. 40vsplit
  216. 0put ='some text at the top'
  217. put =' one more text'
  218. put =' two more text'
  219. put =' three more text'
  220. put =' four more text'
  221. END
  222. call writefile(lines, 'Xtest_put_other', 'D')
  223. let buf = RunVimInTerminal('-S Xtest_put_other', #{rows: 10})
  224. call VerifyScreenDump(buf, 'Test_put_other_window_1', {})
  225. call StopVimInTerminal(buf)
  226. endfunc
  227. func Test_put_in_last_displayed_line()
  228. CheckRunVimInTerminal
  229. let lines =<< trim END
  230. vim9script
  231. autocmd CursorMoved * eval line('w$')
  232. @a = 'x'->repeat(&columns * 2 - 2)
  233. range(&lines)->setline(1)
  234. feedkeys('G"ap')
  235. END
  236. call writefile(lines, 'Xtest_put_last_line', 'D')
  237. let buf = RunVimInTerminal('-S Xtest_put_last_line', #{rows: 10})
  238. call VerifyScreenDump(buf, 'Test_put_in_last_displayed_line_1', {})
  239. call StopVimInTerminal(buf)
  240. endfunc
  241. func Test_put_visual_replace_whole_fold()
  242. new
  243. let lines = repeat(['{{{1', 'foo', 'bar', ''], 2)
  244. call setline(1, lines)
  245. setlocal foldmethod=marker
  246. call setreg('"', 'baz')
  247. call setreg('1', '')
  248. normal! Vp
  249. call assert_equal("{{{1\nfoo\nbar\n\n", getreg('1'))
  250. call assert_equal(['baz', '{{{1', 'foo', 'bar', ''], getline(1, '$'))
  251. bwipe!
  252. endfunc
  253. func Test_put_visual_replace_fold_marker()
  254. new
  255. let lines = repeat(['{{{1', 'foo', 'bar', ''], 4)
  256. call setline(1, lines)
  257. setlocal foldmethod=marker
  258. normal! Gkzo
  259. call setreg('"', '{{{1')
  260. call setreg('1', '')
  261. normal! Vp
  262. call assert_equal("{{{1\n", getreg('1'))
  263. call assert_equal(lines, getline(1, '$'))
  264. bwipe!
  265. endfunc
  266. func Test_put_dict()
  267. new
  268. let d = #{a: #{b: 'abc'}, c: [1, 2], d: 0z10}
  269. put! =d
  270. call assert_equal(["{'a': {'b': 'abc'}, 'c': [1, 2], 'd': 0z10}", ''],
  271. \ getline(1, '$'))
  272. bw!
  273. endfunc
  274. func Test_put_list()
  275. new
  276. let l = ['a', 'b', 'c']
  277. put! =l
  278. call assert_equal(['a', 'b', 'c', ''], getline(1, '$'))
  279. bw!
  280. endfunc
  281. " vim: shiftwidth=2 sts=2 expandtab