lib/cucumber/formatter/json.rb in cucumber-6.0.0 vs lib/cucumber/formatter/json.rb in cucumber-6.1.0
- old
+ new
@@ -3,27 +3,18 @@
require 'json'
require 'base64'
require 'cucumber/formatter/backtrace_filter'
require 'cucumber/formatter/io'
require 'cucumber/formatter/ast_lookup'
-require 'cucumber/deprecate'
module Cucumber
module Formatter
# The formatter used for <tt>--format json</tt>
class Json
include Io
def initialize(config)
- Cucumber::Deprecate::CliOption.deprecate(
- config.error_stream,
- '--format=json',
- "Please use --format=message and stand-alone json-formatter.\n" \
- 'json-formatter homepage: https://github.com/cucumber/cucumber/tree/master/json-formatter#cucumber-json-formatter',
- '6.0.0'
- )
-
@io = ensure_io(config.out_stream, config.error_stream)
@ast_lookup = AstLookup.new(config)
@feature_hashes = []
@step_or_hook_hash = {}
config.on_event :test_case_started, &method(:on_test_case_started)
@@ -39,25 +30,27 @@
unless same_feature_as_previous_test_case?(test_case)
@feature_hash = builder.feature_hash
@feature_hashes << @feature_hash
end
@test_case_hash = builder.test_case_hash
- if builder.background?
- @in_background = true
- feature_elements << builder.background_hash
- @element_hash = builder.background_hash
- else
- @in_background = false
- feature_elements << @test_case_hash
- @element_hash = @test_case_hash
- end
+
+ @element_hash = nil
+ @element_background_hash = builder.background_hash
+ @in_background = builder.background?
+
@any_step_failed = false
end
def on_test_step_started(event)
test_step = event.test_step
return if internal_hook?(test_step)
+
+ if @element_hash.nil?
+ @element_hash = create_element_hash(test_step)
+ feature_elements << @element_hash
+ end
+
if test_step.hook?
@step_or_hook_hash = {}
hooks_of_type(test_step) << @step_or_hook_hash
return
end
@@ -78,10 +71,12 @@
add_match_and_result(test_step, result)
@any_step_failed = true if result.failed?
end
def on_test_case_finished(event)
+ feature_elements << @test_case_hash if @in_background
+
_test_case, result = *event.attributes
result = result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter)
add_failed_around_hook(result) if result.failed? && !@any_step_failed
end
@@ -164,10 +159,17 @@
def test_step_embeddings
@step_or_hook_hash[:embeddings] ||= []
end
+ def create_element_hash(test_step)
+ return @element_background_hash if @in_background && !first_step_after_background?(test_step)
+
+ @in_background = false
+ @test_case_hash
+ end
+
def create_step_hash(test_step)
step_source = @ast_lookup.step_source(test_step).step
step_hash = {
keyword: step_source.keyword,
name: test_step.text,
@@ -264,11 +266,12 @@
@background_hash = {
keyword: background.keyword,
name: background.name,
description: value_or_empty_string(background.description),
line: background.location.line,
- type: 'background'
+ type: 'background',
+ steps: []
}
end
def scenario(scenario_source, test_case)
scenario = scenario_source.type == :Scenario ? scenario_source.scenario : scenario_source.scenario_outline
@@ -276,10 +279,11 @@
id: "#{@feature_hash[:id]};#{create_id_from_scenario_source(scenario_source)}",
keyword: scenario.keyword,
name: test_case.name,
description: value_or_empty_string(scenario.description),
line: test_case.location.lines.max,
- type: 'scenario'
+ type: 'scenario',
+ steps: []
}
@test_case_hash[:tags] = create_tags_array_from_tags_array(test_case.tags) unless test_case.tags.empty?
end
private