Skip to content

Commit

Permalink
rspec testing in place
Browse files Browse the repository at this point in the history
back-fill vimrunner specs
small bugfix
  • Loading branch information
Andrew Bruce committed Apr 1, 2012
1 parent 85b7932 commit faf0677
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .rspec
@@ -0,0 +1,4 @@
--color
--debugger
--order random
-fd
81 changes: 81 additions & 0 deletions .rvmrc
@@ -0,0 +1,81 @@
#!/usr/bin/env bash

# This is an RVM Project .rvmrc file, used to automatically load the ruby
# development environment upon cd'ing into the directory

# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
environment_id="ruby-1.9.2-p290@test_server"

#
# Uncomment the following lines if you want to verify rvm version per project
#
# rvmrc_rvm_version="1.10.2" # 1.10.1 seams as a safe start
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
# return 1
# }
#

#
# Uncomment following line if you want options to be set only for given project.
#
# PROJECT_JRUBY_OPTS=( --1.9 )
#
# The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
#
# chmod +x ${rvm_path}/hooks/after_use_jruby_opts
#

#
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
#
if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"

if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
then
. "${rvm_path:-$HOME/.rvm}/hooks/after_use"
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
if ! rvm --create use "$environment_id"
then
echo "Failed to create RVM environment '${environment_id}'."
return 1
fi
fi

#
# If you use an RVM gemset file to install a list of gems (*.gems), you can have
# it be automatically loaded. Uncomment the following and adjust the filename if
# necessary.
#
# filename=".gems"
# if [[ -s "$filename" ]]
# then
# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
# fi

# If you use bundler, this might be useful to you:
# if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
# then
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
# gem install bundler
# fi
# if [[ -s Gemfile ]] && command -v bundle
# then
# bundle install
# fi

if [[ $- == *i* ]] # check for interactive shells
then
echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
else
echo "Using: $GEM_HOME" # don't use colors in interactive shells
fi

5 changes: 5 additions & 0 deletions Gemfile
@@ -0,0 +1,5 @@
source "http://rubygems.org"

gem 'rspec'
gem 'vimrunner', git: "https://github.com/camelpunch/vimrunner"
gem 'ruby-debug19'
40 changes: 40 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,40 @@
GIT
remote: https://github.com/camelpunch/vimrunner
revision: 9e1ebfc1026f8d24f93c5fe8f3db526dc8299557
specs:
vimrunner (0.0.2)

GEM
remote: http://rubygems.org/
specs:
archive-tar-minitar (0.5.2)
columnize (0.3.6)
diff-lcs (1.1.3)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
rspec-mocks (~> 2.9.0)
rspec-core (2.9.0)
rspec-expectations (2.9.0)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)

PLATFORMS
ruby

DEPENDENCIES
rspec
ruby-debug19
vimrunner!
1 change: 1 addition & 0 deletions fakepipe
@@ -0,0 +1 @@

96 changes: 53 additions & 43 deletions plugin/test_client.vim
Expand Up @@ -24,48 +24,58 @@ let g:non_test_filename_replacements = [
\ ['.rb', '_spec.rb']
\]

function! RunTest()
fun! RunTest()
if s:TestIsExecutable()
call s:SendToTestServer(s:AppropriateTestFilename())
endfunction

function! RunTestLine()
if s:InTestFile()
call s:SendToTestServer(s:AppropriateTestFilename() . ':' . line('.'))
else
echom "Focused test doesn't make sense (not in a test)."
endif
endfunction

function! s:AppropriateTestFilename()
let l:filename = expand('%')

if s:InTestFile()
return g:test_cmd_for_test_types[&ft] . ' ' . l:filename
else
return g:test_cmd_for_src_types[&ft] . ' ' . s:AssociatedTestFilename(l:filename)
endif
endfunction

function! s:SendToTestServer(command)
call writefile([a:command], g:test_server_pipe)
echom "Sent " . a:command
endfunction

function! s:InTestFile()
return has_key(g:test_cmd_for_test_types, &ft)
endfunction

function! s:AssociatedTestFilename(src_filename)
return s:MultiSubString(a:src_filename, g:non_test_filename_replacements)
endfunction

function! s:MultiSubString(string, substitutions)
let l:substituted = substitute(a:string, a:substitutions[0][0], a:substitutions[0][1], '')

if len(a:substitutions) == 1
return l:substituted
else
return s:MultiSubString(l:substituted, a:substitutions[1:-1])
endif
endfunction
endif
endf

fun! RunTestLine()
if s:InTestFile()
call s:SendToTestServer(s:AppropriateTestFilename() . ':' . line('.'))
else
echom "Focused test doesn't make sense (not in a test)."
endif
endf

fun! s:AppropriateTestFilename()
let l:filename = expand('%')

if s:InTestFile()
return g:test_cmd_for_test_types[&ft] . ' ' . l:filename
else
return g:test_cmd_for_src_types[&ft] . ' ' . s:AssociatedTestFilename(l:filename)
endif
endf

fun! s:SendToTestServer(command)
call writefile([a:command], g:test_server_pipe)
echom "Sent " . a:command
endf

fun! s:InTestFile()
return has_key(g:test_cmd_for_test_types, &ft)
endf

fun! s:InSrcFile()
return has_key(g:test_cmd_for_src_types, &ft)
endf

fun! s:TestIsExecutable()
return s:InTestFile() || s:InSrcFile()
endf

fun! s:AssociatedTestFilename(src_filename)
return s:MultiSubString(a:src_filename, g:non_test_filename_replacements)
endf

fun! s:MultiSubString(string, substitutions)
let l:substituted = substitute(a:string, a:substitutions[0][0], a:substitutions[0][1], '')

if len(a:substitutions) == 1
return l:substituted
else
return s:MultiSubString(l:substituted, a:substitutions[1:-1])
endif
endf

Empty file added spec/example_spec.rb
Empty file.
87 changes: 87 additions & 0 deletions spec/test_client_spec.rb
@@ -0,0 +1,87 @@
require 'tempfile'
require 'vimrunner'

# NB: don't type / move mouse while spec is running!
describe "test_client.vim" do
let!(:vim) do
Vimrunner::Runner.start_vim.tap do |vim|
vim.normal ":let g:test_server_pipe = '#{file.path}'<CR>"
vim.command "source plugin/test_client.vim"
end
end

after do
vim.normal ":q!<CR>"
FileUtils.rm_f ".#{filename}.swp" if defined? filename
end

let(:file) { Tempfile.new('test_client_spec_pipe') }
let(:spec_filename) { 'example_spec.rb' }

def run_test
vim.command 'call RunTest()'
end

def run_line
vim.command 'call RunTestLine()'
end

def edit(path)
vim.edit path
end

def pipe
file.read
end

describe "running when there is no file open" do
it "does not send anything to the pipe" do
run_test
pipe.should be_empty
end
end

shared_examples_for "the whole spec is run" do
context "(shared)" do
before { edit filename }

it "sends the spec file path to the pipe" do
run_test
pipe.should =~ /#{spec_filename}$/
end

it "shows an appropriate message" do
run_test.should =~ /#{spec_filename}/
end
end
end

describe "running a whole spec from the spec file" do
let(:filename) { 'example_spec.rb' }
it_behaves_like "the whole spec is run"
end

describe "running a whole spec from the source file" do
let(:filename) { 'example.rb' }
it_behaves_like "the whole spec is run"
end

describe "running a line spec focused to a line from the spec file" do
let(:filename) { 'example_spec.rb' }

before do
edit filename
vim.insert((['some text'] * 10).join("\n"))
vim.normal "5gg"
end

it "sends the spec file path to the pipe, with the line number" do
run_line
pipe.should =~ /#{spec_filename}:5$/
end

it "shows an appropriate message" do
run_line.should =~ /#{spec_filename}:5/
end
end
end
11 changes: 11 additions & 0 deletions spec/test_client_spec.vim
@@ -0,0 +1,11 @@
call writefile([''], 'fakepipe')

let g:dir = expand('<sfile>:p:h')
let g:spec_filename = [g:dir . '/example_spec.rb']
SpecBegin 'title': 'test client',
\ 'sfile': g:dir . '/../plugin/test_client.vim',
\ 'file': g:spec_filename,
\ 'before': 'call RunTest()'

It should be able to run the whole test file.
Should be equal readfile('fakepipe'), g:spec_filename

0 comments on commit faf0677

Please sign in to comment.