Class: Gherkin::AstBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/ast_builder.rb

Instance Method Summary collapse

Constructor Details

#initializeAstBuilder

Returns a new instance of AstBuilder.



6
7
8
# File 'lib/gherkin/ast_builder.rb', line 6

def initialize
  reset
end

Instance Method Details

#build(token) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/gherkin/ast_builder.rb', line 24

def build(token)
  if token.matched_type == :Comment
    @comments.push(Cucumber::Messages::GherkinDocument::Comment.new(
      location: get_location(token, 0),
      text: token.matched_text
    ))
  else
    current_node.add(token.matched_type, token)
  end
end

#current_nodeObject



39
40
41
# File 'lib/gherkin/ast_builder.rb', line 39

def current_node
  @stack.last
end

#end_rule(rule_type) ⇒ Object



19
20
21
22
# File 'lib/gherkin/ast_builder.rb', line 19

def end_rule(rule_type)
  node = @stack.pop
  current_node.add(node.rule_type, transform_node(node))
end

#ensure_cell_count(rows) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/gherkin/ast_builder.rb', line 79

def ensure_cell_count(rows)
  return if rows.empty?
  cell_count = rows[0].cells.length
  rows.each do |row|
    if row.cells.length != cell_count
      location = {line: row.location.line, column: row.location.column}
      raise AstBuilderException.new("inconsistent cell count within the table", location)
    end
  end
end

#get_cells(table_row_token) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/gherkin/ast_builder.rb', line 90

def get_cells(table_row_token)
  table_row_token.matched_items.map do |cell_item|
    Cucumber::Messages::GherkinDocument::Feature::TableRow::TableCell.new(
      location: get_location(table_row_token, cell_item.column),
      value: cell_item.text
    )
  end
end

#get_description(node) ⇒ Object



99
100
101
# File 'lib/gherkin/ast_builder.rb', line 99

def get_description(node)
  node.get_single(:Description)
end

#get_location(token, column) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/gherkin/ast_builder.rb', line 43

def get_location(token, column)
  column = column == 0 ? token.location[:column] : column
  Cucumber::Messages::Location.new(
    line: token.location[:line],
    column: column
  )
end

#get_resultObject



35
36
37
# File 'lib/gherkin/ast_builder.rb', line 35

def get_result
  current_node.get_single(:GherkinDocument)
end

#get_steps(node) ⇒ Object



103
104
105
# File 'lib/gherkin/ast_builder.rb', line 103

def get_steps(node)
  node.get_items(:Step)
end

#get_table_rows(node) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/gherkin/ast_builder.rb', line 68

def get_table_rows(node)
  rows = node.get_tokens(:TableRow).map do |token|
    Cucumber::Messages::GherkinDocument::Feature::TableRow.new(
      location: get_location(token, 0),
      cells: get_cells(token)
    )
  end
  ensure_cell_count(rows)
  rows
end

#get_tags(node) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gherkin/ast_builder.rb', line 51

def get_tags(node)
  tags = []
  tags_node = node.get_single(:Tags)
  return tags unless tags_node

  tags_node.get_tokens(:TagLine).each do |token|
    token.matched_items.each do |tag_item|
      tags.push(Cucumber::Messages::GherkinDocument::Feature::Tag.new(
        location: get_location(token, tag_item.column),
        name: tag_item.text
      ))
    end
  end

  tags
end

#resetObject



10
11
12
13
# File 'lib/gherkin/ast_builder.rb', line 10

def reset
  @stack = [AstNode.new(:None)]
  @comments = []
end

#start_rule(rule_type) ⇒ Object



15
16
17
# File 'lib/gherkin/ast_builder.rb', line 15

def start_rule(rule_type)
  @stack.push AstNode.new(rule_type)
end

#transform_node(node) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/gherkin/ast_builder.rb', line 107

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)

    props = {
      location: get_location(step_line, 0),
      keyword: step_line.matched_keyword,
      text: step_line.matched_text
    }
    props[:data_table] = data_table if data_table
    props[:doc_string] = doc_string if doc_string

    Cucumber::Messages::GherkinDocument::Feature::Step.new(props)
  when :DocString
    separator_token = node.get_tokens(:DocStringSeparator)[0]
    content_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")

    props = {
      location: get_location(separator_token, 0),
      content: content,
      delimiter: separator_token.matched_keyword
    }
    props[:content_type] = content_type if content_type
    Cucumber::Messages::GherkinDocument::Feature::Step::DocString.new(props)
  when :DataTable
    rows = get_table_rows(node)
    Cucumber::Messages::GherkinDocument::Feature::Step::DataTable.new(
      location: rows[0].location,
      rows: rows,
    )
  when :Background
    background_line = node.get_token(:BackgroundLine)
    description = get_description(node)
    steps = get_steps(node)

    props = {
      location: get_location(background_line, 0),
      keyword: background_line.matched_keyword,
      name: background_line.matched_text,
      steps: steps
    }
    props[:description] = description if description
    Cucumber::Messages::GherkinDocument::Feature::Background.new(props)
  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)
    props = {
      tags: tags,
      location: get_location(scenario_line, 0),
      keyword: scenario_line.matched_keyword,
      name: scenario_line.matched_text,
      steps: steps,
      examples: examples
    }
    props[:description] = description if description
    Cucumber::Messages::GherkinDocument::Feature::Scenario.new(props)
  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? ? nil : rows[1..-1]

    props = {
      tags: tags,
      location: get_location(examples_line, 0),
      keyword: examples_line.matched_keyword,
      name: examples_line.matched_text,
    }
    props[:table_header] = table_header if table_header
    props[:table_body] = table_body if table_body
    props[:description] = description if description
    Cucumber::Messages::GherkinDocument::Feature::Scenario::Examples.new(props)
  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? }
    description = line_tokens[0..last_non_empty].map { |token| token.matched_text }.join("\n")
    return description
  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::GherkinDocument::Feature::FeatureChild.new(background: background)) if background
    node.get_items(:ScenarioDefinition).each do |scenario|
      children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild.new(scenario: scenario))
    end
    node.get_items(:Rule).each do |rule|
      children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild.new(rule: rule))
    end
    description = get_description(header)
    language = feature_line.matched_gherkin_dialect

    props = {
      tags: tags,
      location: get_location(feature_line, 0),
      language: language,
      keyword: feature_line.matched_keyword,
      name: feature_line.matched_text,
      children: children,
    }
    props[:description] = description if description

    Cucumber::Messages::GherkinDocument::Feature.new(props)
  when :Rule
    header = node.get_single(:RuleHeader)
    return unless header
    rule_line = header.get_token(:RuleLine)
    return unless rule_line
    children = []
    background = node.get_single(:Background)
    children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild::RuleChild.new(background: background)) if background
    node.get_items(:ScenarioDefinition).each do |scenario|
      children.push(Cucumber::Messages::GherkinDocument::Feature::FeatureChild::RuleChild.new(scenario: scenario))
    end
    description = get_description(header)

    Cucumber::Messages::GherkinDocument::Feature::FeatureChild::Rule.new(
      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)
    {
      feature: feature,
      comments: @comments
    }
  else
    return node
  end
end