blob: 8eada3fea5b21ec546c09f93225d4e268ad95d46 [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
5
Bram Moolenaar4f416e42016-08-16 16:08:18 +02006function Test_match()
Bram Moolenaard76a0c12016-08-06 15:29:22 +02007 highlight MyGroup1 term=bold ctermbg=red guibg=red
8 highlight MyGroup2 term=italic ctermbg=green guibg=green
9 highlight MyGroup3 term=underline ctermbg=blue guibg=blue
10
11 " --- Check that "matcharg()" returns the correct group and pattern if a match
12 " --- is defined.
13 match MyGroup1 /TODO/
14 2match MyGroup2 /FIXME/
15 3match MyGroup3 /XXX/
16 call assert_equal(['MyGroup1', 'TODO'], matcharg(1))
17 call assert_equal(['MyGroup2', 'FIXME'], matcharg(2))
18 call assert_equal(['MyGroup3', 'XXX'], matcharg(3))
19
20 " --- Check that "matcharg()" returns an empty list if the argument is not 1,
21 " --- 2 or 3 (only 0 and 4 are tested).
22 call assert_equal([], matcharg(0))
23 call assert_equal([], matcharg(4))
24
25 " --- Check that "matcharg()" returns ['', ''] if a match is not defined.
26 match
27 2match
28 3match
29 call assert_equal(['', ''], matcharg(1))
30 call assert_equal(['', ''], matcharg(2))
31 call assert_equal(['', ''], matcharg(3))
32
33 " --- Check that "matchadd()" and "getmatches()" agree on added matches and
34 " --- that default values apply.
35 let m1 = matchadd("MyGroup1", "TODO")
36 let m2 = matchadd("MyGroup2", "FIXME", 42)
37 let m3 = matchadd("MyGroup3", "XXX", 60, 17)
38 let ans = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 4},
39 \ {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 42, 'id': 5},
40 \ {'group': 'MyGroup3', 'pattern': 'XXX', 'priority': 60, 'id': 17}]
41 call assert_equal(ans, getmatches())
42
43 " --- Check that "matchdelete()" deletes the matches defined in the previous
44 " --- test correctly.
45 call matchdelete(m1)
46 call matchdelete(m2)
47 call matchdelete(m3)
48 call assert_equal([], getmatches())
49
50 " --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
51 let m = matchadd("MyGroup1", "TODO")
52 call assert_equal(0, matchdelete(m))
53 call assert_fails('call matchdelete(42)', 'E803:')
54
55 " --- Check that "clearmatches()" clears all matches defined by ":match" and
56 " --- "matchadd()".
57 let m1 = matchadd("MyGroup1", "TODO")
58 let m2 = matchadd("MyGroup2", "FIXME", 42)
59 let m3 = matchadd("MyGroup3", "XXX", 60, 17)
60 match MyGroup1 /COFFEE/
61 2match MyGroup2 /HUMPPA/
62 3match MyGroup3 /VIM/
63 call clearmatches()
64 call assert_equal([], getmatches())
65
66 " --- Check that "setmatches()" restores a list of matches saved by
67 " --- "getmatches()" without changes. (Matches with equal priority must also
68 " --- remain in the same order.)
69 let m1 = matchadd("MyGroup1", "TODO")
70 let m2 = matchadd("MyGroup2", "FIXME", 42)
71 let m3 = matchadd("MyGroup3", "XXX", 60, 17)
72 match MyGroup1 /COFFEE/
73 2match MyGroup2 /HUMPPA/
74 3match MyGroup3 /VIM/
75 let ml = getmatches()
76 call clearmatches()
77 call setmatches(ml)
78 call assert_equal(ml, getmatches())
79 call clearmatches()
80
81 " --- Check that "setmatches()" will not add two matches with the same ID. The
82 " --- expected behaviour (for now) is to add the first match but not the
83 " --- second and to return 0 (even though it is a matter of debate whether
84 " --- this can be considered successful behaviour).
85 let data = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1},
86 \ {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}]
87 call assert_fails('call setmatches(data)', 'E801:')
88 call assert_equal([data[0]], getmatches())
89 call clearmatches()
90
91 " --- Check that "setmatches()" returns 0 if successful and otherwise -1.
92 " --- (A range of valid and invalid input values are tried out to generate the
93 " --- return values.)
94 call assert_equal(0, setmatches([]))
95 call assert_equal(0, setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}]))
96 call clearmatches()
97 call assert_fails('call setmatches(0)', 'E714:')
98 call assert_fails('call setmatches([0])', 'E474:')
99 call assert_fails("call setmatches([{'wrong key': 'wrong value'}])", 'E474:')
100
101 call setline(1, 'abcdefghijklmnopq')
102 call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
103 1
104 redraw!
105 let v1 = screenattr(1, 1)
106 let v5 = screenattr(1, 5)
107 let v6 = screenattr(1, 6)
108 let v8 = screenattr(1, 8)
109 let v10 = screenattr(1, 10)
110 let v11 = screenattr(1, 11)
111 call assert_notequal(v1, v5)
112 call assert_equal(v6, v1)
113 call assert_equal(v8, v5)
114 call assert_equal(v10, v5)
115 call assert_equal(v11, v1)
116 call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
117 call clearmatches()
118
Bram Moolenaar30276f22019-01-24 17:59:39 +0100119 call setline(1, 'abcdΣabcdef')
120 call matchaddpos("MyGroup1", [[1, 4, 2], [1, 9, 2]])
121 1
122 redraw!
123 let v1 = screenattr(1, 1)
124 let v4 = screenattr(1, 4)
125 let v5 = screenattr(1, 5)
126 let v6 = screenattr(1, 6)
127 let v7 = screenattr(1, 7)
128 let v8 = screenattr(1, 8)
129 let v9 = screenattr(1, 9)
130 let v10 = screenattr(1, 10)
131 call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches())
132 call assert_notequal(v1, v4)
133 call assert_equal(v5, v4)
134 call assert_equal(v6, v1)
135 call assert_equal(v7, v1)
136 call assert_equal(v8, v4)
137 call assert_equal(v9, v4)
138 call assert_equal(v10, v1)
Bram Moolenaard76a0c12016-08-06 15:29:22 +0200139
Bram Moolenaar30276f22019-01-24 17:59:39 +0100140 " Check, that setmatches() can correctly restore the matches from matchaddpos()
141 call matchadd('MyGroup1', '\%2lmatchadd')
142 let m=getmatches()
143 call clearmatches()
144 call setmatches(m)
145 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 +0200146
147 highlight MyGroup1 NONE
148 highlight MyGroup2 NONE
149 highlight MyGroup3 NONE
150endfunc
151
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200152func Test_matchaddpos()
153 syntax on
154 set hlsearch
155
156 call setline(1, ['12345', 'NP'])
157 call matchaddpos('Error', [[1,2], [1,6], [2,2]])
158 redraw!
159 call assert_notequal(screenattr(2,2), 0)
160 call assert_equal(screenattr(2,2), screenattr(1,2))
161 call assert_notequal(screenattr(2,2), screenattr(1,6))
162 1
163 call matchadd('Search', 'N\|\n')
164 redraw!
165 call assert_notequal(screenattr(2,1), 0)
166 call assert_equal(screenattr(2,1), screenattr(1,6))
167 exec "norm! i0\<Esc>"
168 redraw!
169 call assert_equal(screenattr(2,2), screenattr(1,6))
170
Bram Moolenaara6c27ee2016-10-15 14:56:30 +0200171 " Check overlapping pos
172 call clearmatches()
173 call setline(1, ['1234567890', 'NH'])
174 call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
175 redraw!
176 call assert_notequal(screenattr(2,2), 0)
177 call assert_equal(screenattr(2,2), screenattr(1,5))
178 call assert_equal(screenattr(2,2), screenattr(1,7))
179 call assert_notequal(screenattr(2,2), screenattr(1,8))
180
Bram Moolenaar85077472016-10-16 14:35:48 +0200181 call clearmatches()
182 call matchaddpos('Error', [[1], [2,2]])
183 redraw!
184 call assert_equal(screenattr(2,2), screenattr(1,1))
185 call assert_equal(screenattr(2,2), screenattr(1,10))
186 call assert_notequal(screenattr(2,2), screenattr(1,11))
187
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200188 nohl
Bram Moolenaar85077472016-10-16 14:35:48 +0200189 call clearmatches()
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200190 syntax off
191 set hlsearch&
192endfunc
193
Bram Moolenaar95e51472018-07-28 16:55:56 +0200194func Test_matchaddpos_otherwin()
195 syntax on
196 new
197 call setline(1, ['12345', 'NP'])
198 let winid = win_getid()
199
200 wincmd w
201 call matchadd('Search', '4', 10, -1, {'window': winid})
202 call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
203 redraw!
204 call assert_notequal(screenattr(1,2), 0)
205 call assert_notequal(screenattr(1,4), 0)
206 call assert_notequal(screenattr(2,2), 0)
207 call assert_equal(screenattr(1,2), screenattr(2,2))
208 call assert_notequal(screenattr(1,2), screenattr(1,4))
209
Bram Moolenaaraff74912019-03-30 18:11:49 +0100210 let savematches = getmatches(winid)
211 let expect = [
212 \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4},
213 \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
214 \]
215 call assert_equal(expect, savematches)
216
217 call clearmatches(winid)
218 call assert_equal([], getmatches(winid))
219
220 call setmatches(savematches, winid)
221 call assert_equal(expect, savematches)
222
Bram Moolenaar95e51472018-07-28 16:55:56 +0200223 wincmd w
224 bwipe!
225 call clearmatches()
226 syntax off
227endfunc
228
Bram Moolenaare17bdff2016-08-27 18:34:29 +0200229func Test_matchaddpos_using_negative_priority()
230 set hlsearch
231
232 call clearmatches()
233
234 call setline(1, 'x')
235 let @/='x'
236 redraw!
237 let search_attr = screenattr(1,1)
238
239 let @/=''
240 call matchaddpos('Error', [1], 10)
241 redraw!
242 let error_attr = screenattr(1,1)
243
244 call setline(2, '-1 match priority')
245 call matchaddpos('Error', [2], -1)
246 redraw!
247 let negative_match_priority_attr = screenattr(2,1)
248
249 call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
250 call assert_equal(negative_match_priority_attr, error_attr)
251
252 nohl
253 set hlsearch&
254endfunc
255
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200256func OtherWindowCommon()
Bram Moolenaar06029a82019-07-24 14:25:26 +0200257 let lines =<< trim END
258 call setline(1, 'Hello Vim world')
259 let mid = matchadd('Error', 'world', 1)
260 let winid = win_getid()
261 new
262 END
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200263 call writefile(lines, 'XscriptMatchCommon')
264 let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
Bram Moolenaar06029a82019-07-24 14:25:26 +0200265 call term_wait(buf)
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200266 return buf
267endfunc
268
269func Test_matchdelete_other_window()
270 if !CanRunVimInTerminal()
271 throw 'Skipped: cannot make screendumps'
272 endif
273 let buf = OtherWindowCommon()
Bram Moolenaar06029a82019-07-24 14:25:26 +0200274 call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
275 call VerifyScreenDump(buf, 'Test_matchdelete_1', {})
276
277 call StopVimInTerminal(buf)
Bram Moolenaar4ef18dc2019-07-24 15:28:18 +0200278 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')
Bram Moolenaar06029a82019-07-24 14:25:26 +0200304endfunc
305
306
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200307" vim: shiftwidth=2 sts=2 expandtab