Class: GherkinLint::UnknownVariable

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

Overview

service class to lint for unknown variables

Instance Attribute Summary

Attributes inherited from Linter

#issues

Instance Method Summary collapse

Methods inherited from Linter

#add_error, #add_warning, #backgrounds, descendants, #elements, #features, #files, #filled_scenarios, #filter_tag, #initialize, #line, #lint_files, #name, #reference, #render_step, #render_step_argument, #scenarios, #steps, #suppress, #suppress_tags, #tag?

Constructor Details

This class inherits a constructor from GherkinLint::Linter

Instance Method Details

#gather_vars(string) ⇒ Object



32
33
34
# File 'lib/gherkin_lint/linter/unknown_variable.rb', line 32

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

#gather_vars_from_argument(argument) ⇒ Object



25
26
27
28
29
30
# File 'lib/gherkin_lint/linter/unknown_variable.rb', line 25

def gather_vars_from_argument(argument)
  return gather_vars argument[:content] if argument[:type] == :DocString
  (argument[:rows] || []).map do |row|
    row[:cells].map { |value| gather_vars value[:value] }.flatten
  end.flatten
end

#known_variables(scenario) ⇒ Object



36
37
38
39
40
41
# File 'lib/gherkin_lint/linter/unknown_variable.rb', line 36

def known_variables(scenario)
  (scenario[:examples] || []).map do |example|
    next unless example.key? :tableHeader
    example[:tableHeader][:cells].map { |cell| cell[:value].strip }
  end.flatten
end

#lintObject



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

def lint
  filled_scenarios do |file, 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
        references = [reference(file, feature, scenario)]
        add_error(references, "'<#{used_var}>' is unknown")
      end
    end
  end
end

#step_vars(step) ⇒ Object



19
20
21
22
23
# File 'lib/gherkin_lint/linter/unknown_variable.rb', line 19

def step_vars(step)
  vars = gather_vars step[:text]
  return vars unless step.include? :argument
  vars + gather_vars_from_argument(step[:argument])
end