|
| 1 | +" Get file path |
| 2 | +let s:plugin_path = expand("<sfile>:p:h:h") |
| 3 | + |
| 4 | +" Determine which command based on filetype |
| 5 | +function! s:GetCorrectCommand() |
| 6 | + " Set default {rspec} command (ruby/rails) |
| 7 | + if &filetype ==? 'ruby' |
| 8 | + if !exists("g:rspec_command") |
| 9 | + let s:cmd = "rspec {spec}" |
| 10 | + call s:GUIRunning() |
| 11 | + else |
| 12 | + let g:spec_command = g:rspec_command |
| 13 | + endif |
| 14 | + " Set default {mocha} command (javascript) |
| 15 | + elseif &filetype ==? 'javascript' |
| 16 | + if !exists("g:mocha_js_command") |
| 17 | + let s:cmd = "mocha {spec}" |
| 18 | + call s:GUIRunning() |
| 19 | + else |
| 20 | + let g:spec_command = g:mocha_js_command |
| 21 | + endif |
| 22 | + " Set default {mocha} command (coffeescript) |
| 23 | + elseif &filetype ==? 'coffee' |
| 24 | + if !exists("g:mocha_coffee_command") |
| 25 | + let s:cmd = "mocha --compilers coffee:coffee-script {spec}" |
| 26 | + call s:GUIRunning() |
| 27 | + else |
| 28 | + let g:spec_command = g:mocha_coffee_command |
| 29 | + endif |
| 30 | + " Fallthrough default |
| 31 | + else |
| 32 | + let g:spec_command = "" |
| 33 | + endif |
| 34 | +endfunction |
| 35 | + |
| 36 | +" Run GUI version or Terminal version |
| 37 | +function! s:GUIRunning() |
| 38 | + if has("gui_running") && has("gui_macvim") |
| 39 | + let g:spec_command = "silent !" . s:plugin_path . "/bin/run_in_os_x_terminal '" . s:cmd . "'" |
| 40 | + else |
| 41 | + let g:spec_command = "!echo " . s:cmd . " && " . s:cmd |
| 42 | + endif |
| 43 | +endfunction |
| 44 | + |
| 45 | +" Mocha Nearest Test |
| 46 | +function! s:GetNearestTest() |
| 47 | + let callLine = line (".") "cursor line |
| 48 | + let file = readfile(expand("%:p")) "read current file |
| 49 | + let lineCount = 0 "file line counter |
| 50 | + let lineDiff = 999 "arbituary large number |
| 51 | + let descPattern='\v\_^\s*it\s*[(]*\s*[''"]{1}\zs[^''"]+\ze[''"]{1}' |
| 52 | + for line in file |
| 53 | + let lineCount += 1 |
| 54 | + let match = match(line,descPattern) |
| 55 | + if(match != -1) |
| 56 | + let currentDiff = callLine - lineCount |
| 57 | + " break if closest test is the next test |
| 58 | + if(currentDiff < 0 && lineDiff != 999) |
| 59 | + break |
| 60 | + endif |
| 61 | + " if closer test is found, cache new nearest test |
| 62 | + if(currentDiff <= lineDiff) |
| 63 | + let lineDiff = currentDiff |
| 64 | + let s:nearestTest = matchstr(line,descPattern) |
| 65 | + endif |
| 66 | + endif |
| 67 | + endfor |
| 68 | +endfunction |
| 69 | + |
| 70 | +" All Specs |
| 71 | +function! RunAllSpecs() |
| 72 | + if isdirectory('spec') |
| 73 | + let l:spec = "spec" |
| 74 | + else |
| 75 | + let l:spec = "test" |
| 76 | + endif |
| 77 | + call SetLastSpecCommand(l:spec) |
| 78 | + call RunSpecs(l:spec) |
| 79 | +endfunction |
| 80 | + |
| 81 | +" Current File |
| 82 | +function! RunCurrentSpecFile() |
| 83 | + if InSpecFile() |
| 84 | + let l:spec = @% |
| 85 | + call SetLastSpecCommand(l:spec) |
| 86 | + call RunSpecs(l:spec) |
| 87 | + else |
| 88 | + call RunLastSpec() |
| 89 | + endif |
| 90 | +endfunction |
| 91 | + |
| 92 | +" Nearest Spec |
| 93 | +function! RunNearestSpec() |
| 94 | + if InSpecFile() |
| 95 | + if &filetype ==? "ruby" |
| 96 | + let l:spec = @% . ":" . line(".") |
| 97 | + else |
| 98 | + call s:GetNearestTest() |
| 99 | + let l:spec = @% . " -g '" . s:nearestTest . "'" |
| 100 | + end |
| 101 | + call SetLastSpecCommand(l:spec) |
| 102 | + call RunSpecs(l:spec) |
| 103 | + else |
| 104 | + call RunLastSpec() |
| 105 | + endif |
| 106 | +endfunction |
| 107 | + |
| 108 | +" Last Spec |
| 109 | +function! RunLastSpec() |
| 110 | + if exists("s:last_spec_command") |
| 111 | + call RunSpecs(s:last_spec_command) |
| 112 | + endif |
| 113 | +endfunction |
| 114 | + |
| 115 | +" Current Spec File Name |
| 116 | +function! InSpecFile() |
| 117 | + if &filetype ==? "ruby" |
| 118 | + return match(expand("%"), "_spec.rb$") != -1 || match(expand("%"), ".feature$") != -1 |
| 119 | + else |
| 120 | + return match(expand("%"), "^test/") != -1 || match(expand("%"), "^spec/") != -1 |
| 121 | + end |
| 122 | +endfunction |
| 123 | + |
| 124 | +" Cache Last Spec Command |
| 125 | +function! SetLastSpecCommand(spec) |
| 126 | + let s:last_spec_command = a:spec |
| 127 | +endfunction |
| 128 | + |
| 129 | +" Spec Runner |
| 130 | +function! RunSpecs(spec) |
| 131 | + call s:GetCorrectCommand() |
| 132 | + if g:spec_command ==? "" |
| 133 | + echom "No spec command specified." |
| 134 | + else |
| 135 | + execute substitute(g:spec_command, "{spec}", a:spec, "g") |
| 136 | + end |
| 137 | +endfunction |
| 138 | + |
0 commit comments