blob: dafe3ceb65555f9793647d2d7c1de7d12979a13b [file] [log] [blame]
Bram Moolenaard76a0c12016-08-06 15:29:22 +02001" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
Bram Moolenaar1190cf62017-09-14 14:31:18 +02002" matchaddpos(), matcharg(), matchdelete(), and setmatches().
Bram Moolenaard76a0c12016-08-06 15:29:22 +02003
Bram Moolenaar06029a82019-07-24 14:25:26 +02004source screendump.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02005source check.vim
Bram Moolenaar06029a82019-07-24 14:25:26 +02006
Bram Moolenaar4f416e42016-08-16 16:08:18 +02007function Test_match()
Bram Moolenaard76a0c12016-08-06 15:29:22 +02008 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))
Bram Moolenaara1449832019-09-01 20:16:52 +020018 call assert_equal(['MyGroup2', 'FIXME'], 2->matcharg())
Bram Moolenaard76a0c12016-08-06 15:29:22 +020019 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)
Bram Moolenaara1449832019-09-01 20:16:52 +020047 eval m2->matchdelete()
Bram Moolenaard76a0c12016-08-06 15:29:22 +020048 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")
Bram Moolenaara1449832019-09-01 20:16:52 +020059 let m2 = "MyGroup2"->matchadd("FIXME", 42)
Bram Moolenaard76a0c12016-08-06 15:29:22 +020060 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
Bram Moolenaar30276f22019-01-24 17:59:39 +0100120 call setline(1, 'abcdΣabcdef')
Bram Moolenaara1449832019-09-01 20:16:52 +0200121 eval "MyGroup1"->matchaddpos([[1, 4, 2], [1, 9, 2]])
Bram Moolenaar30276f22019-01-24 17:59:39 +0100122 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)
Bram Moolenaard76a0c12016-08-06 15:29:22 +0200140
Bram Moolenaar30276f22019-01-24 17:59:39 +0100141 " 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())
Bram Moolenaard76a0c12016-08-06 15:29:22 +0200147
148 highlight MyGroup1 NONE
149 highlight MyGroup2 NONE
150 highlight MyGroup3 NONE
151endfunc
152
Bram Moolenaar49d68bf2019-12-24 15:17:00 +0100153func Test_match_error()
154 call assert_fails('match Error', 'E475:')
155 call assert_fails('match Error /', 'E475:')
156 call assert_fails('4match Error /x/', 'E476:')
157 call assert_fails('match Error /x/ x', 'E488:')
158endfunc
159
160func Test_matchadd_error()
161 call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
162 call assert_fails("call matchadd('Search', '\\(')", 'E475:')
163 call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
164 call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
165 call assert_fails("call matchadd('Error', 'XXX', 1, 0)", 'E799:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200166 call assert_fails("call matchadd('Error', 'XXX', [], 0)", 'E745:')
Bram Moolenaar49d68bf2019-12-24 15:17:00 +0100167endfunc
168
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200169func Test_matchaddpos()
170 syntax on
171 set hlsearch
172
173 call setline(1, ['12345', 'NP'])
174 call matchaddpos('Error', [[1,2], [1,6], [2,2]])
175 redraw!
176 call assert_notequal(screenattr(2,2), 0)
177 call assert_equal(screenattr(2,2), screenattr(1,2))
178 call assert_notequal(screenattr(2,2), screenattr(1,6))
179 1
180 call matchadd('Search', 'N\|\n')
181 redraw!
182 call assert_notequal(screenattr(2,1), 0)
183 call assert_equal(screenattr(2,1), screenattr(1,6))
184 exec "norm! i0\<Esc>"
185 redraw!
186 call assert_equal(screenattr(2,2), screenattr(1,6))
187
Bram Moolenaara6c27ee2016-10-15 14:56:30 +0200188 " Check overlapping pos
189 call clearmatches()
190 call setline(1, ['1234567890', 'NH'])
191 call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
192 redraw!
193 call assert_notequal(screenattr(2,2), 0)
194 call assert_equal(screenattr(2,2), screenattr(1,5))
195 call assert_equal(screenattr(2,2), screenattr(1,7))
196 call assert_notequal(screenattr(2,2), screenattr(1,8))
197
Bram Moolenaar85077472016-10-16 14:35:48 +0200198 call clearmatches()
199 call matchaddpos('Error', [[1], [2,2]])
200 redraw!
201 call assert_equal(screenattr(2,2), screenattr(1,1))
202 call assert_equal(screenattr(2,2), screenattr(1,10))
203 call assert_notequal(screenattr(2,2), screenattr(1,11))
204
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200205 nohl
Bram Moolenaar85077472016-10-16 14:35:48 +0200206 call clearmatches()
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200207 syntax off
208 set hlsearch&
209endfunc
210
Bram Moolenaar95e51472018-07-28 16:55:56 +0200211func Test_matchaddpos_otherwin()
212 syntax on
213 new
214 call setline(1, ['12345', 'NP'])
215 let winid = win_getid()
216
217 wincmd w
218 call matchadd('Search', '4', 10, -1, {'window': winid})
219 call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
220 redraw!
221 call assert_notequal(screenattr(1,2), 0)
222 call assert_notequal(screenattr(1,4), 0)
223 call assert_notequal(screenattr(2,2), 0)
224 call assert_equal(screenattr(1,2), screenattr(2,2))
225 call assert_notequal(screenattr(1,2), screenattr(1,4))
226
Bram Moolenaaraff74912019-03-30 18:11:49 +0100227 let savematches = getmatches(winid)
228 let expect = [
229 \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4},
230 \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
231 \]
232 call assert_equal(expect, savematches)
233
Bram Moolenaar1a3a8912019-08-23 22:31:37 +0200234 eval winid->clearmatches()
Bram Moolenaaraff74912019-03-30 18:11:49 +0100235 call assert_equal([], getmatches(winid))
236
237 call setmatches(savematches, winid)
238 call assert_equal(expect, savematches)
239
Bram Moolenaar95e51472018-07-28 16:55:56 +0200240 wincmd w
241 bwipe!
242 call clearmatches()
243 syntax off
244endfunc
245
Bram Moolenaare17bdff2016-08-27 18:34:29 +0200246func Test_matchaddpos_using_negative_priority()
247 set hlsearch
248
249 call clearmatches()
250
251 call setline(1, 'x')
252 let @/='x'
253 redraw!
254 let search_attr = screenattr(1,1)
255
256 let @/=''
257 call matchaddpos('Error', [1], 10)
258 redraw!
259 let error_attr = screenattr(1,1)
260
261 call setline(2, '-1 match priority')
262 call matchaddpos('Error', [2], -1)
263 redraw!
264 let negative_match_priority_attr = screenattr(2,1)
265
266 call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
267 call assert_equal(negative_match_priority_attr, error_attr)
268
269 nohl
270 set hlsearch&
271endfunc
272
Bram Moolenaar49d68bf2019-12-24 15:17:00 +0100273func Test_matchaddpos_error()
274 call assert_fails("call matchaddpos('Error', 1)", 'E686:')
275 call assert_fails("call matchaddpos('Error', [1], 1, 1)", 'E798:')
276 call assert_fails("call matchaddpos('Error', [1], 1, 2)", 'E798:')
277 call assert_fails("call matchaddpos('Error', [1], 1, 0)", 'E799:')
278 call assert_fails("call matchaddpos('Error', [1], 1, 123, 1)", 'E715:')
279 call assert_fails("call matchaddpos('Error', [1], 1, 5, {'window':12345})", 'E957:')
280 " Why doesn't the following error have an error code E...?
281 call assert_fails("call matchaddpos('Error', [{}])", 'E290:')
Bram Moolenaar99fa7212020-04-26 15:59:55 +0200282 call assert_equal(-1, matchaddpos('Error', test_null_list()))
283 call assert_fails("call matchaddpos('Error', [1], [], 1)", 'E745:')
Bram Moolenaar49d68bf2019-12-24 15:17:00 +0100284endfunc
285
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200286func OtherWindowCommon()
Bram Moolenaar06029a82019-07-24 14:25:26 +0200287 let lines =<< trim END
288 call setline(1, 'Hello Vim world')
289 let mid = matchadd('Error', 'world', 1)
290 let winid = win_getid()
291 new
292 END
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200293 call writefile(lines, 'XscriptMatchCommon')
294 let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +0200295 call TermWait(buf)
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200296 return buf
297endfunc
298
299func Test_matchdelete_other_window()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +0200300 CheckScreendump
301
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200302 let buf = OtherWindowCommon()
Bram Moolenaar06029a82019-07-24 14:25:26 +0200303 call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
304 call VerifyScreenDump(buf, 'Test_matchdelete_1', {})
305
306 call StopVimInTerminal(buf)
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200307 call delete('XscriptMatchCommon')
308endfunc
309
Bram Moolenaar49d68bf2019-12-24 15:17:00 +0100310func Test_matchdelete_error()
311 call assert_fails("call matchdelete(0)", 'E802:')
312 call assert_fails("call matchdelete(1, -1)", 'E957:')
313endfunc
314
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200315func Test_matchclear_other_window()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200316 CheckRunVimInTerminal
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200317 let buf = OtherWindowCommon()
318 call term_sendkeys(buf, ":call clearmatches(winid)\<CR>")
319 call VerifyScreenDump(buf, 'Test_matchclear_1', {})
320
321 call StopVimInTerminal(buf)
322 call delete('XscriptMatchCommon')
323endfunc
324
325func Test_matchadd_other_window()
Bram Moolenaar494e9062020-05-31 21:28:02 +0200326 CheckRunVimInTerminal
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200327 let buf = OtherWindowCommon()
328 call term_sendkeys(buf, ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>")
329 call term_sendkeys(buf, ":\<CR>")
330 call VerifyScreenDump(buf, 'Test_matchadd_1', {})
331
332 call StopVimInTerminal(buf)
333 call delete('XscriptMatchCommon')
Bram Moolenaar06029a82019-07-24 14:25:26 +0200334endfunc
335
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200336" vim: shiftwidth=2 sts=2 expandtab