Class: Cucumber::Parser::CityBuilder

Inherits:
Gherkin::AstBuilder
  • Object
show all
Defined in:
lib/cucumber/city_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CityBuilder

The Gherkin Parser is going to call the various methods within this class as it finds items. This is similar to how Cucumber generates it’s Abstract Syntax Tree (AST). Here instead this generates the various YARD::CodeObjects defined within this template.

A namespace is specified and that is the place in the YARD namespacing where all cucumber features generated will reside. The namespace specified is the root namespaces.

Parameters:

  • file (String)

    the name of the file which the content belongs



17
18
19
20
21
22
# File 'lib/cucumber/city_builder.rb', line 17

def initialize(file)
  super()
  @namespace = YARD::CodeObjects::Cucumber::CUCUMBER_NAMESPACE
  find_or_create_namespace(file)
  @file = file
end

Instance Method Details

#astYARD::CodeObject::Cucumber::Feature

Return the feature that has been defined. This method is the final method that is called when all the work is done. It is called by the feature parser to return the complete Feature object that was created

Returns:

  • (YARD::CodeObject::Cucumber::Feature)

    the completed feature

See Also:



31
32
33
34
# File 'lib/cucumber/city_builder.rb', line 31

def ast
  feature(get_result) unless @feature
  @feature
end

#background(background) ⇒ Object

Called when a background has been found

See Also:



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cucumber/city_builder.rb', line 131

def background(background)
  #log.debug "BACKGROUND"

  @background = YARD::CodeObjects::Cucumber::Scenario.new(@feature,"background") do |b|
    b.comments = background[:comments] ? background[:comments].map{|comment| comment.value}.join("\n") : ''
    b.description = background[:description] || ''
    b.keyword = background[:keyword]
    b.value = background[:name]
    b.add_file(@file,background[:location][:line])
  end

  @feature.background = @background
  @background.feature = @feature
  @step_container = @background
  background[:steps].each { |s|
    step(s)
  }
end

#eofObject

Defined in the cucumber version so left here. No events for the end-of-file



370
371
# File 'lib/cucumber/city_builder.rb', line 370

def eof
end

#examples(examples, outline) ⇒ Object

Examples for a scenario outline are called here. This section differs from the Cucumber parser because here each of the examples are exploded out here as individual scenarios and step definitions. This is so that later we can ensure that we have all the variations of the scenario outline defined to be displayed.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/cucumber/city_builder.rb', line 259

def examples(examples, outline)
  #log.debug "EXAMPLES"
  return if has_exclude_tags?(examples[:tags].map { |t| t[:name].gsub(/^@/, '') })
  example = YARD::CodeObjects::Cucumber::ScenarioOutline::Examples.new(:keyword => examples[:keyword],
                                                                       :name => examples[:name],
                                                                       :line => examples[:location][:line],
                                                                       :comments => examples[:comments] ? examples.comments.map{|comment| comment.value}.join("\n") : '',
                                                                       :rows => [],
                                                                       :tags => [],
                                                                       :scenario => outline )

  unless !examples[:tags].any?
    examples[:tags].each {|example_tag| find_or_create_tag(example_tag[:name], example)}
  end

  example.rows = [examples[:tableHeader][:cells].map{ |c| c[:value] }] if examples[:tableHeader]
  example.rows += matrix(examples[:tableBody]) if examples[:tableBody]

  # add the example to the step containers list of examples

  @step_container.examples << example

  # For each example data row we want to generate a new scenario using our
  # current scenario as the template.

  example.data.length.times do |row_index|

    # Generate a copy of the scenario.

    scenario = YARD::CodeObjects::Cucumber::Scenario.new(@step_container,"example_#{@step_container.scenarios.length + 1}") do |s|
      s.comments = @step_container.comments
      s.description = @step_container.description
      s.add_file(@file,@step_container.line_number)
      s.keyword = @step_container.keyword
      s.value = "#{@step_container.value} (#{@step_container.scenarios.length + 1})"
    end

    # Generate a copy of the scenario steps.

    @step_container.steps.each do |step|
      step_instance = YARD::CodeObjects::Cucumber::Step.new(scenario,step.line_number.to_s) do |s|
        s.keyword = step.keyword.dup
        s.value = step.value.dup
        s.add_file(@file,step.line_number)

        s.text = step.text.dup if step.has_text?
        s.table = clone_table(step.table) if step.has_table?
      end

      # Look at the particular data for the example row and do a simple
      # find and replace of the <key> with the associated values.

      example.values_for_row(row_index).each do |key,text|
        text ||= "" #handle empty cells in the example table
        step_instance.value.gsub!("<#{key}>",text)
        step_instance.text.gsub!("<#{key}>",text) if step_instance.has_text?
        step_instance.table.each{|row| row.each{|col| col.gsub!("<#{key}>",text)}} if step_instance.has_table?
      end

      # Connect these steps that we created to the scenario we created
      # and then add the steps to the scenario created.

      step_instance.scenario = scenario
      scenario.steps << step_instance
    end

    # Add the scenario to the list of scenarios maintained by the  feature
    # and add the feature to the scenario

    scenario.feature = @feature
    @step_container.scenarios << scenario

  end

  return example
end

#feature(document) ⇒ Object

Each feature found will call this method, generating the feature object. This is once, as the gherkin parser does not like multiple feature per file.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/cucumber/city_builder.rb', line 90

def feature(document)
  #log.debug "FEATURE"
  feature = document[:feature]
  return unless document[:feature]
  return if has_exclude_tags?(feature[:tags].map { |t| t[:name].gsub(/^@/, '') })

  @feature = YARD::CodeObjects::Cucumber::Feature.new(@namespace,File.basename(@file.gsub('.feature','').gsub('.','_'))) do |f|
    f.comments = feature[:comments] ? feature[:comments].map{|comment| comment[:text]}.join("\n") : ''
    f.description = feature[:description] || ''
    f.add_file(@file,feature[:location][:line])
    f.keyword = feature[:keyword]
    f.value = feature[:name]
    f.tags = []

    feature[:tags].each {|feature_tag| find_or_create_tag(feature_tag[:name],f) }
  end

  background(feature[:background]) if feature[:background]

  feature[:children].each do |child|
    case child[:type]
      when :Background
        background(child)
      when :ScenarioOutline
        outline = scenario_outline(child)
        @feature.total_scenarios += outline.scenarios.size
      when :Scenario
        scenario(child)
    end
  end

  @feature.tags.each do |feature_tag|
    tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.name.to_s == feature_tag[:name].to_s }
    tag_code_object.total_scenarios += @feature.total_scenarios
  end
end

#find_or_create_namespace(file) ⇒ Object

Feature that are found in sub-directories are considered, in the way that I chose to implement it, in another namespace. This is because when you execute a cucumber test run on a directory any sub-directories of features will be executed with that directory so the file is split and then namespaces are generated if they have not already have been.

The other duty that this does is look for a README.md file within the specified directory of the file and loads it as the description for the namespace. This is useful if you want to give a particular directory some flavor or text to describe what is going on.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cucumber/city_builder.rb', line 48

def find_or_create_namespace(file)
  @namespace = YARD::CodeObjects::Cucumber::CUCUMBER_NAMESPACE

  File.dirname(file).split('/').each do |directory|
    @namespace = @namespace.children.find {|child| child.is_a?(YARD::CodeObjects::Cucumber::FeatureDirectory) && child.name.to_s == directory } ||
      @namespace = YARD::CodeObjects::Cucumber::FeatureDirectory.new(@namespace,directory) {|dir| dir.add_file(directory)}
  end

  if @namespace.description == "" && File.exists?("#{File.dirname(file)}/README.md")
    @namespace.description = File.read("#{File.dirname(file)}/README.md")
  end
end

#find_or_create_tag(tag_name, parent) ⇒ Object

Find the tag if it exists within the YARD Registry, if it doesn’t then create it.

We note that the tag was used in this file at the current line.

Then we add the tag to the current scenario or feature. We also add the feature or scenario to the tag.

Parameters:

  • tag_name (String)

    the name of the tag

  • parent (parent)

    the scenario or feature that is going to adopt this tag.



74
75
76
77
78
79
80
81
82
83
# File 'lib/cucumber/city_builder.rb', line 74

def find_or_create_tag(tag_name,parent)
  #log.debug "Processing tag #{tag_name}"
  tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.value == tag_name } ||
    YARD::CodeObjects::Cucumber::Tag.new(YARD::CodeObjects::Cucumber::CUCUMBER_TAG_NAMESPACE,tag_name.gsub('@','')) {|t| t.owners = [] ; t.value = tag_name ; t.total_scenarios = 0}

  tag_code_object.add_file(@file,parent.line)

  parent.tags << tag_code_object unless parent.tags.find {|tag| tag == tag_code_object }
  tag_code_object.owners << parent unless tag_code_object.owners.find {|owner| owner == parent}
end

#scenario(statement) ⇒ Object

Called when a scenario has been found

- create a scenario
- assign the scenario to the feature
- assign the feature to the scenario
- find or create tags associated with the scenario

The scenario is set as the @step_container, which means that any steps found before another scenario is defined belong to this scenario

Parameters:

  • statement (Scenario)

    is a scenario object returned from Gherkin

See Also:



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
# File 'lib/cucumber/city_builder.rb', line 163

def scenario(statement)
  #log.debug "SCENARIO"

  return if has_exclude_tags?(statement[:tags].map { |t| t[:name].gsub(/^@/, '') })

  scenario = YARD::CodeObjects::Cucumber::Scenario.new(@feature,"scenario_#{@feature.scenarios.length + 1}") do |s|
    s.comments = statement[:comments] ? statement[:comments].map{|comment| comment.value}.join("\n") : ''
    s.description = statement[:description] || ''
    s.add_file(@file,statement[:location][:line])
    s.keyword = statement[:keyword]
    s.value = statement[:name]

    statement[:tags].each {|scenario_tag| find_or_create_tag(scenario_tag[:name],s) }
  end

  scenario.feature = @feature
  @feature.scenarios << scenario
  @step_container = scenario
  statement[:steps].each { |s|
    step(s)
  }

  # count scenarios for scenario level tags
  scenario.tags.uniq.each { |scenario_tag|
    if !scenario.feature.tags.include?(scenario_tag)
      tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.name.to_s == scenario_tag[:name].to_s }
      tag_code_object.total_scenarios += 1
    end
  }
end

#scenario_outline(statement) ⇒ Object

Called when a scenario outline is found. Very similar to a scenario, the ScenarioOutline is still a distinct object as it can contain multiple different example groups that can contain different values.

See Also:



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
# File 'lib/cucumber/city_builder.rb', line 201

def scenario_outline(statement)
  #log.debug "SCENARIO OUTLINE"

  return if has_exclude_tags?(statement[:tags].map { |t| t[:name].gsub(/^@/, '') })

  outline = YARD::CodeObjects::Cucumber::ScenarioOutline.new(@feature,"scenario_#{@feature.scenarios.length + 1}") do |s|
    s.comments = statement[:comments] ? statement[:comments].map{|comment| comment.value}.join("\n") : ''
    s.description = statement[:description] || ''
    s.add_file(@file,statement[:location][:line])
    s.keyword = statement[:keyword]
    s.value = statement[:name]

    statement[:tags].each {|scenario_tag| find_or_create_tag(scenario_tag[:name],s) }
  end

  outline.feature = @feature
  @step_container = outline
  statement[:steps].each { |s|
    step(s)
  }

  statement[:examples].each { |e|
    example = examples(e, outline)
  }

  @feature.scenarios << outline

  # count scenarios for scenario outline level tags
  outline.tags.uniq.each { |outline_tag|
    if !outline.feature.tags.include?(outline_tag)
      tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.name.to_s == outline_tag[:name].to_s }
      tag_code_object.total_scenarios += outline.scenarios.size
    end
  }

  # count scenarios for example table level tags
  outline.examples.each { |example|
    unless !example.tags.any?
      example.tags.uniq.each { |example_tag|
        if !outline.feature.tags.include?(example_tag) && !outline.tags.include?(example_tag)
          tag_code_object = YARD::Registry.all(:tag).find {|tag| tag.name.to_s == example_tag[:name].to_s }
          tag_code_object.total_scenarios += example.data.size
        end
      }
    end
  }

  return outline
end

#step(step) ⇒ Object

Called when a step is found. The step is refered to a table owner, though not all steps have a table or multliline arguments associated with them.

If a multiline string is present with the step it is included as the text of the step. If the step has a table it is added to the step using the same method used by the Cucumber Gherkin model.



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/cucumber/city_builder.rb', line 344

def step(step)
  #log.debug "STEP"

  @table_owner = YARD::CodeObjects::Cucumber::Step.new(@step_container,"#{step[:location][:line]}") do |s|
    s.keyword = step[:keyword]
    s.value = step[:text]
    s.add_file(@file,step[:location][:line])
  end

  @table_owner.comments = step[:comments] ? step[:comments].map{|comment| comment.value}.join("\n") : ''

  multiline_arg = step[:argument]

  case(multiline_arg[:type])
  when :DocString
    @table_owner.text = multiline_arg[:content]
  when :DataTable
    #log.info "Matrix: #{matrix(multiline_arg).collect{|row| row.collect{|cell| cell.class } }.flatten.join("\n")}"
    @table_owner.table = matrix(multiline_arg[:rows])
  end if multiline_arg

  @table_owner.scenario = @step_container
  @step_container.steps << @table_owner
end

#syntax_error(state, event, legal_events, line) ⇒ Object

When a syntax error were to occurr. This parser is not interested in errors



374
375
376
# File 'lib/cucumber/city_builder.rb', line 374

def syntax_error(state, event, legal_events, line)
  # raise "SYNTAX ERROR"
end