xref: /vim-8.2.3635/src/testdir/test_paste.vim (revision dabfde04)
1" Tests for bracketed paste and other forms of pasting.
2
3" Bracketed paste only works with "xterm".  Not in GUI or Windows console.
4if has('gui_running') || has('win32')
5  finish
6endif
7set term=xterm
8
9source shared.vim
10
11func Test_paste_normal_mode()
12  new
13  " In first column text is inserted
14  call setline(1, ['a', 'b', 'c'])
15  call cursor(2, 1)
16  call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
17  call assert_equal('foo', getline(2))
18  call assert_equal('barb', getline(3))
19  call assert_equal('c', getline(4))
20
21  " When repeating text is appended
22  normal .
23  call assert_equal('barfoo', getline(3))
24  call assert_equal('barb', getline(4))
25  call assert_equal('c', getline(5))
26  bwipe!
27
28  " In second column text is appended
29  call setline(1, ['a', 'bbb', 'c'])
30  call cursor(2, 2)
31  call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
32  call assert_equal('bbfoo', getline(2))
33  call assert_equal('barb', getline(3))
34  call assert_equal('c', getline(4))
35
36  " In last column text is appended
37  call setline(1, ['a', 'bbb', 'c'])
38  call cursor(2, 3)
39  call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
40  call assert_equal('bbbfoo', getline(2))
41  call assert_equal('bar', getline(3))
42  call assert_equal('c', getline(4))
43endfunc
44
45func Test_paste_insert_mode()
46  new
47  call setline(1, ['a', 'b', 'c'])
48  2
49  call feedkeys("i\<Esc>[200~foo\<CR>bar\<Esc>[201~ done\<Esc>", 'xt')
50  call assert_equal('foo', getline(2))
51  call assert_equal('bar doneb', getline(3))
52  call assert_equal('c', getline(4))
53
54  normal .
55  call assert_equal('bar donfoo', getline(3))
56  call assert_equal('bar doneeb', getline(4))
57  call assert_equal('c', getline(5))
58
59  set ai et tw=10
60  call setline(1, ['a', '    b', 'c'])
61  2
62  call feedkeys("A\<Esc>[200~foo\<CR> bar bar bar\<Esc>[201~\<Esc>", 'xt')
63  call assert_equal('    bfoo', getline(2))
64  call assert_equal(' bar bar bar', getline(3))
65  call assert_equal('c', getline(4))
66
67  set ai& et& tw=0
68  bwipe!
69endfunc
70
71func Test_paste_clipboard()
72  if !WorkingClipboard()
73    return
74  endif
75  let @+ = "nasty\<Esc>:!ls\<CR>command"
76  new
77  exe "normal i\<C-R>+\<Esc>"
78  call assert_equal("nasty\<Esc>:!ls\<CR>command", getline(1))
79  bwipe!
80endfunc
81
82func Test_paste_cmdline()
83  call feedkeys(":a\<Esc>[200~foo\<CR>bar\<Esc>[201~b\<Home>\"\<CR>", 'xt')
84  call assert_equal("\"afoo\<CR>barb", getreg(':'))
85endfunc
86
87func Test_paste_visual_mode()
88  new
89  call setline(1, 'here are some words')
90  call feedkeys("0fsve\<Esc>[200~more\<Esc>[201~", 'xt')
91  call assert_equal('here are more words', getline(1))
92  call assert_equal('some', getreg('-'))
93
94  " include last char in the line
95  call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
96  call assert_equal('here are more noises', getline(1))
97  call assert_equal('words', getreg('-'))
98
99  " exclude last char in the line
100  call setline(1, 'some words!')
101  call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
102  call assert_equal('some noises!', getline(1))
103  call assert_equal('words', getreg('-'))
104
105  " multi-line selection
106  call setline(1, ['some words', 'and more'])
107  call feedkeys("0fwvj0fd\<Esc>[200~letters\<Esc>[201~", 'xt')
108  call assert_equal('some letters more', getline(1))
109  call assert_equal("words\nand", getreg('1'))
110
111  bwipe!
112endfunc
113
114func CheckCopyPaste()
115  call setline(1, ['copy this', ''])
116  normal 1G0"*y$
117  normal j"*p
118  call assert_equal('copy this', getline(2))
119endfunc
120
121func Test_xrestore()
122  if !has('xterm_clipboard')
123    return
124  endif
125call ch_logfile('logfile', 'w')
126  let display = $DISPLAY
127  new
128  call CheckCopyPaste()
129
130  xrestore
131  call CheckCopyPaste()
132
133  exe "xrestore " .. display
134  call CheckCopyPaste()
135
136call ch_logfile('', '')
137  bwipe!
138endfunc
139