Class: Hiptest::GherkinAdder
- Inherits:
-
Object
- Object
- Hiptest::GherkinAdder
- Defined in:
- lib/hiptest-publisher/gherkin_adder.rb
Class Method Summary collapse
Instance Method Summary collapse
- #all_valued_arguments_for(call) ⇒ Object
- #annotation(call) ⇒ Object
- #code_annotation(call) ⇒ Object
- #evaluate(value) ⇒ Object
- #evaluated_map(named_values) ⇒ Object
- #get_actionword(call) ⇒ Object
- #get_actionword_parameters(call) ⇒ Object
-
#initialize(project) ⇒ GherkinAdder
constructor
A new instance of GherkinAdder.
- #pattern(actionword) ⇒ Object
- #prettified(call) ⇒ Object
- #text_annotation(call) ⇒ Object
- #update_calls ⇒ Object
Constructor Details
#initialize(project) ⇒ GherkinAdder
Returns a new instance of GherkinAdder.
10 11 12 13 14 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 10 def initialize(project) @project = project @indexer = ActionwordIndexer.new(project) @annotations_counter = AnnotationsCounter.new end |
Class Method Details
.add(project) ⇒ Object
6 7 8 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 6 def self.add(project) GherkinAdder.new(project).update_calls end |
Instance Method Details
#all_valued_arguments_for(call) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 98 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
33 34 35 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 33 def annotation(call) call.children[:annotation].capitalize if call.children[:annotation] end |
#code_annotation(call) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 41 def code_annotation(call) call_annotation = annotation(call) if call_annotation if call_annotation == "And" call_annotation = @last_annotation || "Given" end @last_annotation = call_annotation end end |
#evaluate(value) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 129 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
120 121 122 123 124 125 126 127 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 120 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
115 116 117 118 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 115 def get_actionword(call) actionword = @indexer.get_index(call.children[:actionword]) actionword && actionword[:actionword] || nil end |
#get_actionword_parameters(call) ⇒ Object
110 111 112 113 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 110 def get_actionword_parameters(call) actionword = get_actionword(call) actionword && actionword.children[:parameters] || [] end |
#pattern(actionword) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 74 def pattern(actionword) name = actionword.children[:name] actionword_parameters = evaluated_map(actionword.children[:parameters]) name_chunks = name.split("\"", -1) result = [] inline_parameter_names = [] name_chunks.each_slice(2) do |text, inline_parameter_name| result << text.gsub(/[.|()\\.+*?\[\]{}^$]/) { |c| "\\#{c}" } inline_parameter_names << inline_parameter_name if inline_parameter_name if actionword_parameters.has_key?(inline_parameter_name) result << "(.*)" else result << inline_parameter_name if inline_parameter_name end end missing_parameter_names = actionword_parameters.keys - inline_parameter_names patterned = result.join("\"") missing_parameter_names.each do |missing_parameter_name| patterned << " \"(.*)\"" end "^#{patterned}$" end |
#prettified(call) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 51 def prettified(call) all_arguments = all_valued_arguments_for(call) inline_parameter_names = [] call_chunks = call.children[:actionword].split("\"", -1) call_chunks.each_slice(2) do |text, inline_parameter_name| 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) end end missing_parameter_names = all_arguments.keys - inline_parameter_names prettified = call_chunks.join("\"") missing_parameter_names.each do |missing_parameter_name| value = all_arguments[missing_parameter_name] prettified << " \"#{value}\"" end prettified end |
#text_annotation(call) ⇒ Object
37 38 39 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 37 def text_annotation(call) annotation(call) || "*" end |
#update_calls ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 16 def update_calls @project.each_sub_nodes(Hiptest::Nodes::Scenario, Hiptest::Nodes::Actionword, Hiptest::Nodes::Test) do |item| @last_annotation = nil item.each_sub_nodes(Hiptest::Nodes::Call) do |call| call.children[:gherkin_text] ||= "#{text_annotation(call)} #{prettified(call)}" if actionword = get_actionword(call) @annotations_counter.increment(actionword, code_annotation(call)) actionword.children[:gherkin_pattern] ||= pattern(actionword) end end end @annotations_counter.actionwords.each do |actionword| actionword.children[:gherkin_annotation] = @annotations_counter.most_used_annotation(actionword) || "Given" end end |