blob: 9ac1db157f9986dc85f85fe3121390e620e679ec [file] [log] [blame]
Bram Moolenaard76a0c12016-08-06 15:29:22 +02001" Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(),
2" matchaddpos(), matcharg(), matchdelete(), matchstrpos() and setmatches().
3
Bram Moolenaar4f416e42016-08-16 16:08:18 +02004function Test_match()
Bram Moolenaard76a0c12016-08-06 15:29:22 +02005 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_matchstrpos()
154 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
155
156 call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
157
158 call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
159
160 call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
161
162 call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
163endfunc
164
Bram Moolenaar4f416e42016-08-16 16:08:18 +0200165func Test_matchaddpos()
166 syntax on
167 set hlsearch
168
169 call setline(1, ['12345', 'NP'])
170 call matchaddpos('Error', [[1,2], [1,6], [2,2]])
171 redraw!
172 call assert_notequal(screenattr(2,2), 0)
173 call assert_equal(screenattr(2,2), screenattr(1,2))
174 call assert_notequal(screenattr(2,2), screenattr(1,6))
175 1
176 call matchadd('Search', 'N\|\n')
177 redraw!
178 call assert_notequal(screenattr(2,1), 0)
179 call assert_equal(screenattr(2,1), screenattr(1,6))
180 exec "norm! i0\<Esc>"
181 redraw!
182 call assert_equal(screenattr(2,2), screenattr(1,6))
183
184 nohl
185 syntax off
186 set hlsearch&
187endfunc
188
Bram Moolenaare17bdff2016-08-27 18:34:29 +0200189func Test_matchaddpos_using_negative_priority()
190 set hlsearch
191
192 call clearmatches()
193
194 call setline(1, 'x')
195 let @/='x'
196 redraw!
197 let search_attr = screenattr(1,1)
198
199 let @/=''
200 call matchaddpos('Error', [1], 10)
201 redraw!
202 let error_attr = screenattr(1,1)
203
204 call setline(2, '-1 match priority')
205 call matchaddpos('Error', [2], -1)
206 redraw!
207 let negative_match_priority_attr = screenattr(2,1)
208
209 call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.")
210 call assert_equal(negative_match_priority_attr, error_attr)
211
212 nohl
213 set hlsearch&
214endfunc
215
Bram Moolenaar9e4d8212016-08-18 23:04:48 +0200216" vim: shiftwidth=2 sts=2 expandtab