Class: Hiptest::GherkinAdder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ GherkinAdder

Returns a new instance of GherkinAdder.



10
11
12
13
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 10

def initialize(project)
  @project = project
  @indexer = ActionwordIndexer.new(project)
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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 84

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



25
26
27
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 25

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

#code_annotation(call) ⇒ Object



33
34
35
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 33

def code_annotation(call)
  annotation(call) || "Given"
end

#evaluate(value) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 115

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



106
107
108
109
110
111
112
113
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 106

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



101
102
103
104
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 101

def get_actionword(call)
  actionword = @indexer.get_index(call.children[:actionword])
  actionword && actionword[:actionword] || nil
end

#get_actionword_parameters(call) ⇒ Object



96
97
98
99
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 96

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

#pattern(actionword) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 60

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 37

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



29
30
31
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 29

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

#update_callsObject



15
16
17
18
19
20
21
22
23
# File 'lib/hiptest-publisher/gherkin_adder.rb', line 15

def update_calls
  @project.each_sub_nodes(Hiptest::Nodes::Call) do |call|
    call.children[:gherkin_text] ||= "#{text_annotation(call)} #{prettified(call)}"
    if actionword = get_actionword(call)
      actionword.children[:gherkin_annotation] ||= code_annotation(call)
      actionword.children[:gherkin_pattern] ||= pattern(actionword)
    end
  end
end