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  bwipe!
84  set virtualedit=
85endfunc
86
87" Tests for pasting at the beginning, end and middle of a tab character
88" in virtual edit mode.
89func Test_paste_in_tab()
90  new
91  call append(0, '')
92  set virtualedit=all
93
94  " Tests for pasting a register with characterwise mode type
95  call setreg('"', 'xyz', 'c')
96
97  " paste (p) unnamed register at the beginning of a tab
98  call setline(1, "a\tb")
99  call cursor(1, 2, 0)
100  normal p
101  call assert_equal('a xyz      b', getline(1))
102
103  " paste (P) unnamed register at the beginning of a tab
104  call setline(1, "a\tb")
105  call cursor(1, 2, 0)
106  normal P
107  call assert_equal("axyz\tb", getline(1))
108
109  " paste (p) unnamed register at the end of a tab
110  call setline(1, "a\tb")
111  call cursor(1, 2, 6)
112  normal p
113  call assert_equal("a\txyzb", getline(1))
114
115  " paste (P) unnamed register at the end of a tab
116  call setline(1, "a\tb")
117  call cursor(1, 2, 6)
118  normal P
119  call assert_equal('a      xyz b', getline(1))
120
121  " Tests for pasting a register with blockwise mode type
122  call setreg('"', 'xyz', 'b')
123
124  " paste (p) unnamed register at the beginning of a tab
125  call setline(1, "a\tb")
126  call cursor(1, 2, 0)
127  normal p
128  call assert_equal('a xyz      b', getline(1))
129
130  " paste (P) unnamed register at the beginning of a tab
131  call setline(1, "a\tb")
132  call cursor(1, 2, 0)
133  normal P
134  call assert_equal("axyz\tb", getline(1))
135
136  " paste (p) unnamed register at the end of a tab
137  call setline(1, "a\tb")
138  call cursor(1, 2, 6)
139  normal p
140  call assert_equal("a\txyzb", getline(1))
141
142  " paste (P) unnamed register at the end of a tab
143  call setline(1, "a\tb")
144  call cursor(1, 2, 6)
145  normal P
146  call assert_equal('a      xyz b', getline(1))
147
148  " Tests for pasting with gp and gP in virtual edit mode
149
150  " paste (gp) unnamed register at the beginning of a tab
151  call setline(1, "a\tb")
152  call cursor(1, 2, 0)
153  normal gp
154  call assert_equal('a xyz      b', getline(1))
155  call assert_equal([0, 1, 12, 0, 12], getcurpos())
156
157  " paste (gP) unnamed register at the beginning of a tab
158  call setline(1, "a\tb")
159  call cursor(1, 2, 0)
160  normal gP
161  call assert_equal("axyz\tb", getline(1))
162  call assert_equal([0, 1, 5, 0, 5], getcurpos())
163
164  " paste (gp) unnamed register at the end of a tab
165  call setline(1, "a\tb")
166  call cursor(1, 2, 6)
167  normal gp
168  call assert_equal("a\txyzb", getline(1))
169  call assert_equal([0, 1, 6, 0, 12], getcurpos())
170
171  " paste (gP) unnamed register at the end of a tab
172  call setline(1, "a\tb")
173  call cursor(1, 2, 6)
174  normal gP
175  call assert_equal('a      xyz b', getline(1))
176  call assert_equal([0, 1, 12, 0, 12], getcurpos())
177
178  " Tests for pasting a named register
179  let @r = 'xyz'
180
181  " paste (gp) named register in the middle of a tab
182  call setline(1, "a\tb")
183  call cursor(1, 2, 2)
184  normal "rgp
185  call assert_equal('a   xyz    b', getline(1))
186  call assert_equal([0, 1, 8, 0, 8], getcurpos())
187
188  " paste (gP) named register in the middle of a tab
189  call setline(1, "a\tb")
190  call cursor(1, 2, 2)
191  normal "rgP
192  call assert_equal('a  xyz     b', getline(1))
193  call assert_equal([0, 1, 7, 0, 7], getcurpos())
194
195  bwipe!
196  set virtualedit=
197endfunc
198
199" Test for yanking a few spaces within a tab to a register
200func Test_yank_in_tab()
201  new
202  let @r = ''
203  call setline(1, "a\tb")
204  set virtualedit=all
205  call cursor(1, 2, 2)
206  normal "ry5l
207  call assert_equal('     ', @r)
208
209  bwipe!
210  set virtualedit=
211endfunc
212
213" Insert "keyword keyw", ESC, C CTRL-N, shows "keyword ykeyword".
214" Repeating CTRL-N fixes it. (Mary Ellen Foster)
215func Test_ve_completion()
216  new
217  set completeopt&vim
218  set virtualedit=all
219  exe "normal ikeyword keyw\<Esc>C\<C-N>"
220  call assert_equal('keyword keyword', getline(1))
221  bwipe!
222  set virtualedit=
223endfunc
224
225" Using "C" then then <CR> moves the last remaining character to the next
226" line.  (Mary Ellen Foster)
227func Test_ve_del_to_eol()
228  new
229  set virtualedit=all
230  call append(0, 'all your base are belong to us')
231  call search('are', 'w')
232  exe "normal C\<CR>are belong to vim"
233  call assert_equal(['all your base ', 'are belong to vim'], getline(1, 2))
234  bwipe!
235  set virtualedit=
236endfunc
237
238" When past the end of a line that ends in a single character "b" skips
239" that word.
240func Test_ve_b_past_eol()
241  new
242  set virtualedit=all
243  call append(0, '1 2 3 4 5 6')
244  normal gg^$15lbC7
245  call assert_equal('1 2 3 4 5 7', getline(1))
246  bwipe!
247  set virtualedit=
248endfunc
249
250" Make sure 'i', 'C', 'a', 'A' and 'D' works
251func Test_ve_ins_del()
252  new
253  set virtualedit=all
254  call append(0, ["'i'", "'C'", "'a'", "'A'", "'D'"])
255  call cursor(1, 1)
256  normal $4lix
257  call assert_equal("'i'   x", getline(1))
258  call cursor(2, 1)
259  normal $4lCx
260  call assert_equal("'C'   x", getline(2))
261  call cursor(3, 1)
262  normal $4lax
263  call assert_equal("'a'    x", getline(3))
264  call cursor(4, 1)
265  normal $4lAx
266  call assert_equal("'A'x", getline(4))
267  call cursor(5, 1)
268  normal $4lDix
269  call assert_equal("'D'   x", getline(5))
270  bwipe!
271  set virtualedit=
272endfunc
273
274" Test for yank bug reported by Mark Waggoner.
275func Test_yank_block()
276  new
277  set virtualedit=block
278  call append(0, repeat(['this is a test'], 3))
279  exe "normal gg^2w\<C-V>3jy"
280  call assert_equal("a\na\na\n ", @")
281  bwipe!
282  set virtualedit=
283endfunc
284
285" Test "r" beyond the end of the line
286func Test_replace_after_eol()
287  new
288  set virtualedit=all
289  call append(0, '"r"')
290  normal gg$5lrxa
291  call assert_equal('"r"    x', getline(1))
292  bwipe!
293  set virtualedit=
294endfunc
295
296" Test "r" on a tab
297" Note that for this test, 'ts' must be 8 (the default).
298func Test_replace_on_tab()
299  new
300  set virtualedit=all
301  call append(0, "'r'\t")
302  normal gg^5lrxAy
303  call assert_equal("'r'  x  y", getline(1))
304  call setline(1, 'aaaaaaaaaaaa')
305  exe "normal! gg2lgR\<Tab>"
306  call assert_equal("aa\taaaa", getline(1))
307  bwipe!
308  set virtualedit=
309endfunc
310
311" Test to make sure 'x' can delete control characters
312func Test_ve_del_ctrl_chars()
313  new
314  set virtualedit=all
315  call append(0, "a\<C-V>b\<CR>sd")
316  set display=uhex
317  normal gg^xxxxxxi[text]
318  set display=
319  call assert_equal('[text]', getline(1))
320  bwipe!
321  set virtualedit=
322endfunc
323
324" Test for ^Y/^E due to bad w_virtcol value, reported by
325" Roy <[email protected]>.
326func Test_ins_copy_char()
327  new
328  set virtualedit=all
329  call append(0, 'abcv8efi.him2kl')
330  exe "normal gg^O\<Esc>3li\<C-E>\<Esc>4li\<C-E>\<Esc>4li\<C-E>   <--"
331  exe "normal j^o\<Esc>4li\<C-Y>\<Esc>4li\<C-Y>\<Esc>4li\<C-Y>   <--"
332  call assert_equal('   v   i   m   <--', getline(1))
333  call assert_equal('    8   .   2   <--', getline(3))
334  bwipe!
335  set virtualedit=
336endfunc
337
338" Test for yanking and pasting using the small delete register
339func Test_yank_paste_small_del_reg()
340  new
341  set virtualedit=all
342  call append(0, "foo, bar")
343  normal ggdewve"-p
344  call assert_equal(', foo', getline(1))
345  bwipe!
346  set virtualedit=
347endfunc
348
349" Test for delete that breaks a tab into spaces
350func Test_delete_break_tab()
351  new
352  call setline(1, "one\ttwo")
353  set virtualedit=all
354  normal v3ld
355  call assert_equal('    two', getline(1))
356  set virtualedit&
357  close!
358endfunc
359
360" Test for using <BS>, <C-W> and <C-U> in virtual edit mode
361" to erase character, word and line.
362func Test_ve_backspace()
363  new
364  call setline(1, 'sample')
365  set virtualedit=all
366  set backspace=indent,eol,start
367  exe "normal 15|i\<BS>\<BS>"
368  call assert_equal([0, 1, 7, 5], getpos('.'))
369  exe "normal 15|i\<C-W>"
370  call assert_equal([0, 1, 6, 0], getpos('.'))
371  exe "normal 15|i\<C-U>"
372  call assert_equal([0, 1, 1, 0], getpos('.'))
373  set backspace&
374  set virtualedit&
375  close!
376endfunc
377
378" vim: shiftwidth=2 sts=2 expandtab
379