diff options
author | w0rp <devw0rp@gmail.com> | 2018-07-06 22:46:36 +0100 |
---|---|---|
committer | w0rp <devw0rp@gmail.com> | 2018-07-06 22:46:36 +0100 |
commit | 4999ad7e7860459f4ebea2d36d63317e88dd3870 (patch) | |
tree | 86ac5b5e5d2e32fedda53d6538b51e3bd4e0f729 /autoload | |
parent | 344e0fec971eccb525200665bab550380b80f9a1 (diff) | |
download | ale-4999ad7e7860459f4ebea2d36d63317e88dd3870.zip |
Add an AssertLinter command for tests
Diffstat (limited to 'autoload')
-rw-r--r-- | autoload/ale/assert.vim | 50 | ||||
-rw-r--r-- | autoload/ale/linter.vim | 7 |
2 files changed, 57 insertions, 0 deletions
diff --git a/autoload/ale/assert.vim b/autoload/ale/assert.vim new file mode 100644 index 00000000..eae41596 --- /dev/null +++ b/autoload/ale/assert.vim @@ -0,0 +1,50 @@ +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) + + if has_key(l:linter, 'command_chain') + let l:command = [] + + for l:chain_item in l:linter.command_chain + if empty(l:command) + call add(l:command, call(l:chain_item.callback, [l:buffer])) + else + call add(l:command, call(l:chain_item.callback, [l:buffer, []])) + endif + endfor + else + let l:command = ale#linter#GetCommand(l:buffer, l:linter) + " 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') + endif + + AssertEqual + \ [a:expected_executable, a:expected_command], + \ [l:executable, l:command] +endfunction + +command! -nargs=+ AssertLinter :call ale#assert#Linter(<args>) + +" A dummy function for making sure this module is loaded. +function! ale#assert#Init() abort + call ale#linter#Reset() +endfunction diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index b0237bcb..9e518551 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -47,6 +47,13 @@ function! ale#linter#Reset() abort let s:linters = {} endfunction +" Return a reference to the linters loaded. +" This is only for tests. +" Do not call this function. +function! ale#linter#GetLintersLoaded() abort + return s:linters +endfunction + function! s:IsCallback(value) abort return type(a:value) == type('') || type(a:value) == type(function('type')) endfunction |