diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..04c4a030 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test/$VADER_OUTPUT_FILE diff --git a/.travis.yml b/.travis.yml index eb92b351..edcd3b98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,4 +7,4 @@ install: - sudo pip install vim-vint script: - - bash test/test.sh; + - bash test/test.sh diff --git a/autoload/neoformat.vim b/autoload/neoformat.vim index f44f308d..7f741f26 100644 --- a/autoload/neoformat.vim +++ b/autoload/neoformat.vim @@ -10,7 +10,7 @@ function! neoformat#Neoformat(user_formatter) abort else let formatters = s:get_enabled_formatters(&filetype) if formatters == [] - call neoformat#utils#msg('no formatter defined for the ' . &filetype . ' filetype') + call neoformat#utils#msg('formatter not defined for ' . &filetype . ' filetype') return neoformat#format#BasicFormat() endif @@ -27,7 +27,7 @@ function! neoformat#Neoformat(user_formatter) abort elseif exists('g:neoformat#' . &filetype . '#' . formatter) let definition = g:neoformat#{&filetype}#{formatter} else - call neoformat#utils#log('no definition found for formatter: ' . formatter) + call neoformat#utils#log('definition not found for formatter: ' . formatter) if !empty(a:user_formatter) call neoformat#utils#msg('formatter definition for ' . a:user_formatter . ' not found') return neoformat#format#BasicFormat() diff --git a/test/autocomplete_test.vim b/test/autocomplete_test.vim new file mode 100644 index 00000000..fe753961 --- /dev/null +++ b/test/autocomplete_test.vim @@ -0,0 +1,64 @@ +let s:tests = {} +" Utilities +function! s:Run(function) + let o = &filetype + " let output = call(a:function, ['']) + " call function(a:function) + let output = call(a:function, []) + let &filetype = o + " expecting output to either be a 1 or 0 + let s:tests[a:function[2:]] = output + return output +endfunction + +function! s:Cleanup() + let error = 0 + for test in keys(s:tests) + if !s:tests[test] + let error = 1 + endif + echom test . ' : ' . s:tests[test] + endfor + + if error + " make vim exit with a non-zero value + cquit! + else + qall! + endif +endfunction + +" Test Definitions +function! s:valid_option() + let &filetype = 'python' + + let g:neoformat_python_enabled = ['autopep8', 'yapf'] + let out = neoformat#CompleteFormatters('auto','', 0) + + return ['autopep8'] == out +endfunction + +function! s:invalid_option() + let &filetype = 'javascript' + + let g:neoformat_python_enabled = ['autopep8'] + let out = neoformat#CompleteFormatters('autopep8', '', 0) + + return [] == out +endfunction + +function! s:multi_options() + let &filetype = 'python' + let g:neoformat_python_enabled = ['autopep8', 'yapf'] + let out = neoformat#CompleteFormatters('autopep8', '', 0) + + return [] == out +endfunction + +" Run Tests +call s:Run('s:valid_option') +call s:Run('s:invalid_option') + + +" Check the outputs +call s:Cleanup() diff --git a/test/neoformat.vader b/test/neoformat.vader new file mode 100644 index 00000000..74ccaa32 --- /dev/null +++ b/test/neoformat.vader @@ -0,0 +1,43 @@ +Before: + function! g:CmdOutput(cmd) + redir => out + silent exe a:cmd + redir END + return out[1:] + endfunction + +Execute (empty filetype): + let &filetype = '' + let out = g:CmdOutput('Neoformat') + AssertEqual 'Neoformat: formatter not defined for filetype', out + +Execute (filetype without defined formatter): + let &filetype = 'not_real_filetype' + let out = g:CmdOutput('Neoformat') + + AssertEqual 'Neoformat: formatter not defined for ' . &filetype . ' filetype', out + +Execute (formatter defined for other filetype, but is called via user cmd): + let &filetype = 'javascript' + let out = g:CmdOutput('Neoformat autopep8') + + AssertEqual 'Neoformat: formatter definition for autopep8 not found', out + +Execute (user disabled all formatters for current (python) filetype): + let g:neoformat_enabled_python= [] + let &filetype = 'python' + let out = g:CmdOutput('Neoformat') + + AssertEqual 'Neoformat: formatter not defined for ' . &filetype . ' filetype', out + +Execute (user disabled all formatters for current (python) filetype): + let g:neoformat_enabled_python= ['not_real_formatter'] + let &filetype = 'python' + let out = g:CmdOutput('Neoformat') + + AssertEqual 'Neoformat: attempted all formatters available for current filetype', out + +Execute (invalid user cmd: formatter not defined): + let out = g:CmdOutput('Neoformat not_a_real_formatter') + + AssertEqual 'Neoformat: formatter definition for not_a_real_formatter not found', out diff --git a/test/test.sh b/test/test.sh index f4f50c5e..cd175239 100755 --- a/test/test.sh +++ b/test/test.sh @@ -1,7 +1,37 @@ #!/usr/bin/env bash +if [[ $OSTYPE == darwin* ]]; then + OS='mac' +elif [[ $OSTYPE == linux-gnu* ]]; then + OS='linux' +else + OS='unknown' +fi + +# Vint if ! hash vint 2>/dev/null; then pip install vint fi - vint . + +# Vader +if [ ! -d "$HOME/.vim/plugged/vader.vim" ]; then + git clone https://github.com/junegunn/vader.vim.git ~/.vim/plugged/vader.vim +fi + +# Make sure neovim is installed +if ! hash nvim 2>/dev/null; then + if [[ $OS == 'linux' ]]; then + sudo add-apt-repository -y ppa:neovim-ppa/unstable + sudo apt-get update + sudo apt-get install -y neovim + elif [[ $OS == 'mac' ]]; then + brew install neovim + fi +fi + +# Run Vader Tests +cd "$( dirname "${BASH_SOURCE[0]}" )" && nvim -u vimrc -c 'Vader! *.vader' > /dev/null + +# Run Autocomplete Tests +nvim -u vimrc -c 'source autocomplete_test.vim' > /dev/null diff --git a/test/utils.vader b/test/utils.vader new file mode 100644 index 00000000..7ee1349e --- /dev/null +++ b/test/utils.vader @@ -0,0 +1,26 @@ +Before: + function! g:CmdOutput(cmd) + redir => out + silent exe 'call ' . a:cmd + redir END + return out[1:] + endfunction + +Execute (test log util): + let g:neoformat_verbose = 1 + let out = g:CmdOutput('neoformat#utils#log("test")') + + let g:neoformat_verbose = 0 + AssertEqual 'Neoformat: test', out + +Execute (test msg util): + let g:neoformat_verbose = 0 + let out = g:CmdOutput('neoformat#utils#msg("test")') + + AssertEqual 'Neoformat: test', out + +Execute (test warn util): + let g:neoformat_verbose = 0 + let out = g:CmdOutput('neoformat#utils#warn("test")') + + AssertEqual 'Neoformat: test', out diff --git a/test/vimrc b/test/vimrc new file mode 100644 index 00000000..2ed4bfde --- /dev/null +++ b/test/vimrc @@ -0,0 +1,6 @@ +filetype off +set runtimepath+=$HOME/.vim/plugged/vader.vim +set runtimepath+=../ +filetype plugin indent on +syntax enable +set nocompatible