Class: CukeSniffer::StepDefinition

Inherits:
RuleTarget show all
Defined in:
lib/cuke_sniffer/step_definition.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Translates and evaluates Cucumber step definitions Extends CukeSniffer::RulesEvaluator

Constant Summary

Constants included from RuleConfig

RuleConfig::ERROR, RuleConfig::FATAL, RuleConfig::INFO, RuleConfig::RULES, RuleConfig::WARNING

Constants included from Constants

Constants::COMMENT_REGEX, Constants::DATE_REGEX, Constants::DEFAULT_OUTPUT_FILE_NAME, Constants::FILE_IGNORE_LIST, Constants::HOOK_REGEX, Constants::HOOK_STYLES, Constants::MARKUP_SOURCE, Constants::SCENARIO_TITLE_STYLES, Constants::STEP_DEFINITION_REGEX, Constants::STEP_REGEX, Constants::STEP_STYLES, Constants::TAG_REGEX, Constants::THRESHOLDS

Instance Attribute Summary collapse

Attributes inherited from RuleTarget

#location, #rules_hash, #score, #type

Instance Method Summary collapse

Methods inherited from RuleTarget

#good?, #is_comment?, #problem_percentage, #store_rule, #store_rule_many_times

Constructor Details

#initialize(location, step_definition_block) ⇒ StepDefinition

location must be in the format of “file_pathfile_name.rb:line_number” step_definition_block is an array of strings that represents the step definition must contain the regex line and its pairing end



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cuke_sniffer/step_definition.rb', line 45

def initialize(location, step_definition_block)
  @parameters = []
  @calls = {}
  @nested_steps = {}
  super(location)
  extract_start_line(location)
  extract_code(step_definition_block)
  extract_step_definition_signature(step_definition_block)

  detect_nested_steps
end

Instance Attribute Details

#callsObject

hash: Contains each call that is made to a step definition

  • Key: Location in which the step definition is called from

  • Value: The step string that matched the regex

In the case of a fuzzy match it will be a regex of the step call that was the inverse match of the regex translated into a string.



37
38
39
# File 'lib/cuke_sniffer/step_definition.rb', line 37

def calls
  @calls
end

#codeObject

string array: List of all of the content between the regex and the end of the step definition.



40
41
42
# File 'lib/cuke_sniffer/step_definition.rb', line 40

def code
  @code
end

#nested_stepsObject

hash: Contains each nested step call a step definition has

  • Key: location:line of the nested step

  • Value: The step call that appears on the line



29
30
31
# File 'lib/cuke_sniffer/step_definition.rb', line 29

def nested_steps
  @nested_steps
end

#parametersObject

string array: List of the parameters a step definition has



24
25
26
# File 'lib/cuke_sniffer/step_definition.rb', line 24

def parameters
  @parameters
end

#regexObject

Regex: Regex that cucumber uses to match step calls



21
22
23
# File 'lib/cuke_sniffer/step_definition.rb', line 21

def regex
  @regex
end

#start_lineObject

int: Line on which a step definition starts



18
19
20
# File 'lib/cuke_sniffer/step_definition.rb', line 18

def start_line
  @start_line
end

Instance Method Details

#==(comparison_object) ⇒ Object

:nodoc:



62
63
64
# File 'lib/cuke_sniffer/step_definition.rb', line 62

def ==(comparison_object) # :nodoc:
  comparison_object.regex == regex && comparison_object.parameters == parameters
end

#add_call(location, step_string) ⇒ Object

Adds new location => step_string pairs to the calls hash



58
59
60
# File 'lib/cuke_sniffer/step_definition.rb', line 58

def add_call(location, step_string)
  @calls[location] = step_string
end

#condensed_call_listObject



66
67
68
69
70
71
72
73
# File 'lib/cuke_sniffer/step_definition.rb', line 66

def condensed_call_list
  condensed_list = {}
  @calls.each do |call, step_string|
    condensed_list[step_string] ||= []
    condensed_list[step_string] << call
  end
  condensed_list
end

#recursive_nested_stepsObject



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

def recursive_nested_steps
  recursive_nested_steps_map = {}
  @nested_steps.each{|location, nested_step|
    if nested_step =~ @regex
      recursive_nested_steps_map[location] = nested_step
    end
  }
  recursive_nested_steps_map
end

#todoObject



85
86
87
# File 'lib/cuke_sniffer/step_definition.rb', line 85

def todo
  @code.select {|line|line =~ /#(TODO|todo)/ }
end