xref: /vim-8.2.3635/src/testdir/test_match.vim (revision 5be4ceec)
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
102  call setline(1, 'abcdefghijklmnopq')
103  call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
104  1
105  redraw!
106  let v1 = screenattr(1, 1)
107  let v5 = screenattr(1, 5)
108  let v6 = screenattr(1, 6)
109  let v8 = screenattr(1, 8)
110  let v10 = screenattr(1, 10)
111  let v11 = screenattr(1, 11)
112  call assert_notequal(v1, v5)
113  call assert_equal(v6, v1)
114  call assert_equal(v8, v5)
115  call assert_equal(v10, v5)
116  call assert_equal(v11, v1)
117  call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
118  call clearmatches()
119
120  call setline(1, 'abcdΣabcdef')
121  eval "MyGroup1"->matchaddpos([[1, 4, 2], [1, 9, 2]])
122  1
123  redraw!
124  let v1 = screenattr(1, 1)
125  let v4 = screenattr(1, 4)
126  let v5 = screenattr(1, 5)
127  let v6 = screenattr(1, 6)
128  let v7 = screenattr(1, 7)
129  let v8 = screenattr(1, 8)
130  let v9 = screenattr(1, 9)
131  let v10 = screenattr(1, 10)
132  call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches())
133  call assert_notequal(v1, v4)
134  call assert_equal(v5, v4)
135  call assert_equal(v6, v1)
136  call assert_equal(v7, v1)
137  call assert_equal(v8, v4)
138  call assert_equal(v9, v4)
139  call assert_equal(v10, v1)
140
141  " Check, that setmatches() can correctly restore the matches from matchaddpos()
142  call matchadd('MyGroup1', '\%2lmatchadd')
143  let m=getmatches()
144  call clearmatches()
145  call setmatches(m)
146  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())
147
148  highlight MyGroup1 NONE
149  highlight MyGroup2 NONE
150  highlight MyGroup3 NONE
151endfunc
152
153func Test_matchaddpos()
154  syntax on
155  set hlsearch
156
157  call setline(1, ['12345', 'NP'])
158  call matchaddpos('Error', [[1,2], [1,6], [2,2]])
159  redraw!
160  call assert_notequal(screenattr(2,2), 0)
161  call assert_equal(screenattr(2,2), screenattr(1,2))
162  call assert_notequal(screenattr(2,2), screenattr(1,6))
163  1
164  call matchadd('Search', 'N\|\n')
165  redraw!
166  call assert_notequal(screenattr(2,1), 0)
167  call assert_equal(screenattr(2,1), screenattr(1,6))
168  exec "norm! i0\<Esc>"
169  redraw!
170  call assert_equal(screenattr(2,2), screenattr(1,6))
171
172  " Check overlapping pos
173  call clearmatches()
174  call setline(1, ['1234567890', 'NH'])
175  call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
176  redraw!
177  call assert_notequal(screenattr(2,2), 0)
178  call assert_equal(screenattr(2,2), screenattr(1,5))
179  call assert_equal(screenattr(2,2), screenattr(1,7))
180  call assert_notequal(screenattr(2,2), screenattr(1,8))
181
182  call clearmatches()
183  call matchaddpos('Error', [[1], [2,2]])
184  redraw!
185  call assert_equal(screenattr(2,2), screenattr(1,1))
186  call assert_equal(screenattr(2,2), screenattr(1,10))
187  call assert_notequal(screenattr(2,2), screenattr(1,11))
188
189  nohl
190  call clearmatches()
191  syntax off
192  set hlsearch&
193endfunc
194
195func Test_matchaddpos_otherwin()
196  syntax on
197  new
198  call setline(1, ['12345', 'NP'])
199  let winid = win_getid()
200
201  wincmd w
202  call matchadd('Search', '4', 10, -1, {'window': winid})
203  call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
204  redraw!
205  call assert_notequal(screenattr(1,2), 0)
206  call assert_notequal(screenattr(1,4), 0)
207  call assert_notequal(screenattr(2,2), 0)
208  call assert_equal(screenattr(1,2), screenattr(2,2))
209  call assert_notequal(screenattr(1,2), screenattr(1,4))
210
211  let savematches = getmatches(winid)
212  let expect = [
213        \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4},
214        \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
215        \]
216  call assert_equal(expect, savematches)
217
218  eval winid->clearmatches()
219  call assert_equal([], getmatches(winid))
220
221  call setmatches(savematches, winid)
222  call assert_equal(expect, savematches)
223
224  wincmd w
225  bwipe!
226  call clearmatches()
227  syntax off
228endfunc
229
230func Test_matchaddpos_using_negative_priority()
231  set hlsearch
232
233  call clearmatches()
234
235  call setline(1, 'x')
236  let @/='x'
237  redraw!
238  let search_attr = screenattr(1,1)
239
240  let @/=''
241  call matchaddpos('Error', [1], 10)
242  redraw!
243  let error_attr = screenattr(1,1)
244
245  call setline(2, '-1 match priority')
246  call matchaddpos('Error', [2], -1)
247  redraw!
248  let negative_match_priority_attr = screenattr(2,1)
249
250  call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
251  call assert_equal(negative_match_priority_attr, error_attr)
252
253  nohl
254  set hlsearch&
255endfunc
256
257func OtherWindowCommon()
258  let lines =<< trim END
259    call setline(1, 'Hello Vim world')
260    let mid = matchadd('Error', 'world', 1)
261    let winid = win_getid()
262    new
263  END
264  call writefile(lines, 'XscriptMatchCommon')
265  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
266  call term_wait(buf)
267  return buf
268endfunc
269
270func Test_matchdelete_other_window()
271  CheckScreendump
272
273  let buf = OtherWindowCommon()
274  call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
275  call VerifyScreenDump(buf, 'Test_matchdelete_1', {})
276
277  call StopVimInTerminal(buf)
278  call delete('XscriptMatchCommon')
279endfunc
280
281func Test_matchclear_other_window()
282  if !CanRunVimInTerminal()
283    throw 'Skipped: cannot make screendumps'
284  endif
285  let buf = OtherWindowCommon()
286  call term_sendkeys(buf, ":call clearmatches(winid)\<CR>")
287  call VerifyScreenDump(buf, 'Test_matchclear_1', {})
288
289  call StopVimInTerminal(buf)
290  call delete('XscriptMatchCommon')
291endfunc
292
293func Test_matchadd_other_window()
294  if !CanRunVimInTerminal()
295    throw 'Skipped: cannot make screendumps'
296  endif
297  let buf = OtherWindowCommon()
298  call term_sendkeys(buf, ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>")
299  call term_sendkeys(buf, ":\<CR>")
300  call VerifyScreenDump(buf, 'Test_matchadd_1', {})
301
302  call StopVimInTerminal(buf)
303  call delete('XscriptMatchCommon')
304endfunc
305
306
307" vim: shiftwidth=2 sts=2 expandtab
308