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.



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

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

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

Class Method Details

.add(project) ⇒ Object



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

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

Instance Method Details

#all_valued_arguments_for(call) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 171

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



42
43
44
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 42

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

#code_annotation(call) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 50

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



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

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



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

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



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

def get_actionword(call)

  actionword = call.is_a?(Hiptest::Nodes::UIDCall) ? @indexer.get_uid_index(call.children[:uid]) : @indexer.get_index(call.children[:actionword])

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

#get_actionword_parameters(call) ⇒ Object



183
184
185
186
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 183

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

#order_parameters_by_pattern(actionword) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 160

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



111
112
113
114
115
116
117
118
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 111

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



60
61
62
63
64
65
66
67
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 60

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



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/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 120

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



69
70
71
72
73
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
# File 'lib/hiptest-publisher/node_modifiers/gherkin_adder.rb', line 69

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

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

  call_chunks = call.is_a?(Hiptest::Nodes::Call) ? call.children[:actionword].split("\"", -1) : call.children[: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



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

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

#update_callsObject



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

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, Hiptest::Nodes::UIDCall) do |call|
      set_call_chunks(call)
      call.children[:gherkin_text] ||= "#{text_annotation(call)} #{prettified(call)}"

      if actionword = get_actionword(call)
        @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