Skip to content

Commit

Permalink
Refactored GherkinBuilder to use new Gherkin Formatter API.
Browse files Browse the repository at this point in the history
  • Loading branch information
aslakhellesoy committed Jun 14, 2010
1 parent 04c9ab6 commit 1754176
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 98 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
24 changes: 6 additions & 18 deletions gem_tasks/rspec.rake
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
begin
require 'rspec/core/rake_task'
require 'rspec/core/rake_task'

desc "Run RSpec"
RSpec::Core::RakeTask.new do |t|
t.rcov = ENV['RCOV']
t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/}
t.verbose = true
end
rescue LoadError => e
require 'spec/rake/spectask'

desc "Run RSpec"
Spec::Rake::SpecTask.new do |t|
t.spec_opts = %w{--color --diff}
t.rcov = ENV['RCOV']
t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/}
t.verbose = true
end
desc "Run RSpec"
RSpec::Core::RakeTask.new do |t|
t.rcov = ENV['RCOV']
t.rcov_opts = %w{--exclude osx\/objc,gems\/,spec\/}
t.verbose = true
end
1 change: 1 addition & 0 deletions lib/cucumber/ast/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Ast
#
class Comment #:nodoc:
def initialize(value)
raise "?????" if Array === value
@value = value
end

Expand Down
7 changes: 5 additions & 2 deletions lib/cucumber/ast/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def initialize
@rows = []
end

def uri(uri)
end

def row(row, line_number)
@rows << row
end
Expand All @@ -58,10 +61,10 @@ def self.default_arg_name #:nodoc:
"table"
end

def self.parse(text)
def self.parse(text, uri)
builder = Builder.new
lexer = Gherkin::I18nLexer.new(builder)
lexer.scan(text)
lexer.scan(text, uri)
new(builder.rows)
end

Expand Down
12 changes: 7 additions & 5 deletions lib/cucumber/feature_file.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'cucumber/parser/gherkin_builder'
require 'gherkin/parser/filter_listener'
require 'gherkin/parser/formatter_listener'
require 'gherkin/parser/parser'
require 'gherkin/i18n_lexer'

Expand All @@ -26,14 +27,15 @@ def initialize(uri, source=nil)
def parse(step_mother, options)
filters = @lines || options.filters

builder = Cucumber::Parser::GherkinBuilder.new
filter_listener = Gherkin::Parser::FilterListener.new(builder, filters)
parser = Gherkin::Parser::Parser.new(filter_listener, true, "root")
lexer = Gherkin::I18nLexer.new(parser, false)
builder = Cucumber::Parser::GherkinBuilder.new
formatter_listener = Gherkin::Parser::FormatterListener.new(builder)
filter_listener = Gherkin::Parser::FilterListener.new(formatter_listener, filters)
parser = Gherkin::Parser::Parser.new(filter_listener, true, "root")
lexer = Gherkin::I18nLexer.new(parser, false)

begin
s = ENV['FILTER_PML_CALLOUT'] ? source.gsub(C_CALLOUT, '') : source
lexer.scan(s)
lexer.scan(s, @path)
ast = builder.ast
return nil if ast.nil? # Filter caused nothing to match
ast.language = lexer.i18n_language
Expand Down
103 changes: 31 additions & 72 deletions lib/cucumber/parser/gherkin_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,53 @@ def ast
@feature || @multiline_arg
end

def tag(name, line)
@tags ||= []
@tags << name
end

def comment(content, line)
@comments ||= []
@comments << content
end

def feature(keyword, name, line)
def feature(comments, tags, keyword, name, description, uri)
@feature = Ast::Feature.new(
nil,
Ast::Comment.new(grab_comments!('')),
Ast::Tags.new(nil, grab_tags!('')),
Ast::Comment.new(comments.join("\n")),
Ast::Tags.new(nil, tags),
keyword,
name,
legacy_name_for(name, description),
[]
)
end

def background(keyword, name, line)
def background(comments, keyword, name, description, line)
@background = Ast::Background.new(
Ast::Comment.new(grab_comments!('')),
Ast::Comment.new(comments.join("\n")),
line,
keyword,
name,
legacy_name_for(name, description),
steps=[]
)
@feature.background = @background
@background.feature = @feature
@step_container = @background
end

def scenario(keyword, name, line)
grab_table!
def scenario(comments, tags, keyword, name, description, line)
scenario = Ast::Scenario.new(
@background,
Ast::Comment.new(grab_comments!('')),
Ast::Tags.new(nil, grab_tags!('')),
Ast::Comment.new(comments.join("\n")),
Ast::Tags.new(nil, tags),
line,
keyword,
name,
legacy_name_for(name, description),
steps=[]
)
@feature.add_feature_element(scenario)
@background.feature_elements << scenario if @background
@step_container = scenario
end

def scenario_outline(keyword, name, line)
grab_table!
def scenario_outline(comments, tags, keyword, name, description, line)
scenario_outline = Ast::ScenarioOutline.new(
@background,
Ast::Comment.new(grab_comments!('')),
Ast::Tags.new(nil, grab_tags!('')),
Ast::Comment.new(comments.join("\n")),
Ast::Tags.new(nil, tags),
line,
keyword,
name,
legacy_name_for(name, description),
steps=[],
example_sections=[]
)
Expand All @@ -81,64 +69,35 @@ def scenario_outline(keyword, name, line)
@step_container = scenario_outline
end

def examples(keyword, name, line)
grab_table!
@examples_fields = [Ast::Comment.new(grab_comments!('')), line, keyword, name]
def examples(comments, tags, keyword, name, description, line, examples_table)
examples_fields = [Ast::Comment.new(comments.join("\n")), line, keyword, name, examples_table]
@step_container.add_examples(examples_fields)
end

def step(keyword, name, line)
grab_table!
def step(comments, keyword, name, line, multiline_arg, status, exception, arguments, stepdef_location)
@table_owner = Ast::Step.new(line, keyword, name)
@step_container.add_step(@table_owner)
end

def row(row, line)
@rows ||= []
@rows << row
class << row
attr_accessor :line
case(multiline_arg)
when String
@table_owner.multiline_arg = Ast::PyString.new(multiline_arg)
when Array
@table_owner.multiline_arg = Ast::Table.new(multiline_arg)
end
row.line = line
end

def py_string(string, line)
@multiline_arg = Ast::PyString.new(string)
@table_owner.multiline_arg = @multiline_arg if @table_owner
@step_container.add_step(@table_owner)
end

def eof
grab_table!
end

def syntax_error(state, event, legal_events, line)
# raise "SYNTAX ERROR"
end

private

def grab_table!
return if @rows.nil?
if @examples_fields
@examples_fields << @rows
@step_container.add_examples(@examples_fields)
@examples_fields = nil
else
@multiline_arg = Ast::Table.new(@rows)
@table_owner.multiline_arg = @multiline_arg if @table_owner
end
@rows = nil
end

def grab_tags!(indent)
tags = @tags ? @tags : []
@tags = nil
tags
end

def grab_comments!(indent)
comments = @comments ? indent + @comments.join("\n#{indent}") : ''
@comments = nil
comments

def legacy_name_for(name, description)
s = name
s += "\n#{description}" if description != ""
s
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/cucumber/ast/table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def @table.columns; super; end
end

def table(text, file=nil, line_offset=0)
Table.parse(text)
Table.parse(text, __FILE__)
end
end

Expand Down

0 comments on commit 1754176

Please sign in to comment.