summaryrefslogtreecommitdiff
path: root/autoload/ale/assert.vim
blob: 6c2586a6b4534369ac18ca76194944309c2cbbf3 (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
let s:chain_results = []

function! ale#assert#WithChainResults(...) abort
    let s:chain_results = a:000
endfunction

function! s:GetLinter() abort
    let l:linters = ale#linter#GetLintersLoaded()
    let l:filetype_linters = get(values(l:linters), 0, [])

    if len(l:linters) is 0 || len(l:filetype_linters) is 0
        throw 'No linters were loaded'
    endif

    if len(l:linters) > 1 || len(l:filetype_linters) > 1
        throw 'More than one linter was loaded'
    endif

    return l:filetype_linters[0]
endfunction

" Load the currently loaded linter for a test case, and check that the command
" matches the given string.
function! ale#assert#Linter(expected_executable, expected_command) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:executable = ale#linter#GetExecutable(l:buffer, l:linter)

    while ale#command#IsDeferred(l:executable)
        call ale#test#FlushJobs()
        let l:executable = l:executable.value
    endwhile

    if has_key(l:linter, 'command_chain')
        let l:callbacks = map(copy(l:linter.command_chain), 'v:val.callback')

        " If the expected command is a string, just check the last one.
        if type(a:expected_command) is v:t_string
            if len(l:callbacks) is 1
                let l:command = call(l:callbacks[0], [l:buffer])
            else
                let l:input = get(s:chain_results, len(l:callbacks) - 2, [])
                let l:command = call(l:callbacks[-1], [l:buffer, l:input])
            endif
        else
            let l:command = []
            let l:chain_index = 0

            for l:Callback in l:callbacks
                if l:chain_index is 0
                    call add(l:command, call(l:Callback, [l:buffer]))
                else
                    let l:input = get(s:chain_results, l:chain_index - 1, [])
                    call add(l:command, call(l:Callback, [l:buffer, l:input]))
                endif

                let l:chain_index += 1
            endfor
        endif
    else
        let l:command = ale#linter#GetCommand(l:buffer, l:linter)

        while ale#command#IsDeferred(l:command)
            call ale#test#FlushJobs()
            let l:command = l:command.value
        endwhile
    endif

    if type(l:command) is v:t_string
        " Replace %e with the escaped executable, so tests keep passing after
        " linters are changed to use %e.
        let l:command = substitute(l:command, '%e', '\=ale#Escape(l:executable)', 'g')
    elseif type(l:command) is v:t_list
        call map(l:command, 'substitute(v:val, ''%e'', ''\=ale#Escape(l:executable)'', ''g'')')
    endif

    AssertEqual
    \   [a:expected_executable, a:expected_command],
    \   [l:executable, l:command]
endfunction

function! ale#assert#LinterNotExecuted() abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:executable = ale#linter#GetExecutable(l:buffer, l:linter)

    Assert empty(l:executable), "The linter will be executed when it shouldn't be"
endfunction

function! ale#assert#LSPOptions(expected_options) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:initialization_options = ale#lsp_linter#GetOptions(l:buffer, l:linter)

    AssertEqual a:expected_options, l:initialization_options
endfunction

function! ale#assert#LSPConfig(expected_config) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:config = ale#lsp_linter#GetConfig(l:buffer, l:linter)

    AssertEqual a:expected_config, l:config
endfunction

function! ale#assert#LSPLanguage(expected_language) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:language = ale#util#GetFunction(l:linter.language_callback)(l:buffer)

    AssertEqual a:expected_language, l:language
endfunction

function! ale#assert#LSPProject(expected_root) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:root = ale#lsp_linter#FindProjectRoot(l:buffer, l:linter)

    AssertEqual a:expected_root, l:root
endfunction

function! ale#assert#LSPAddress(expected_address) abort
    let l:buffer = bufnr('')
    let l:linter = s:GetLinter()
    let l:address = ale#linter#GetAddress(l:buffer, l:linter)

    AssertEqual a:expected_address, l:address
endfunction

function! ale#assert#SetUpLinterTestCommands() abort
    command! -nargs=+ WithChainResults :call ale#assert#WithChainResults(<args>)
    command! -nargs=+ AssertLinter :call ale#assert#Linter(<args>)
    command! -nargs=0 AssertLinterNotExecuted :call ale#assert#LinterNotExecuted()
    command! -nargs=+ AssertLSPOptions :call ale#assert#LSPOptions(<args>)
    command! -nargs=+ AssertLSPConfig :call ale#assert#LSPConfig(<args>)
    command! -nargs=+ AssertLSPLanguage :call ale#assert#LSPLanguage(<args>)
    command! -nargs=+ AssertLSPProject :call ale#assert#LSPProject(<args>)
    command! -nargs=+ AssertLSPAddress :call ale#assert#LSPAddress(<args>)
endfunction

" A dummy function for making sure this module is loaded.
function! ale#assert#SetUpLinterTest(filetype, name) abort
    " Set up a marker so ALE doesn't create real random temporary filenames.
    let g:ale_create_dummy_temporary_file = 1

    " Remove current linters.
    call ale#linter#Reset()
    call ale#linter#PreventLoading(a:filetype)

    let l:prefix = 'ale_' . a:filetype . '_' . a:name
    let b:filter_expr = 'v:val[: len(l:prefix) - 1] is# l:prefix'

    Save g:ale_lsp_root
    let g:ale_lsp_root = {}

    Save b:ale_lsp_root
    unlet! b:ale_lsp_root

    Save g:ale_c_build_dir
    unlet! g:ale_c_build_dir

    " Save and clear linter variables.
    " We'll load the runtime file to reset them to defaults.
    for l:key in filter(keys(g:), b:filter_expr)
        execute 'Save g:' . l:key
        unlet g:[l:key]
    endfor

    unlet! b:ale_c_build_dir

    for l:key in filter(keys(b:), b:filter_expr)
        unlet b:[l:key]
    endfor

    execute 'runtime ale_linters/' . a:filetype . '/' . a:name . '.vim'

    if !exists('g:dir')
        call ale#test#SetDirectory('/testplugin/test/command_callback')
    endif

    call ale#assert#SetUpLinterTestCommands()
endfunction

function! ale#assert#TearDownLinterTest() abort
    unlet! g:ale_create_dummy_temporary_file
    let s:chain_results = []

    if exists(':WithChainResults')
        delcommand WithChainResults
    endif

    if exists(':AssertLinter')
        delcommand AssertLinter
    endif

    if exists(':AssertLinterNotExecuted')
        delcommand AssertLinterNotExecuted
    endif

    if exists(':AssertLSPOptions')
        delcommand AssertLSPOptions
    endif

    if exists(':AssertLSPConfig')
        delcommand AssertLSPConfig
    endif

    if exists(':AssertLSPLanguage')
        delcommand AssertLSPLanguage
    endif

    if exists(':AssertLSPProject')
        delcommand AssertLSPProject
    endif

    if exists(':AssertLSPAddress')
        delcommand AssertLSPAddress
    endif

    if exists('g:dir')
        call ale#test#RestoreDirectory()
    endif

    Restore

    call ale#linter#Reset()

    if exists('*ale#semver#ResetVersionCache')
        call ale#semver#ResetVersionCache()
    endif
endfunction