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