xref: /vim-8.2.3635/src/testdir/test_put.vim (revision 8ea05de6)
1" Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc.
2
3func Test_put_block()
4  new
5  call feedkeys("i\<C-V>u2500\<CR>x\<ESC>", 'x')
6  call feedkeys("\<C-V>y", 'x')
7  call feedkeys("gg0p", 'x')
8  call assert_equal("\u2500x", getline(1))
9  bwipe!
10endfunc
11
12func Test_put_char_block()
13  new
14  call setline(1, ['Line 1', 'Line 2'])
15  f Xfile_put
16  " visually select both lines and put the cursor at the top of the visual
17  " selection and then put the buffer name over it
18  exe "norm! G0\<c-v>ke\"%p"
19  call assert_equal(['Xfile_put 1', 'Xfile_put 2'], getline(1,2))
20  bw!
21endfunc
22
23func Test_put_char_block2()
24  new
25  call setreg('a', ' one ', 'v')
26  call setline(1, ['Line 1', '', 'Line 3', ''])
27  " visually select the first 3 lines and put register a over it
28  exe "norm! ggl\<c-v>2j2l\"ap"
29  call assert_equal(['L one  1', '', 'L one  3', ''], getline(1, 4))
30  " clean up
31  bw!
32endfunc
33
34func Test_put_lines()
35  new
36  let a = [ getreg('a'), getregtype('a') ]
37  call setline(1, ['Line 1', 'Line2', 'Line 3', ''])
38  exe 'norm! gg"add"AddG""p'
39  call assert_equal(['Line 3', '', 'Line 1', 'Line2'], getline(1, '$'))
40  " clean up
41  bw!
42  eval a[0]->setreg('a', a[1])
43endfunc
44
45func Test_put_expr()
46  new
47  call setline(1, repeat(['A'], 6))
48  exec "1norm! \"=line('.')\<cr>p"
49  norm! j0.
50  norm! j0.
51  exec "4norm! \"=\<cr>P"
52  norm! j0.
53  norm! j0.
54  call assert_equal(['A1','A2','A3','4A','5A','6A'], getline(1, '$'))
55  bw!
56endfunc
57
58func Test_put_fails_when_nomodifiable()
59  new
60  setlocal nomodifiable
61
62  normal! yy
63  call assert_fails(':put', 'E21:')
64  call assert_fails(':put!', 'E21:')
65  call assert_fails(':normal! p', 'E21:')
66  call assert_fails(':normal! gp', 'E21:')
67  call assert_fails(':normal! P', 'E21:')
68  call assert_fails(':normal! gP', 'E21:')
69
70  if has('mouse')
71    set mouse=n
72    call assert_fails('execute "normal! \<MiddleMouse>"', 'E21:')
73    set mouse&
74  endif
75
76  bwipeout!
77endfunc
78
79" A bug was discovered where the Normal mode put commands (e.g., "p") would
80" output duplicate error messages when invoked in a non-modifiable buffer.
81func Test_put_p_errmsg_nodup()
82  new
83  setlocal nomodifiable
84
85  normal! yy
86
87  func Capture_p_error()
88    redir => s:p_err
89    normal! p
90    redir END
91  endfunc
92
93  silent! call Capture_p_error()
94
95  " Error message output within a function should be three lines (the function
96  " name, the line number, and the error message).
97  call assert_equal(3, count(s:p_err, "\n"))
98
99  delfunction Capture_p_error
100  bwipeout!
101endfunc
102
103func Test_put_p_indent_visual()
104  new
105  call setline(1, ['select this text', 'select that text'])
106  " yank "that" from the second line
107  normal 2Gwvey
108  " select "this" in the first line and put
109  normal k0wve[p
110  call assert_equal('select that text', getline(1))
111  call assert_equal('select that text', getline(2))
112  bwipe!
113endfunc
114
115" Test for deleting all the contents of a buffer with a put
116func Test_put_visual_delete_all_lines()
117  new
118  call setline(1, ['one', 'two', 'three'])
119  let @r = ''
120  normal! VG"rgp
121  call assert_equal(1, line('$'))
122  close!
123endfunc
124
125" vim: shiftwidth=2 sts=2 expandtab
126