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