summaryrefslogtreecommitdiff
path: root/test/test_ale_fix.vader
blob: b5c16724af7fe25009c80a34ab7d531ed1450a00 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
Before:
  Save g:ale_fixers
  Save &shell
  Save g:ale_enabled
  Save g:ale_fix_on_save
  Save g:ale_lint_on_save
  Save g:ale_echo_cursor

  silent! cd /testplugin/test

  let g:ale_enabled = 0
  let g:ale_echo_cursor = 0
  let g:ale_run_synchronously = 1
  let g:ale_fix_buffer_data = {}
  let g:ale_fixers = {
  \ 'testft': [],
  \}
  let &shell = '/bin/bash'

  function AddCarets(buffer, lines) abort
    " map() is applied to the original lines here.
    " This way, we can ensure that defensive copies are made.
    return map(a:lines, '''^'' . v:val')
  endfunction

  function AddDollars(buffer, lines) abort
    return map(a:lines, '''$'' . v:val')
  endfunction

  function DoNothing(buffer, lines) abort
    return 0
  endfunction

  function CatLine(buffer, lines) abort
    return {'command': 'cat - <(echo d)'}
  endfunction

  function CatLineOneArg(buffer) abort
    return {'command': 'cat - <(echo d)'}
  endfunction

  function ReplaceWithTempFile(buffer, lines) abort
    return {'command': 'echo x > %t', 'read_temporary_file': 1}
  endfunction

  function RemoveLastLine(buffer, lines) abort
    return ['a', 'b']
  endfunction

  function RemoveLastLineOneArg(buffer) abort
    return ['a', 'b']
  endfunction

  function! TestCallback(buffer, output)
    return [{'lnum': 1, 'col': 1, 'text': 'xxx'}]
  endfunction

  function! SetUpLinters()
    call ale#linter#Define('testft', {
    \ 'name': 'testlinter',
    \ 'callback': 'TestCallback',
    \ 'executable': 'true',
    \ 'command': 'true',
    \})
  endfunction

After:
  Restore
  unlet! g:ale_run_synchronously
  unlet! g:ale_emulate_job_failure
  unlet! b:ale_fixers
  delfunction AddCarets
  delfunction AddDollars
  delfunction DoNothing
  delfunction CatLine
  delfunction CatLineOneArg
  delfunction ReplaceWithTempFile
  delfunction RemoveLastLine
  delfunction RemoveLastLineOneArg
  delfunction TestCallback
  delfunction SetUpLinters
  call ale#fix#registry#ResetToDefaults()
  call ale#linter#Reset()

  setlocal buftype=nofile

  if filereadable('fix_test_file')
    call delete('fix_test_file')
  endif

  call setloclist(0, [])

  let g:ale_fix_buffer_data = {}

Given testft (A file with three lines):
  a
  b
  c

Execute(ALEFix should complain when there are no functions to call):
  AssertThrows ALEFix
  AssertEqual 'Vim(echoerr):No fixers have been defined. Try :ALEFixSuggest', g:vader_exception

Execute(ALEFix should apply simple functions):
  let g:ale_fixers.testft = ['AddCarets']
  ALEFix

Expect(The first function should be used):
  ^a
  ^b
  ^c

Execute(ALEFix should apply simple functions in a chain):
  let g:ale_fixers.testft = ['AddCarets', 'AddDollars']
  ALEFix

Expect(Both functions should be used):
  $^a
  $^b
  $^c

Execute(ALEFix should allow 0 to be returned to skip functions):
  let g:ale_fixers.testft = ['DoNothing', 'AddDollars']
  ALEFix

Expect(Only the second function should be applied):
  $a
  $b
  $c

Execute(ALEFix should allow commands to be run):
  let g:ale_fixers.testft = ['CatLine']
  ALEFix

Expect(An extra line should be added):
  a
  b
  c
  d

Execute(ALEFix should allow temporary files to be read):
  let g:ale_fixers.testft = ['ReplaceWithTempFile']
  ALEFix

Expect(The line we wrote to the temporary file should be used here):
  x

Execute(ALEFix should allow jobs and simple functions to be combined):
  let g:ale_fixers.testft = ['ReplaceWithTempFile', 'AddDollars']
  ALEFix

Expect(The lines from the temporary file should be modified):
  $x

Execute(ALEFix should send lines modified by functions to jobs):
  let g:ale_fixers.testft = ['AddDollars', 'CatLine']
  ALEFix

Expect(The lines should first be modified by the function, then the job):
  $a
  $b
  $c
  d

Execute(ALEFix should skip commands when jobs fail to run):
  let g:ale_emulate_job_failure = 1
  let g:ale_fixers.testft = ['CatLine', 'AddDollars']
  ALEFix

Expect(Only the second function should be applied):
  $a
  $b
  $c

Execute(ALEFix should handle strings for selecting a single function):
  let g:ale_fixers.testft = 'AddCarets'
  ALEFix

Expect(The first function should be used):
  ^a
  ^b
  ^c

Execute(ALEFix should use functions from the registry):
  call ale#fix#registry#Add('add_carets', 'AddCarets', [], 'Add some carets')
  let g:ale_fixers.testft = ['add_carets']
  ALEFix

Expect(The registry function should be used):
  ^a
  ^b
  ^c

Execute(ALEFix should be able to remove the last line for files):
  let g:ale_fixers.testft = ['RemoveLastLine']
  ALEFix

Expect(There should be only two lines):
  a
  b

Execute(ALEFix should accept funcrefs):
  let g:ale_fixers.testft = [function('RemoveLastLine')]
  ALEFix

Expect(There should be only two lines):
  a
  b

Execute(ALEFix should accept lambdas):
  if has('nvim')
    " NeoVim 0.1.7 can't interpret lambdas correctly, so just set the lines
    " to make the test pass.
    call setline(1, ['a', 'b', 'c', 'd'])
  else
    let g:ale_fixers.testft = [{buffer, lines -> lines + ['d']}]
    ALEFix
  endif

Expect(There should be an extra line):
  a
  b
  c
  d

Execute(ALEFix should user buffer-local fixer settings):
  let g:ale_fixers.testft = ['AddCarets', 'AddDollars']
  let b:ale_fixers = {'testft': ['RemoveLastLine']}
  ALEFix

Expect(There should be only two lines):
  a
  b

Given testft (A file with three lines):
  a
  b
  c

Execute(ALEFix should save files on the save event):
  let g:ale_fix_on_save = 1
  let g:ale_lint_on_save = 1
  let g:ale_enabled = 1

  noautocmd silent file fix_test_file
  call writefile(getline(1, '$'), 'fix_test_file')

  let g:ale_fixers.testft = ['AddDollars']

  " We have to set the buftype to empty so the file will be written.
  setlocal buftype=

  call SetUpLinters()
  call ale#events#SaveEvent(bufnr(''))

  " We should save the file.
  AssertEqual ['$a', '$b', '$c'], readfile('fix_test_file')
  Assert !&modified, 'The was marked as ''modified'''

  " We have run the linter.
  AssertEqual [{
  \   'bufnr': bufnr('%'),
  \   'lnum': 1,
  \   'vcol': 0,
  \   'col': 1,
  \   'text': 'xxx',
  \   'type': 'E',
  \   'nr': -1,
  \   'pattern': '',
  \   'valid': 1,
  \}], getloclist(0)

Expect(The buffer should be modified):
  $a
  $b
  $c

Given testft (A file with three lines):
  a
  b
  c

Execute(ALEFix should still lint with no linters to be applied):
  let g:ale_fix_on_save = 1
  let g:ale_lint_on_save = 1
  let g:ale_enabled = 1

  noautocmd silent file fix_test_file

  let g:ale_fixers.testft = []

  call SetUpLinters()
  call ale#events#SaveEvent(bufnr(''))

  Assert !filereadable('fix_test_file'), 'The file should not have been saved'

  " We have run the linter.
  AssertEqual [{
  \   'bufnr': bufnr('%'),
  \   'lnum': 1,
  \   'vcol': 0,
  \   'col': 1,
  \   'text': 'xxx',
  \   'type': 'E',
  \   'nr': -1,
  \   'pattern': '',
  \   'valid': 1,
  \}], getloclist(0)

Expect(The buffer should be the same):
  a
  b
  c

Execute(ALEFix should still lint when nothing was fixed on save):
  let g:ale_fix_on_save = 1
  let g:ale_lint_on_save = 1
  let g:ale_enabled = 1

  noautocmd silent file fix_test_file

  let g:ale_fixers.testft = ['DoNothing']

  call SetUpLinters()
  call ale#events#SaveEvent(bufnr(''))

  Assert !filereadable('fix_test_file'), 'The file should not have been saved'

  " We have run the linter.
  AssertEqual [{
  \   'bufnr': bufnr('%'),
  \   'lnum': 1,
  \   'vcol': 0,
  \   'col': 1,
  \   'text': 'xxx',
  \   'type': 'E',
  \   'nr': -1,
  \   'pattern': '',
  \   'valid': 1,
  \}], getloclist(0)

Expect(The buffer should be the same):
  a
  b
  c

Given testft (A file with three lines):
  a
  b
  c

Execute(ale#fix#InitBufferData() should set up the correct data):
  noautocmd silent file fix_test_file

  call ale#fix#InitBufferData(bufnr(''), 'save_file')

  AssertEqual {
  \ bufnr(''): {
  \   'temporary_directory_list': [],
  \   'vars': b:,
  \   'filename': simplify(getcwd() . '/fix_test_file'),
  \   'done': 0,
  \   'lines_before': ['a', 'b', 'c'],
  \   'should_save': 1,
  \ },
  \}, g:ale_fix_buffer_data

Execute(ALEFix simple functions should be able to accept one argument, the buffer):
  let g:ale_fixers.testft = ['RemoveLastLineOneArg']
  ALEFix

Expect(There should be only two lines):
  a
  b

Execute(ALEFix functions returning jobs should be able to accept one argument):
  let g:ale_fixers.testft = ['CatLine']
  ALEFix

Expect(An extra line should be added):
  a
  b
  c
  d