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