Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions lib/cucumber/formatter/message_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
require 'base64'
require 'json'

require 'cucumber/formatter/backtrace_filter'
require 'cucumber/query'

require_relative 'backtrace_filter'
require_relative 'message_handlers'

module Cucumber
Expand All @@ -23,8 +23,7 @@ def initialize(config)

@test_run_started_id = config.test_run_started_id

# Fake Query objects
@test_case_by_step_id = {}
# Fake Query objects - used to build specific parts of the messages defined inside handlers
@pickle_id_by_test_case_id = {}
@pickle_id_step_by_test_step_id = {}
@hook_id_by_test_step_id = {}
Expand All @@ -33,8 +32,6 @@ def initialize(config)

# Ensure all handlers for events occur after all ivars are instantiated

config.on_event :gherkin_source_parsed, &method(:on_gherkin_source_parsed)

config.on_event :hook_test_step_created, &method(:on_hook_test_step_created)

config.on_event :step_activated, &method(:on_step_activated)
Expand Down Expand Up @@ -94,28 +91,31 @@ def on_attach_called(event)

private

def on_gherkin_source_parsed(_event)
# TODO: Handle GherkinSourceParsed
end

def on_hook_test_step_created(event)
# Set iVar value for `hook_id`
@hook_id_by_test_step_id[event.test_step.id] = event.hook.id
end

def on_step_activated(event)
# Add/Update iVar value for `step_definition_ids...`
@step_definition_ids_by_test_step_id[event.test_step.id] << event.step_match.step_definition.id
# Add/Update iVar value for `step_match_arguments...`
@step_match_arguments_by_test_step_id[event.test_step.id] = event.step_match.step_arguments
end

def on_test_case_created(event)
# Set iVar value for `pickle_id`
@pickle_id_by_test_case_id[event.test_case.id] = event.pickle.id
end

def on_test_case_ready(event)
# Use iVar value for `pickle_id`
pickle_id = @pickle_id_by_test_case_id.fetch(event.test_case.id)

message = Cucumber::Messages::Envelope.new(
test_case: Cucumber::Messages::TestCase.new(
id: event.test_case.id,
pickle_id: fake_query_pickle_id(event.test_case),
pickle_id: pickle_id,
test_steps: event.test_case.test_steps.map { |step| test_step_to_message(step) },
test_run_started_id: @test_run_started_id
)
Expand Down Expand Up @@ -200,7 +200,9 @@ def on_test_run_finished(event)
end

def on_test_step_created(event)
# Set iVar value for `pickle_id_step_...`
@pickle_id_step_by_test_step_id[event.test_step.id] = event.pickle_step.id
# Set iVar value for `step_definition_ids...`
@step_definition_ids_by_test_step_id[event.test_step.id] = []
end

Expand Down Expand Up @@ -311,15 +313,18 @@ def test_step_to_message(step)

Cucumber::Messages::TestStep.new(
id: step.id,
# Use iVar value for `pickle_id_step_...`
pickle_step_id: @pickle_id_step_by_test_step_id[step.id],
step_definition_ids: fake_query_step_definition_ids(step),
# Use iVar value for `step_definition_ids...`
step_definition_ids: @step_definition_ids_by_test_step_id.fetch(step.id),
step_match_arguments_lists: step_match_arguments_lists(step)
)
end

def hook_step_to_message(step)
Cucumber::Messages::TestStep.new(
id: step.id,
# Use iVar value for `hook_id`
hook_id: @hook_id_by_test_step_id[step.id]
)
end
Expand All @@ -334,7 +339,9 @@ def step_match_arguments_lists(step)
end

def step_match_arguments(step)
fake_query_step_match_arguments(step)&.map do |argument|
# Use iVar value for `step_match_arguments...`
step_matches = @step_match_arguments_by_test_step_id[step.id]
step_matches&.map do |argument|
Cucumber::Messages::StepMatchArgument.new(
group: argument_group_to_message(argument.group),
parameter_type_name: parameter_type_name(argument)
Expand Down Expand Up @@ -370,22 +377,6 @@ def create_exception_object(result, message_element)
stack_trace: message_element.backtrace.join("\n")
)
end

def fake_query_hook_id(test_step)
@hook_id_by_test_step_id.fetch(test_step.id)
end

def fake_query_pickle_id(test_case)
@pickle_id_by_test_case_id.fetch(test_case.id)
end

def fake_query_step_definition_ids(test_step)
@step_definition_ids_by_test_step_id.fetch(test_step.id)
end

def fake_query_step_match_arguments(test_step)
@step_match_arguments_by_test_step_id.fetch(test_step.id, nil)
end
end
end
end