Bram Moolenaar | d76a0c1 | 2016-08-06 15:29:22 +0200 | [diff] [blame] | 1 | " Test for :match, :2match, :3match, clearmatches(), getmatches(), matchadd(), |
Bram Moolenaar | 1190cf6 | 2017-09-14 14:31:18 +0200 | [diff] [blame] | 2 | " matchaddpos(), matcharg(), matchdelete(), and setmatches(). |
Bram Moolenaar | d76a0c1 | 2016-08-06 15:29:22 +0200 | [diff] [blame] | 3 | |
Bram Moolenaar | 4f416e4 | 2016-08-16 16:08:18 +0200 | [diff] [blame] | 4 | function Test_match() |
Bram Moolenaar | d76a0c1 | 2016-08-06 15:29:22 +0200 | [diff] [blame] | 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 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 117 | call setline(1, 'abcdΣabcdef') |
| 118 | call matchaddpos("MyGroup1", [[1, 4, 2], [1, 9, 2]]) |
| 119 | 1 |
| 120 | redraw! |
| 121 | let v1 = screenattr(1, 1) |
| 122 | let v4 = screenattr(1, 4) |
| 123 | let v5 = screenattr(1, 5) |
| 124 | let v6 = screenattr(1, 6) |
| 125 | let v7 = screenattr(1, 7) |
| 126 | let v8 = screenattr(1, 8) |
| 127 | let v9 = screenattr(1, 9) |
| 128 | let v10 = screenattr(1, 10) |
| 129 | call assert_equal([{'group': 'MyGroup1', 'id': 11, 'priority': 10, 'pos1': [1, 4, 2], 'pos2': [1, 9, 2]}], getmatches()) |
| 130 | call assert_notequal(v1, v4) |
| 131 | call assert_equal(v5, v4) |
| 132 | call assert_equal(v6, v1) |
| 133 | call assert_equal(v7, v1) |
| 134 | call assert_equal(v8, v4) |
| 135 | call assert_equal(v9, v4) |
| 136 | call assert_equal(v10, v1) |
Bram Moolenaar | d76a0c1 | 2016-08-06 15:29:22 +0200 | [diff] [blame] | 137 | |
Bram Moolenaar | 30276f2 | 2019-01-24 17:59:39 +0100 | [diff] [blame] | 138 | " Check, that setmatches() can correctly restore the matches from matchaddpos() |
| 139 | call matchadd('MyGroup1', '\%2lmatchadd') |
| 140 | let m=getmatches() |
| 141 | call clearmatches() |
| 142 | call setmatches(m) |
| 143 | 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 Moolenaar | d76a0c1 | 2016-08-06 15:29:22 +0200 | [diff] [blame] | 144 | |
| 145 | highlight MyGroup1 NONE |
| 146 | highlight MyGroup2 NONE |
| 147 | highlight MyGroup3 NONE |
| 148 | endfunc |
| 149 | |
Bram Moolenaar | 4f416e4 | 2016-08-16 16:08:18 +0200 | [diff] [blame] | 150 | func Test_matchaddpos() |
| 151 | syntax on |
| 152 | set hlsearch |
| 153 | |
| 154 | call setline(1, ['12345', 'NP']) |
| 155 | call matchaddpos('Error', [[1,2], [1,6], [2,2]]) |
| 156 | redraw! |
| 157 | call assert_notequal(screenattr(2,2), 0) |
| 158 | call assert_equal(screenattr(2,2), screenattr(1,2)) |
| 159 | call assert_notequal(screenattr(2,2), screenattr(1,6)) |
| 160 | 1 |
| 161 | call matchadd('Search', 'N\|\n') |
| 162 | redraw! |
| 163 | call assert_notequal(screenattr(2,1), 0) |
| 164 | call assert_equal(screenattr(2,1), screenattr(1,6)) |
| 165 | exec "norm! i0\<Esc>" |
| 166 | redraw! |
| 167 | call assert_equal(screenattr(2,2), screenattr(1,6)) |
| 168 | |
Bram Moolenaar | a6c27ee | 2016-10-15 14:56:30 +0200 | [diff] [blame] | 169 | " Check overlapping pos |
| 170 | call clearmatches() |
| 171 | call setline(1, ['1234567890', 'NH']) |
| 172 | call matchaddpos('Error', [[1,1,5], [1,3,5], [2,2]]) |
| 173 | redraw! |
| 174 | call assert_notequal(screenattr(2,2), 0) |
| 175 | call assert_equal(screenattr(2,2), screenattr(1,5)) |
| 176 | call assert_equal(screenattr(2,2), screenattr(1,7)) |
| 177 | call assert_notequal(screenattr(2,2), screenattr(1,8)) |
| 178 | |
Bram Moolenaar | 8507747 | 2016-10-16 14:35:48 +0200 | [diff] [blame] | 179 | call clearmatches() |
| 180 | call matchaddpos('Error', [[1], [2,2]]) |
| 181 | redraw! |
| 182 | call assert_equal(screenattr(2,2), screenattr(1,1)) |
| 183 | call assert_equal(screenattr(2,2), screenattr(1,10)) |
| 184 | call assert_notequal(screenattr(2,2), screenattr(1,11)) |
| 185 | |
Bram Moolenaar | 4f416e4 | 2016-08-16 16:08:18 +0200 | [diff] [blame] | 186 | nohl |
Bram Moolenaar | 8507747 | 2016-10-16 14:35:48 +0200 | [diff] [blame] | 187 | call clearmatches() |
Bram Moolenaar | 4f416e4 | 2016-08-16 16:08:18 +0200 | [diff] [blame] | 188 | syntax off |
| 189 | set hlsearch& |
| 190 | endfunc |
| 191 | |
Bram Moolenaar | 95e5147 | 2018-07-28 16:55:56 +0200 | [diff] [blame] | 192 | func Test_matchaddpos_otherwin() |
| 193 | syntax on |
| 194 | new |
| 195 | call setline(1, ['12345', 'NP']) |
| 196 | let winid = win_getid() |
| 197 | |
| 198 | wincmd w |
| 199 | call matchadd('Search', '4', 10, -1, {'window': winid}) |
| 200 | call matchaddpos('Error', [[1,2], [2,2]], 10, -1, {'window': winid}) |
| 201 | redraw! |
| 202 | call assert_notequal(screenattr(1,2), 0) |
| 203 | call assert_notequal(screenattr(1,4), 0) |
| 204 | call assert_notequal(screenattr(2,2), 0) |
| 205 | call assert_equal(screenattr(1,2), screenattr(2,2)) |
| 206 | call assert_notequal(screenattr(1,2), screenattr(1,4)) |
| 207 | |
Bram Moolenaar | aff7491 | 2019-03-30 18:11:49 +0100 | [diff] [blame] | 208 | let savematches = getmatches(winid) |
| 209 | let expect = [ |
| 210 | \ {'group': 'Search', 'pattern': '4', 'priority': 10, 'id': 4}, |
| 211 | \ {'group': 'Error', 'id': 5, 'priority': 10, 'pos1': [1, 2, 1], 'pos2': [2, 2, 1]}, |
| 212 | \] |
| 213 | call assert_equal(expect, savematches) |
| 214 | |
| 215 | call clearmatches(winid) |
| 216 | call assert_equal([], getmatches(winid)) |
| 217 | |
| 218 | call setmatches(savematches, winid) |
| 219 | call assert_equal(expect, savematches) |
| 220 | |
Bram Moolenaar | 95e5147 | 2018-07-28 16:55:56 +0200 | [diff] [blame] | 221 | wincmd w |
| 222 | bwipe! |
| 223 | call clearmatches() |
| 224 | syntax off |
| 225 | endfunc |
| 226 | |
Bram Moolenaar | e17bdff | 2016-08-27 18:34:29 +0200 | [diff] [blame] | 227 | func Test_matchaddpos_using_negative_priority() |
| 228 | set hlsearch |
| 229 | |
| 230 | call clearmatches() |
| 231 | |
| 232 | call setline(1, 'x') |
| 233 | let @/='x' |
| 234 | redraw! |
| 235 | let search_attr = screenattr(1,1) |
| 236 | |
| 237 | let @/='' |
| 238 | call matchaddpos('Error', [1], 10) |
| 239 | redraw! |
| 240 | let error_attr = screenattr(1,1) |
| 241 | |
| 242 | call setline(2, '-1 match priority') |
| 243 | call matchaddpos('Error', [2], -1) |
| 244 | redraw! |
| 245 | let negative_match_priority_attr = screenattr(2,1) |
| 246 | |
| 247 | call assert_notequal(negative_match_priority_attr, search_attr, "Match with negative priority is incorrectly highlighted with Search highlight.") |
| 248 | call assert_equal(negative_match_priority_attr, error_attr) |
| 249 | |
| 250 | nohl |
| 251 | set hlsearch& |
| 252 | endfunc |
| 253 | |
Bram Moolenaar | 9e4d821 | 2016-08-18 23:04:48 +0200 | [diff] [blame] | 254 | " vim: shiftwidth=2 sts=2 expandtab |