Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testy #7

Closed
wants to merge 11 commits into from
38 changes: 27 additions & 11 deletions indent/cucumber.vim
Expand Up @@ -29,32 +29,48 @@ function! GetCucumberIndent()
let csyn = s:syn(v:lnum)
let nsyn = s:syn(nextnonblank(v:lnum+1))
if csyn ==# 'cucumberFeature' || cline =~# '^\s*Feature:'
" feature heading
return 0
elseif csyn ==# 'cucumberExamples' || cline =~# '^\s*\%(Examples\|Scenarios\):'
" examples heading
return 2 * &sw
elseif csyn =~# '^cucumber\%(Background\|Scenario\|ScenarioOutline\)$' || cline =~# '^\s*\%(Background\|Scenario\|Scenario Outline\):'
" background, scenario or outline heading
return &sw
elseif syn ==# 'cucumberFeature' || line =~# '^\s*Feature:'
" line after feature heading
return &sw
elseif syn ==# 'cucumberExamples' || line =~# '^\s*\%(Examples\|Scenarios\):'
" line after examples heading
return 3 * &sw

elseif cline =~# '^\s*[@#]' && (nsyn == 'cucumberFeature' || nline =~# '^\s*Feature:' || indent(prevnonblank(v:lnum-1)) <= 0)
" tag or comment before a feature heading
return 0

elseif cline =~# '^\s*#' && getline(v:lnum-1) =~# '^\s*$'
" comments after blank lines
return &sw

elseif syn =~# '^cucumber\%(Background\|Scenario\|ScenarioOutline\)$' || line =~# '^\s*\%(Background\|Scenario\|Scenario Outline\):'
" line after background, scenario or outline heading
return 2 * &sw
elseif cline =~# '^\s*@' && (nsyn == 'cucumberFeature' || nline =~# '^\s*Feature:' || indent(prevnonblank(v:lnum-1)) <= 0)
return 0
elseif line =~# '^\s*@'

elseif cline =~# '^\s*@'
" other tags
return &sw
elseif cline =~# '^\s*|' && line =~# '^\s*|'
elseif cline =~# '^\s*#' && getline(v:lnum-1) =~# '^\s*#'
" comments after comments, indent = indent
return indent(prevnonblank(v:lnum-1))
elseif cline =~# '^\s*[#|]' && line =~# '^\s*[#|]'
" mid-table, indent = indent
return indent(prevnonblank(v:lnum-1))
elseif cline =~# '^\s*|' && line =~# '^\s*[^|#]'
elseif cline =~# '^\s*|' && line =~# '^\s*[^|]'
" first line of a table, indent += sw
return indent(prevnonblank(v:lnum-1)) + &sw
elseif cline =~# '^\s*[^|# \t]' && line =~# '^\s*|'
elseif cline =~# '^\s*[^|]' && line =~# '^\s*|'
" line after a table, indent -= sw
return indent(prevnonblank(v:lnum-1)) - &sw
elseif cline =~# '^\s*$' && line =~# '^\s*|'
let in = indent(prevnonblank(v:lnum-1))
return in == indent(v:lnum) ? in : in - &sw
elseif cline =~# '^\s*#' && getline(v:lnum-1) =~ '^\s*$' && getline(v:lnum+1) =~# '\S'
return indent(getline(v:lnum+1))
endif
return indent(prevnonblank(v:lnum-1))
endfunction
Expand Down
20 changes: 20 additions & 0 deletions spec/be_same_text.rb
@@ -0,0 +1,20 @@
RSpec::Matchers.define :be_same_text do |expected|
match do |actual|
expected == actual
end

failure_message_for_should do |actual|
require 'tempfile'
diff = nil
Tempfile.open('actual') do |actual_f|
Tempfile.open('expected') do |expected_f|
actual_f.print actual
expected_f.print expected
actual_f.close
expected_f.close
diff = %x(diff -u '#{expected_f.path}' '#{actual_f.path}')
end
end
diff.split($/)[2..-1].join($/)
end
end
76 changes: 76 additions & 0 deletions spec/cucumber_spec.rb
@@ -0,0 +1,76 @@
begin
require 'rubygems'
gem 'rspec', '>=2.1.0'
gem 'escape', '>=0.0.4'
rescue
end

THIS_DIR = Pathname.new(__FILE__).dirname

require 'yaml'
require 'pp'
require 'pathname'
require 'escape'
require THIS_DIR.join('be_same_text')

def vim_cmd(*args)
[
'gvim',
'-n', # no swap file
'-N', # set nocompatible
'--noplugin', # don't load plugins
'-u', 'vimrc', # don't load normal ~/.vimrc
'-U', 'gvimrc', # don't load normal ~/.gvimrc
'--servername', 'cuketest', # use a server name to sandbox from other vim instances
*args
]
end

def vim_eval(*args)
cmd = Escape.shell_command(vim_cmd(*args))
`#{cmd}`.to_s.chomp
end

def vim(*args)
Dir.chdir(THIS_DIR) do
system(*vim_cmd(*args))
end
end

describe 'GetCucumberIndent in indent/cucumber.vim' do
before(:all) do
# start vim server
vim
sleep 1
end

after(:all) do
# stop vim server
sleep 1
vim('--remote-send', ':qall!<CR>')
end

examples = %w[example-basic example-comments example-table]
examples.each do |base|
input = "#{base}.feature"
expected = "#{base}.expected.feature"
it "indents the example #{input.inspect} correctly" do
# open input.feature, indent and copy whole file to clipboard
vim('--remote-send', %{:e #{input}<CR>gg=G"+yG:bd!<CR>})
output_text = `pbpaste`
expected_text = THIS_DIR.join(expected).read
output_text.should be_same_text(expected_text)
end
end

it "indents blank lines correctly" do
input = 'example-blanks.feature'
vim('--remote-send', %{:e #{input}<CR>jjo})
begin
col = vim_eval('--remote-expr', %{col('.')})
col.should == '3'
ensure
vim('--remote-send', %{<Esc>})
end
end
end
29 changes: 29 additions & 0 deletions spec/example-basic.expected.feature
@@ -0,0 +1,29 @@
Feature: An unindented feature
As a developer
I want cucumber.vim to indent features correctly
So that I can easily correct my BA's sloppiness

Background:
Given there is a sloppily written feature

Scenario: Indent the feature correctly
Given I have a file with the lines:
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
When I open the file
And I press gg=G
Then I should see the file reindent good

Scenario Outline: Indent the feature correctly with examples
Given I have a scenario with "<stuff>"
Examples:
| stuff |
| tables |
| comments |
| examples |
| backgrounds |
When I open the file
And I press gg=G
Then I should see the file reindent good
29 changes: 29 additions & 0 deletions spec/example-basic.feature
@@ -0,0 +1,29 @@
Feature: An unindented feature
As a developer
I want cucumber.vim to indent features correctly
So that I can easily correct my BA's sloppiness

Background:
Given there is a sloppily written feature

Scenario: Indent the feature correctly
Given I have a file with the lines:
| 1 | one |
| 2 | two |
| 3 | three |
| 4 | four |
When I open the file
And I press gg=G
Then I should see the file reindent good

Scenario Outline: Indent the feature correctly with examples
Given I have a scenario with "<stuff>"
Examples:
| stuff |
| tables |
| comments |
| examples |
| backgrounds |
When I open the file
And I press gg=G
Then I should see the file reindent good
6 changes: 6 additions & 0 deletions spec/example-blanks.feature
@@ -0,0 +1,6 @@
Feature: A feature to test blank lines
As a developer
I want cucumber.vim to indent blank lines correctly


Background:
56 changes: 56 additions & 0 deletions spec/example-comments.expected.feature
@@ -0,0 +1,56 @@
# Comments for a feature.
# Maybe even multi-line comments.
Feature: An feature with lots of comments
As a developer
# A good developer, too
I want to put comments everywhere
# Everywhere!
So that I can remember things for later
# So many things...

# Comments for the background.
# Maybe even multi-line comments.
Background:
# Comment on this given line:
Given there is a heavily commented feature
# Comments trailing the background steps.
# Maybe even multi-line comments.

# Comments above scenarios are really common. People are always like,
# justifying their choice of tags.
@tag1 @tag2
Scenario: Indent the feature with comments correctly
Given I have a file with the lines:
# A comment at the start of a table.
| 1 | one |
| 2 | two |
# A comment in the middle of a table.
| 3 | three |
| 4 | four |
# A comment after a table.
When I open the file
And I press gg=G
Then I should see the file reindent good
# Comments trailing the steps.

# Single line comment above some tags.
@tag1
Scenario: A scenario with a table and then a comment
Then I have a table
| 5 | five |
| 6 | six |

# A comment for a scenario after a table and whitspace.
Scenario: Another scenario
Then I am testin' testin' testin'
# Comments trailing the steps.

# A comment for a scenario after some trailing comments.
Scenario: Yet more scenariage
Then I am not really done at all

Scenario: An empty scenario

# A comment after an empty scenario for the next scenario
@tag1
Scenario: A scenario after an empty scenario
56 changes: 56 additions & 0 deletions spec/example-comments.feature
@@ -0,0 +1,56 @@
# Comments for a feature.
# Maybe even multi-line comments.
Feature: An feature with lots of comments
As a developer
# A good developer, too
I want to put comments everywhere
# Everywhere!
So that I can remember things for later
# So many things...

# Comments for the background.
# Maybe even multi-line comments.
Background:
# Comment on this given line:
Given there is a heavily commented feature
# Comments trailing the background steps.
# Maybe even multi-line comments.

# Comments above scenarios are really common. People are always like,
# justifying their choice of tags.
@tag1 @tag2
Scenario: Indent the feature with comments correctly
Given I have a file with the lines:
# A comment at the start of a table.
| 1 | one |
| 2 | two |
# A comment in the middle of a table.
| 3 | three |
| 4 | four |
# A comment after a table.
When I open the file
And I press gg=G
Then I should see the file reindent good
# Comments trailing the steps.

# Single line comment above some tags.
@tag1
Scenario: A scenario with a table and then a comment
Then I have a table
| 5 | five |
| 6 | six |

# A comment for a scenario after a table and whitspace.
Scenario: Another scenario
Then I am testin' testin' testin'
# Comments trailing the steps.

# A comment for a scenario after some trailing comments.
Scenario: Yet more scenariage
Then I am not really done at all

Scenario: An empty scenario

# A comment after an empty scenario for the next scenario
@tag1
Scenario: A scenario after an empty scenario
15 changes: 15 additions & 0 deletions spec/example-table.expected.feature
@@ -0,0 +1,15 @@
Feature: An unindented feature with lots of tables
As a developer
I want cucumber.vim to indent tables in features correctly
So that I can have eyes that don't bleed

Background:
Given there is a feature with lotsa tables

Scenario: Indent the feature correctly
Given I have a table with gaps in it:
| 1 | one |
| 2 | two |

| 3 | three |
| 4 | four |
15 changes: 15 additions & 0 deletions spec/example-table.feature
@@ -0,0 +1,15 @@
Feature: An unindented feature with lots of tables
As a developer
I want cucumber.vim to indent tables in features correctly
So that I can have eyes that don't bleed

Background:
Given there is a feature with lotsa tables

Scenario: Indent the feature correctly
Given I have a table with gaps in it:
| 1 | one |
| 2 | two |

| 3 | three |
| 4 | four |
Empty file added spec/gvimrc
Empty file.
3 changes: 3 additions & 0 deletions spec/vimrc
@@ -0,0 +1,3 @@
autocmd BufNewFile,BufReadPost *.feature,*.story set filetype=cucumber
autocmd FileType cucumber source ../indent/cucumber.vim
autocmd FileType cucumber setl et sw=2 sts=2 ts=2