class Cucumber::Formatter::Json
The formatter used for --format json
Public Class Methods
new(config)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 15 def initialize(config) @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) config.on_event :test_case_finished, &method(:on_test_case_finished) config.on_event :test_step_started, &method(:on_test_step_started) config.on_event :test_step_finished, &method(:on_test_step_finished) config.on_event :test_run_finished, &method(:on_test_run_finished) end
Public Instance Methods
attach(src, mime_type)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 87 def attach(src, mime_type) if mime_type == 'text/x.cucumber.log+plain' test_step_output << src return end if mime_type =~ /;base64$/ mime_type = mime_type[0..-8] data = src else data = encode64(src) end test_step_embeddings << { mime_type: mime_type, data: data } end
on_test_case_finished(event)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 75 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
on_test_case_started(event)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 27 def on_test_case_started(event) test_case = event.test_case builder = Builder.new(test_case, @ast_lookup) 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 @element_hash = nil @element_background_hash = builder.background_hash @in_background = builder.background? @any_step_failed = false end
on_test_run_finished(_event)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 83 def on_test_run_finished(_event) @io.write(JSON.generate(@feature_hashes, pretty: true)) end
on_test_step_finished(event)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 67 def on_test_step_finished(event) test_step, result = *event.attributes result = result.with_filtered_backtrace(Cucumber::Formatter::BacktraceFilter) return if internal_hook?(test_step) add_match_and_result(test_step, result) @any_step_failed = true if result.failed? end
on_test_step_started(event)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 43 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 if first_step_after_background?(test_step) @in_background = false feature_elements << @test_case_hash @element_hash = @test_case_hash end @step_or_hook_hash = create_step_hash(test_step) steps << @step_or_hook_hash @step_hash = @step_or_hook_hash end
Private Instance Methods
add_failed_around_hook(result)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 204 def add_failed_around_hook(result) @step_or_hook_hash = {} around_hooks << @step_or_hook_hash @step_or_hook_hash[:match] = { location: 'unknown_hook_location:1' } @step_or_hook_hash[:result] = create_result_hash(result) end
add_match_and_result(test_step, result)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 198 def add_match_and_result(test_step, result) @step_or_hook_hash[:match] = create_match_hash(test_step, result) @step_or_hook_hash[:result] = create_result_hash(result) result.embeddings.each { |e| embed(e['src'], e['mime_type'], e['label']) } if result.respond_to?(:embeddings) end
after_hooks()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 144 def after_hooks @element_hash[:after] ||= [] end
after_step_hooks()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 152 def after_step_hooks @step_hash[:after] ||= [] end
around_hooks()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 148 def around_hooks @element_hash[:around] ||= [] end
before_hooks()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 140 def before_hooks @element_hash[:before] ||= [] end
create_data_table_value(data_table)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 192 def create_data_table_value(data_table) data_table.rows.map do |row| { cells: row.cells.map(&:value) } end end
create_doc_string_hash(doc_string)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 183 def create_doc_string_hash(doc_string) content_type = doc_string.media_type || '' { value: doc_string.content, content_type: content_type, line: doc_string.location.line } end
create_element_hash(test_step)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 164 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
create_error_message(result)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 225 def create_error_message(result) message_element = result.failed? ? result.exception : result message = "#{message_element.message} (#{message_element.class})" ([message] + message_element.backtrace).join("\n") end
create_match_hash(test_step, _result)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 212 def create_match_hash(test_step, _result) { location: test_step.action_location.to_s } end
create_result_hash(result)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 216 def create_result_hash(result) result_hash = { status: result.to_sym } result_hash[:error_message] = create_error_message(result) if result.failed? || result.pending? result.duration.tap { |duration| result_hash[:duration] = duration.nanoseconds } result_hash end
create_step_hash(test_step)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 171 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, line: test_step.location.lines.min } step_hash[:doc_string] = create_doc_string_hash(step_source.doc_string) unless step_source.doc_string.nil? step_hash[:rows] = create_data_table_value(step_source.data_table) unless step_source.data_table.nil? step_hash end
current_feature()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 115 def current_feature @feature_hash ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName end
encode64(data)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 231 def encode64(data) # strip newlines from the encoded data Base64.encode64(data).delete("\n") end
feature_elements()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 119 def feature_elements @feature_hash[:elements] ||= [] end
first_step_after_background?(test_step)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 107 def first_step_after_background?(test_step) @in_background && test_step.location.lines.max >= @test_case_hash[:line] end
hooks_of_type(hook_step)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 127 def hooks_of_type(hook_step) case hook_step.text when 'Before hook' before_hooks when 'After hook' after_hooks when 'AfterStep hook' after_step_hooks else raise 'Unknown hook type ' + hook_step.to_s end end
internal_hook?(test_step)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 111 def internal_hook?(test_step) test_step.location.file.include?('lib/cucumber/') end
same_feature_as_previous_test_case?(test_case)
click to toggle source
# File lib/cucumber/formatter/json.rb, line 103 def same_feature_as_previous_test_case?(test_case) current_feature[:uri] == test_case.location.file end
steps()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 123 def steps @element_hash[:steps] ||= [] end
test_step_embeddings()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 160 def test_step_embeddings @step_or_hook_hash[:embeddings] ||= [] end
test_step_output()
click to toggle source
# File lib/cucumber/formatter/json.rb, line 156 def test_step_output @step_or_hook_hash[:output] ||= [] end