/ test / test_ale_toggle.vader
test_ale_toggle.vader
  1  Before:
  2    Save g:ale_buffer_info
  3    Save g:ale_set_signs
  4    Save g:ale_set_lists_synchronously
  5    Save g:ale_run_synchronously
  6    Save g:ale_pattern_options
  7    Save g:ale_pattern_options_enabled
  8    Save g:ale_set_balloons
  9  
 10    let g:ale_set_signs = 1
 11    let g:ale_set_lists_synchronously = 1
 12    let g:ale_run_synchronously = 1
 13    unlet! g:ale_run_synchronously_callbacks
 14    let g:ale_pattern_options = {}
 15    let g:ale_pattern_options_enabled = 1
 16    let g:ale_set_balloons =
 17    \   has('balloon_eval') && has('gui_running') ||
 18    \   has('balloon_eval_term') && !has('gui_running')
 19  
 20    unlet! b:ale_enabled
 21  
 22    let g:ale_buffer_info = {}
 23    let g:expected_loclist = [{
 24    \ 'bufnr': bufnr('%'),
 25    \ 'lnum': 2,
 26    \ 'vcol': 0,
 27    \ 'col': 3,
 28    \ 'text': 'foo bar',
 29    \ 'type': 'E',
 30    \ 'nr': -1,
 31    \ 'pattern': '',
 32    \ 'valid': 1,
 33    \}]
 34    let g:expected_groups = [
 35    \ 'ALECleanupGroup',
 36    \ 'ALEEvents',
 37    \ 'ALEHighlightBufferGroup',
 38    \]
 39    let g:has_nvim_highlight = exists('*nvim_buf_add_highlight') && exists('*nvim_buf_clear_namespace')
 40  
 41    function! ToggleTestCallback(buffer, output)
 42      return [{
 43      \ 'bufnr': a:buffer,
 44      \ 'lnum': 2,
 45      \ 'vcol': 0,
 46      \ 'col': 3,
 47      \ 'text': 'foo bar',
 48      \ 'type': 'E',
 49      \ 'nr': -1,
 50      \}]
 51    endfunction
 52  
 53    function! ParseAuGroups()
 54      redir => l:output
 55         silent exec 'autocmd'
 56      redir end
 57  
 58      let l:results = []
 59  
 60      for l:line in split(l:output, "\n")
 61        let l:match = matchlist(l:line, '^ALE[a-zA-Z]\+')
 62  
 63        " We don't care about some groups here.
 64        if !empty(l:match)
 65        \&& l:match[0] !=# 'ALECompletionGroup'
 66        \&& l:match[0] !=# 'ALEBufferFixGroup'
 67        \&& l:match[0] !=# 'ALEPatternOptionsGroup'
 68          call add(l:results, l:match[0])
 69        endif
 70      endfor
 71  
 72      call uniq(sort(l:results))
 73  
 74      return l:results
 75    endfunction
 76  
 77    call ale#linter#Define('foobar', {
 78    \ 'name': 'testlinter',
 79    \ 'callback': 'ToggleTestCallback',
 80    \ 'executable': has('win32') ? 'cmd' : 'echo',
 81    \ 'command': 'echo',
 82    \ 'read_buffer': 0,
 83    \})
 84  
 85    call ale#sign#Clear()
 86  
 87  After:
 88    Restore
 89  
 90    unlet! g:ale_run_synchronously_callbacks
 91    unlet! g:expected_loclist
 92    unlet! g:expected_groups
 93    unlet! b:ale_enabled
 94    unlet! g:output
 95    unlet! g:has_nvim_highlight
 96  
 97    call ale#linter#Reset()
 98  
 99    " Toggle ALE back on if we fail when it's disabled.
100    if !g:ale_enabled
101      ALEToggle
102    endif
103  
104    delfunction ToggleTestCallback
105    delfunction ParseAuGroups
106  
107    call setloclist(0, [])
108    call ale#sign#Clear()
109    call clearmatches()
110  
111  Given foobar (Some imaginary filetype):
112    foo
113    bar
114    baz
115  
116  Execute(ALEToggle should reset everything and then run again):
117    AssertEqual 'foobar', &filetype
118  
119    ALELint
120    call ale#test#FlushJobs()
121  
122    " First check that everything is there...
123    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
124    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
125  
126    " Only check the legacy matches if not using the new NeoVIM API.
127    if !g:has_nvim_highlight
128      AssertEqual
129      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
130      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
131    endif
132  
133    AssertEqual g:expected_groups, ParseAuGroups()
134    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
135  
136    " Now Toggle ALE off.
137    ALEToggle
138  
139    " Everything should be cleared.
140    Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
141    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys(), 'The loclist was not cleared'
142    AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
143  
144    if !g:has_nvim_highlight
145      AssertEqual [], getmatches(), 'The highlights were not cleared'
146    endif
147  
148    AssertEqual g:expected_groups, ParseAuGroups()
149  
150    " Toggle ALE on, everything should be set up and run again.
151    ALEToggle
152    call ale#test#FlushJobs()
153  
154    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
155    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
156  
157    if !g:has_nvim_highlight
158      AssertEqual
159      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
160      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
161    endif
162  
163    AssertEqual g:expected_groups, ParseAuGroups()
164    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
165  
166  Execute(ALEToggle should skip filename keys and preserve them):
167    AssertEqual 'foobar', &filetype
168  
169    let g:ale_buffer_info['/foo/bar/baz.txt'] = {
170    \ 'job_list': [],
171    \ 'active_linter_list': [],
172    \ 'loclist': [],
173    \ 'temporary_file_list': [],
174    \ 'temporary_directory_list': [],
175    \ 'history': [],
176    \}
177  
178    ALELint
179    call ale#test#FlushJobs()
180  
181    " Now Toggle ALE off.
182    ALEToggle
183  
184    AssertEqual
185    \ {
186    \   'job_list': [],
187    \   'active_linter_list': [],
188    \   'loclist': [],
189    \   'temporary_file_list': [],
190    \   'temporary_directory_list': [],
191    \   'history': [],
192    \ },
193    \ get(g:ale_buffer_info, '/foo/bar/baz.txt', {})
194  
195    " Toggle ALE on again.
196    ALEToggle
197    call ale#test#FlushJobs()
198  
199    AssertEqual
200    \ {
201    \   'job_list': [],
202    \   'active_linter_list': [],
203    \   'loclist': [],
204    \   'temporary_file_list': [],
205    \   'temporary_directory_list': [],
206    \   'history': [],
207    \ },
208    \ get(g:ale_buffer_info, '/foo/bar/baz.txt', {})
209  
210  Execute(ALEDisable should reset everything and stay disabled):
211    ALELint
212    call ale#test#FlushJobs()
213  
214    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
215  
216    ALEDisable
217    call ale#test#FlushJobs()
218  
219    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys()
220    AssertEqual 0, g:ale_enabled
221  
222    ALEDisable
223    call ale#test#FlushJobs()
224  
225    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys()
226    AssertEqual 0, g:ale_enabled
227  
228  Execute(ALEEnable should enable ALE and lint again):
229    let g:ale_enabled = 0
230  
231    ALEEnable
232    call ale#test#FlushJobs()
233  
234    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
235    AssertEqual 1, g:ale_enabled
236  
237  Execute(ALEReset should reset everything for a buffer):
238    AssertEqual 'foobar', &filetype
239  
240    ALELint
241    call ale#test#FlushJobs()
242  
243    " First check that everything is there...
244    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
245    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
246  
247    if !g:has_nvim_highlight
248      AssertEqual
249      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
250      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
251    endif
252  
253    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
254  
255    " Now Toggle ALE off.
256    ALEReset
257    call ale#test#FlushJobs()
258  
259    " Everything should be cleared.
260    Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
261    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys(), 'The loclist was not cleared'
262    AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
263  
264    if !g:has_nvim_highlight
265      AssertEqual [], getmatches(), 'The highlights were not cleared'
266    endif
267  
268    AssertEqual 1, g:ale_enabled
269  
270  Execute(ALEToggleBuffer should reset everything and then run again):
271    AssertEqual 'foobar', &filetype
272  
273    ALELint
274    call ale#test#FlushJobs()
275  
276    " First check that everything is there...
277    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
278    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
279  
280    if !g:has_nvim_highlight
281      AssertEqual
282      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
283      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
284    endif
285  
286    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
287  
288    " Now Toggle ALE off.
289    ALEToggleBuffer
290  
291    " Everything should be cleared.
292    Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
293    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys(), 'The loclist was not cleared'
294    AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
295  
296    if !g:has_nvim_highlight
297      AssertEqual [], getmatches(), 'The highlights were not cleared'
298    endif
299  
300    " Toggle ALE on, everything should be set up and run again.
301    ALEToggleBuffer
302    call ale#test#FlushJobs()
303  
304    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
305    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
306  
307    if !g:has_nvim_highlight
308      AssertEqual
309      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
310      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
311    endif
312  
313    AssertEqual g:expected_groups, ParseAuGroups()
314    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
315  
316  Execute(ALEDisableBuffer should reset everything and stay disabled):
317    ALELint
318    call ale#test#FlushJobs()
319  
320    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
321  
322    ALEDisableBuffer
323    call ale#test#FlushJobs()
324  
325    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys()
326    AssertEqual 0, b:ale_enabled
327  
328  Execute(ALEEnableBuffer should enable ALE and lint again):
329    let b:ale_enabled = 0
330  
331    ALEEnableBuffer
332    call ale#test#FlushJobs()
333  
334    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
335    AssertEqual 1, b:ale_enabled
336  
337  Execute(ALEEnableBuffer should complain when ALE is disabled globally):
338    let g:ale_enabled = 0
339    let b:ale_enabled = 0
340  
341    redir => g:output
342      ALEEnableBuffer
343    redir END
344  
345    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys()
346    AssertEqual 0, b:ale_enabled
347    AssertEqual 0, g:ale_enabled
348    AssertEqual
349    \ 'ALE cannot be enabled locally when disabled globally',
350    \ join(split(g:output))
351  
352  Execute(ALEResetBuffer should reset everything for a buffer):
353    AssertEqual 'foobar', &filetype
354  
355    ALELint
356    call ale#test#FlushJobs()
357  
358    " First check that everything is there...
359    AssertEqual g:expected_loclist, ale#test#GetLoclistWithoutNewerKeys()
360    AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
361  
362    if !g:has_nvim_highlight
363      AssertEqual
364      \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
365      \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
366    endif
367  
368    AssertEqual [{'lnum': 2, 'bufnr': bufnr(''), 'col': 3, 'linter_name': 'testlinter', 'vcol': 0, 'nr': -1, 'type': 'E', 'text': 'foo bar', 'sign_id': 1000001}], g:ale_buffer_info[bufnr('')].loclist
369  
370    " Now Toggle ALE off.
371    ALEResetBuffer
372    call ale#test#FlushJobs()
373  
374    " Everything should be cleared.
375    Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
376    AssertEqual [], ale#test#GetLoclistWithoutNewerKeys(), 'The loclist was not cleared'
377    AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
378  
379    if !g:has_nvim_highlight
380      AssertEqual [], getmatches(), 'The highlights were not cleared'
381    endif
382  
383    AssertEqual 1, g:ale_enabled
384    AssertEqual 1, get(b:, 'ale_enabled', 1)
385  
386  Execute(Disabling ALE should disable balloons):
387    " These tests won't run in the console, but we can run them manually in GVim.
388    if has('balloon_eval') && has('gui_running')
389    \|| (has('balloon_eval_term') && !has('gui_running'))
390      call ale#linter#Reset()
391  
392      " Enable balloons, so we can check the expr value.
393      call ale#balloon#Enable()
394  
395      if has('balloon_eval') && has('gui_running')
396        AssertEqual 1, &ballooneval
397      else
398        AssertEqual 1, &balloonevalterm
399      endif
400  
401      AssertEqual 'ale#balloon#Expr()', &balloonexpr
402  
403      " Toggle ALE off.
404      ALEToggle
405  
406      " The balloon settings should be reset.
407      if has('balloon_eval') && has('gui_running')
408        AssertEqual 0, &ballooneval
409      else
410        AssertEqual 0, &balloonevalterm
411      endif
412  
413      AssertEqual '', &balloonexpr
414    endif
415  
416  Execute(Enabling ALE should enable balloons if the setting is on):
417    if has('balloon_eval') && has('gui_running')
418    \|| (has('balloon_eval_term') && !has('gui_running'))
419      call ale#linter#Reset()
420      call ale#balloon#Disable()
421      ALEDisable
422      let g:ale_set_balloons = 0
423      ALEEnable
424  
425      if has('balloon_eval') && has('gui_running')
426        AssertEqual 0, &ballooneval
427      else
428        AssertEqual 0, &balloonevalterm
429      endif
430  
431      AssertEqual '', &balloonexpr
432  
433      ALEDisable
434      let g:ale_set_balloons = 1
435      ALEEnable
436  
437      if has('balloon_eval') && has('gui_running')
438        AssertEqual 1, &ballooneval
439      else
440        AssertEqual 1, &balloonevalterm
441      endif
442  
443      AssertEqual 'ale#balloon#Expr()', &balloonexpr
444    endif