summaryrefslogtreecommitdiff
path: root/src/testdir/test_match.vim
blob: 869ad6c62efd737e193ee64afa66b4d5282b8f5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
" matchaddpos(), matcharg(), matchdelete(), and setmatches().

source screendump.vim
source check.vim

function Test_match()
  highlight MyGroup1 term=bold ctermbg=red guibg=red
  highlight MyGroup2 term=italic ctermbg=green guibg=green
  highlight MyGroup3 term=underline ctermbg=blue guibg=blue

  " --- Check that "matcharg()" returns the correct group and pattern if a match
  " --- is defined.
  match MyGroup1 /TODO/
  2match MyGroup2 /FIXME/
  3match MyGroup3 /XXX/
  call assert_equal(['MyGroup1', 'TODO'], matcharg(1))
  call assert_equal(['MyGroup2', 'FIXME'], 2->matcharg())
  call assert_equal(['MyGroup3', 'XXX'], matcharg(3))

  " --- Check that "matcharg()" returns an empty list if the argument is not 1,
  " --- 2 or 3 (only 0 and 4 are tested).
  call assert_equal([], matcharg(0))
  call assert_equal([], matcharg(4))

  " --- Check that "matcharg()" returns ['', ''] if a match is not defined.
  match
  2match
  3match
  call assert_equal(['', ''], matcharg(1))
  call assert_equal(['', ''], matcharg(2))
  call assert_equal(['', ''], matcharg(3))

  " --- Check that "matchadd()" and "getmatches()" agree on added matches and
  " --- that default values apply.
  let m1 = matchadd("MyGroup1", "TODO")
  let m2 = matchadd("MyGroup2", "FIXME", 42)
  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  let ans = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 4},
        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 42, 'id': 5},
        \    {'group': 'MyGroup3', 'pattern': 'XXX', 'priority': 60, 'id': 17}]
  call assert_equal(ans, getmatches())

  " --- Check that "matchdelete()" deletes the matches defined in the previous
  " --- test correctly.
  call matchdelete(m1)
  eval m2->matchdelete()
  call matchdelete(m3)
  call assert_equal([], getmatches())

  " --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
  let m = matchadd("MyGroup1", "TODO")
  call assert_equal(0, matchdelete(m))
  call assert_fails('call matchdelete(42)', 'E803:')

  " --- Check that "clearmatches()" clears all matches defined by ":match" and
  " --- "matchadd()".
  let m1 = matchadd("MyGroup1", "TODO")
  let m2 = "MyGroup2"->matchadd("FIXME", 42)
  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  match MyGroup1 /COFFEE/
  2match MyGroup2 /HUMPPA/
  3match MyGroup3 /VIM/
  call clearmatches()
  call assert_equal([], getmatches())

  " --- Check that "setmatches()" restores a list of matches saved by
  " --- "getmatches()" without changes. (Matches with equal priority must also
  " --- remain in the same order.)
  let m1 = matchadd("MyGroup1", "TODO")
  let m2 = matchadd("MyGroup2", "FIXME", 42)
  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
  match MyGroup1 /COFFEE/
  2match MyGroup2 /HUMPPA/
  3match MyGroup3 /VIM/
  let ml = getmatches()
  call clearmatches()
  call setmatches(ml)
  call assert_equal(ml, getmatches())
  call clearmatches()

  " --- Check that "setmatches()" will not add two matches with the same ID. The
  " --- expected behaviour (for now) is to add the first match but not the
  " --- second and to return 0 (even though it is a matter of debate whether
  " --- this can be considered successful behaviour).
  let data = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1},
        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}]
  call assert_fails('call setmatches(data)', 'E801:')
  call assert_equal([data[0]], getmatches())
  call clearmatches()

  " --- Check that "setmatches()" returns 0 if successful and otherwise -1.
  " --- (A range of valid and invalid input values are tried out to generate the
  " --- return values.)
  call assert_equal(0, setmatches([]))
  call assert_equal(0, setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}]))
  call clearmatches()
  call assert_fails('call setmatches(0)', 'E714:')
  call assert_fails('call setmatches([0])', 'E474:')
  call assert_fails("call setmatches([{'wrong key': 'wrong value'}])", 'E474:')
  call assert_equal(-1, setmatches([{'group' : 'Search', 'priority' : 10, 'id' : 5, 'pos1' : {}}]))

  call setline(1, 'abcdefghijklmnopq')
  call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
  1
  redraw!
  let v1 = screenattr(1, 1)
  let v5 = screenattr(1, 5)
  let v6 = screenattr(1, 6)
  let v8 = screenattr(1, 8)
  let v10 = screenattr(1, 10)
  let v11 = screenattr(1, 11)
  call assert_notequal(v1, v5)
  call assert_equal(v6, v1)
  call assert_equal(v8, v5)
  call assert_equal(v10, v5)
  call assert_equal(v11, v1)
  call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
  call clearmatches()

  call setline(1, 'abcdΣabcdef')
  eval "MyGroup1"->matchaddpos([[1, 4, 2], [1, 9, 2]])
  1
  redraw!
  let v1 = screenattr(1, 1)
  let v4 = screenattr(1, 4)
  let v5 = screenattr(1, 5)
  let v6 = screenattr(1, 6)
  let v7 = screenattr(1, 7)
  let v8 = screenattr(1, 8)
  let v9 = screenattr(1, 9)
  let v10 = screenattr(1, 10)
  call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches())
  call assert_notequal(v1, v4)
  call assert_equal(v5, v4)
  call assert_equal(v6, v1)
  call assert_equal(v7, v1)
  call assert_equal(v8, v4)
  call assert_equal(v9, v4)
  call assert_equal(v10, v1)

  " Check, that setmatches() can correctly restore the matches from matchaddpos()
  call matchadd('MyGroup1', '\%2lmatchadd')
  let m=getmatches()
  call clearmatches()
  call setmatches(m)
  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())

  highlight MyGroup1 NONE
  highlight MyGroup2 NONE
  highlight MyGroup3 NONE
endfunc

func Test_match_error()
  call assert_fails('match Error', 'E475:')
  call assert_fails('match Error /', 'E475:')
  call assert_fails('4match Error /x/', 'E476:')
  call assert_fails('match Error /x/ x', 'E488:')
endfunc

func Test_matchadd_error()
  call assert_fails("call matchadd('GroupDoesNotExist', 'X')", 'E28:')
  call assert_fails("call matchadd('Search', '\\(')", 'E54:')
  call assert_fails("call matchadd('Search', 'XXX', 1, 123, 1)", 'E715:')
  call assert_fails("call matchadd('Error', 'XXX', 1, 3)", 'E798:')
  call assert_fails("call matchadd('Error', 'XXX', 1, 0)", 'E799:')
  call assert_fails("call matchadd('Error', 'XXX', [], 0)", 'E745:')
  call assert_equal(-1, matchadd('', 'pat'))
  call assert_equal(-1, matchadd('Search', ''))
endfunc

func Test_matchaddpos()
  syntax on
  set hlsearch

  call setline(1, ['12345', 'NP'])
  call matchaddpos('Error', [[1,2], [1,6], [2,2]])
  redraw!
  call assert_notequal(screenattr(2,2), 0)
  call assert_equal(screenattr(2,2), screenattr(1,2))
  call assert_notequal(screenattr(2,2), screenattr(1,6))
  1
  call matchadd('Search', 'N\|\n')
  redraw!
  call assert_notequal(screenattr(2,1), 0)
  call assert_equal(screenattr(2,1), screenattr(1,6))
  exec "norm! i0\<Esc>"
  redraw!
  call assert_equal(screenattr(2,2), screenattr(1,6))

  " Check overlapping pos
  call clearmatches()
  call setline(1, ['1234567890', 'NH'])
  call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]])
  redraw!
  call assert_notequal(screenattr(2,2), 0)
  call assert_equal(screenattr(2,2), screenattr(1,5))
  call assert_equal(screenattr(2,2), screenattr(1,7))
  call assert_notequal(screenattr(2,2), screenattr(1,8))

  call clearmatches()
  call matchaddpos('Error', [[1], [2,2]])
  redraw!
  call assert_equal(screenattr(2,2), screenattr(1,1))
  call assert_equal(screenattr(2,2), screenattr(1,10))
  call assert_notequal(screenattr(2,2), screenattr(1,11))

  " matchaddpos() with line number as 0
  call clearmatches()
  let id = matchaddpos('Search', [[0], [3], [0]])
  call assert_equal([{'group' : 'Search', 'priority' : 10, 'id' : id, 'pos1' : [3]}], getmatches())
  call clearmatches()
  let id = matchaddpos('Search', [0, 3, 0])
  call assert_equal([{'group' : 'Search', 'priority' : 10, 'id' : id, 'pos1' : [3]}], getmatches())

  nohl
  call clearmatches()
  syntax off
  set hlsearch&
endfunc

func Test_matchaddpos_otherwin()
  syntax on
  new
  call setline(1, ['12345', 'NP'])
  let winid = win_getid()

  wincmd w
  call matchadd('Search', '4', 10, -1, {'window': winid})
  call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid})
  redraw!
  call assert_notequal(screenattr(1,2), 0)
  call assert_notequal(screenattr(1,4), 0)
  call assert_notequal(screenattr(2,2), 0)
  call assert_equal(screenattr(1,2), screenattr(2,2))
  call assert_notequal(screenattr(1,2), screenattr(1,4))

  let savematches = getmatches(winid)
  let expect = [
        \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4},
        \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]},
        \]
  call assert_equal(expect, savematches)

  eval winid->clearmatches()
  call assert_equal([], getmatches(winid))
  call assert_fails('echo getmatches(-1)', 'E957:')

  call setmatches(savematches, winid)
  call assert_equal(expect, savematches)

  wincmd w
  bwipe!
  call clearmatches()
  syntax off
endfunc

func Test_matchaddpos_using_negative_priority()
  set hlsearch

  call clearmatches()

  call setline(1, 'x')
  let @/='x'
  redraw!
  let search_attr = screenattr(1,1)

  let @/=''
  call matchaddpos('Error', [1], 10)
  redraw!
  let error_attr = screenattr(1,1)

  call setline(2, '-1 match priority')
  call matchaddpos('Error', [2], -1)
  redraw!
  let negative_match_priority_attr = screenattr(2,1)

  call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
  call assert_equal(negative_match_priority_attr, error_attr)

  nohl
  set hlsearch&
endfunc

func Test_matchaddpos_error()
  call assert_fails("call matchaddpos('Error', 1)", 'E686:')
  call assert_fails("call matchaddpos('Error', [1], 1, 1)", 'E798:')
  call assert_fails("call matchaddpos('Error', [1], 1, 2)", 'E798:')
  call assert_fails("call matchaddpos('Error', [1], 1, 0)", 'E799:')
  call assert_fails("call matchaddpos('Error', [1], 1, 123, 1)", 'E715:')
  call assert_fails("call matchaddpos('Error', [1], 1, 5, {'window':12345})", 'E957:')
  " Why doesn't the following error have an error code E...?
  call assert_fails("call matchaddpos('Error', [{}])", 'E290:')
  call assert_equal(-1, matchaddpos('Error', test_null_list()))
  call assert_fails("call matchaddpos('Error', [1], [], 1)", 'E745:')
  call assert_equal(-1, matchaddpos('Search', [[]]))
  call assert_fails("call matchaddpos('Search', [[{}]])", 'E728:')
  call assert_fails("call matchaddpos('Search', [[2, {}]])", 'E728:')
  call assert_fails("call matchaddpos('Search', [[3, 4, {}]])", 'E728:')
endfunc

func OtherWindowCommon()
  let lines =<< trim END
    call setline(1, 'Hello Vim world')
    let mid = matchadd('Error', 'world', 1)
    let winid = win_getid()
    new
  END
  call writefile(lines, 'XscriptMatchCommon')
  let buf = RunVimInTerminal('-S XscriptMatchCommon', #{rows: 12})
  call TermWait(buf)
  return buf
endfunc

func Test_matchdelete_other_window()
  CheckScreendump

  let buf = OtherWindowCommon()
  call term_sendkeys(buf, ":call matchdelete(mid, winid)\<CR>")
  call VerifyScreenDump(buf, 'Test_matchdelete_1', {})

  call StopVimInTerminal(buf)
  call delete('XscriptMatchCommon')
endfunc

func Test_matchdelete_error()
  call assert_fails("call matchdelete(0)", 'E802:')
  call assert_fails("call matchdelete(1, -1)", 'E957:')
endfunc

func Test_matchclear_other_window()
  CheckRunVimInTerminal
  let buf = OtherWindowCommon()
  call term_sendkeys(buf, ":call clearmatches(winid)\<CR>")
  call VerifyScreenDump(buf, 'Test_matchclear_1', {})

  call StopVimInTerminal(buf)
  call delete('XscriptMatchCommon')
endfunc

func Test_matchadd_other_window()
  CheckRunVimInTerminal
  let buf = OtherWindowCommon()
  call term_sendkeys(buf, ":call matchadd('Search', 'Hello', 1, -1, #{window: winid})\<CR>")
  call term_sendkeys(buf, ":\<CR>")
  call VerifyScreenDump(buf, 'Test_matchadd_1', {})

  call StopVimInTerminal(buf)
  call delete('XscriptMatchCommon')
endfunc

" Test for deleting matches outside of the screen redraw top/bottom lines
" This should cause a redraw of those lines.
func Test_matchdelete_redraw()
  new
  call setline(1, range(1, 500))
  call cursor(250, 1)
  let m1 = matchaddpos('Search', [[250]])
  let m2 = matchaddpos('Search', [[10], [450]])
  redraw!
  let m3 = matchaddpos('Search', [[240], [260]])
  call matchdelete(m2)
  let m = getmatches()
  call assert_equal(2, len(m))
  call assert_equal([250], m[0].pos1)
  redraw!
  call matchdelete(m1)
  call assert_equal(1, len(getmatches()))
  bw!
endfunc

" vim: shiftwidth=2 sts=2 expandtab