Class: Chutney::UnknownVariable

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

Overview

service class to lint for unknown variables

Instance Attribute Summary

Attributes inherited from Linter

#configuration, #filename, #issues

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

Instance Method Details

#gather_vars(string) ⇒ Object



36
37
38
# File 'lib/chutney/linter/unknown_variable.rb', line 36

def gather_vars(string)
  string.scan(/<.+?>/).map { |val| val[1..-2] }
end

#gather_vars_from_argument(argument) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/chutney/linter/unknown_variable.rb', line 28

def gather_vars_from_argument(argument)
  return gather_vars argument.content if argument.is_a? CukeModeler::DocString

  argument.rows.map do |row|
    row.cells.map { |cell| gather_vars cell.value }.flatten
  end.flatten
end

#known_variables(scenario) ⇒ Object



40
41
42
43
44
# File 'lib/chutney/linter/unknown_variable.rb', line 40

def known_variables(scenario)
  return [] unless scenario.is_a? CukeModeler::Outline

  scenario.examples.map { |ex| ex.rows.first.cells.map(&:value) }.flatten
end

#lintObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/chutney/linter/unknown_variable.rb', line 6

def lint
  filled_scenarios do |feature, scenario|
    known_vars = Set.new(known_variables(scenario))
    scenario.steps.each do |step|
      step_vars(step).each do |used_var|
        next if known_vars.include? used_var

        add_issue(
          I18n.t('linters.unknown_variable', variable: used_var), feature, scenario
        )
      end
    end
  end
end

#step_vars(step) ⇒ Object



21
22
23
24
25
26
# File 'lib/chutney/linter/unknown_variable.rb', line 21

def step_vars(step)
  vars = gather_vars step.text
  return vars unless step.block

  vars + gather_vars_from_argument(step.block)
end