def transform_node(node)
case node.rule_type
when :Step
step_line = node.get_token(:StepLine)
data_table = node.get_single(:DataTable)
doc_string = node.get_single(:DocString)
Cucumber::Messages::Step.new(
location: get_location(step_line, 0),
keyword: step_line.matched_keyword,
keyword_type: step_line.matched_keyword_type,
text: step_line.matched_text,
data_table: data_table,
doc_string: doc_string,
id: @id_generator.new_id
)
when :DocString
separator_token = node.get_tokens(:DocStringSeparator)[0]
media_type = separator_token.matched_text == '' ? nil : separator_token.matched_text
line_tokens = node.get_tokens(:Other)
content = line_tokens.map { |t| t.matched_text }.join("\n")
Cucumber::Messages::DocString.new(
location: get_location(separator_token, 0),
content: content,
delimiter: separator_token.matched_keyword,
media_type: media_type
)
when :DataTable
rows = get_table_rows(node)
Cucumber::Messages::DataTable.new(
location: rows[0].location,
rows: rows
)
when :Background
background_line = node.get_token(:BackgroundLine)
description = get_description(node)
steps = get_steps(node)
Cucumber::Messages::Background.new(
id: @id_generator.new_id,
location: get_location(background_line, 0),
keyword: background_line.matched_keyword,
name: background_line.matched_text,
description: description,
steps: steps
)
when :ScenarioDefinition
tags = get_tags(node)
scenario_node = node.get_single(:Scenario)
scenario_line = scenario_node.get_token(:ScenarioLine)
description = get_description(scenario_node)
steps = get_steps(scenario_node)
examples = scenario_node.get_items(:ExamplesDefinition)
Cucumber::Messages::Scenario.new(
id: @id_generator.new_id,
tags: tags,
location: get_location(scenario_line, 0),
keyword: scenario_line.matched_keyword,
name: scenario_line.matched_text,
description: description,
steps: steps,
examples: examples
)
when :ExamplesDefinition
tags = get_tags(node)
examples_node = node.get_single(:Examples)
examples_line = examples_node.get_token(:ExamplesLine)
description = get_description(examples_node)
rows = examples_node.get_single(:ExamplesTable)
table_header = rows.nil? ? nil : rows.first
table_body = rows.nil? ? [] : rows[1..-1]
Cucumber::Messages::Examples.new(
id: @id_generator.new_id,
tags: tags,
location: get_location(examples_line, 0),
keyword: examples_line.matched_keyword,
name: examples_line.matched_text,
description: description,
table_header: table_header,
table_body: table_body
)
when :ExamplesTable
get_table_rows(node)
when :Description
line_tokens = node.get_tokens(:Other)
# Trim trailing empty lines
last_non_empty = line_tokens.rindex { |token| !token.line.trimmed_line_text.empty? }
line_tokens[0..last_non_empty].map { |token| token.matched_text }.join("\n")
when :Feature
header = node.get_single(:FeatureHeader)
return unless header
tags = get_tags(header)
feature_line = header.get_token(:FeatureLine)
return unless feature_line
children = []
background = node.get_single(:Background)
children.push(Cucumber::Messages::FeatureChild.new(background: background)) if background
node.get_items(:ScenarioDefinition).each do |scenario|
children.push(Cucumber::Messages::FeatureChild.new(scenario: scenario))
end
node.get_items(:Rule).each do |rule|
children.push(Cucumber::Messages::FeatureChild.new(rule: rule))
end
description = get_description(header)
language = feature_line.matched_gherkin_dialect
Cucumber::Messages::Feature.new(
tags: tags,
location: get_location(feature_line, 0),
language: language,
keyword: feature_line.matched_keyword,
name: feature_line.matched_text,
description: description,
children: children,
)
when :Rule
header = node.get_single(:RuleHeader)
return unless header
rule_line = header.get_token(:RuleLine)
return unless rule_line
tags = get_tags(header)
children = []
background = node.get_single(:Background)
children.push(Cucumber::Messages::RuleChild.new(background: background)) if background
node.get_items(:ScenarioDefinition).each do |scenario|
children.push(Cucumber::Messages::RuleChild.new(scenario: scenario))
end
description = get_description(header)
Cucumber::Messages::Rule.new(
id: @id_generator.new_id,
tags: tags,
location: get_location(rule_line, 0),
keyword: rule_line.matched_keyword,
name: rule_line.matched_text,
description: description,
children: children,
)
when :GherkinDocument
feature = node.get_single(:Feature)
Cucumber::Messages::GherkinDocument.new(
comments: @comments,
feature: feature
)
else
node
end
end