Skip to content

Commit b73a168

Browse files
committed
Init dump
0 parents  commit b73a168

File tree

4 files changed

+233
-0
lines changed

4 files changed

+233
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2013 Nicholas 'Geekjuice' Hwang, inc.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# vim-spec
2+
3+
Spec runner for Vim. Run Rspec and Mocha tests straight from Vim.
4+
5+
Inspired by [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec) and
6+
[geekjuice/vim-mocha](https://github.com/geekjuice/vim-mocha).
7+
8+
## Installation
9+
10+
Recommended installation with [vundle](https://github.com/gmarik/vundle):
11+
12+
```vim
13+
Bundle 'geekjuice/vim-spec'
14+
```
15+
16+
If using zsh on OS X it may be necessary to run move `/etc/zshenv` to `/etc/zshrc`.
17+
18+
## Example of key mappings
19+
20+
```vim
21+
map <Leader>t :call RunCurrentSpecFile()<CR>
22+
map <Leader>s :call RunNearestSpec()<CR>
23+
map <Leader>l :call RunLastSpec()<CR>
24+
map <Leader>a :call RunAllSpecs()<CR>
25+
```
26+
27+
## Configuration
28+
29+
Like [thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec), the
30+
following variables can be overwritten for custom spec commands:
31+
32+
* `g:rspec_command`
33+
* `g:mocha_js_command`
34+
* `g:mocha_coffee_command`
35+
36+
Examples:
37+
38+
```vim
39+
let g:rspec_command = "!rspec --drb {spec}"
40+
let g:mocha_js_command = "!mocha --recursive --no-colors {spec}"
41+
let g:mocha_coffee_command = "!mocha -b --compilers coffee:coffee-script{spec}"
42+
43+
" Using test runners
44+
let g:rspec_command = "Dispatch zeus rspec {spec}"
45+
let g:mocha_coffee_command = "!coratdo {spec}" "See vim-mocha
46+
```
47+
48+
Credits
49+
-------
50+
51+
[thoughtbot/vim-rspec](https://github.com/thoughtbot/vim-rspec)
52+
53+
[geekjuice/vim-mocha](https://github.com/geekjuice/vim-mocha)
54+
55+
## License
56+
57+
vim-spec is released under the [MIT License](LICENSE).

bin/run_in_os_x_terminal

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/osascript
2+
3+
on run argv
4+
tell application "Terminal"
5+
if (count of windows) is 0 then
6+
do script argv
7+
else
8+
do script argv in window 1
9+
end if
10+
11+
activate
12+
end tell
13+
14+
tell application "MacVim"
15+
activate
16+
end tell
17+
end run

plugin/spec.vim

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

Comments
 (0)