xref: /vim-8.2.3635/src/testdir/test_match.vim (revision 95bafa29)
1" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
2" matchaddpos(), matcharg(), matchdelete(), and setmatches().
3
4function Test_match()
5  highlight MyGroup1 term=bold ctermbg=red guibg=red
6  highlight MyGroup2 term=italic ctermbg=green guibg=green
7  highlight MyGroup3 term=underline ctermbg=blue guibg=blue
8
9  " --- Check that "matcharg()" returns the correct group and pattern if a match
10  " --- is defined.
11  match MyGroup1 /TODO/
12  2match MyGroup2 /FIXME/
13  3match MyGroup3 /XXX/
14  call assert_equal(['MyGroup1', 'TODO'], matcharg(1))
15  call assert_equal(['MyGroup2', 'FIXME'], matcharg(2))
16  call assert_equal(['MyGroup3', 'XXX'], matcharg(3))
17
18  " --- Check that "matcharg()" returns an empty list if the argument is not 1,
19  " --- 2 or 3 (only 0 and 4 are tested).
20  call assert_equal([], matcharg(0))
21  call assert_equal([], matcharg(4))
22
23  " --- Check that "matcharg()" returns ['', ''] if a match is not defined.
24  match
25  2match
26  3match
27  call assert_equal(['', ''], matcharg(1))
28  call assert_equal(['', ''], matcharg(2))
29  call assert_equal(['', ''], matcharg(3))
30
31  " --- Check that "matchadd()" and "getmatches()" agree on added matches and
32  " --- that default values apply.
33  let m1 = matchadd("MyGroup1", "TODO")
34  let m2 = matchadd("MyGroup2", "FIXME", 42)
35  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
36  let ans = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 4},
37        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 42, 'id': 5},
38        \    {'group': 'MyGroup3', 'pattern': 'XXX', 'priority': 60, 'id': 17}]
39  call assert_equal(ans, getmatches())
40
41  " --- Check that "matchdelete()" deletes the matches defined in the previous
42  " --- test correctly.
43  call matchdelete(m1)
44  call matchdelete(m2)
45  call matchdelete(m3)
46  call assert_equal([], getmatches())
47
48  " --- Check that "matchdelete()" returns 0 if successful and otherwise -1.
49  let m = matchadd("MyGroup1", "TODO")
50  call assert_equal(0, matchdelete(m))
51  call assert_fails('call matchdelete(42)', 'E803:')
52
53  " --- Check that "clearmatches()" clears all matches defined by ":match" and
54  " --- "matchadd()".
55  let m1 = matchadd("MyGroup1", "TODO")
56  let m2 = matchadd("MyGroup2", "FIXME", 42)
57  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
58  match MyGroup1 /COFFEE/
59  2match MyGroup2 /HUMPPA/
60  3match MyGroup3 /VIM/
61  call clearmatches()
62  call assert_equal([], getmatches())
63
64  " --- Check that "setmatches()" restores a list of matches saved by
65  " --- "getmatches()" without changes. (Matches with equal priority must also
66  " --- remain in the same order.)
67  let m1 = matchadd("MyGroup1", "TODO")
68  let m2 = matchadd("MyGroup2", "FIXME", 42)
69  let m3 = matchadd("MyGroup3", "XXX", 60, 17)
70  match MyGroup1 /COFFEE/
71  2match MyGroup2 /HUMPPA/
72  3match MyGroup3 /VIM/
73  let ml = getmatches()
74  call clearmatches()
75  call setmatches(ml)
76  call assert_equal(ml, getmatches())
77  call clearmatches()
78
79  " --- Check that "setmatches()" will not add two matches with the same ID. The
80  " --- expected behaviour (for now) is to add the first match but not the
81  " --- second and to return 0 (even though it is a matter of debate whether
82  " --- this can be considered successful behaviour).
83  let data = [{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1},
84        \    {'group': 'MyGroup2', 'pattern': 'FIXME', 'priority': 10, 'id': 1}]
85  call assert_fails('call setmatches(data)', 'E801:')
86  call assert_equal([data[0]], getmatches())
87  call clearmatches()
88
89  " --- Check that "setmatches()" returns 0 if successful and otherwise -1.
90  " --- (A range of valid and invalid input values are tried out to generate the
91  " --- return values.)
92  call assert_equal(0, setmatches([]))
93  call assert_equal(0, setmatches([{'group': 'MyGroup1', 'pattern': 'TODO', 'priority': 10, 'id': 1}]))
94  call clearmatches()
95  call assert_fails('call setmatches(0)', 'E714:')
96  call assert_fails('call setmatches([0])', 'E474:')
97  call assert_fails("call setmatches([{'wrong key': 'wrong value'}])", 'E474:')
98
99  call setline(1, 'abcdefghijklmnopq')
100  call matchaddpos("MyGroup1", [[1, 5], [1, 8, 3]], 10, 3)
101  1
102  redraw!
103  let v1 = screenattr(1, 1)
104  let v5 = screenattr(1, 5)
105  let v6 = screenattr(1, 6)
106  let v8 = screenattr(1, 8)
107  let v10 = screenattr(1, 10)
108  let v11 = screenattr(1, 11)
109  call assert_notequal(v1, v5)
110  call assert_equal(v6, v1)
111  call assert_equal(v8, v5)
112  call assert_equal(v10, v5)
113  call assert_equal(v11, v1)
114  call assert_equal([{'group': 'MyGroup1', 'id': 3, 'priority': 10, 'pos1': [1, 5, 1], 'pos2': [1, 8, 3]}], getmatches())
115  call clearmatches()
116
117  "
118  if has('multi_byte')
119    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)
139
140    " 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())
146  endif
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  wincmd w
212  bwipe!
213  call clearmatches()
214  syntax off
215endfunc
216
217func Test_matchaddpos_using_negative_priority()
218  set hlsearch
219
220  call clearmatches()
221
222  call setline(1, 'x')
223  let @/='x'
224  redraw!
225  let search_attr = screenattr(1,1)
226
227  let @/=''
228  call matchaddpos('Error', [1], 10)
229  redraw!
230  let error_attr = screenattr(1,1)
231
232  call setline(2, '-1 match priority')
233  call matchaddpos('Error', [2], -1)
234  redraw!
235  let negative_match_priority_attr = screenattr(2,1)
236
237  call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
238  call assert_equal(negative_match_priority_attr, error_attr)
239
240  nohl
241  set hlsearch&
242endfunc
243
244" vim: shiftwidth=2 sts=2 expandtab
245