neovim

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

test_gn.vim (5610B)


      1 " Test for gn command
      2 
      3 func Test_gn_command()
      4  noautocmd new
      5  " replace a single char by itself quoted:
      6  call setline('.', 'abc x def x ghi x jkl')
      7  let @/ = 'x'
      8  exe "norm! cgn'x'\<esc>.."
      9  call assert_equal("abc 'x' def 'x' ghi 'x' jkl", getline('.'))
     10  sil! %d_
     11 
     12  " simple search match
     13  call setline('.', 'foobar')
     14  let @/ = 'foobar'
     15  exe "norm! gncsearchmatch"
     16  call assert_equal('searchmatch', getline('.'))
     17  sil! %d _
     18 
     19  " replace a multi-line match
     20  call setline('.', ['', 'one', 'two'])
     21  let @/ = 'one\_s*two\_s'
     22  exe "norm! gnceins\<CR>zwei"
     23  call assert_equal(['','eins','zwei'], getline(1,'$'))
     24  sil! %d _
     25 
     26  " test count argument
     27  call setline('.', ['', 'abcdx | abcdx | abcdx'])
     28  let @/ = '[a]bcdx'
     29  exe "norm! 2gnd"
     30  call assert_equal(['','abcdx |  | abcdx'], getline(1,'$'))
     31  sil! %d _
     32 
     33  " join lines
     34  call setline('.', ['join ', 'lines'])
     35  let @/ = '$'
     36  exe "norm! 0gnd"
     37  call assert_equal(['join lines'], getline(1,'$'))
     38  sil! %d _
     39 
     40  " zero-width match
     41  call setline('.', ['', 'zero width pattern'])
     42  let @/ = '\>\zs'
     43  exe "norm! 0gnd"
     44  call assert_equal(['', 'zerowidth pattern'], getline(1,'$'))
     45  sil! %d _
     46 
     47  " delete first and last chars
     48  call setline('.', ['delete first and last chars'])
     49  let @/ = '^'
     50  exe "norm! 0gnd$"
     51  let @/ = '\zs'
     52  exe "norm! gnd"
     53  call assert_equal(['elete first and last char'], getline(1,'$'))
     54  sil! %d _
     55 
     56  " using visual mode
     57  call setline('.', ['', 'uniquepattern uniquepattern'])
     58  exe "norm! /[u]niquepattern/s\<cr>vlgnd"
     59  call assert_equal(['', ' uniquepattern'], getline(1,'$'))
     60  sil! %d _
     61 
     62  " backwards search
     63  call setline('.', ['my very excellent mother just served us nachos'])
     64  let @/ = 'mother'
     65  exe "norm! $cgNmongoose"
     66  call assert_equal(['my very excellent mongoose just served us nachos'], getline(1,'$'))
     67  sil! %d _
     68 
     69  " search for single char
     70  call setline('.', ['','for (i=0; i<=10; i++)'])
     71  let @/ = 'i'
     72  exe "norm! cgnj"
     73  call assert_equal(['','for (j=0; i<=10; i++)'], getline(1,'$'))
     74  sil! %d _
     75 
     76  " search hex char
     77  call setline('.', ['','Y'])
     78  set noignorecase
     79  let @/ = '\%x59'
     80  exe "norm! gnd"
     81  call assert_equal(['',''], getline(1,'$'))
     82  sil! %d _
     83 
     84  " test repeating gdn
     85  call setline('.', ['', '1', 'Johnny', '2', 'Johnny', '3'])
     86  let @/ = 'Johnny'
     87  exe "norm! dgn."
     88  call assert_equal(['','1', '', '2', '', '3'], getline(1,'$'))
     89  sil! %d _
     90 
     91  " test repeating gUgn
     92  call setline('.', ['', '1', 'Depp', '2', 'Depp', '3'])
     93  let @/ = 'Depp'
     94  exe "norm! gUgn."
     95  call assert_equal(['', '1', 'DEPP', '2', 'DEPP', '3'], getline(1,'$'))
     96  sil! %d _
     97 
     98  " test using look-ahead assertions
     99  call setline('.', ['a:10', '', 'a:1', '', 'a:20'])
    100  let @/ = 'a:0\@!\zs\d\+'
    101  exe "norm! 2nygno\<esc>p"
    102  call assert_equal(['a:10', '', 'a:1', '1', '', 'a:20'], getline(1,'$'))
    103  sil! %d _
    104 
    105  " test using nowrapscan
    106  set nowrapscan
    107  call setline(1, 'foo bar baz')
    108  exe "norm! /bar/e\<cr>"
    109  exe "norm! gnd"
    110  call assert_equal(['foo  baz'], getline(1,'$'))
    111  sil! %d_
    112 
    113  " search upwards with nowrapscan set
    114  call setline('.', ['foo', 'bar', 'foo', 'baz'])
    115  set nowrapscan
    116  let @/ = 'foo'
    117  $
    118  norm! dgN
    119  call assert_equal(['foo', 'bar', '', 'baz'], getline(1,'$'))
    120  sil! %d_
    121 
    122  " search using the \zs atom
    123  call setline(1, [' nnoremap', '' , 'nnoremap'])
    124  set wrapscan&vim
    125  let @/ = '\_s\zsnnoremap'
    126  $
    127  norm! cgnmatch
    128  call assert_equal([' nnoremap', '', 'match'], getline(1,'$'))
    129  sil! %d_
    130 
    131  " make sure it works correctly for one-char wide search items
    132  call setline('.', ['abcdefghi'])
    133  let @/ = 'a'
    134  exe "norm! 0fhvhhgNgU"
    135  call assert_equal(['ABCDEFGHi'], getline(1,'$'))
    136  call setline('.', ['abcdefghi'])
    137  let @/ = 'b'
    138  " this gn wraps around the end of the file
    139  exe "norm! 0fhvhhgngU"
    140  call assert_equal(['aBCDEFGHi'], getline(1,'$'))
    141  sil! %d _
    142  call setline('.', ['abcdefghi'])
    143  let @/ = 'f'
    144  exe "norm! 0vllgngU"
    145  call assert_equal(['ABCDEFghi'], getline(1,'$'))
    146  sil! %d _
    147  call setline('.', ['12345678'])
    148  let @/ = '5'
    149  norm! gg0f7vhhhhgnd
    150  call assert_equal(['12348'], getline(1,'$'))
    151  sil! %d _
    152  call setline('.', ['12345678'])
    153  let @/ = '5'
    154  norm! gg0f2vf7gNd
    155  call assert_equal(['1678'], getline(1,'$'))
    156  sil! %d _
    157  set wrapscan&vim
    158 
    159  " Without 'wrapscan', in visual mode, running gn without a match should fail
    160  " but the visual mode should be kept.
    161  set nowrapscan
    162  call setline('.', 'one two')
    163  let @/ = 'one'
    164  call assert_beeps('normal 0wvlgn')
    165  exe "normal y"
    166  call assert_equal('tw', @")
    167 
    168  " with exclusive selection, run gn and gN
    169  set selection=exclusive
    170  normal 0gny
    171  call assert_equal('one', @")
    172  normal 0wgNy
    173  call assert_equal('one', @")
    174  set selection&
    175 endfunc
    176 
    177 func Test_gN_repeat()
    178  new
    179  call setline(1, 'this list is a list with a list of a list.')
    180  /list
    181  normal $gNgNgNx
    182  call assert_equal('list with a list of a list', @")
    183  bwipe!
    184 endfunc
    185 
    186 func Test_gN_then_gn()
    187  new
    188 
    189  call setline(1, 'this list is a list with a list of a last.')
    190  /l.st
    191  normal $gNgNgnx
    192  call assert_equal('last', @")
    193 
    194  call setline(1, 'this list is a list with a lust of a last.')
    195  /l.st
    196  normal $gNgNgNgnx
    197  call assert_equal('lust of a last', @")
    198 
    199  bwipe!
    200 endfunc
    201 
    202 func Test_gn_multi_line()
    203  new
    204  call setline(1, [
    205        \ 'func Tm1()',
    206        \ ' echo "one"',
    207        \ 'endfunc',
    208        \ 'func Tm2()',
    209        \ ' echo "two"',
    210        \ 'endfunc',
    211        \ 'func Tm3()',
    212        \ ' echo "three"',
    213        \ 'endfunc',
    214        \])
    215  /\v^func Tm\d\(\)\n.*\zs".*"\ze$
    216  normal jgnrx
    217  call assert_equal(' echo xxxxx', getline(5))
    218  bwipe!
    219 endfunc
    220 
    221 " vim: shiftwidth=2 sts=2 expandtab