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