1" Tests for 'virtualedit'.
2
3func Test_yank_move_change()
4  new
5  call setline(1, [
6	\ "func foo() error {",
7	\ "\tif n, err := bar();",
8	\ "\terr != nil {",
9	\ "\t\treturn err",
10	\ "\t}",
11	\ "\tn = n * n",
12	\ ])
13  set virtualedit=all
14  set ts=4
15  function! MoveSelectionDown(count) abort
16    normal! m`
17    silent! exe "'<,'>move'>+".a:count
18    norm! ``
19  endfunction
20
21  xmap ]e :<C-U>call MoveSelectionDown(v:count1)<CR>
22  2
23  normal 2gg
24  normal J
25  normal jVj
26  normal ]e
27  normal ce
28  bwipe!
29  set virtualedit=
30  set ts=8
31endfunc
32
33func Test_paste_end_of_line()
34  new
35  set virtualedit=all
36  call setline(1, ['456', '123'])
37  normal! gg0"ay$
38  exe "normal! 2G$lllA\<C-O>:normal! \"agP\r"
39  call assert_equal('123456', getline(2))
40
41  bwipe!
42  set virtualedit=
43endfunc
44
45func Test_replace_end_of_line()
46  new
47  set virtualedit=all
48  call setline(1, range(20))
49  exe "normal! gg2jv10lr-"
50  call assert_equal(["1", "-----------", "3"], getline(2,4))
51  call setline(1, range(20))
52  exe "normal! gg2jv10lr\<c-k>hh"
53  call assert_equal(["1", "───────────", "3"], getline(2,4))
54
55  bwipe!
56  set virtualedit=
57endfunc
58
59func Test_edit_CTRL_G()
60  new
61  set virtualedit=insert
62  call setline(1, ['123', '1', '12'])
63  exe "normal! ggA\<c-g>jx\<c-g>jx"
64  call assert_equal(['123', '1  x', '12 x'], getline(1,'$'))
65
66  set virtualedit=all
67  %d_
68  call setline(1, ['1', '12'])
69  exe "normal! ggllix\<c-g>jx"
70  call assert_equal(['1 x', '12x'], getline(1,'$'))
71
72
73  bwipe!
74  set virtualedit=
75endfunc
76
77func Test_edit_change()
78  new
79  set virtualedit=all
80  call setline(1, "\t⒌")
81  normal Cx
82  call assert_equal('x', getline(1))
83  " Do a visual block change
84  call setline(1, ['a', 'b', 'c'])
85  exe "normal gg3l\<C-V>2jcx"
86  call assert_equal(['a  x', 'b  x', 'c  x'], getline(1, '$'))
87  bwipe!
88  set virtualedit=
89endfunc
90
91" Tests for pasting at the beginning, end and middle of a tab character
92" in virtual edit mode.
93func Test_paste_in_tab()
94  new
95  call append(0, '')
96  set virtualedit=all
97
98  " Tests for pasting a register with characterwise mode type
99  call setreg('"', 'xyz', 'c')
100
101  " paste (p) unnamed register at the beginning of a tab
102  call setline(1, "a\tb")
103  call cursor(1, 2, 0)
104  normal p
105  call assert_equal('a xyz      b', getline(1))
106
107  " paste (P) unnamed register at the beginning of a tab
108  call setline(1, "a\tb")
109  call cursor(1, 2, 0)
110  normal P
111  call assert_equal("axyz\tb", getline(1))
112
113  " paste (p) unnamed register at the end of a tab
114  call setline(1, "a\tb")
115  call cursor(1, 2, 6)
116  normal p
117  call assert_equal("a\txyzb", getline(1))
118
119  " paste (P) unnamed register at the end of a tab
120  call setline(1, "a\tb")
121  call cursor(1, 2, 6)
122  normal P
123  call assert_equal('a      xyz b', getline(1))
124
125  " Tests for pasting a register with blockwise mode type
126  call setreg('"', 'xyz', 'b')
127
128  " paste (p) unnamed register at the beginning of a tab
129  call setline(1, "a\tb")
130  call cursor(1, 2, 0)
131  normal p
132  call assert_equal('a xyz      b', getline(1))
133
134  " paste (P) unnamed register at the beginning of a tab
135  call setline(1, "a\tb")
136  call cursor(1, 2, 0)
137  normal P
138  call assert_equal("axyz\tb", getline(1))
139
140  " paste (p) unnamed register at the end of a tab
141  call setline(1, "a\tb")
142  call cursor(1, 2, 6)
143  normal p
144  call assert_equal("a\txyzb", getline(1))
145
146  " paste (P) unnamed register at the end of a tab
147  call setline(1, "a\tb")
148  call cursor(1, 2, 6)
149  normal P
150  call assert_equal('a      xyz b', getline(1))
151
152  " Tests for pasting with gp and gP in virtual edit mode
153
154  " paste (gp) unnamed register at the beginning of a tab
155  call setline(1, "a\tb")
156  call cursor(1, 2, 0)
157  normal gp
158  call assert_equal('a xyz      b', getline(1))
159  call assert_equal([0, 1, 12, 0, 12], getcurpos())
160
161  " paste (gP) unnamed register at the beginning of a tab
162  call setline(1, "a\tb")
163  call cursor(1, 2, 0)
164  normal gP
165  call assert_equal("axyz\tb", getline(1))
166  call assert_equal([0, 1, 5, 0, 5], getcurpos())
167
168  " paste (gp) unnamed register at the end of a tab
169  call setline(1, "a\tb")
170  call cursor(1, 2, 6)
171  normal gp
172  call assert_equal("a\txyzb", getline(1))
173  call assert_equal([0, 1, 6, 0, 12], getcurpos())
174
175  " paste (gP) unnamed register at the end of a tab
176  call setline(1, "a\tb")
177  call cursor(1, 2, 6)
178  normal gP
179  call assert_equal('a      xyz b', getline(1))
180  call assert_equal([0, 1, 12, 0, 12], getcurpos())
181
182  " Tests for pasting a named register
183  let @r = 'xyz'
184
185  " paste (gp) named register in the middle of a tab
186  call setline(1, "a\tb")
187  call cursor(1, 2, 2)
188  normal "rgp
189  call assert_equal('a   xyz    b', getline(1))
190  call assert_equal([0, 1, 8, 0, 8], getcurpos())
191
192  " paste (gP) named register in the middle of a tab
193  call setline(1, "a\tb")
194  call cursor(1, 2, 2)
195  normal "rgP
196  call assert_equal('a  xyz     b', getline(1))
197  call assert_equal([0, 1, 7, 0, 7], getcurpos())
198
199  bwipe!
200  set virtualedit=
201endfunc
202
203" Test for yanking a few spaces within a tab to a register
204func Test_yank_in_tab()
205  new
206  let @r = ''
207  call setline(1, "a\tb")
208  set virtualedit=all
209  call cursor(1, 2, 2)
210  normal "ry5l
211  call assert_equal('     ', @r)
212
213  bwipe!
214  set virtualedit=
215endfunc
216
217" Insert "keyword keyw", ESC, C CTRL-N, shows "keyword ykeyword".
218" Repeating CTRL-N fixes it. (Mary Ellen Foster)
219func Test_ve_completion()
220  new
221  set completeopt&vim
222  set virtualedit=all
223  exe "normal ikeyword keyw\<Esc>C\<C-N>"
224  call assert_equal('keyword keyword', getline(1))
225  bwipe!
226  set virtualedit=
227endfunc
228
229" Using "C" then then <CR> moves the last remaining character to the next
230" line.  (Mary Ellen Foster)
231func Test_ve_del_to_eol()
232  new
233  set virtualedit=all
234  call append(0, 'all your base are belong to us')
235  call search('are', 'w')
236  exe "normal C\<CR>are belong to vim"
237  call assert_equal(['all your base ', 'are belong to vim'], getline(1, 2))
238  bwipe!
239  set virtualedit=
240endfunc
241
242" When past the end of a line that ends in a single character "b" skips
243" that word.
244func Test_ve_b_past_eol()
245  new
246  set virtualedit=all
247  call append(0, '1 2 3 4 5 6')
248  normal gg^$15lbC7
249  call assert_equal('1 2 3 4 5 7', getline(1))
250  bwipe!
251  set virtualedit=
252endfunc
253
254" Make sure 'i', 'C', 'a', 'A' and 'D' works
255func Test_ve_ins_del()
256  new
257  set virtualedit=all
258  call append(0, ["'i'", "'C'", "'a'", "'A'", "'D'"])
259  call cursor(1, 1)
260  normal $4lix
261  call assert_equal("'i'   x", getline(1))
262  call cursor(2, 1)
263  normal $4lCx
264  call assert_equal("'C'   x", getline(2))
265  call cursor(3, 1)
266  normal $4lax
267  call assert_equal("'a'    x", getline(3))
268  call cursor(4, 1)
269  normal $4lAx
270  call assert_equal("'A'x", getline(4))
271  call cursor(5, 1)
272  normal $4lDix
273  call assert_equal("'D'   x", getline(5))
274  bwipe!
275  set virtualedit=
276endfunc
277
278" Test for yank bug reported by Mark Waggoner.
279func Test_yank_block()
280  new
281  set virtualedit=block
282  call append(0, repeat(['this is a test'], 3))
283  exe "normal gg^2w\<C-V>3jy"
284  call assert_equal("a\na\na\n ", @")
285  bwipe!
286  set virtualedit=
287endfunc
288
289" Test "r" beyond the end of the line
290func Test_replace_after_eol()
291  new
292  set virtualedit=all
293  call append(0, '"r"')
294  normal gg$5lrxa
295  call assert_equal('"r"    x', getline(1))
296  " visual block replace
297  %d _
298  call setline(1, ['a', '', 'b'])
299  exe "normal 2l\<C-V>2jrx"
300  call assert_equal(['a x', '  x', 'b x'], getline(1, '$'))
301  " visual characterwise selection replace after eol
302  %d _
303  call setline(1, 'a')
304  normal 4lv2lrx
305  call assert_equal('a   xxx', getline(1))
306  bwipe!
307  set virtualedit=
308endfunc
309
310" Test "r" on a tab
311" Note that for this test, 'ts' must be 8 (the default).
312func Test_replace_on_tab()
313  new
314  set virtualedit=all
315  call append(0, "'r'\t")
316  normal gg^5lrxAy
317  call assert_equal("'r'  x  y", getline(1))
318  call setline(1, 'aaaaaaaaaaaa')
319  exe "normal! gg2lgR\<Tab>"
320  call assert_equal("aa\taaaa", getline(1))
321  bwipe!
322  set virtualedit=
323endfunc
324
325" Test to make sure 'x' can delete control characters
326func Test_ve_del_ctrl_chars()
327  new
328  set virtualedit=all
329  call append(0, "a\<C-V>b\<CR>sd")
330  set display=uhex
331  normal gg^xxxxxxi[text]
332  set display=
333  call assert_equal('[text]', getline(1))
334  bwipe!
335  set virtualedit=
336endfunc
337
338" Test for ^Y/^E due to bad w_virtcol value, reported by
339" Roy <[email protected]>.
340func Test_ins_copy_char()
341  new
342  set virtualedit=all
343  call append(0, 'abcv8efi.him2kl')
344  exe "normal gg^O\<Esc>3li\<C-E>\<Esc>4li\<C-E>\<Esc>4li\<C-E>   <--"
345  exe "normal j^o\<Esc>4li\<C-Y>\<Esc>4li\<C-Y>\<Esc>4li\<C-Y>   <--"
346  call assert_equal('   v   i   m   <--', getline(1))
347  call assert_equal('    8   .   2   <--', getline(3))
348  bwipe!
349  set virtualedit=
350endfunc
351
352" Test for yanking and pasting using the small delete register
353func Test_yank_paste_small_del_reg()
354  new
355  set virtualedit=all
356  call append(0, "foo, bar")
357  normal ggdewve"-p
358  call assert_equal(', foo', getline(1))
359  bwipe!
360  set virtualedit=
361endfunc
362
363" Test for delete that breaks a tab into spaces
364func Test_delete_break_tab()
365  new
366  call setline(1, "one\ttwo")
367  set virtualedit=all
368  normal v3ld
369  call assert_equal('    two', getline(1))
370  set virtualedit&
371  close!
372endfunc
373
374" Test for using <BS>, <C-W> and <C-U> in virtual edit mode
375" to erase character, word and line.
376func Test_ve_backspace()
377  new
378  call setline(1, 'sample')
379  set virtualedit=all
380  set backspace=indent,eol,start
381  exe "normal 15|i\<BS>\<BS>"
382  call assert_equal([0, 1, 7, 5], getpos('.'))
383  exe "normal 15|i\<C-W>"
384  call assert_equal([0, 1, 6, 0], getpos('.'))
385  exe "normal 15|i\<C-U>"
386  call assert_equal([0, 1, 1, 0], getpos('.'))
387  set backspace&
388  set virtualedit&
389  close!
390endfunc
391
392" Test for delete (x) on EOL character and after EOL
393func Test_delete_past_eol()
394  new
395  call setline(1, "ab")
396  set virtualedit=all
397  exe "normal 2lx"
398  call assert_equal('ab', getline(1))
399  exe "normal 10lx"
400  call assert_equal('ab', getline(1))
401  set virtualedit&
402  bw!
403endfunc
404
405" vim: shiftwidth=2 sts=2 expandtab
406