xref: /vim-8.2.3635/src/testdir/test_match.vim (revision 9b7bf9e9)
1" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
2" matchaddpos(), matcharg(), matchdelete(), and setmatches().
3
4source screendump.vim
5source check.vim
6
7function Test_match()
8  highlight MyGroup1 term=bold ctermbg=red guibg=red
9  highlight MyGroup2 term=italic ctermbg=green guibg=green
10  highlight MyGroup3 term=underline ctermbg=blue guibg=blue
11
12  " --- Check that "matcharg()" returns the correct group and pattern if a match
13  " --- is defined.
14  match MyGroup1 /TODO/
15  2match MyGroup2 /FIXME/
16  3match MyGroup3 /XXX/
17  call assert_equal(['MyGroup1', 'TODO'], matcharg(1))
18  call assert_equal(['MyGroup2', 'FIXME'], 2->matcharg())
19  call assert_equal(['MyGroup3', 'XXX'], matcharg(3))
20
21  " --- Check that "matcharg()" returns an empty list if the argument is not 1,
22  " --- 2 or 3 (only 0 and 4 are tested).
23  call assert_equal([], matcharg(0))
24  call assert_equal([], matcharg(4))
25
26  " --- Check that "matcharg()" returns ['', ''] if a match is not defined.
27  match
28  2match
29  3match
30  call assert_equal(['', ''], matcharg(1))
31  call assert_equal(['', ''], matcharg(2))
32  call assert_equal(['', ''], matcharg(3))
33
34  " --- Check that "matchadd()" and "getmatches()" agree on added matches and
35  " --- that default values apply.
36  let m1 = matchadd("MyGroup1", "TODO")
37  let m2 = matchadd("MyGroup2", "FIXME", 42)
38  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
39  let ans = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 4},
40        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 42, 'id': 5},
41        \    {'group': 'MyGroup3', 'pattern': 'XXX', 'priority': 60, 'id': 17}]
42  call assert_equal(ans, getmatches())
43
44  " --- Check that "matchdelete()" deletes the matches defined in the previous
45  " --- test correctly.
46  call matchdelete(m1)
47  eval m2->matchdelete()
48  call matchdelete(m3)
49  call assert_equal([], getmatches())
50
51  " --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
52  let m = matchadd("MyGroup1", "TODO")
53  call assert_equal(0, matchdelete(m))
54  call assert_fails('call matchdelete(42)', 'E803:')
55
56  " --- Check that "clearmatches()" clears all matches defined by ":match" and
57  " --- "matchadd()".
58  let m1 = matchadd("MyGroup1", "TODO")
59  let m2 = "MyGroup2"->matchadd("FIXME", 42)
60  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
61  match MyGroup1 /COFFEE/
62  2match MyGroup2 /HUMPPA/
63  3match MyGroup3 /VIM/
64  call clearmatches()
65  call assert_equal([], getmatches())
66
67  " --- Check that "setmatches()" restores a list of matches saved by
68  " --- "getmatches()" without changes. (Matches with equal priority must also
69  " --- remain in the same order.)
70  let m1 = matchadd("MyGroup1", "TODO")
71  let m2 = matchadd("MyGroup2", "FIXME", 42)
72  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
73  match MyGroup1 /COFFEE/
74  2match MyGroup2 /HUMPPA/
75  3match MyGroup3 /VIM/
76  let ml = getmatches()
77  call clearmatches()
78  call setmatches(ml)
79  call assert_equal(ml, getmatches())
80  call clearmatches()
81
82  " --- Check that "setmatches()" will not add two matches with the same ID. The
83  " --- expected behaviour (for now) is to add the first match but not the
84  " --- second and to return 0 (even though it is a matter of debate whether
85  " --- this can be considered successful behaviour).
86  let data = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1},
87        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}]
88  call assert_fails('call setmatches(data)', 'E801:')
89  call assert_equal([data[0]], getmatches())
90  call clearmatches()
91
92  " --- Check that "setmatches()" returns 0 if successful and otherwise -1.
93  " --- (A range of valid and invalid input values are tried out to generate the
94  " --- return values.)
95  call assert_equal(0, setmatches([]))
96  call assert_equal(0, setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}]))
97  call clearmatches()
98  call assert_fails('call setmatches(0)', 'E714:')
99  call assert_fails('call setmatches([0])', 'E474:')
100  call assert_fails("call setmatches([{'wrong key': 'wrong value'}])", 'E474:')
101  call assert_equal(-1, setmatches([{'group' : 'Search', 'priority' : 10, 'id' : 5, 'pos1' : {}}]))
102
103  call setline(1, 'abcdefghijklmnopq')
104  call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
105  1
106  redraw!
107  let v1 = screenattr(1, 1)
108  let v5 = screenattr(1, 5)
109  let v6 = screenattr(1, 6)
110  let v8 = screenattr(1, 8)
111  let v10 = screenattr(1, 10)
112  let v11 = screenattr(1, 11)
113  call assert_notequal(v1, v5)
114  call assert_equal(v6, v1)
115  call assert_equal(v8, v5)
116  call assert_equal(v10, v5)
117  call assert_equal(v11, v1)
118  call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
119  call clearmatches()
120
121  call setline(1, 'abcdΣabcdef')
122  eval "MyGroup1"->matchaddpos([[1, 4, 2], [1, 9, 2]])
123  1
124  redraw!
125  let v1 = screenattr(1, 1)
126  let v4 = screenattr(1, 4)
127  let v5 = screenattr(1, 5)
128  let v6 = screenattr(1, 6)
129  let v7 = screenattr(1, 7)
130  let v8 = screenattr(1, 8)
131  let v9 = screenattr(1, 9)
132  let v10 = screenattr(1, 10)
133  call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches())
134  call assert_notequal(v1, v4)
135  call assert_equal(v5, v4)
136  call assert_equal(v6, v1)
137  call assert_equal(v7, v1)
138  call assert_equal(v8, v4)
139  call assert_equal(v9, v4)
140  call assert_equal(v10, v1)
141
142  " Check, that setmatches() can correctly restore the matches from matchaddpos()
143  call matchadd('MyGroup1', '\%2lmatchadd')
144  let m=getmatches()
145  call clearmatches()
146  call setmatches(m)
147  call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1,9, 2]}, {'group': 'MyGroup1', 'pattern': '\%2lmatchadd', 'priority': 10, 'id': 12}], getmatches())
148
149  highlight MyGroup1 NONE
150  highlight MyGroup2 NONE
151  highlight MyGroup3 NONE
152endfunc
153
154func Test_match_error()
155  call assert_fails('match Error', 'E475:')
156  call assert_fails('match Error /', 'E475:')
157  call assert_fails('4match Error /x/', 'E476:')
158  call assert_fails('match Error /x/ x', 'E488:')
159endfunc
160
161func Test_matchadd_error()
162  call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
163  call assert_fails("call matchadd('Search', '\\(')", 'E54:')
164  call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
165  call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
166  call assert_fails("call matchadd('Error', 'XXX', 1, 0)", 'E799:')
167  call assert_fails("call matchadd('Error', 'XXX', [], 0)", 'E745:')
168  call assert_equal(-1, matchadd('', 'pat'))
169  call assert_equal(-1, matchadd('Search', ''))
170endfunc
171
172func Test_matchaddpos()
173  syntax on
174  set hlsearch
175
176  call setline(1, ['12345', 'NP'])
177  call matchaddpos('Error', [[1,2], [1,6], [2,2]])
178  redraw!
179  call assert_notequal(screenattr(2,2), 0)
180  call assert_equal(screenattr(2,2), screenattr(1,2))
181  call assert_notequal(screenattr(2,2), screenattr(1,6))
182  1
183  call matchadd('Search', 'N\|\n')
184  redraw!
185  call assert_notequal(screenattr(2,1), 0)
186  call assert_equal(screenattr(2,1), screenattr(1,6))
187  exec "norm! i0\<Esc>"
188  redraw!
189  call assert_equal(screenattr(2,2), screenattr(1,6))
190
191  " Check overlapping pos
192  call clearmatches()
193  call setline(1, ['1234567890', 'NH'])
194  call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
195  redraw!
196  call assert_notequal(screenattr(2,2), 0)
197  call assert_equal(screenattr(2,2), screenattr(1,5))
198  call assert_equal(screenattr(2,2), screenattr(1,7))
199  call assert_notequal(screenattr(2,2), screenattr(1,8))
200
201  call clearmatches()
202  call matchaddpos('Error', [[1], [2,2]])
203  redraw!
204  call assert_equal(screenattr(2,2), screenattr(1,1))
205  call assert_equal(screenattr(2,2), screenattr(1,10))
206  call assert_notequal(screenattr(2,2), screenattr(1,11))
207
208  " matchaddpos() with line number as 0
209  call clearmatches()
210  let id = matchaddpos('Search', [[0], [3], [0]])
211  call assert_equal([{'group' : 'Search', 'priority' : 10, 'id' : id, 'pos1' : [3]}], getmatches())
212  call clearmatches()
213  let id = matchaddpos('Search', [0, 3, 0])
214  call assert_equal([{'group' : 'Search', 'priority' : 10, 'id' : id, 'pos1' : [3]}], getmatches())
215
216  nohl
217  call clearmatches()
218  syntax off
219  set hlsearch&
220endfunc
221
222func Test_matchaddpos_otherwin()
223  syntax on
224  new
225  call setline(1, ['12345', 'NP'])
226  let winid = win_getid()
227
228  wincmd w
229  call matchadd('Search', '4', 10, -1, {'window': winid})
230  call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
231  redraw!
232  call assert_notequal(screenattr(1,2), 0)
233  call assert_notequal(screenattr(1,4), 0)
234  call assert_notequal(screenattr(2,2), 0)
235  call assert_equal(screenattr(1,2), screenattr(2,2))
236  call assert_notequal(screenattr(1,2), screenattr(1,4))
237
238  let savematches = getmatches(winid)
239  let expect = [
240        \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4},
241        \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
242        \]
243  call assert_equal(expect, savematches)
244
245  eval winid->clearmatches()
246  call assert_equal([], getmatches(winid))
247  call assert_fails('echo getmatches(-1)', 'E957:')
248
249  call setmatches(savematches, winid)
250  call assert_equal(expect, savematches)
251
252  wincmd w
253  bwipe!
254  call clearmatches()
255  syntax off
256endfunc
257
258func Test_matchaddpos_using_negative_priority()
259  set hlsearch
260
261  call clearmatches()
262
263  call setline(1, 'x')
264  let @/='x'
265  redraw!
266  let search_attr = screenattr(1,1)
267
268  let @/=''
269  call matchaddpos('Error', [1], 10)
270  redraw!
271  let error_attr = screenattr(1,1)
272
273  call setline(2, '-1 match priority')
274  call matchaddpos('Error', [2], -1)
275  redraw!
276  let negative_match_priority_attr = screenattr(2,1)
277
278  call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
279  call assert_equal(negative_match_priority_attr, error_attr)
280
281  nohl
282  set hlsearch&
283endfunc
284
285func Test_matchaddpos_error()
286  call assert_fails("call matchaddpos('Error', 1)", 'E686:')
287  call assert_fails("call matchaddpos('Error', [1], 1, 1)", 'E798:')
288  call assert_fails("call matchaddpos('Error', [1], 1, 2)", 'E798:')
289  call assert_fails("call matchaddpos('Error', [1], 1, 0)", 'E799:')
290  call assert_fails("call matchaddpos('Error', [1], 1, 123, 1)", 'E715:')
291  call assert_fails("call matchaddpos('Error', [1], 1, 5, {'window':12345})", 'E957:')
292  " Why doesn't the following error have an error code E...?
293  call assert_fails("call matchaddpos('Error', [{}])", 'E290:')
294  call assert_equal(-1, matchaddpos('Error', test_null_list()))
295  call assert_fails("call matchaddpos('Error', [1], [], 1)", 'E745:')
296  call assert_equal(-1, matchaddpos('Search', [[]]))
297  call assert_fails("call matchaddpos('Search', [[{}]])", 'E728:')
298  call assert_fails("call matchaddpos('Search', [[2, {}]])", 'E728:')
299  call assert_fails("call matchaddpos('Search', [[3, 4, {}]])", 'E728:')
300endfunc
301
302func OtherWindowCommon()
303  let lines =<< trim END
304    call setline(1, 'Hello Vim world')
305    let mid = matchadd('Error', 'world', 1)
306    let winid = win_getid()
307    new
308  END
309  call writefile(lines, 'XscriptMatchCommon')
310  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
311  call TermWait(buf)
312  return buf
313endfunc
314
315func Test_matchdelete_other_window()
316  CheckScreendump
317
318  let buf = OtherWindowCommon()
319  call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
320  call VerifyScreenDump(buf, 'Test_matchdelete_1', {})
321
322  call StopVimInTerminal(buf)
323  call delete('XscriptMatchCommon')
324endfunc
325
326func Test_matchdelete_error()
327  call assert_fails("call matchdelete(0)", 'E802:')
328  call assert_fails("call matchdelete(1, -1)", 'E957:')
329endfunc
330
331func Test_matchclear_other_window()
332  CheckRunVimInTerminal
333  let buf = OtherWindowCommon()
334  call term_sendkeys(buf, ":call clearmatches(winid)\<CR>")
335  call VerifyScreenDump(buf, 'Test_matchclear_1', {})
336
337  call StopVimInTerminal(buf)
338  call delete('XscriptMatchCommon')
339endfunc
340
341func Test_matchadd_other_window()
342  CheckRunVimInTerminal
343  let buf = OtherWindowCommon()
344  call term_sendkeys(buf, ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>")
345  call term_sendkeys(buf, ":\<CR>")
346  call VerifyScreenDump(buf, 'Test_matchadd_1', {})
347
348  call StopVimInTerminal(buf)
349  call delete('XscriptMatchCommon')
350endfunc
351
352" Test for deleting matches outside of the screen redraw top/bottom lines
353" This should cause a redraw of those lines.
354func Test_matchdelete_redraw()
355  new
356  call setline(1, range(1, 500))
357  call cursor(250, 1)
358  let m1 = matchaddpos('Search', [[250]])
359  let m2 = matchaddpos('Search', [[10], [450]])
360  redraw!
361  let m3 = matchaddpos('Search', [[240], [260]])
362  call matchdelete(m2)
363  let m = getmatches()
364  call assert_equal(2, len(m))
365  call assert_equal([250], m[0].pos1)
366  redraw!
367  call matchdelete(m1)
368  call assert_equal(1, len(getmatches()))
369  bw!
370endfunc
371
372" vim: shiftwidth=2 sts=2 expandtab
373