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