summaryrefslogtreecommitdiff
path: root/test/test_ale_toggle.vader
blob: d56f8c2b511d6dbe66feaa1afb01c32c34b27bf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
Before:
  Save g:ale_buffer_info
  Save g:ale_set_signs
  Save g:ale_set_lists_synchronously
  Save g:ale_run_synchronously
  Save g:ale_pattern_options
  Save g:ale_pattern_options_enabled

  let g:ale_set_signs = 1
  let g:ale_set_lists_synchronously = 1
  let g:ale_run_synchronously = 1
  let g:ale_pattern_options = {}
  let g:ale_pattern_options_enabled = 1

  unlet! b:ale_enabled

  let g:ale_buffer_info = {}
  let g:expected_loclist = [{
  \ 'bufnr': bufnr('%'),
  \ 'lnum': 2,
  \ 'vcol': 0,
  \ 'col': 3,
  \ 'text': 'foo bar',
  \ 'type': 'E',
  \ 'nr': -1,
  \ 'pattern': '',
  \ 'valid': 1,
  \}]
  let g:expected_groups = [
  \ 'ALECleanupGroup',
  \ 'ALECursorGroup',
  \ 'ALEHighlightBufferGroup',
  \ 'ALERunOnEnterGroup',
  \ 'ALERunOnFiletypeChangeGroup',
  \ 'ALERunOnSaveGroup',
  \ 'ALERunOnTextChangedGroup',
  \]

  function! ToggleTestCallback(buffer, output)
    return [{
    \ 'bufnr': a:buffer,
    \ 'lnum': 2,
    \ 'vcol': 0,
    \ 'col': 3,
    \ 'text': 'foo bar',
    \ 'type': 'E',
    \ 'nr': -1,
    \}]
  endfunction

  function! ParseAuGroups()
    redir => l:output
       silent exec 'autocmd'
    redir end

    let l:results = []

    for l:line in split(l:output, "\n")
      let l:match = matchlist(l:line, '^ALE[a-zA-Z]\+Group')

      " We don't care about some groups here.
      if !empty(l:match)
      \&& l:match[0] !=# 'ALECompletionGroup'
      \&& l:match[0] !=# 'ALEBufferFixGroup'
      \&& l:match[0] !=# 'ALEPatternOptionsGroup'
        call add(l:results, l:match[0])
      endif
    endfor

    call uniq(sort(l:results))

    return l:results
  endfunction

  call ale#linter#Define('foobar', {
  \ 'name': 'testlinter',
  \ 'callback': 'ToggleTestCallback',
  \ 'executable': has('win32') ? 'cmd' : 'echo',
  \ 'command': 'echo',
  \ 'read_buffer': 0,
  \})

  sign unplace *

After:
  Restore

  unlet! g:expected_loclist
  unlet! g:expected_groups
  unlet! b:ale_enabled
  unlet! g:output

  call ale#linter#Reset()

  " Toggle ALE back on if we fail when it's disabled.
  if !g:ale_enabled
    ALEToggle
  endif

  delfunction ToggleTestCallback
  delfunction ParseAuGroups

  call setloclist(0, [])
  sign unplace *
  call clearmatches()

Given foobar (Some imaginary filetype):
  foo
  bar
  baz

Execute(ALEToggle should reset everything and then run again):
  " Run this test asynchrously.
  let g:ale_run_synchronously = 0

  AssertEqual 'foobar', &filetype

  call ale#Lint()
  call ale#engine#WaitForJobs(2000)

  " First check that everything is there...
  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  AssertEqual g:expected_groups, ParseAuGroups()
  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

  " Now Toggle ALE off.
  ALEToggle

  " Everything should be cleared.
  Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
  AssertEqual [], getloclist(0), 'The loclist was not cleared'
  AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
  AssertEqual [], getmatches(), 'The highlights were not cleared'
  AssertEqual
  \ [
  \   'ALECleanupGroup',
  \   'ALEHighlightBufferGroup',
  \   'ALERunOnSaveGroup',
  \ ],
  \ ParseAuGroups()

  " Toggle ALE on, everything should be set up and run again.
  ALEToggle
  call ale#engine#WaitForJobs(2000)

  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  AssertEqual g:expected_groups, ParseAuGroups()
  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

Execute(ALEToggle should skip filename keys and preserve them):
  " Run this test asynchrously.
  let g:ale_run_synchronously = 0

  AssertEqual 'foobar', &filetype

  let g:ale_buffer_info['/foo/bar/baz.txt'] = {
  \ 'job_list': [],
  \ 'active_linter_list': [],
  \ 'loclist': [],
  \ 'temporary_file_list': [],
  \ 'temporary_directory_list': [],
  \ 'history': [],
  \}

  call ale#Lint()
  call ale#engine#WaitForJobs(2000)

  " Now Toggle ALE off.
  ALEToggle

  AssertEqual
  \ {
  \   'job_list': [],
  \   'active_linter_list': [],
  \   'loclist': [],
  \   'temporary_file_list': [],
  \   'temporary_directory_list': [],
  \   'history': [],
  \ },
  \ get(g:ale_buffer_info, '/foo/bar/baz.txt', {})

  " Toggle ALE on again.
  ALEToggle
  call ale#engine#WaitForJobs(2000)

  AssertEqual
  \ {
  \   'job_list': [],
  \   'active_linter_list': [],
  \   'loclist': [],
  \   'temporary_file_list': [],
  \   'temporary_directory_list': [],
  \   'history': [],
  \ },
  \ get(g:ale_buffer_info, '/foo/bar/baz.txt', {})

Execute(ALEDisable should reset everything and stay disabled):
  call ale#Lint()

  AssertEqual g:expected_loclist, getloclist(0)

  ALEDisable

  AssertEqual [], getloclist(0)
  AssertEqual 0, g:ale_enabled

  ALEDisable

  AssertEqual [], getloclist(0)
  AssertEqual 0, g:ale_enabled

Execute(ALEEnable should enable ALE and lint again):
  let g:ale_enabled = 0

  ALEEnable

  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual 1, g:ale_enabled

Execute(ALEReset should reset everything for a buffer):
  AssertEqual 'foobar', &filetype

  call ale#Lint()

  " First check that everything is there...
  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  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

  " Now Toggle ALE off.
  ALEReset

  " Everything should be cleared.
  Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
  AssertEqual [], getloclist(0), 'The loclist was not cleared'
  AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
  AssertEqual [], getmatches(), 'The highlights were not cleared'

  AssertEqual 1, g:ale_enabled

Execute(ALEToggleBuffer should reset everything and then run again):
  " Run this test asynchrously.
  let g:ale_run_synchronously = 0

  AssertEqual 'foobar', &filetype

  call ale#Lint()
  call ale#engine#WaitForJobs(2000)

  " First check that everything is there...
  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  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

  " Now Toggle ALE off.
  ALEToggleBuffer

  " Everything should be cleared.
  Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
  AssertEqual [], getloclist(0), 'The loclist was not cleared'
  AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
  AssertEqual [], getmatches(), 'The highlights were not cleared'

  " Toggle ALE on, everything should be set up and run again.
  ALEToggleBuffer
  call ale#engine#WaitForJobs(2000)

  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  AssertEqual g:expected_groups, ParseAuGroups()
  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

Execute(ALEDisableBuffer should reset everything and stay disabled):
  call ale#Lint()

  AssertEqual g:expected_loclist, getloclist(0)

  ALEDisableBuffer

  AssertEqual [], getloclist(0)
  AssertEqual 0, b:ale_enabled

Execute(ALEEnableBuffer should enable ALE and lint again):
  let b:ale_enabled = 0

  ALEEnableBuffer

  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual 1, b:ale_enabled

Execute(ALEEnableBuffer should complain when ALE is disabled globally):
  let g:ale_enabled = 0
  let b:ale_enabled = 0

  redir => g:output
    ALEEnableBuffer
  redir END

  AssertEqual [], getloclist(0)
  AssertEqual 0, b:ale_enabled
  AssertEqual 0, g:ale_enabled
  AssertEqual
  \ 'ALE cannot be enabled locally when disabled globally',
  \ join(split(g:output))

Execute(ALEResetBuffer should reset everything for a buffer):
  AssertEqual 'foobar', &filetype

  call ale#Lint()

  " First check that everything is there...
  AssertEqual g:expected_loclist, getloclist(0)
  AssertEqual [0, [[2, 1000001, 'ALEErrorSign']]], ale#sign#FindCurrentSigns(bufnr('%'))
  AssertEqual
  \ [{'group': 'ALEError', 'pos1': [2, 3, 1]}],
  \ map(getmatches(), '{''group'': v:val.group, ''pos1'': v:val.pos1}')
  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

  " Now Toggle ALE off.
  ALEResetBuffer

  " Everything should be cleared.
  Assert !has_key(g:ale_buffer_info, bufnr('')), 'The g:ale_buffer_info Dictionary was not removed'
  AssertEqual [], getloclist(0), 'The loclist was not cleared'
  AssertEqual [0, []], ale#sign#FindCurrentSigns(bufnr('%')), 'The signs were not cleared'
  AssertEqual [], getmatches(), 'The highlights were not cleared'

  AssertEqual 1, g:ale_enabled
  AssertEqual 1, get(b:, 'ale_enabled', 1)