Class: CukeSniffer::CukeSnifferHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cuke_sniffer/cuke_sniffer_helper.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Static class used for aiding cuke_sniffer in various tasks

Class Method Summary collapse

Class Method Details

.build_rule(symbol, rule_hash) ⇒ Object

Builds rule object out of a hash. See CukeSniffer::RulesConfig for hash example.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 95

def self.build_rule(symbol, rule_hash)
  rule = CukeSniffer::Rule.new
  rule.symbol = symbol
  rule.phrase = rule_hash[:phrase]
  rule.score = rule_hash[:score]
  rule.enabled = rule_hash[:enabled]
  conditional_keys = rule_hash.keys - [:phrase, :score, :enabled, :targets, :reason]
  conditions = {}
  conditional_keys.each do |key|
    conditions[key] = (rule_hash[key].kind_of? Array) ? Array.new(rule_hash[key]) : rule_hash[key]
  end
  rule.conditions = conditions
  rule.reason = rule_hash[:reason]
  rule.targets = rule_hash[:targets]
  rule
end

.build_rules(rules) ⇒ Object

Builds a list of rule objects out of a hash. See CukeSniffer::RulesConfig for hash example.



87
88
89
90
91
92
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 87

def self.build_rules(rules)
  return [] if rules.nil?
  rules.collect do |symbol, value|
    CukeSniffer::CukeSnifferHelper.build_rule(symbol, value)
  end
end

.build_updated_step_from_example(step, variable_list, row_variables) ⇒ Object

Creates a step call from the details of an example table



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 41

def self.build_updated_step_from_example(step, variable_list, row_variables)
  new_step = step.dup
  variable_list.each do |variable|
    if step.include? variable
      table_variable_to_insert = row_variables[variable_list.index(variable)]
      table_variable_to_insert ||= ""
      new_step.gsub!("<#{variable}>", table_variable_to_insert)
    end
  end
  new_step
end

.catalog_possible_dead_steps(step_definitions, steps_with_expressions) ⇒ Object

Applies all possible fuzzy calls to a step definition.



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 142

def self.catalog_possible_dead_steps(step_definitions, steps_with_expressions)
  step_definitions.each do |step_definition|
    next unless step_definition.calls.empty?
    regex_as_string = step_definition.regex.to_s.gsub(/\(\?-mix:\^?/, "").gsub(/\$\)$/, "")
    steps_with_expressions.each do |step_location, step_value|
      if regex_as_string =~ step_value
        step_definition.add_call(step_location, step_value)
      end
    end
  end
  step_definitions
end

.convert_steps_with_expressions(steps_with_expressions) ⇒ Object

Returns a fuzzy match for a step definition for cataloging steps.



124
125
126
127
128
129
130
131
132
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 124

def self.convert_steps_with_expressions(steps_with_expressions)
  step_regex_hash = {}
  steps_with_expressions.each do |step_location, step_value|
    modified_step = step_value.gsub(/\#{[^}]*}/, '.*')
    next if modified_step == '.*'
    step_regex_hash[step_location] = Regexp.new('^' + modified_step + '$')
  end
  step_regex_hash
end

.extract_scenario_outline_steps(scenario) ⇒ Object

Creates a hash of steps with the build up example step calls.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 54

def self.extract_scenario_outline_steps(scenario)
  steps = {}
  examples = scenario.examples_table
  return {} if examples.empty?
  variable_list = extract_variables_from_example(examples.first)
  (1...examples.size).each do |example_counter|
    #TODO Abstraction needed for this regex matcher (constants?)
    next if examples[example_counter] =~ /^\#.*$/
    row_variables = extract_variables_from_example(examples[example_counter])
    step_counter = 1
    scenario.steps.each do |step|
      step_line = scenario.start_line + step_counter
      location = "#{scenario.location.gsub(/\d+$/, step_line.to_s)}(Example #{example_counter})"
      steps[location] = build_updated_step_from_example(step, variable_list, row_variables)
      step_counter += 1
    end
  end
  steps
end

.extract_scenario_steps(scenario) ⇒ Object

Returns all steps found in a scenario



75
76
77
78
79
80
81
82
83
84
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 75

def self.extract_scenario_steps(scenario)
  steps_hash = {}
  counter = 1
  scenario.steps.each do |step|
    location = scenario.location.gsub(/:\d*$/, ":#{scenario.start_line + counter}")
    steps_hash[location] = step
    counter += 1
  end
  steps_hash
end

.extract_steps_from_features(features) ⇒ Object

Iterates over the passed features list and returns all steps found in scenarios and backgrounds.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 9

def self.extract_steps_from_features(features)
  steps = {}
  features.each do |feature|
    steps.merge! extract_scenario_steps(feature.background) unless feature.background.nil?
    feature.scenarios.each do |scenario|
      if scenario.type == "Scenario Outline"
        steps.merge! extract_scenario_outline_steps(scenario)
      else
        steps.merge! extract_scenario_steps(scenario)
      end
    end
  end
  steps
end

.extract_steps_from_step_definitions(step_definitions) ⇒ Object

Returns a list of all nested step calls found in a step definition.



113
114
115
116
117
118
119
120
121
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 113

def self.extract_steps_from_step_definitions(step_definitions)
  steps = {}
  step_definitions.each do |definition|
    definition.nested_steps.each do |location, step|
      steps[location] = step
    end
  end
  steps
end

.extract_variables_from_example(example) ⇒ Object

Grabs the values from an example without the bars



35
36
37
38
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 35

def self.extract_variables_from_example(example)
  example = example[example.index('|')..example.length]
  example.split(/\s*\|\s*/) - [""]
end

.get_all_scenarios(features) ⇒ Object

Iterates over the passed features list and returns all scenarios and backgrounds found.



25
26
27
28
29
30
31
32
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 25

def self.get_all_scenarios(features)
  scenarios = []
  features.each do |feature|
    scenarios << feature.background unless feature.background.nil?
    scenarios << feature.scenarios
  end
  scenarios.flatten
end

.get_all_steps(features, step_definitions) ⇒ Object

Extracts all possible step calls from the passed features and step definitions.



135
136
137
138
139
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 135

def self.get_all_steps(features, step_definitions)
  feature_steps = CukeSniffer::CukeSnifferHelper.extract_steps_from_features(features)
  step_definition_steps = CukeSniffer::CukeSnifferHelper.extract_steps_from_step_definitions(step_definitions)
  feature_steps.merge step_definition_steps
end

.get_steps_with_expressions(steps) ⇒ Object

Returns a list of all step definitions with a capture group



156
157
158
159
160
161
162
163
164
# File 'lib/cuke_sniffer/cuke_sniffer_helper.rb', line 156

def self.get_steps_with_expressions(steps)
  steps_with_expressions = {}
  steps.each do |step_location, step_value|
    if step_value =~ /\#{.*}/
      steps_with_expressions[step_location] = step_value
    end
  end
  steps_with_expressions
end