blob: 0fde026401a9b54dbd55324192bd674b0e1d5152 [file] [log] [blame]
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +02001" Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc.
Bram Moolenaarc8129962017-01-22 20:04:51 +01002
Dominique Pellea9173d02021-10-11 20:26:55 +01003source check.vim
4
Bram Moolenaarc8129962017-01-22 20:04:51 +01005func Test_put_block()
Bram Moolenaarc8129962017-01-22 20:04:51 +01006 new
7 call feedkeys("i\<C-V>u2500\<CR>x\<ESC>", 'x')
8 call feedkeys("\<C-V>y", 'x')
9 call feedkeys("gg0p", 'x')
10 call assert_equal("\u2500x", getline(1))
11 bwipe!
12endfunc
Bram Moolenaar9957a102017-01-23 21:53:53 +010013
14func Test_put_char_block()
15 new
16 call setline(1, ['Line 1', 'Line 2'])
17 f Xfile_put
18 " visually select both lines and put the cursor at the top of the visual
19 " selection and then put the buffer name over it
20 exe "norm! G0\<c-v>ke\"%p"
21 call assert_equal(['Xfile_put 1', 'Xfile_put 2'], getline(1,2))
22 bw!
23endfunc
Bram Moolenaar941c12d2017-01-24 19:55:43 +010024
25func Test_put_char_block2()
26 new
Bram Moolenaar941c12d2017-01-24 19:55:43 +010027 call setreg('a', ' one ', 'v')
28 call setline(1, ['Line 1', '', 'Line 3', ''])
29 " visually select the first 3 lines and put register a over it
30 exe "norm! ggl\<c-v>2j2l\"ap"
Bram Moolenaar02c037a2020-08-30 19:26:45 +020031 call assert_equal(['L one 1', '', 'L one 3', ''], getline(1, 4))
Bram Moolenaar941c12d2017-01-24 19:55:43 +010032 " clean up
33 bw!
Bram Moolenaar941c12d2017-01-24 19:55:43 +010034endfunc
Bram Moolenaar18d90b92017-06-27 15:39:14 +020035
36func Test_put_lines()
37 new
38 let a = [ getreg('a'), getregtype('a') ]
39 call setline(1, ['Line 1', 'Line2', 'Line 3', ''])
40 exe 'norm! gg"add"AddG""p'
Bram Moolenaar02c037a2020-08-30 19:26:45 +020041 call assert_equal(['Line 3', '', 'Line 1', 'Line2'], getline(1, '$'))
Bram Moolenaar18d90b92017-06-27 15:39:14 +020042 " clean up
43 bw!
Bram Moolenaaraad222c2019-09-06 22:46:09 +020044 eval a[0]->setreg('a', a[1])
Bram Moolenaar18d90b92017-06-27 15:39:14 +020045endfunc
Bram Moolenaar833093b2018-05-23 21:53:52 +020046
47func Test_put_expr()
48 new
49 call setline(1, repeat(['A'], 6))
50 exec "1norm! \"=line('.')\<cr>p"
51 norm! j0.
52 norm! j0.
53 exec "4norm! \"=\<cr>P"
54 norm! j0.
55 norm! j0.
Bram Moolenaar02c037a2020-08-30 19:26:45 +020056 call assert_equal(['A1','A2','A3','4A','5A','6A'], getline(1, '$'))
Bram Moolenaar833093b2018-05-23 21:53:52 +020057 bw!
58endfunc
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020059
60func Test_put_fails_when_nomodifiable()
61 new
Bram Moolenaarec12d642018-06-27 23:12:36 +020062 setlocal nomodifiable
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020063
64 normal! yy
Bram Moolenaare2e40752020-09-04 21:18:46 +020065 call assert_fails(':put', 'E21:')
66 call assert_fails(':put!', 'E21:')
67 call assert_fails(':normal! p', 'E21:')
68 call assert_fails(':normal! gp', 'E21:')
69 call assert_fails(':normal! P', 'E21:')
70 call assert_fails(':normal! gP', 'E21:')
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020071
72 if has('mouse')
73 set mouse=n
Bram Moolenaare2e40752020-09-04 21:18:46 +020074 call assert_fails('execute "normal! \<MiddleMouse>"', 'E21:')
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020075 set mouse&
76 endif
77
78 bwipeout!
79endfunc
80
81" A bug was discovered where the Normal mode put commands (e.g., "p") would
82" output duplicate error messages when invoked in a non-modifiable buffer.
83func Test_put_p_errmsg_nodup()
84 new
Bram Moolenaarec12d642018-06-27 23:12:36 +020085 setlocal nomodifiable
Bram Moolenaarf52f9ea2018-06-27 20:49:44 +020086
87 normal! yy
88
89 func Capture_p_error()
90 redir => s:p_err
91 normal! p
92 redir END
93 endfunc
94
95 silent! call Capture_p_error()
96
97 " Error message output within a function should be three lines (the function
98 " name, the line number, and the error message).
99 call assert_equal(3, count(s:p_err, "\n"))
100
101 delfunction Capture_p_error
102 bwipeout!
103endfunc
Bram Moolenaar0ab190c2019-05-23 23:27:36 +0200104
105func Test_put_p_indent_visual()
106 new
107 call setline(1, ['select this text', 'select that text'])
108 " yank "that" from the second line
109 normal 2Gwvey
110 " select "this" in the first line and put
111 normal k0wve[p
112 call assert_equal('select that text', getline(1))
113 call assert_equal('select that text', getline(2))
114 bwipe!
115endfunc
Bram Moolenaar1671f442020-03-10 07:48:13 +0100116
117" Test for deleting all the contents of a buffer with a put
118func Test_put_visual_delete_all_lines()
119 new
120 call setline(1, ['one', 'two', 'three'])
121 let @r = ''
122 normal! VG"rgp
123 call assert_equal(1, line('$'))
124 close!
125endfunc
126
Bram Moolenaar23003e52021-09-22 16:37:07 +0200127func Test_gp_with_count_leaves_cursor_at_end()
128 new
129 call setline(1, '<---->')
130 call setreg('@', "foo\nbar", 'c')
Bram Moolenaar56858e42021-09-22 16:43:59 +0200131 normal 1G3|3gp
132 call assert_equal([0, 4, 4, 0], getpos("."))
133 call assert_equal(['<--foo', 'barfoo', 'barfoo', 'bar-->'], getline(1, '$'))
134 call assert_equal([0, 4, 3, 0], getpos("']"))
Bram Moolenaar23003e52021-09-22 16:37:07 +0200135
136 bwipe!
137endfunc
138
Bram Moolenaarf47ebf12021-10-19 20:08:45 +0100139func Test_p_with_count_leaves_mark_at_end()
140 new
141 call setline(1, '<---->')
142 call setreg('@', "start\nend", 'c')
143 normal 1G3|3p
144 call assert_equal([0, 1, 4, 0], getpos("."))
145 call assert_equal(['<--start', 'endstart', 'endstart', 'end-->'], getline(1, '$'))
146 call assert_equal([0, 4, 3, 0], getpos("']"))
147
148 bwipe!
149endfunc
150
Bram Moolenaarcddd5ac2021-10-10 13:54:01 +0100151func Test_very_large_count()
ichizokfa537222021-11-16 12:50:46 +0000152 new
153 " total put-length (21474837 * 100) brings 32 bit int overflow
154 let @" = repeat('x', 100)
155 call assert_fails('norm 21474837p', 'E1240:')
156 bwipe!
157endfunc
158
159func Test_very_large_count_64bit()
160 if v:sizeoflong < 8
161 throw 'Skipped: only works with 64 bit long ints'
Bram Moolenaar0f004412021-11-03 13:43:16 +0000162 endif
163
Bram Moolenaareeed1c72021-10-10 12:35:17 +0100164 new
Bram Moolenaar8bc07e82021-11-24 14:59:17 +0000165 let @" = repeat('x', 100)
166 call assert_fails('norm 999999999p', 'E1240:')
Bram Moolenaareeed1c72021-10-10 12:35:17 +0100167 bwipe!
168endfunc
169
ichizokfa537222021-11-16 12:50:46 +0000170func Test_very_large_count_block()
171 new
172 " total put-length (21474837 * 100) brings 32 bit int overflow
173 call setline(1, repeat('x', 100))
174 exe "norm \<C-V>99ly"
175 call assert_fails('norm 21474837p', 'E1240:')
176 bwipe!
177endfunc
178
179func Test_very_large_count_block_64bit()
180 if v:sizeoflong < 8
181 throw 'Skipped: only works with 64 bit long ints'
182 endif
183
184 new
Bram Moolenaar8bc07e82021-11-24 14:59:17 +0000185 call setline(1, repeat('x', 100))
186 exe "norm \<C-V>$y"
187 call assert_fails('norm 999999999p', 'E1240:')
ichizokfa537222021-11-16 12:50:46 +0000188 bwipe!
189endfunc
190
Bram Moolenaar0b5b06c2021-11-04 15:10:11 +0000191func Test_put_above_first_line()
192 new
193 let @" = 'text'
194 silent! normal 0o00
195 0put
196 call assert_equal('text', getline(1))
197 bwipe!
198endfunc
199
Bram Moolenaar4d072532021-11-25 19:31:15 +0000200func Test_multibyte_op_end_mark()
201 new
202 call setline(1, 'ั‚ะตัั‚')
203 normal viwdp
204 call assert_equal([0, 1, 7, 0], getpos("'>"))
205 call assert_equal([0, 1, 7, 0], getpos("']"))
206
207 normal Vyp
naohiro ono56200ee2022-01-01 14:59:44 +0000208 call assert_equal([0, 1, v:maxcol, 0], getpos("'>"))
Bram Moolenaar4d072532021-11-25 19:31:15 +0000209 call assert_equal([0, 2, 7, 0], getpos("']"))
210 bwipe!
211endfunc
212
Bram Moolenaareeed1c72021-10-10 12:35:17 +0100213
Bram Moolenaar1671f442020-03-10 07:48:13 +0100214" vim: shiftwidth=2 sts=2 expandtab