Skip to content

Instantly share code, notes, and snippets.

@joemsak
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joemsak/55d3ad8486967f02ebe5 to your computer and use it in GitHub Desktop.
Save joemsak/55d3ad8486967f02ebe5 to your computer and use it in GitHub Desktop.
function! RunTests(filename)
" Write the file and run tests for the given filename
if filereadable(@%)
:wa
else
:w
endif
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
:silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo
if match(a:filename, '\.feature$') != -1
exec ":!script/features " . a:filename
else
if filereadable("script/test")
exec ":!script/test " . a:filename
elseif filereadable("Gemfile")
if match(a:filename, '_test') != -1
exec ":!bundle exec rake test " . a:filename " Ruby TestUnit
else
exec ":!bundle exec rspec --color " . a:filename " Ruby RSpec with bundle exec
end
elseif match(a:filename, '_tests') != -1
exec ":!nosetests --rednose " . a:filename " Python
elseif match(a:filename, '_test') != -1
exec ":!lein difftest " . a:filename " Clojure
elseif match(a:filename, 'Test') != -1
exec ":!javac " . a:filename . ".java && java " . a:filename " java
else
exec ":!rspec --color " . a:filename " Ruby RSpec without bundle exec
end
end
endfunction
function! SetTestFile()
" Set the spec file that tests will be run for.
let t:jms_test_file=@%
endfunction
function! RunTestFile(...)
if a:0
let command_suffix = a:1
else
let command_suffix = ""
endif
" Run the tests for the previously-marked file.
let in_spec_file = match(expand("%"), '\(.feature\|_spec.rb\)$') != -1
let in_test_file = match(expand("%"), '\(.feature\|_test.\(?:rb\|clj|java\)\)$') != -1
let in_py_file = match(expand("%"), '\(.feature\|_tests.py\)$') != -1
if in_test_file || in_spec_file || in_py_file
call SetTestFile()
elseif !exists("t:jms_test_file")
return
end
call RunTests(t:jms_test_file . command_suffix)
endfunction
function! RunNearestTest()
let spec_line_number = line('.')
call RunTestFile(":" . spec_line_number . " -b")
endfunction
map <leader>t :call RunTestFile()<cr> " == Saves the current file and Runs either the test file you're in,
" or the last test file you ran if you're in a production file
map <leader>T :call RunNearestTest()<cr> " == Runs just the test that your cursor is in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment