Class: Hiptest::NodeModifiers::GherkinAdder

Inherits:
Object
  • Object
show all
Defined in:
lib/hiptest-publisher/node_modifiers/gherkin_adder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ GherkinAdder

Returns a new instance of GherkinAdder.



12
13
14
15
16
17
18
19
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 12

def initialize(project)
  @project = project
  @indexer = ActionwordIndexer.new(project)
  @library_indexer = LibraryActionwordIndexer.new(project)
  @annotations_counter = AnnotationsCounter.new

  @special_params = ['__free_text', '__datatable']
end

Class Method Details

.add(project) ⇒ Object



8
9
10
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 8

def self.add(project)
  self.new(project).update_calls
end

Instance Method Details

#all_valued_arguments_for(call) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 177

def all_valued_arguments_for(call)
  evaluated_call_arguments = evaluated_map(call.children[:arguments])
  evaluated_actionword_parameters = evaluated_map(get_actionword_parameters(call))
  names = evaluated_actionword_parameters.keys

  hash_array = names.map { |name|
    value = evaluated_call_arguments[name] || evaluated_actionword_parameters[name] || ""
    [name, value]
  }
  Hash[hash_array]
end

#annotation(call) ⇒ Object



47
48
49
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 47

def annotation(call)
  call.children[:annotation].capitalize if call.children[:annotation]
end

#code_annotation(call) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 55

def code_annotation(call)
  call_annotation = annotation(call)
  if call_annotation
    if ["And", "But"].include?(call_annotation)
      call_annotation = @last_annotation || "Given"
    end
    @last_annotation = call_annotation
  end
end

#evaluate(value) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 213

def evaluate(value)
  if value.nil?
    nil
  elsif Hiptest::Nodes::Variable === value
    "<#{value.children[:name]}>"
  elsif value.children[:chunks]
    value.children[:chunks].map {|chunk| evaluate(chunk) }.join('')
  elsif value.children[:value]
    value.children[:value]
  else
    nil
  end
end

#evaluated_map(named_values) ⇒ Object



204
205
206
207
208
209
210
211
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 204

def evaluated_map(named_values)
  hash_array = named_values.map do |named_value|
    name = named_value.children[:name]
    value = evaluate(named_value.children[:value] || named_value.children[:default])
    [name, value]
  end
  Hash[hash_array]
end

#get_actionword(call) ⇒ Object



194
195
196
197
198
199
200
201
202
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 194

def get_actionword(call)
  actionword = @library_indexer.get_index(call.children[:actionword])

  if actionword.nil?
    actionword = @indexer.get_index(call.children[:actionword])
  end

  actionword && actionword[:actionword] || nil
end

#get_actionword_parameters(call) ⇒ Object



189
190
191
192
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 189

def get_actionword_parameters(call)
  actionword = get_actionword(call)
  actionword && actionword.children[:parameters] || []
end

#order_parameters_by_pattern(actionword) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 166

def order_parameters_by_pattern(actionword)
  inline_parameter_names = actionword.children[:name].scan(/\"(.*?)\"/).flatten
  actionword_parameters = {}
  actionword.children[:parameters].map {|p| actionword_parameters[p.children[:name]] = p}

  missing_parameter_names = actionword_parameters.keys - inline_parameter_names - @special_params
  [inline_parameter_names, missing_parameter_names, @special_params].flatten.map do |name|
    actionword_parameters[name]
  end.compact
end

#pattern(actionword) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 117

def pattern(actionword)
  patterned = actionword.chunks.map {|chunk| chunk[:value]}.join("\"")
  actionword.extra_inlined_parameters.each do |param|
    patterned += " \"#{param[:value]}\""
  end

  "^#{patterned.strip}$"
end

#prettified(call) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 65

def prettified(call)
  base = call.chunks.map {|chunk| chunk[:value]}.join("\"").strip
  call.extra_inlined_arguments.each do |chunk|
    base += " \"#{chunk[:value]}\""
  end

  base
end

#set_actionwords_chunks(actionword) ⇒ Object



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
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 126

def set_actionwords_chunks(actionword)
  name = actionword.uniq_name
  actionword_parameters = evaluated_map(actionword.children[:parameters])
  name_chunks = name.split("\"", -1)
  inline_parameter_names = []

  actionword.chunks = []
  actionword.extra_inlined_parameters = []

  name_chunks.each_slice(2) do |text, inline_parameter_name|
    actionword.chunks << {
      value: text.gsub(/[.|()\\.+*?\[\]{}^$]/) { |c| "\\#{c}" },
      is_parameter: false
    }

    inline_parameter_names << inline_parameter_name if inline_parameter_name
    if actionword_parameters.has_key?(inline_parameter_name)
      actionword.chunks << {
        value: "(.*)",
        name: inline_parameter_name,
        is_parameter: true
      }
    else
      actionword.chunks << {
        value: inline_parameter_name,
        is_parameter: false
      } if inline_parameter_name
    end
  end
  missing_parameter_names = actionword_parameters.keys - inline_parameter_names - @special_params

  missing_parameter_names.each do |missing_parameter_name|
    actionword.extra_inlined_parameters << {
      value: "(.*)",
      name: missing_parameter_name,
      is_parameter: true
    }
  end
end

#set_call_chunks(call) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 74

def set_call_chunks(call)
  all_arguments = all_valued_arguments_for(call)
  inline_parameter_names = []
  extra_inlined_arguments = []

  call.chunks = []
  call.extra_inlined_arguments = []

  actionword_name = call.is_a?(Hiptest::Nodes::Call) ? call.children[:actionword] : call.children[:actionword_name]
  call_chunks = actionword_name.split("\"", -1)

  call_chunks.each_slice(2) do |text, inline_parameter_name|
    call.chunks << {
      value: text,
      is_argument: false
    }

    if all_arguments.has_key?(inline_parameter_name)
      inline_parameter_names << inline_parameter_name.clone
      value = all_arguments[inline_parameter_name]
      inline_parameter_name.replace(value)

      call.chunks << {
        value: inline_parameter_name,
        is_argument: true
      }
    else
     call.chunks << {
        value: inline_parameter_name,
        is_argument: false
      } unless inline_parameter_name.nil?
    end
  end

  missing_parameter_names = all_arguments.keys - inline_parameter_names - @special_params
  call.extra_inlined_arguments = missing_parameter_names.map do |missing_parameter_name|
    {
      value: all_arguments[missing_parameter_name],
      is_argument: true
    }
  end
end

#text_annotation(call) ⇒ Object



51
52
53
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 51

def text_annotation(call)
  annotation(call) || "*"
end

#update_callsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 21

def update_calls
  @project.each_sub_nodes(Hiptest::Nodes::Scenario, Hiptest::Nodes::Actionword, Hiptest::Nodes::Test, Hiptest::Nodes::Folder) do |item|
    @last_annotation = nil
    item.each_sub_nodes(Hiptest::Nodes::Call) do |call|
      set_call_chunks(call)
      call.children[:gherkin_text] ||= "#{text_annotation(call)} #{prettified(call)}"

      if actionword = get_actionword(call)
        if actionword.is_a?(Hiptest::Nodes::LibraryActionword)
          call.children[:library_name] = actionword.parent.children[:name]
        end
        @annotations_counter.increment(actionword, code_annotation(call))
        set_actionwords_chunks(actionword)

        actionword.children[:gherkin_pattern] ||= pattern(actionword)
        actionword.children[:parameters_ordered_by_pattern] ||= order_parameters_by_pattern(actionword)
      end
    end
  end

  @annotations_counter.actionwords.each do |actionword|
    actionword.children[:gherkin_annotation] = @annotations_counter.most_used_annotation(actionword) || "Given"
    actionword.children[:gherkin_used_annotations] = @annotations_counter.all_used_annotations(actionword) || ['Given']
  end
end