Skip to content

Commit

Permalink
Finish off whitespace respect for all other relevant keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwynne committed Dec 4, 2010
1 parent dcb1324 commit 853ec29
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
72 changes: 71 additions & 1 deletion features/json_formatter.feature
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,91 @@ Feature: JSON formatter
but this means we need to respect whitespace at the start and end of lines
in the description.

Pay close attention to the whitespace in this example.

Given the following text is parsed:
"""
Feature: Foo
one line
another line
some pre-formatted stuff
Background: name
test
Scenario: name
test
Scenario Outline: name
test
Given <foo>
Examples: name
test
| foo |
| table |
"""
Then the outputted JSON should be:
"""
{
"keyword": "Feature",
"name": "Foo",
"description": "one line \nanother line \n\n some pre-formatted stuff",
"line": 1
"line": 1,
"elements": [
{
"description": " test ",
"keyword": "Background",
"line": 7,
"name": "name",
"type": "background"
},
{
"description": " test ",
"keyword": "Scenario",
"line": 10,
"name": "name",
"type": "scenario"
},
{
"description": " test ",
"examples": [
{
"description": " test ",
"keyword": "Examples",
"line": 18,
"name": "name",
"rows": [
{
"cells": [
"foo"
],
"line": 20
},
{
"cells": [
"table"
],
"line": 21
}
]
}
],
"keyword": "Scenario Outline",
"line": 13,
"name": "name",
"steps": [
{
"keyword": "Given ",
"line": 16,
"name": "<foo>"
}
],
"type": "scenario_outline"
}
]
}
"""

Expand Down
15 changes: 4 additions & 11 deletions ragel/lexer.rb.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,25 @@ module Gherkin
}

action store_background_content {
store_keyword_content(:background, data, p, eof) { |con| multiline_strip(con) }
store_keyword_content(:background, data, p, eof) { |con| unindent(@start_col + 2, con) }
p = @next_keyword_start - 1 if @next_keyword_start
@next_keyword_start = nil
}

action store_scenario_content {
store_keyword_content(:scenario, data, p, eof) { |con| multiline_strip(con) }
store_keyword_content(:scenario, data, p, eof) { |con| unindent(@start_col + 2, con) }
p = @next_keyword_start - 1 if @next_keyword_start
@next_keyword_start = nil
}

action store_scenario_outline_content {
store_keyword_content(:scenario_outline, data, p, eof) { |con| multiline_strip(con) }
store_keyword_content(:scenario_outline, data, p, eof) { |con| unindent(@start_col + 2, con) }
p = @next_keyword_start - 1 if @next_keyword_start
@next_keyword_start = nil
}

action store_examples_content {
store_keyword_content(:examples, data, p, eof) { |con| multiline_strip(con) }
store_keyword_content(:examples, data, p, eof) { |con| unindent(@start_col + 2, con) }
p = @next_keyword_start - 1 if @next_keyword_start
@next_keyword_start = nil
}
Expand Down Expand Up @@ -147,13 +147,6 @@ module Gherkin
CRLF = "\r\n"
LF = "\n"

def multiline_strip(text)
crlf_count = text.scan(CRLF_RE).size
lf_count = text.scan(LF_RE).size
eol = crlf_count > lf_count ? CRLF : LF
text.split(/\r?\n/).map{|s| s.strip}.join(eol).strip
end

def unindent(startcol, text)
text.gsub(/^[\t ]{0,#{startcol}}/, "")
end
Expand Down

0 comments on commit 853ec29

Please sign in to comment.