Class: Chutney::SameScenario

Inherits:
Linter
  • Object
show all
Defined in:
lib/chutney/linter/same_scenario.rb

Overview

Rule to find all the duplicated scenarios

Instance Attribute Summary

Attributes inherited from Linter

#configuration, #filename, #issues

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Linter

#add_issue, #and_word?, #background, #background_word?, #but_word?, descendants, #dialect, #dialect_word, #elements, #examples_word?, #feature, #feature_word?, #filled_scenarios, #given_word?, #initialize, linter_name, #linter_name, #location, #off_switch?, #render_step, #render_step_argument, #scenario_outline_word?, #scenarios, #steps, #tags_for, #then_word?, #type, #when_word?

Constructor Details

This class inherits a constructor from Chutney::Linter

Class Method Details

.dictionaryObject



10
11
12
# File 'lib/chutney/linter/same_scenario.rb', line 10

def self.dictionary
  @dictionary ||= Hash.new { |hash, key| hash[key] = [] }
end

.resetObject



6
7
8
# File 'lib/chutney/linter/same_scenario.rb', line 6

def self.reset
  @dictionary = nil
end

Instance Method Details

#child_text(step) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chutney/linter/same_scenario.rb', line 39

def child_text(step)
  step.children.map do |child|
    if child.is_a?(CukeModeler::Table)
      child.rows.flat_map(&:cells).map(&:value).join(' | ')
    elsif child.is_a?(CukeModeler::DocString)
      child.content
    else
      ''
    end
  end
end

#example_text(scenario) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/chutney/linter/same_scenario.rb', line 51

def example_text(scenario)
  if scenario.is_a?(CukeModeler::Outline)
    "\n" + scenario.examples
                   .flat_map(&:rows)
                   .flat_map(&:cells)
                   .map(&:value).join(' | ')
  else
    ''
  end
end

#lintObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/chutney/linter/same_scenario.rb', line 14

def lint
  scenarios do |feature, scenario|
    text = background ? background.steps.map(&:to_s).join("\n").concat("\n") : ''
    text += scenario
           .steps
           .map { |step| "#{step.text} #{child_text(step)}" }
           .join("\n") + example_text(scenario)
    digest = Digest::SHA2.hexdigest(text)
    SameScenario.dictionary[digest] << { scenario:, feature: }
  end

  SameScenario.dictionary.filter { |_k, v| v.size > 1 }
              .each_value do |v|
    v[1...].each_with_index do |problem, index|
      add_issue(I18n.t('linters.same_scenario',
                       feature: problem[:feature].name,
                       scenario: problem[:scenario].name,
                       original_feature: v.first[:feature].name,
                       original_scenario: v.first[:scenario].name),
                problem[:feature], problem[:scenario], nil)
      v.delete_at(index + 1)
    end
  end
end