]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/test/test_highlight_placement.vader

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Do not set EDITOR/VISUAL for shell
[etc/vim.git] / .vim / bundle / ale / test / test_highlight_placement.vader
1 Before:
2   Save g:ale_buffer_info
3   Save g:ale_echo_cursor
4   Save g:ale_enabled
5   Save g:ale_run_synchronously
6   Save g:ale_set_highlights
7   Save g:ale_set_loclist
8   Save g:ale_set_quickfix
9   Save g:ale_set_signs
10   Save g:ale_exclude_highlights
11   Save b:ale_exclude_highlights
12
13   runtime autoload/ale/virtualtext.vim
14   runtime autoload/ale/highlight.vim
15
16   let g:ale_run_synchronously = 1
17   unlet! g:ale_run_synchronously_callbacks
18   let g:ale_set_highlights = 1
19   let g:ale_set_signs = 1
20   let g:ale_buffer_info = {}
21
22   " Disable features we don't need for these tests.
23   let g:ale_set_quickfix = 0
24   let g:ale_set_loclist = 0
25   let g:ale_echo_cursor = 0
26   let g:ale_exclude_highlights = []
27   let b:ale_exclude_highlights = []
28
29   function! GenerateResults(buffer, output)
30     return [
31     \ {
32     \   'lnum': 1,
33     \   'col': 1,
34     \   'type': 'E',
35     \   'text': 'foo',
36     \ },
37     \ {
38     \   'lnum': 2,
39     \   'col': 1,
40     \   'type': 'W',
41     \   'text': 'bar',
42     \ },
43     \ {
44     \   'lnum': 3,
45     \   'col': 5,
46     \   'type': 'E',
47     \   'text': 'wat',
48     \ },
49     \]
50   endfunction
51
52   let g:has_nvim_highlight = exists('*nvim_buf_add_highlight') && exists('*nvim_buf_clear_namespace')
53   let g:nvim_highlight_matches = {}
54
55   function! ale#highlight#nvim_buf_clear_namespace(buffer, ns_id, line_start, line_end) abort
56     if a:line_end != -1
57       throw 'nvim api behavior not supported'
58     endif
59
60     let l:matches = get(g:nvim_highlight_matches, a:buffer, [])
61     call filter(
62     \ l:matches,
63     \ {_, val -> val.pos1[0] < (a:line_start + 1) },
64     \)
65   endfunction
66
67   function! ale#highlight#nvim_buf_add_highlight(buffer, ns_id, hl_group, line, col_start, col_end) abort
68     if a:col_end == -1
69       throw 'nvim api behavior not supported'
70     endif
71
72     let l:matches = get(g:nvim_highlight_matches, a:buffer, [])
73     let g:nvim_highlight_matches[a:buffer] = l:matches
74
75     let l:new_match = {
76     \ 'group': a:hl_group,
77     \ 'priority': 10,
78     \ 'pos1': [a:line + 1, a:col_start + 1, a:col_end - a:col_start],
79     \}
80
81     call add(l:matches, l:new_match)
82     " sort by line number to emulate getmatches faithfully
83     call sort(l:matches, {m1, m2 -> m1.pos1[0] - m2.pos1[0]})
84   endfunction
85
86   " We don't care what the IDs are, just that we have some matches.
87   " The IDs are generated.
88   function! GetMatchesWithoutIDs() abort
89     if g:has_nvim_highlight
90       return get(g:nvim_highlight_matches, bufnr(''), [])
91     else
92       let l:list = getmatches()
93
94       for l:item in l:list
95         call remove(l:item, 'id')
96       endfor
97
98       return l:list
99     endif
100   endfunction
101
102   function! GetLinkedGroup(grp) abort
103     return synIDattr(synIDtrans(hlID(a:grp)), 'name')
104   endfunction
105
106   call ale#linter#Define('testft', {
107   \ 'name': 'x',
108   \ 'executable': has('win32') ? 'cmd': 'echo',
109   \ 'command': has('win32') ? 'echo' : '/bin/sh -c ''echo''',
110   \ 'callback': 'GenerateResults',
111   \})
112   highlight link SomeOtherGroup SpellBad
113
114 After:
115   Restore
116
117   unlet! g:ale_run_synchronously_callbacks
118   unlet! g:items
119   unlet! b:ale_enabled
120   unlet! g:has_nvim_highlight
121   unlet! g:nvim_highlight_matches
122
123   delfunction GenerateResults
124   call ale#linter#Reset()
125   call clearmatches()
126   call ale#sign#Clear()
127   if has('textprop') && has('popupwin')
128     call prop_type_delete('ale')
129   endif
130   highlight clear SomeOtherGroup
131
132   runtime autoload/ale/highlight.vim
133
134 Given testft(A Javscript file with warnings/errors):
135   foo
136   bar
137   baz wat
138   line four
139
140 " Autoloading virtualtext.vim first should still properly initialize hl-groups
141 Execute(Loading virtualtext first does not break highlight groups):
142   AssertEqual
143   \ "SpellBad",
144   \ GetLinkedGroup("ALEError")
145   AssertEqual
146   \ "SpellCap",
147   \ GetLinkedGroup("ALEWarning")
148
149 Execute(Highlights should be set when a linter runs):
150   ALELint
151   call ale#test#FlushJobs()
152
153   AssertEqual
154   \ [
155   \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
156   \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [2, 1, 1]},
157   \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 5, 1]}
158   \ ],
159   \ GetMatchesWithoutIDs()
160
161 " This test is important for preventing ALE from showing highlights for
162 " the wrong files.
163 Execute(Highlights set by ALE should be removed when buffer cleanup is done):
164   call ale#engine#InitBufferInfo(bufnr('%'))
165
166   call ale#highlight#SetHighlights(bufnr('%'), [
167   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 3, 'col': 2},
168   \])
169
170   if !g:has_nvim_highlight
171     " This check doesn't work with the new API, for some reason.
172     AssertEqual
173     \ [{'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 1]}],
174     \ GetMatchesWithoutIDs()
175   endif
176
177   call ale#engine#Cleanup(bufnr('%'))
178
179   AssertEqual [], GetMatchesWithoutIDs()
180
181 Execute(Highlights should be cleared when buffers are hidden):
182   call ale#engine#InitBufferInfo(bufnr('%'))
183   " The second item should be ignored, as it has no column infomration.
184   let g:ale_buffer_info[bufnr('%')].loclist =  [
185   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 3, 'col': 2},
186   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 4, 'col': 0},
187   \]
188   call ale#highlight#SetHighlights(
189   \ bufnr('%'),
190   \ g:ale_buffer_info[bufnr('%')].loclist
191   \)
192
193   AssertEqual 1, len(GetMatchesWithoutIDs()), 'The highlights weren''t initially set!'
194
195   call ale#highlight#BufferHidden(bufnr('%'))
196
197   AssertEqual 0, len(GetMatchesWithoutIDs()), 'The highlights weren''t cleared!'
198
199   call ale#highlight#UpdateHighlights()
200
201   AssertEqual 1, len(GetMatchesWithoutIDs()), 'The highlights weren''t set again!'
202
203 Execute(Only ALE highlights should be restored when buffers are restored):
204   call ale#engine#InitBufferInfo(bufnr('%'))
205   let g:ale_buffer_info[bufnr('%')].loclist =  [
206   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 3, 'col': 2},
207   \]
208   call ale#highlight#SetHighlights(
209   \ bufnr('%'),
210   \ g:ale_buffer_info[bufnr('%')].loclist
211   \)
212
213   call matchaddpos('SomeOtherGroup', [[1, 1, 1]])
214
215   " We should have both highlights.
216   if g:has_nvim_highlight
217     " When the newer NeoVim API is used, we don't have to worry about
218     " other highlights, namespacing is available.
219     AssertEqual
220     \ [
221     \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 1]},
222     \ ],
223     \ GetMatchesWithoutIDs()
224   else
225     AssertEqual
226     \ [
227     \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 1]},
228     \   {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]},
229     \ ],
230     \ sort(GetMatchesWithoutIDs(), {m1, m2 -> m1.group < m2.group ? -1 : 1})
231   endif
232
233   call ale#highlight#BufferHidden(bufnr('%'))
234
235   " We should remove our highlight, but not the other one.
236   if g:has_nvim_highlight
237     AssertEqual [], GetMatchesWithoutIDs()
238   else
239     AssertEqual
240     \ [
241     \   {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]}
242     \ ],
243     \ GetMatchesWithoutIDs()
244   endif
245
246   call ale#highlight#UpdateHighlights()
247
248   " Our highlight should apper again.
249   if g:has_nvim_highlight
250     AssertEqual
251     \ [
252     \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 1]},
253     \ ],
254     \ GetMatchesWithoutIDs()
255   else
256     AssertEqual
257     \ [
258     \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 1]},
259     \   {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]},
260     \ ],
261     \ sort(GetMatchesWithoutIDs(), {m1, m2 -> m1.group < m2.group ? -1 : 1})
262   endif
263
264 Execute(Highlight end columns should set an appropriate size):
265   call ale#highlight#SetHighlights(bufnr('%'), [
266   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 3, 'col': 2, 'end_col': 5},
267   \ {'bufnr': bufnr('%'), 'type': 'W', 'lnum': 4, 'col': 1, 'end_col': 5},
268   \])
269
270   AssertEqual
271   \ [
272   \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 2, 4]},
273   \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [4, 1, 5]},
274   \ ],
275   \ GetMatchesWithoutIDs()
276
277 Execute(Highlight end columns should set an appropriate size):
278   call ale#highlight#SetHighlights(bufnr('%'), [
279   \ {'bufnr': bufnr('%') - 1, 'type': 'E', 'lnum': 1, 'col': 1},
280   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 1, 'col': 1},
281   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 2, 'col': 1},
282   \ {'bufnr': bufnr('%'), 'type': 'E', 'sub_type': 'style', 'lnum': 3, 'col': 1},
283   \ {'bufnr': bufnr('%'), 'type': 'W', 'lnum': 4, 'col': 1},
284   \ {'bufnr': bufnr('%'), 'type': 'W', 'lnum': 5, 'col': 1},
285   \ {'bufnr': bufnr('%'), 'type': 'W', 'sub_type': 'style', 'lnum': 6, 'col': 1},
286   \ {'bufnr': bufnr('%'), 'type': 'I', 'lnum': 7, 'col': 1},
287   \ {'bufnr': bufnr('%') + 1, 'type': 'E', 'lnum': 1, 'col': 1},
288   \])
289
290   AssertEqual
291   \ [
292   \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
293   \   {'group': 'ALEError', 'priority': 10, 'pos1': [2, 1, 1]},
294   \   {'group': 'ALEStyleError', 'priority': 10, 'pos1': [3, 1, 1]},
295   \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [4, 1, 1]},
296   \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [5, 1, 1]},
297   \   {'group': 'ALEStyleWarning', 'priority': 10, 'pos1': [6, 1, 1]},
298   \   {'group': 'ALEInfo', 'priority': 10, 'pos1': [7, 1, 1]},
299   \ ],
300   \ GetMatchesWithoutIDs()
301
302 Execute(Highlighting should support errors spanning many lines):
303   let g:items = [
304   \ {'bufnr': bufnr(''), 'type': 'E', 'lnum': 1, 'col': 1, 'end_lnum': 10, 'end_col': 3},
305   \]
306
307   call ale#highlight#SetHighlights(bufnr(''), g:items)
308
309   if g:has_nvim_highlight
310     " The newer NeoVim highlight API produces different output.
311     AssertEqual
312     \ [
313     \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1073741824]},
314     \   {'group': 'ALEError', 'priority': 10, 'pos1': [2, 1, 1073741824]},
315     \   {'group': 'ALEError', 'priority': 10, 'pos1': [3, 1, 1073741824]},
316     \   {'group': 'ALEError', 'priority': 10, 'pos1': [4, 1, 1073741824]},
317     \   {'group': 'ALEError', 'priority': 10, 'pos1': [5, 1, 1073741824]},
318     \   {'group': 'ALEError', 'priority': 10, 'pos1': [6, 1, 1073741824]},
319     \   {'group': 'ALEError', 'priority': 10, 'pos1': [7, 1, 1073741824]},
320     \   {'group': 'ALEError', 'priority': 10, 'pos1': [8, 1, 1073741824]},
321     \   {'group': 'ALEError', 'priority': 10, 'pos1': [9, 1, 1073741824]},
322     \   {'group': 'ALEError', 'priority': 10, 'pos1': [10, 1, 3]},
323     \ ],
324     \ GetMatchesWithoutIDs()
325   else
326     " We should set 2 highlights for the item, as we can only add 8 at a time.
327     AssertEqual
328     \ [
329     \   {
330     \     'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1073741824],
331     \     'pos2': [2], 'pos3': [3], 'pos4': [4], 'pos5': [5], 'pos6': [6],
332     \     'pos7': [7], 'pos8': [8],
333     \   },
334     \   {
335     \     'group': 'ALEError', 'priority': 10,
336     \     'pos1': [9], 'pos2': [10, 1, 3]
337     \   },
338     \ ],
339     \ GetMatchesWithoutIDs()
340   endif
341
342 Execute(Highlights should always be cleared when the buffer highlight list is empty):
343   if g:has_nvim_highlight
344     " The newer API uses namespacing. We'll emulate it here.
345     call ale#highlight#nvim_buf_add_highlight(
346     \ bufnr(''),
347     \ 1,
348     \ 'ALEError',
349     \ 0,
350     \ 0,
351     \ 1,
352     \)
353
354     AssertEqual
355     \ [{'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]}],
356     \ GetMatchesWithoutIDs()
357   else
358     " Add our highlights and something else.
359     call matchaddpos('ALEError', [[1, 1, 1]])
360     call matchaddpos('SomeOtherGroup', [[1, 1, 1]])
361
362     AssertEqual
363     \ [
364     \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
365     \   {'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]},
366     \ ],
367     \ GetMatchesWithoutIDs()
368   endif
369
370
371   " Set the List we use for holding highlights for buffers.
372   let b:ale_highlight_items = []
373
374   " Call the function for updating the highlights called when buffers
375   " are entered, or when problems are presented.
376   call ale#highlight#UpdateHighlights()
377
378   " Check that we remove our highlights.
379   if g:has_nvim_highlight
380     AssertEqual [], GetMatchesWithoutIDs()
381   else
382     AssertEqual
383     \ [{'group': 'SomeOtherGroup', 'priority': 10, 'pos1': [1, 1, 1]}],
384     \ GetMatchesWithoutIDs()
385   endif
386
387 Execute(Highlights should be hidden when excluded):
388   let b:ale_exclude_highlights = ['ig.*ore', 'nope']
389
390   call ale#highlight#SetHighlights(bufnr('%'), [
391   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 1, 'col': 1, 'text': 'hello'},
392   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 2, 'col': 1, 'text': 'ignore'},
393   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 3, 'col': 1, 'text': 'nope'},
394   \ {'bufnr': bufnr('%'), 'type': 'E', 'lnum': 4, 'col': 1, 'text': 'world'},
395   \])
396
397   AssertEqual
398   \ [
399   \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
400   \   {'group': 'ALEError', 'priority': 10, 'pos1': [4, 1, 1]},
401   \ ],
402   \ GetMatchesWithoutIDs()
403
404 Execute(Highlights should be cleared when ALE is disabled):
405   let g:ale_enabled = 1
406   call ale#highlight#SetHighlights(bufnr(''), [
407   \ {'bufnr': bufnr(''), 'type': 'E', 'lnum': 1, 'col': 1, 'end_lnum': 10, 'end_col': 3},
408   \])
409
410   let g:ale_enabled = 0
411   call ale#highlight#UpdateHighlights()
412
413   AssertEqual [], GetMatchesWithoutIDs()
414
415   let g:ale_enabled = 1
416   call ale#highlight#SetHighlights(bufnr(''), [
417   \ {'bufnr': bufnr(''), 'type': 'E', 'lnum': 1, 'col': 1, 'end_lnum': 10, 'end_col': 3},
418   \])
419
420   let b:ale_enabled = 0
421   call ale#highlight#UpdateHighlights()
422
423   AssertEqual [], GetMatchesWithoutIDs()
424
425 Execute(Line highlights should be set when signs are disabled):
426   " This will mess with your settings, but it needs to be tested.
427   " We need to match highlights case-insensitively when removing them.
428   hi link aleerrorline spellbad
429
430   let g:ale_set_signs = 0
431
432   call ale#highlight#SetHighlights(bufnr(''), [
433   \ {'bufnr': bufnr(''), 'type': 'E', 'lnum': 1, 'col': 1},
434   \ {'bufnr': bufnr(''), 'type': 'W', 'lnum': 2, 'col': 1},
435   \ {'bufnr': bufnr(''), 'type': 'I', 'lnum': 3, 'col': 1},
436   \])
437
438   if g:has_nvim_highlight
439     " The output is different with the newer NeoVIM highlight API.
440     AssertEqual
441     \ [
442     \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
443     \   {'group': 'ALEErrorLine', 'priority': 10, 'pos1': [1, 1, 1073741824]},
444     \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [2, 1, 1]},
445     \   {'group': 'ALEWarningLine', 'priority': 10, 'pos1': [2, 1, 1073741824]},
446     \   {'group': 'ALEInfo', 'priority': 10, 'pos1': [3, 1, 1]},
447     \   {'group': 'ALEInfoLine', 'priority': 10, 'pos1': [3, 1, 1073741824]}
448     \ ],
449     \ GetMatchesWithoutIDs()
450   else
451     AssertEqual
452     \ [
453     \   {'group': 'ALEError', 'priority': 10, 'pos1': [1, 1, 1]},
454     \   {'group': 'ALEWarning', 'priority': 10, 'pos1': [2, 1, 1]},
455     \   {'group': 'ALEInfo', 'priority': 10, 'pos1': [3, 1, 1]},
456     \   {'group': 'aleerrorline', 'priority': 10, 'pos1': [1]},
457     \   {'group': 'ALEWarningLine', 'priority': 10, 'pos1': [2]},
458     \   {'group': 'ALEInfoLine', 'priority': 10, 'pos1': [3]},
459     \ ],
460     \ GetMatchesWithoutIDs()
461   endif
462
463   " All of the highlights should be removed.
464   call ale#highlight#RemoveHighlights()
465   AssertEqual [], GetMatchesWithoutIDs()