Skip to content

Commit

Permalink
Add new outline expand feature to the JSON formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
brasmusson committed Jul 26, 2014
1 parent 02eb585 commit 1740b65
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
82 changes: 76 additions & 6 deletions lib/cucumber/formatter/gherkin_formatter_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ module Formatter
# Adapts Cucumber formatter events to Gherkin formatter events
# This class will disappear when Cucumber is based on Gherkin's model.
class GherkinFormatterAdapter
def initialize(gherkin_formatter, print_empty_match)
def initialize(gherkin_formatter, print_empty_match, options)
@gf = gherkin_formatter
@print_empty_match = print_empty_match
@options = options
end

def before_feature(feature)
Expand All @@ -28,14 +29,42 @@ def before_feature_element(feature_element)
@gf.scenario(feature_element.gherkin_statement)
when Ast::ScenarioOutline
@outline = true
@gf.scenario_outline(feature_element.gherkin_statement)
if @options[:expand]
@current_scenario_hash = to_hash(feature_element.gherkin_statement)
else
@gf.scenario_outline(feature_element.gherkin_statement)
end
else
raise "Bad type: #{feature_element.class}"
end
end

def scenario_name(keyword, name, file_colon_line, source_indent)
if @outline and @options[:expand]
@example_row = @example_row ? @example_row + 1 : 0
if in_instantiated_scenario?
example_row_hash = @current_example_rows[@example_row].to_hash
scenario = Gherkin::Formatter::Model::Scenario.new(
@current_scenario_hash['comments'],
@current_scenario_hash['tags'],
@current_scenario_hash['keyword'],
@current_scenario_hash['name'],
@current_scenario_hash['description'],
example_row_hash['line'],
example_row_hash['id'])
@gf.scenario(scenario)
end
end
end

def before_step(step)
@gf.step(step.gherkin_statement)
unless @outline and @options[:expand]
@gf.step(step.gherkin_statement)
else
if in_instantiated_scenario?
@current_step_hash = to_hash(step.gherkin_statement)
end
end
if @print_empty_match
if(@outline)
match = Gherkin::Formatter::Model::Match.new(step.gherkin_statement.outline_args, nil)
Expand All @@ -55,23 +84,50 @@ def before_step_result(keyword, step_match, multiline_arg, status, exception, so
# Trick the formatter to believe that's what was printed previously so we get arg highlights on #result
@gf.instance_variable_set('@match', match)
else
@gf.match(match)
unless @outline and @options[:expand]
@gf.match(match)
end
end

error_message = exception ? "#{exception.message} (#{exception.class})\n#{exception.backtrace.join("\n")}" : nil
unless @outline
@gf.result(Gherkin::Formatter::Model::Result.new(status, nil, error_message))
else
if @options[:expand] and in_instantiated_scenario?
@current_match = match
@current_result = Gherkin::Formatter::Model::Result.new(status, nil, error_message)
end
end
end

def step_name(keyword, step_match, status, source_indent, background, file_colon_line)
if @outline and @options[:expand] and in_instantiated_scenario?
@gf.step(Gherkin::Formatter::Model::Step.new(
@current_step_hash['comments'],
@current_step_hash['keyword'],
step_match.format_args(),
@current_step_hash['line'],
@current_step_hash['rows'],
@current_step_hash['doc_string']))
@gf.match(@current_match)
@gf.result(@current_result)
end
end

def before_examples(examples)
@gf.examples(examples.gherkin_statement)
unless @options[:expand]
@gf.examples(examples.gherkin_statement)
else
@current_example_rows = to_hash(examples.gherkin_statement)['rows']
end
end

#used for capturing duration
def after_step(step)
step_finish = (Time.now - @step_time)
@gf.append_duration(step_finish)
unless @outline and @options[:expand] and not in_instantiated_scenario?
@gf.append_duration(step_finish)
end
end

def after_feature(feature)
Expand All @@ -93,6 +149,20 @@ def embed(file, mime_type, label)
def puts(message)
@gf.write(message)
end

private

def in_instantiated_scenario?
@example_row > 0
end

def to_hash(gherkin_statement)
if defined?(JRUBY_VERSION)
gherkin_statement.toMap()
else
gherkin_statement.to_hash
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/cucumber/formatter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Json < GherkinFormatterAdapter

def initialize(runtime, io, options)
@io = ensure_io(io, "json")
super(Gherkin::Formatter::JSONFormatter.new(@io), false)
super(Gherkin::Formatter::JSONFormatter.new(@io), false, options)
end
end
end
Expand Down

0 comments on commit 1740b65

Please sign in to comment.