├── README └── plugin └── rubytest.vim /README: -------------------------------------------------------------------------------- 1 | Rubytest.vim 2 | ============ 3 | 4 | Rubytest.vim is a vim (http://www.vim.org) plugin, which helps you to run ruby test (including vanilla test, rspec, shoulda etc.) in vim. 5 | 6 | Installation 7 | ------------ 8 | 9 | Copy all files to your ~/.vim directory. 10 | 11 | Usage 12 | ----- 13 | 14 | After installation, press t will run the test under your cursor if you are editing a ruby test file. 15 | 16 | example: 17 | 18 | $ cd 19 | $ vim test/unit/user_test.rb 20 | (move cursor into a test case, press t) 21 | 22 | ( is mapping to '\' by default in vim) 23 | 24 | By default, only test errors will be shown to you. If some of your tests failed, the errors will be displayed in vim's quickfix window, and you can quick jump to the place where the error raise in source file by moving cursor onto the error message and press return (or ctrl-w return to open a new window and jump). If you don't want quickfix, just want to run tests and get results you can set g:rubytest_in_quickfix to 0 in your .vimrc file: 25 | 26 | let g:rubytest_in_quickfix = 0 27 | 28 | If you would like to use bundler set g:rubytest_cmd_bundler into your .vimrc: 29 | 30 | let g:rubytest_cmd_bundler = 1 31 | 32 | You can customize the command which will be used to run the test case by settting these options in your vimrc file: 33 | 34 | let g:rubytest_cmd_test = "ruby %p" 35 | let g:rubytest_cmd_testcase = "ruby %p -n '/%c/'" 36 | let g:rubytest_cmd_spec = "spec -f specdoc %p" 37 | let g:rubytest_cmd_example = "spec -f specdoc %p -e '%c'" 38 | let g:rubytest_cmd_feature = "cucumber %p" 39 | let g:rubytest_cmd_story = "cucumber %p -n '%c'" 40 | 41 | (%p will be replaced by the path of test file, %c will be replaced by the name of test case under cursor) 42 | 43 | Default Key Bindings 44 | -------------------- 45 | 46 | ts: run test case under cursor 47 | ta: run all tests in file 48 | tl: run the last test, from any buffer 49 | to: opens buffer with tests.tmp file where the test output is stored 50 | tq: closes opened buffer with tests.tmp 51 | 52 | You can change default key bindings: 53 | 54 | map \ RubyTestRun " change from t to \ 55 | map ] RubyFileRun " change from T to ] 56 | map / RubyTestRunLast " change from l to / 57 | 58 | Contributors 59 | ------------ 60 | 61 | * Bogdan Gusiev 62 | * Yung Hwa Kwon (nowk) 63 | * J. Weir 64 | -------------------------------------------------------------------------------- /plugin/rubytest.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin for running ruby tests 2 | " Last Change: Jun 17 2011 3 | " Maintainer: Jan 4 | " License: MIT License 5 | 6 | if exists("rubytest_loaded") 7 | finish 8 | endif 9 | let rubytest_loaded = 1 10 | 11 | if exists("g:rubytest_cmd_bundler") && g:rubytest_cmd_bundler == 1 12 | let g:bundler_command = 'bundle exec' 13 | else 14 | let g:bundler_command = '' 15 | endif 16 | if !exists("g:rubytest_in_quickfix") 17 | let g:rubytest_in_quickfix = 0 18 | endif 19 | if !exists("g:rubytest_spec_drb") 20 | let g:rubytest_spec_drb = 0 21 | endif 22 | if !exists("g:rubytest_cmd_test") 23 | let g:rubytest_cmd_test = " ruby -Itest %p" 24 | endif 25 | if !exists("g:rubytest_cmd_testcase") 26 | let g:rubytest_cmd_testcase = " ruby -Itest %p -n '/%c/'" 27 | endif 28 | if !exists("g:rubytest_cmd_spec") 29 | let g:rubytest_cmd_spec = " rspec -c %p" 30 | endif 31 | if !exists("g:rubytest_cmd_example") 32 | let g:rubytest_cmd_example = " rspec -c %p -l %c" 33 | endif 34 | if !exists("g:rubytest_cmd_feature") 35 | let g:rubytest_cmd_feature = " cucumber %p" 36 | endif 37 | if !exists("g:rubytest_cmd_story") 38 | let g:rubytest_cmd_story = " cucumber %p -n '%c'" 39 | endif 40 | 41 | function s:FindCase(patterns) 42 | let ln = a:firstline 43 | while ln > 0 44 | let line = getline(ln) 45 | for pattern in keys(a:patterns) 46 | if line =~ pattern 47 | if s:pattern == 'spec' 48 | return a:patterns[pattern](ln) 49 | else 50 | return a:patterns[pattern](line) 51 | endif 52 | endif 53 | endfor 54 | let ln -= 1 55 | endwhile 56 | return 'false' 57 | endfunction 58 | 59 | function s:EscapeBackSlash(str) 60 | return substitute(a:str, '\', '\\\\', 'g') 61 | endfunction 62 | 63 | function s:ExecTest(cmd) 64 | let g:rubytest_last_cmd = a:cmd 65 | 66 | if g:rubytest_in_quickfix > 0 67 | let s:oldefm = &efm 68 | let &efm = s:efm . s:efm_backtrace . ',' . s:efm_ruby . ',' . s:oldefm . ',%-G%.%#' 69 | 70 | cex system(a:cmd) 71 | cw 72 | 73 | let &efm = s:oldefm 74 | else 75 | silent execute "!" . g:bundler_command . a:cmd . " &> /tmp/tests.tmp &" | redraw! 76 | endif 77 | endfunction 78 | 79 | function s:RunTest() 80 | if s:test_scope == 1 81 | let cmd = g:rubytest_cmd_testcase 82 | elseif s:test_scope == 2 83 | let cmd = g:rubytest_cmd_test 84 | end 85 | 86 | let case = s:FindCase(s:test_case_patterns['test']) 87 | if s:test_scope == 2 || case != 'false' 88 | let case = substitute(case, "'\\|\"", '.', 'g') 89 | let cmd = substitute(cmd, '%c', case, '') 90 | let cmd = substitute(cmd, '%p', s:EscapeBackSlash(@%), '') 91 | 92 | if @% =~ '^test' 93 | let cmd = substitute(cmd, '^ruby ', 'ruby -Itest ', '') 94 | endif 95 | 96 | call s:ExecTest(cmd) 97 | else 98 | echo 'No test case found.' 99 | endif 100 | endfunction 101 | 102 | function s:RunSpec() 103 | if s:test_scope == 1 104 | let cmd = g:rubytest_cmd_example 105 | elseif s:test_scope == 2 106 | let cmd = g:rubytest_cmd_spec 107 | endif 108 | 109 | if g:rubytest_spec_drb > 0 110 | let cmd = cmd . " --drb" 111 | endif 112 | 113 | let case = s:FindCase(s:test_case_patterns['spec']) 114 | if s:test_scope == 2 || case != 'false' 115 | let cmd = substitute(cmd, '%c', case, '') 116 | let cmd = substitute(cmd, '%p', s:EscapeBackSlash(@%), '') 117 | call s:ExecTest(cmd) 118 | else 119 | echo 'No spec found.' 120 | endif 121 | endfunction 122 | 123 | function s:RunFeature() 124 | let s:old_in_quickfix = g:rubytest_in_quickfix 125 | let g:rubytest_in_quickfix = 0 126 | 127 | if s:test_scope == 1 128 | let cmd = g:rubytest_cmd_story 129 | elseif s:test_scope == 2 130 | let cmd = g:rubytest_cmd_feature 131 | endif 132 | 133 | let case = s:FindCase(s:test_case_patterns['feature']) 134 | if s:test_scope == 2 || case != 'false' 135 | let cmd = substitute(cmd, '%c', case, '') 136 | let cmd = substitute(cmd, '%p', s:EscapeBackSlash(@%), '') 137 | call s:ExecTest(cmd) 138 | else 139 | echo 'No story found.' 140 | endif 141 | 142 | let g:rubytest_in_quickfix = s:old_in_quickfix 143 | endfunction 144 | 145 | let s:test_patterns = {} 146 | let s:test_patterns['test'] = function('s:RunTest') 147 | let s:test_patterns['spec'] = function('s:RunSpec') 148 | let s:test_patterns['\.feature$'] = function('s:RunFeature') 149 | 150 | function s:GetTestCaseName1(str) 151 | return split(a:str)[1] 152 | endfunction 153 | 154 | function s:GetTestCaseName2(str) 155 | return "test_" . join(split(split(a:str, '"')[1]), '_') 156 | endfunction 157 | 158 | function s:GetTestCaseName3(str) 159 | return split(a:str, '"')[1] 160 | endfunction 161 | 162 | function s:GetTestCaseName4(str) 163 | return "test_" . join(split(split(a:str, "'")[1]), '_') 164 | endfunction 165 | 166 | function s:GetTestCaseName5(str) 167 | return split(a:str, "'")[1] 168 | endfunction 169 | 170 | function s:GetSpecLine(str) 171 | return a:str 172 | endfunction 173 | 174 | function s:GetStoryLine(str) 175 | return join(split(split(a:str, "Scenario:")[1])) 176 | endfunction 177 | 178 | let s:test_case_patterns = {} 179 | let s:test_case_patterns['test'] = {'^\s*def test':function('s:GetTestCaseName1'), '^\s*test \s*"':function('s:GetTestCaseName2'), "^\\s*test \\s*'":function('s:GetTestCaseName4'), '^\s*should \s*"':function('s:GetTestCaseName3'), "^\\s*should \\s*'":function('s:GetTestCaseName5')} 180 | let s:test_case_patterns['spec'] = {'^\s*\(it\|example\|describe\|context\) \s*':function('s:GetSpecLine')} 181 | let s:test_case_patterns['feature'] = {'^\s*Scenario:':function('s:GetStoryLine')} 182 | 183 | let s:save_cpo = &cpo 184 | set cpo&vim 185 | 186 | if !hasmapto('RubyTestRun') 187 | noremap ts RubyTestRun 188 | endif 189 | if !hasmapto('RubyFileRun') 190 | noremap ta RubyFileRun 191 | endif 192 | if !hasmapto('RubyTestRunLast') 193 | noremap tl RubyTestRunLast 194 | endif 195 | 196 | function s:IsRubyTest() 197 | for pattern in keys(s:test_patterns) 198 | if @% =~ pattern 199 | let s:pattern = pattern 200 | return 1 201 | endif 202 | endfor 203 | endfunction 204 | 205 | function s:Run(scope) 206 | if !s:IsRubyTest() 207 | echo "This file doesn't contain ruby test." 208 | else 209 | " test scope define what to test 210 | " 1: test case under cursor 211 | " 2: all tests in file 212 | let s:test_scope = a:scope 213 | call s:test_patterns[s:pattern]() 214 | endif 215 | endfunction 216 | 217 | function s:RunLast() 218 | if !exists("g:rubytest_last_cmd") 219 | echo "No previous test has been run" 220 | else 221 | let r = s:ExecTest(g:rubytest_last_cmd) 222 | end 223 | endfunction 224 | 225 | noremap