neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_put.vim (10448B)


      1 " Tests for put commands, e.g. ":put", "p", "gp", "P", "gP", etc.
      2 
      3 source check.vim
      4 source screendump.vim
      5 
      6 func Test_put_block()
      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!
     13 endfunc
     14 
     15 func Test_put_block_unicode()
     16  new
     17  call setreg('a', "À\nÀÀ\naaaaaaaaaaaa", "\<C-V>")
     18  call setline(1, [' 1', ' 2', ' 3'])
     19  exe "norm! \<C-V>jj\"ap"
     20  let expected = ['À           1', 'ÀÀ          2', 'aaaaaaaaaaaa3']
     21  call assert_equal(expected, getline(1, 3))
     22  bw!
     23 endfunc
     24 
     25 func Test_put_char_block()
     26  new
     27  call setline(1, ['Line 1', 'Line 2'])
     28  f Xfile_put
     29  " visually select both lines and put the cursor at the top of the visual
     30  " selection and then put the buffer name over it
     31  exe "norm! G0\<c-v>ke\"%p"
     32  call assert_equal(['Xfile_put 1', 'Xfile_put 2'], getline(1,2))
     33  bw!
     34 endfunc
     35 
     36 func Test_put_char_block2()
     37  new
     38  call setreg('a', ' one ', 'v')
     39  call setline(1, ['Line 1', '', 'Line 3', ''])
     40  " visually select the first 3 lines and put register a over it
     41  exe "norm! ggl\<c-v>2j2l\"ap"
     42  call assert_equal(['L one  1', '', 'L one  3', ''], getline(1, 4))
     43  " clean up
     44  bw!
     45 endfunc
     46 
     47 func Test_put_lines()
     48  new
     49  let a = [ getreg('a'), getregtype('a') ]
     50  call setline(1, ['Line 1', 'Line2', 'Line 3', ''])
     51  exe 'norm! gg"add"AddG""p'
     52  call assert_equal(['Line 3', '', 'Line 1', 'Line2'], getline(1, '$'))
     53  " clean up
     54  bw!
     55  eval a[0]->setreg('a', a[1])
     56 endfunc
     57 
     58 func Test_put_expr()
     59  new
     60  call setline(1, repeat(['A'], 6))
     61  exec "1norm! \"=line('.')\<cr>p"
     62  norm! j0.
     63  norm! j0.
     64  exec "4norm! \"=\<cr>P"
     65  norm! j0.
     66  norm! j0.
     67  call assert_equal(['A1','A2','A3','4A','5A','6A'], getline(1, '$'))
     68  bw!
     69 endfunc
     70 
     71 func Test_put_fails_when_nomodifiable()
     72  new
     73  setlocal nomodifiable
     74 
     75  normal! yy
     76  call assert_fails(':put', 'E21:')
     77  call assert_fails(':put!', 'E21:')
     78  call assert_fails(':iput', 'E21:')
     79  call assert_fails(':iput!', 'E21:')
     80  call assert_fails(':normal! p', 'E21:')
     81  call assert_fails(':normal! gp', 'E21:')
     82  call assert_fails(':normal! P', 'E21:')
     83  call assert_fails(':normal! gP', 'E21:')
     84 
     85  if has('mouse')
     86    set mouse=n
     87    call assert_fails('execute "normal! \<MiddleMouse>"', 'E21:')
     88    set mouse&
     89  endif
     90 
     91  bwipeout!
     92 endfunc
     93 
     94 " A bug was discovered where the Normal mode put commands (e.g., "p") would
     95 " output duplicate error messages when invoked in a non-modifiable buffer.
     96 func Test_put_p_errmsg_nodup()
     97  new
     98  setlocal nomodifiable
     99 
    100  normal! yy
    101 
    102  func Capture_p_error()
    103    redir => s:p_err
    104    normal! p
    105    redir END
    106  endfunc
    107 
    108  silent! call Capture_p_error()
    109 
    110  " Error message output within a function should be three lines (the function
    111  " name, the line number, and the error message).
    112  call assert_equal(3, count(s:p_err, "\n"))
    113 
    114  delfunction Capture_p_error
    115  bwipeout!
    116 endfunc
    117 
    118 func Test_put_p_indent_visual()
    119  new
    120  call setline(1, ['select this text', 'select that text'])
    121  " yank "that" from the second line
    122  normal 2Gwvey
    123  " select "this" in the first line and put
    124  normal k0wve[p
    125  call assert_equal('select that text', getline(1))
    126  call assert_equal('select that text', getline(2))
    127  bwipe!
    128 endfunc
    129 
    130 " Test for deleting all the contents of a buffer with a put
    131 func Test_put_visual_delete_all_lines()
    132  new
    133  call setline(1, ['one', 'two', 'three'])
    134  let @r = ''
    135  normal! VG"rgp
    136  call assert_equal(1, line('$'))
    137  bw!
    138 endfunc
    139 
    140 func Test_gp_with_count_leaves_cursor_at_end()
    141  new
    142  call setline(1, '<---->')
    143  call setreg('@', "foo\nbar", 'c')
    144  normal 1G3|3gp
    145  call assert_equal([0, 4, 4, 0], getpos("."))
    146  call assert_equal(['<--foo', 'barfoo', 'barfoo', 'bar-->'], getline(1, '$'))
    147  call assert_equal([0, 4, 3, 0], getpos("']"))
    148 
    149  bwipe!
    150 endfunc
    151 
    152 func Test_p_with_count_leaves_mark_at_end()
    153  new
    154  call setline(1, '<---->')
    155  call setreg('@', "start\nend", 'c')
    156  normal 1G3|3p
    157  call assert_equal([0, 1, 4, 0], getpos("."))
    158  call assert_equal(['<--start', 'endstart', 'endstart', 'end-->'], getline(1, '$'))
    159  call assert_equal([0, 4, 3, 0], getpos("']"))
    160 
    161  bwipe!
    162 endfunc
    163 
    164 func Test_very_large_count()
    165  new
    166  " total put-length (21474837 * 100) brings 32 bit int overflow
    167  let @" = repeat('x', 100)
    168  call assert_fails('norm 21474837p', 'E1240:')
    169  bwipe!
    170 endfunc
    171 
    172 func Test_very_large_count_64bit()
    173  new
    174  let @" = repeat('x', 100)
    175  call assert_fails('norm 999999999p', 'E1240:')
    176  bwipe!
    177 endfunc
    178 
    179 func Test_very_large_count_block()
    180  new
    181  " total put-length (21474837 * 100) brings 32 bit int overflow
    182  call setline(1, repeat('x', 100))
    183  exe "norm \<C-V>99ly"
    184  call assert_fails('norm 21474837p', 'E1240:')
    185  bwipe!
    186 endfunc
    187 
    188 func Test_very_large_count_block_64bit()
    189  new
    190  call setline(1, repeat('x', 100))
    191  exe "norm \<C-V>$y"
    192  call assert_fails('norm 999999999p', 'E1240:')
    193  bwipe!
    194 endfunc
    195 
    196 func Test_put_above_first_line()
    197  new
    198  let @" = 'text'
    199  silent! normal 0o00
    200  0put
    201  call assert_equal('text', getline(1))
    202  bwipe!
    203 endfunc
    204 
    205 func Test_multibyte_op_end_mark()
    206  new
    207  call setline(1, 'тест')
    208  normal viwdp
    209  call assert_equal([0, 1, 7, 0], getpos("'>"))
    210  call assert_equal([0, 1, 7, 0], getpos("']"))
    211 
    212  normal Vyp
    213  call assert_equal([0, 1, v:maxcol, 0], getpos("'>"))
    214  call assert_equal([0, 2, 7, 0], getpos("']"))
    215  bwipe!
    216 endfunc
    217 
    218 " this was putting a mark before the start of a line
    219 func Test_put_empty_register()
    220  new
    221  norm yy
    222  norm [Pi00ggv)s0
    223  sil! norm [P
    224  bwipe!
    225 endfunc
    226 
    227 " this was putting the end mark after the end of the line
    228 func Test_put_visual_mode()
    229  edit! SomeNewBuffer
    230  set selection=exclusive
    231  exe "norm o\t"
    232  m0
    233  sil! norm pp
    234 
    235  bwipe!
    236  set selection&
    237 endfunc
    238 
    239 func Test_put_visual_block_mode()
    240  enew
    241  exe "norm 0R\<CR>\<C-C>V"
    242  sil exe "norm \<C-V>c	\<MiddleDrag>"
    243  set ve=all
    244  sil norm vz=p
    245 
    246  bwipe!
    247  set ve=
    248 endfunc
    249 
    250 func Test_put_other_window()
    251  CheckScreendump
    252  CheckRunVimInTerminal
    253 
    254  let lines =<< trim END
    255      40vsplit
    256      0put ='some text at the top'
    257      put ='  one more text'
    258      put ='  two more text'
    259      put ='  three more text'
    260      put ='  four more text'
    261  END
    262  call writefile(lines, 'Xtest_put_other', 'D')
    263  let buf = RunVimInTerminal('-S Xtest_put_other', #{rows: 10})
    264 
    265  call VerifyScreenDump(buf, 'Test_put_other_window_1', {})
    266 
    267  call StopVimInTerminal(buf)
    268 endfunc
    269 
    270 func Test_put_in_last_displayed_line()
    271  CheckScreendump
    272  CheckRunVimInTerminal
    273 
    274  let lines =<< trim END
    275      vim9script
    276      autocmd CursorMoved * eval line('w$')
    277      @a = 'x'->repeat(&columns * 2 - 2)
    278      range(&lines)->setline(1)
    279      feedkeys('G"ap')
    280  END
    281  call writefile(lines, 'Xtest_put_last_line', 'D')
    282  let buf = RunVimInTerminal('-S Xtest_put_last_line', #{rows: 10})
    283 
    284  call VerifyScreenDump(buf, 'Test_put_in_last_displayed_line_1', {})
    285 
    286  call StopVimInTerminal(buf)
    287 endfunc
    288 
    289 func Test_put_visual_replace_whole_fold()
    290  new
    291  let lines = repeat(['{{{1', 'foo', 'bar', ''], 2)
    292  call setline(1, lines)
    293  setlocal foldmethod=marker
    294  call setreg('"', 'baz')
    295  call setreg('1', '')
    296  normal! Vp
    297  call assert_equal("{{{1\nfoo\nbar\n\n", getreg('1'))
    298  call assert_equal(['baz', '{{{1', 'foo', 'bar', ''], getline(1, '$'))
    299 
    300  bwipe!
    301 endfunc
    302 
    303 func Test_put_visual_replace_fold_marker()
    304  new
    305  let lines = repeat(['{{{1', 'foo', 'bar', ''], 4)
    306  call setline(1, lines)
    307  setlocal foldmethod=marker
    308  normal! Gkzo
    309  call setreg('"', '{{{1')
    310  call setreg('1', '')
    311  normal! Vp
    312  call assert_equal("{{{1\n", getreg('1'))
    313  call assert_equal(lines, getline(1, '$'))
    314 
    315  bwipe!
    316 endfunc
    317 
    318 func Test_put_dict()
    319  new
    320  let d = #{a: #{b: 'abc'}, c: [1, 2], d: 0z10}
    321  put! =d
    322  call assert_equal(["{'a': {'b': 'abc'}, 'c': [1, 2], 'd': 0z10}", ''],
    323        \ getline(1, '$'))
    324  bw!
    325 endfunc
    326 
    327 func Test_put_list()
    328  new
    329  let l = ['a', 'b', 'c']
    330  put! =l
    331  call assert_equal(['a', 'b', 'c', ''], getline(1, '$'))
    332  bw!
    333 endfunc
    334 
    335 func Test_iput_multiline()
    336  new
    337  setlocal noexpandtab
    338  call setline(1, "\<Tab>foo")
    339  call setreg('0', "bar\n\<Tab>bar2\nbar3", 'l')
    340  iput
    341  call assert_equal(["\<Tab>bar", "\<Tab>\<Tab>bar2", "\<Tab>bar3"], getline(2, 4))
    342  setlocal expandtab tabstop=8 shiftwidth=8 noshiftround
    343  iput
    344  call assert_equal([repeat(' ', 8) . "bar",
    345        \ repeat(' ', 16) . "bar2",
    346        \ repeat(' ', 8) . "bar3"], getline(5, 7))
    347  bw!
    348 endfunc
    349 
    350 func Test_iput_beforeafter_tab()
    351  new
    352  setlocal noexpandtab
    353  call setline(1, "\<Tab>foo")
    354  call setreg('0', "bar", 'l')
    355  iput
    356  call assert_equal(["\<Tab>bar"], getline(2, '$'))
    357  call feedkeys("k", 'x')
    358  iput!
    359  call assert_equal("\<Tab>bar", getline(1))
    360  call assert_equal("\<Tab>bar", getline(3))
    361  bw!
    362 endfunc
    363 
    364 func Test_iput_beforeafter_expandtab()
    365  new
    366  setlocal expandtab tabstop=8 shiftwidth=8 noshiftround
    367  call setline(1, "\<Tab>foo")
    368  call setreg('0', "bar", 'l')
    369  iput
    370  call assert_equal([repeat(' ', 8) . "bar"], getline(2, '$'))
    371  :1iput!
    372  call assert_equal(repeat(' ', 8) . "bar", getline(1))
    373  bw!
    374 endfunc
    375 
    376 func Test_iput_invalidrange()
    377  new
    378  call setreg('0', "bar", 'l')
    379  call assert_fails(':10iput', 'E16:')
    380  bw!
    381 endfunc
    382 
    383 func Test_iput_zero_range()
    384  new
    385  let _var = [getreg('a'), getregtype('a')]
    386  call setreg('a', 'foobar', 'l')
    387  call setline(1, range(1, 2))
    388  call cursor(1, 1)
    389  0iput a
    390  call assert_equal(['foobar', '1', '2'], getline(1, '$'))
    391  %d
    392  call setline(1, range(1, 2))
    393  call cursor(1, 1)
    394  0iput! a
    395  call assert_equal(['foobar', '1', '2'], getline(1, '$'))
    396  call setreg('a', _var[0], _var[1])
    397  bw!
    398 endfunc
    399 
    400 func Test_iput_not_put()
    401  new
    402  call setline(1, "\<Tab>foo")
    403  call setreg('0', "bar", 'l')
    404  iput
    405  call assert_equal("\<Tab>bar", getline(2))
    406  put
    407  call assert_equal("bar", getline(3))
    408  bw!
    409 endfunc
    410 
    411 " Test pasting the '.' register
    412 func Test_put_inserted()
    413  new
    414 
    415  for s in ['', '…', '0', '^', '+0', '+^', '…0', '…^']
    416    call setline(1, 'foobar')
    417    exe $"normal! A{s}\<Esc>"
    418    call assert_equal($'foobar{s}', getline(1))
    419    normal! ".p
    420    call assert_equal($'foobar{s}{s}', getline(1))
    421    normal! ".2p
    422    call assert_equal($'foobar{s}{s}{s}{s}', getline(1))
    423  endfor
    424 
    425  for s in ['0', '^', '+0', '+^', '…0', '…^']
    426    call setline(1, "\t\t\t\t\tfoobar")
    427    exe $"normal! A\<C-D>{s}\<Esc>"
    428    call assert_equal($"\t\t\t\tfoobar{s}", getline(1))
    429    normal! ".p
    430    call assert_equal($"\t\t\tfoobar{s}{s}", getline(1))
    431    normal! ".2p
    432    call assert_equal($"\tfoobar{s}{s}{s}{s}", getline(1))
    433  endfor
    434 
    435  bwipe!
    436 endfunc
    437 
    438 " vim: shiftwidth=2 sts=2 expandtab