neovim

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

test_marks.vim (8300B)


      1 " Test that a deleted mark is restored after delete-undo-redo-undo.
      2 func Test_Restore_DelMark()
      3  enew!
      4  call append(0, ["	textline A", "	textline B", "	textline C"])
      5  normal! 2gg
      6  set nocp viminfo+=nviminfo
      7  exe "normal! i\<C-G>u\<Esc>"
      8  exe "normal! maddu\<C-R>u"
      9  let pos = getpos("'a")
     10  call assert_equal(2, pos[1])
     11  call assert_equal(1, pos[2])
     12  enew!
     13 endfunc
     14 
     15 " Test that CTRL-A and CTRL-X updates last changed mark '[, '].
     16 func Test_Incr_Marks()
     17  enew!
     18  call append(0, ["123 123 123", "123 123 123", "123 123 123"])
     19  normal! gg
     20  execute "normal! \<C-A>`[v`]rAjwvjw\<C-X>`[v`]rX"
     21  call assert_equal("AAA 123 123", getline(1))
     22  call assert_equal("123 XXXXXXX", getline(2))
     23  call assert_equal("XXX 123 123", getline(3))
     24  enew!
     25 endfunc
     26 
     27 func Test_previous_jump_mark()
     28  new
     29  call setline(1, ['']->repeat(6))
     30  normal Ggg
     31  call assert_equal(6, getpos("''")[1])
     32  normal jjjjj
     33  call assert_equal(6, getpos("''")[1])
     34  bwipe!
     35 endfunc
     36 
     37 func Test_setpos()
     38  new Xone
     39  let onebuf = bufnr('%')
     40  let onewin = win_getid()
     41  call setline(1, ['aaa', 'bbb', 'ccc'])
     42  new Xtwo
     43  let twobuf = bufnr('%')
     44  let twowin = win_getid()
     45  call setline(1, ['aaa', 'bbb', 'ccc'])
     46 
     47  " for the cursor the buffer number is ignored
     48  call setpos(".", [0, 2, 1, 0])
     49  call assert_equal([0, 2, 1, 0], getpos("."))
     50  call setpos(".", [onebuf, 3, 3, 0])
     51  call assert_equal([0, 3, 3, 0], getpos("."))
     52 
     53  call setpos("''", [0, 1, 3, 0])
     54  call assert_equal([0, 1, 3, 0], getpos("''"))
     55  call setpos("''", [onebuf, 2, 2, 0])
     56  call assert_equal([0, 2, 2, 0], getpos("''"))
     57 
     58  " buffer-local marks
     59  for mark in ["'a", "'\"", "'[", "']", "'<", "'>"]
     60    call win_gotoid(twowin)
     61    call setpos(mark, [0, 2, 1, 0])
     62    call assert_equal([0, 2, 1, 0], getpos(mark), "for mark " . mark)
     63    call setpos(mark, [onebuf, 1, 3, 0])
     64    call win_gotoid(onewin)
     65    call assert_equal([0, 1, 3, 0], getpos(mark), "for mark " . mark)
     66  endfor
     67 
     68  " global marks
     69  call win_gotoid(twowin)
     70  call setpos("'N", [0, 2, 1, 0])
     71  call assert_equal([twobuf, 2, 1, 0], getpos("'N"))
     72  call setpos("'N", [onebuf, 1, 3, 0])
     73  call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
     74 
     75  " try invalid column and check virtcol()
     76  call win_gotoid(onewin)
     77  call setpos("'a", [0, 1, 2, 0])
     78  call assert_equal([0, 1, 2, 0], getpos("'a"))
     79  call setpos("'a", [0, 1, -5, 0])
     80  call assert_equal([0, 1, 2, 0], getpos("'a"))
     81  call setpos("'a", [0, 1, 0, 0])
     82  call assert_equal([0, 1, 1, 0], getpos("'a"))
     83  call setpos("'a", [0, 1, 4, 0])
     84  call assert_equal([0, 1, 4, 0], getpos("'a"))
     85  call assert_equal(4, virtcol("'a"))
     86  call setpos("'a", [0, 1, 5, 0])
     87  call assert_equal([0, 1, 5, 0], getpos("'a"))
     88  call assert_equal(4, virtcol("'a"))
     89  call setpos("'a", [0, 1, 21341234, 0])
     90  call assert_equal([0, 1, 21341234, 0], getpos("'a"))
     91  call assert_equal(4, virtcol("'a"))
     92 
     93  " Test with invalid buffer number, line number and column number
     94  call cursor(2, 2)
     95  call setpos('.', [-1, 1, 1, 0])
     96  call assert_equal([2, 2], [line('.'), col('.')])
     97  call setpos('.', [0, -1, 1, 0])
     98  call assert_equal([2, 2], [line('.'), col('.')])
     99  call setpos('.', [0, 1, -1, 0])
    100  call assert_equal([2, 2], [line('.'), col('.')])
    101 
    102  call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:')
    103 
    104  bwipe!
    105  call win_gotoid(twowin)
    106  bwipe!
    107 endfunc
    108 
    109 func Test_marks_cmd()
    110  new Xone
    111  call setline(1, ['aaa', 'bbb'])
    112  norm! maG$mB
    113  w!
    114  new Xtwo
    115  call setline(1, ['ccc', 'ddd'])
    116  norm! $mcGmD
    117  exe "norm! GVgg\<Esc>G"
    118  w!
    119 
    120  b Xone
    121  let a = split(execute('marks'), "\n")
    122  call assert_equal(9, len(a))
    123  call assert_equal(['mark line  col file/text',
    124        \ " '      2    0 bbb",
    125        \ ' a      1    0 aaa',
    126        \ ' B      2    2 bbb',
    127        \ ' D      2    0 Xtwo',
    128        \ ' "      1    0 aaa',
    129        \ ' [      1    0 aaa',
    130        \ ' ]      2    0 bbb',
    131        \ ' .      2    0 bbb'], a)
    132 
    133  b Xtwo
    134  let a = split(execute('marks'), "\n")
    135  call assert_equal(11, len(a))
    136  call assert_equal(['mark line  col file/text',
    137        \ " '      1    0 ccc",
    138        \ ' c      1    2 ccc',
    139        \ ' B      2    2 Xone',
    140        \ ' D      2    0 ddd',
    141        \ ' "      2    0 ddd',
    142        \ ' [      1    0 ccc',
    143        \ ' ]      2    0 ddd',
    144        \ ' .      2    0 ddd',
    145        \ ' <      1    0 ccc',
    146        \ ' >      2    0 ddd'], a)
    147  norm! Gdd
    148  w!
    149  let a = split(execute('marks <>'), "\n")
    150  call assert_equal(3, len(a))
    151  call assert_equal(['mark line  col file/text',
    152        \ ' <      1    0 ccc',
    153        \ ' >      2    0 -invalid-'], a)
    154 
    155  b Xone
    156  delmarks aB
    157  let a = split(execute('marks aBcD'), "\n")
    158  call assert_equal(2, len(a))
    159  call assert_equal('mark line  col file/text', a[0])
    160  call assert_equal(' D      2    0 Xtwo', a[1])
    161 
    162  b Xtwo
    163  delmarks cD
    164  call assert_fails('marks aBcD', 'E283:')
    165 
    166  call delete('Xone')
    167  call delete('Xtwo')
    168  %bwipe
    169 endfunc
    170 
    171 func Test_marks_cmd_multibyte()
    172  new Xone
    173  call setline(1, [repeat('á', &columns)])
    174  norm! ma
    175 
    176  let a = split(execute('marks a'), "\n")
    177  call assert_equal(2, len(a))
    178  let expected = ' a      1    0 ' . repeat('á', &columns - 16)
    179  call assert_equal(expected, a[1])
    180 
    181  bwipe!
    182 endfunc
    183 
    184 func Test_delmarks()
    185  new
    186  norm mx
    187  norm `x
    188  delmarks x
    189  call assert_fails('norm `x', 'E20:')
    190 
    191  " Deleting an already deleted mark should not fail.
    192  delmarks x
    193 
    194  " getpos() should return all zeros after deleting a filemark.
    195  norm mA
    196  delmarks A
    197  call assert_equal([0, 0, 0, 0], getpos("'A"))
    198 
    199  " Test deleting a range of marks.
    200  norm ma
    201  norm mb
    202  norm mc
    203  norm mz
    204  delmarks b-z
    205  norm `a
    206  call assert_fails('norm `b', 'E20:')
    207  call assert_fails('norm `c', 'E20:')
    208  call assert_fails('norm `z', 'E20:')
    209  call assert_fails('delmarks z-b', 'E475:')
    210 
    211  call assert_fails('delmarks', 'E471:')
    212  call assert_fails('delmarks /', 'E475:')
    213 
    214  " Test delmarks!
    215  norm mx
    216  norm `x
    217  delmarks!
    218  call assert_fails('norm `x', 'E20:')
    219  call assert_fails('delmarks! x', 'E474:')
    220 
    221  bwipe!
    222 endfunc
    223 
    224 func Test_mark_error()
    225  call assert_fails('mark', 'E471:')
    226  call assert_fails('mark xx', 'E488:')
    227  call assert_fails('mark _', 'E191:')
    228  call assert_beeps('normal! m~')
    229 
    230  call setpos("'k", [0, 100, 1, 0])
    231  call assert_fails("normal 'k", 'E19:')
    232 endfunc
    233 
    234 " Test for :lockmarks when pasting content
    235 func Test_lockmarks_with_put()
    236  new
    237  call append(0, repeat(['sky is blue'], 4))
    238  normal gg
    239  1,2yank r
    240  put r
    241  normal G
    242  lockmarks put r
    243  call assert_equal(2, line("'["))
    244  call assert_equal(3, line("']"))
    245 
    246  bwipe!
    247 endfunc
    248 
    249 " Test for :k command to set a mark
    250 func Test_marks_k_cmd()
    251  new
    252  call setline(1, ['foo', 'bar', 'baz', 'qux'])
    253  1,3kr
    254  call assert_equal([0, 3, 1, 0], getpos("'r"))
    255  bw!
    256 endfunc
    257 
    258 " Test for file marks (A-Z)
    259 func Test_file_mark()
    260  new Xone
    261  call setline(1, ['aaa', 'bbb'])
    262  norm! G$mB
    263  w!
    264  new Xtwo
    265  call setline(1, ['ccc', 'ddd'])
    266  norm! GmD
    267  w!
    268 
    269  enew
    270  normal! `B
    271  call assert_equal('Xone', bufname())
    272  call assert_equal([2, 3], [line('.'), col('.')])
    273  normal! 'D
    274  call assert_equal('Xtwo', bufname())
    275  call assert_equal([2, 1], [line('.'), col('.')])
    276 
    277  call delete('Xone')
    278  call delete('Xtwo')
    279 endfunc
    280 
    281 " Test for the getmarklist() function
    282 func Test_getmarklist()
    283  new
    284  " global marks
    285  delmarks A-Z 0-9 \" ^.[]
    286  call assert_equal([], getmarklist())
    287  call setline(1, ['one', 'two', 'three'])
    288  mark A
    289  call cursor(3, 5)
    290  normal mN
    291  call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]},
    292        \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}],
    293        \ getmarklist())
    294  " buffer local marks
    295  delmarks!
    296  call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]},
    297        \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr()))
    298  call cursor(2, 2)
    299  normal mr
    300  call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]},
    301        \ bufnr()->getmarklist()[0])
    302  call assert_equal([], {}->getmarklist())
    303  normal! yy
    304  call assert_equal([
    305        \ {'mark': "'[", 'pos': [bufnr(), 2, 1, 0]},
    306        \ {'mark': "']", 'pos': [bufnr(), 2, v:maxcol, 0]},
    307        \ ], getmarklist(bufnr())[-2:])
    308  bw!
    309 endfunc
    310 
    311 " This was using freed memory
    312 func Test_jump_mark_autocmd()
    313  next 00
    314  edit 0
    315  sargument
    316  au BufEnter 0 all
    317  sil norm 
    318 
    319  au! BufEnter
    320  bwipe!
    321 endfunc
    322 
    323 
    324 " vim: shiftwidth=2 sts=2 expandtab