Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
* Use `vspec` for RSpec-like syntax
* `vspec` relies on `vim-flavor` to run the specs
* Move command setup to a local function for testability

Closes thoughtbot#42
  • Loading branch information
Greg Lazarev committed Dec 9, 2014
1 parent 65735f6 commit 102821b
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.vim-flavor
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ terminal.
let g:rspec_runner = "os_x_iterm"
```

## Running tests

Tests are written using [`vim-vspec`](https://github.com/kana/vim-vspec)
and run with [`vim-flavor`](https://github.com/kana/vim-flavor).

Install the `vim-flavor` gem, install the dependencies and run the tests:

```
gem install vim-flavor
vim-flavor install
vim-flavor test
```

Credits
-------

Expand Down
1 change: 1 addition & 0 deletions VimFlavor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flavor 'kana/vim-vspec'
1 change: 1 addition & 0 deletions VimFlavor.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kana/vim-vspec (1.4.1)
54 changes: 37 additions & 17 deletions plugin/rspec.vim
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
let s:plugin_path = expand("<sfile>:p:h:h")
let s:forceGUI = 0

if !exists("g:rspec_runner")
let g:rspec_runner = "os_x_terminal"
endif

if exists("g:rspec_command")
if has("gui_running") && has("gui_macvim")
let s:rspec_command = "silent !" . s:plugin_path . "/bin/" . g:rspec_runner . " '" . g:rspec_command . "'"
function! s:SetupCommand()
if exists("g:rspec_command")
if s:isGUI()
let s:rspec_command = "silent !" . s:plugin_path . "/bin/" . g:rspec_runner . " '" . g:rspec_command . "'"
else
let s:rspec_command = g:rspec_command
endif
else
let s:rspec_command = g:rspec_command
endif
else
let s:cmd = "rspec {spec}"
let s:cmd = "rspec {spec}"

if has("gui_running") && has("gui_macvim")
let s:rspec_command = "silent !" . s:plugin_path . "/bin/" . g:rspec_runner . " '" . s:cmd . "'"
elseif has("win32") && fnamemodify(&shell, ':t') ==? "cmd.exe"
let s:rspec_command = "!cls && echo " . s:cmd . " && " . s:cmd
else
let s:rspec_command = "!clear && echo " . s:cmd . " && " . s:cmd
if s:isGUI()
let s:rspec_command = "silent !" . s:plugin_path . "/bin/" . g:rspec_runner . " '" . s:cmd . "'"
elseif has("win32") && fnamemodify(&shell, ':t') ==? "cmd.exe"
let s:rspec_command = "!cls && echo " . s:cmd . " && " . s:cmd
else
let s:rspec_command = "!clear && echo " . s:cmd . " && " . s:cmd
endif
endif
endif
endfunction

function! RunAllSpecs()
let l:spec = "spec"
Expand Down Expand Up @@ -49,8 +52,8 @@ function! RunNearestSpec()
endfunction

function! RunLastSpec()
if exists("s:last_spec_command")
call RunSpecs(s:last_spec_command)
if exists("s:last_spec_location")
call RunSpecs(s:last_spec_location)
endif
endfunction

Expand All @@ -59,9 +62,26 @@ function! InSpecFile()
endfunction

function! SetLastSpecCommand(spec)
let s:last_spec_command = a:spec
let s:last_spec_location = a:spec
endfunction

function! RunSpecs(spec)
execute substitute(s:rspec_command, "{spec}", a:spec, "g")
endfunction

function! s:isGUI()
return s:forceGUI || (has("gui_running") && has("gui_macvim"))
endfunction

" begin vspec config
function! rspec#testScope()
return s:
endfunction

function! rspec#sid()
return maparg('<SID>', 'n')
endfunction
nnoremap <SID> <SID>
" end vspec config

call s:SetupCommand()
83 changes: 83 additions & 0 deletions t/rspec_test.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
source plugin/rspec.vim

call vspec#hint({"scope": "rspec#testScope()", "sid": "rspec#sid()"})

describe "SetupCommand"
it "sets the default command"
Expect Ref("s:rspec_command") == "!clear && echo rspec {spec} && rspec {spec}"
end

context "when g:rspec_command is not defined"
context "when in GUI"
context "when g:rspec_runner is defined"
before
call Set("s:forceGUI", 1)
let s:original_runner = g:rspec_runner
let g:rspec_runner = "iterm"
end

after
let g:rspec_runner = s:original_runner
call Set("s:forceGUI", 0)
end

it "sets the command with provided runner"
call Call("s:SetupCommand")

Expect Ref("s:rspec_command") =~ "^silent !\.\*/bin/iterm 'rspec {spec}'$"
end
end

context "when rspec_runner is not defined"
before
call Set("s:forceGUI", 1)
end

after
call Set("s:forceGUI", 0)
end

it "sets the command with default runner"
call Call("s:SetupCommand")

Expect Ref("s:rspec_command") =~ "^silent !\.\*/bin/os_x_terminal 'rspec {spec}'$"
end
end
end
end

context "when g:rspec_command is defined"
before
call Set("s:forceGUI", 0)
let g:rspec_command = "Dispatch rspec {spec}"
end

after
unlet g:rspec_command
end

context "when not in GUI"
it "sets the provided command"
call Call("s:SetupCommand")

Expect Ref("s:rspec_command") == "Dispatch rspec {spec}"
end
end

context "when in GUI"
before
call Set("s:forceGUI", 1)
end

after
call Set("s:forceGUI", 0)
end

it "sets the provided GUI command"
call Call("s:SetupCommand")

Expect Ref("s:rspec_command") =~ "^silent !\.\*/bin/os_x_terminal 'Dispatch rspec {spec}'$"
end
end
end
end

0 comments on commit 102821b

Please sign in to comment.