Class: CukeSniffer::DeadStepsHelper

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/cuke_sniffer/dead_steps_helper.rb

Overview

Author

Robert Cochran ([email protected])

Copyright

Copyright © 2014 Robert Cochran

License

Distributes under the MIT License

Mixins: CukeSniffer::Constants A static class to aid in the identification of dead steps.

Constant Summary

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

Class Method Summary collapse

Class Method Details

.build_dead_steps_hash(step_definitions) ⇒ Object

Returns a hash of dead steps for displaying in the html.



11
12
13
14
15
16
# File 'lib/cuke_sniffer/dead_steps_helper.rb', line 11

def self.build_dead_steps_hash(step_definitions)
  dead_steps_hash = gather_all_dead_steps_by_file(step_definitions)
  sort_dead_steps_in_file!(dead_steps_hash)
  dead_steps_hash[:total] = count_dead_steps(dead_steps_hash)
  dead_steps_hash
end

.count_dead_steps(dead_steps_hash) ⇒ Object

Returns the count of all possible dead steps in each file



46
47
48
49
50
51
52
# File 'lib/cuke_sniffer/dead_steps_helper.rb', line 46

def self.count_dead_steps(dead_steps_hash)
  count = 0
  dead_steps_hash.each_value do |dead_steps_in_file_list|
    count += dead_steps_in_file_list.size
  end
  count
end

.format_step_definition_regex(regex) ⇒ Object

Formats the regex of a step definition to remove the (?-mix) part of the to_s



34
35
36
# File 'lib/cuke_sniffer/dead_steps_helper.rb', line 34

def self.format_step_definition_regex(regex)
  regex.to_s.match(/\(\?\-mix\:(?<regex>.*)\)/)[:regex]
end

.gather_all_dead_steps_by_file(step_definitions) ⇒ Object

Returns all dead step definitions in a file



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cuke_sniffer/dead_steps_helper.rb', line 19

def self.gather_all_dead_steps_by_file(step_definitions)
  dead_steps_hash = {}
  step_definitions.each do |step_definition|
    location_match = step_definition.location.match(/(?<file>.*).rb:(?<line>\d+)/)
    file_name = location_match[:file]
    regex = format_step_definition_regex(step_definition.regex)
    if step_definition.calls.empty?
      dead_steps_hash[file_name] ||= []
      dead_steps_hash[file_name] << "#{location_match[:line]}: /#{regex}/"
    end
  end
  dead_steps_hash
end

.sort_dead_steps_in_file!(dead_steps_hash) ⇒ Object

Sorts the dead steps found in a hash by the line number in the file



39
40
41
42
43
# File 'lib/cuke_sniffer/dead_steps_helper.rb', line 39

def self.sort_dead_steps_in_file!(dead_steps_hash)
  dead_steps_hash.each_key do |file|
    dead_steps_hash[file].sort_by! { |row| row[/^\d+/].to_i }
  end
end