Class: Cucumber::Parser::CityBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ CityBuilder

Returns a new instance of CityBuilder.



7
8
9
10
11
# File 'lib/cucumber/city_builder.rb', line 7

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

Instance Method Details

#astObject



13
14
15
# File 'lib/cucumber/city_builder.rb', line 13

def ast
  @feature || @multiline_arg
end

#background(background) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/cucumber/city_builder.rb', line 56

def background(background)
  #log.debug "BACKGROUND"
  
  @background = YARD::CodeObjects::Cucumber::Scenario.new(@feature,"background") do |b|
    b.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.line)
  end

  @feature.background = @background
  @background.feature = @feature
  @step_container = @background
end

#eofObject



185
186
# File 'lib/cucumber/city_builder.rb', line 185

def eof
end

#examples(examples) ⇒ Object



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

def examples(examples)
  #log.debug "EXAMPLES"

  @step_container.examples = { :keyword => examples.keyword,
    :name => examples.name,
    :line => examples.line,
    :comments => examples.comments.map{|comment| comment.value}.join("\n"),
    :rows => matrix(examples.rows) }

  # For each example generate a scenario and steps
  
  @step_container.example_data.length.times do |row_index|

    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

    @step_container.steps.each do |step|
      step_instance = YARD::CodeObjects::Cucumber::Step.new(scenario,step.line_number) 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

      @step_container.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

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

    # Scenario instances of an outline link to the feature but are not linked from the feature
    # @feature.scenarios << scenario

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

#feature(feature) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cucumber/city_builder.rb', line 41

def feature(feature)
  #log.debug "FEATURE"
  
  @feature = YARD::CodeObjects::Cucumber::Feature.new(@namespace,File.basename(@file.gsub('.feature','').gsub('.','_'))) do |f|
    f.comments = feature.comments.map{|comment| comment.value}.join("\n")
    f.description = feature.description
    f.add_file(@file,feature.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
end

#find_or_create_namespace(file) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cucumber/city_builder.rb', line 17

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



30
31
32
33
34
35
36
37
38
39
# File 'lib/cucumber/city_builder.rb', line 30

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 }

  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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cucumber/city_builder.rb', line 72

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

  scenario = YARD::CodeObjects::Cucumber::Scenario.new(@feature,"scenario_#{@feature.scenarios.length + 1}") do |s|
    s.comments = statement.comments.map{|comment| comment.value}.join("\n")
    s.description = statement.description
    s.add_file(@file,statement.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
end

#scenario_outline(statement) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cucumber/city_builder.rb', line 90

def scenario_outline(statement)
  #log.debug "SCENARIO OUTLINE"
  
  outline = YARD::CodeObjects::Cucumber::ScenarioOutline.new(@feature,"scenario_#{@feature.scenarios.length + 1}") do |s|
    s.comments = statement.comments.map{|comment| comment.value}.join("\n")
    s.description = statement.description
    s.add_file(@file,statement.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
  @feature.scenarios << outline
  @step_container = outline
end

#step(step) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cucumber/city_builder.rb', line 160

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

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

  @table_owner.comments = step.comments.map{|comment| comment.value}.join("\n")

  multiline_arg = rubify(step.multiline_arg)
  
  case(multiline_arg)
  when Gherkin::Formatter::Model::PyString
    @table_owner.text = multiline_arg.value
  when Array
    #log.info "Matrix: #{matrix(multiline_arg).collect{|row| row.collect{|cell| cell.class } }.flatten.join("\n")}"
    @table_owner.table = matrix(multiline_arg)
  end
  
  @table_owner.scenario = @step_container
  @step_container.steps << @table_owner
end

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



188
189
190
# File 'lib/cucumber/city_builder.rb', line 188

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