Sha256: 10e302c323324702364262624936f4cff0a04b483d24f5a770e61fc44f1d33b9

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

require 'gherkin_lint/linter'

module GherkinLint
  # service class to lint for unknown variables
  class UnknownVariable < Linter
    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

    def step_vars(step)
      vars = gather_vars step['name']
      vars += gather_vars step['doc_string']['value'] if step.key? 'doc_string'
      vars + (step['rows'] || []).map do |row|
        row['cells'].map { |value| gather_vars value }.flatten
      end.flatten
    end

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

    def known_variables(scenario)
      (scenario['examples'] || []).map do |example|
        next unless example.key? 'rows'
        example['rows'].first['cells'].map(&:strip)
      end.flatten
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gherkin_lint-0.3.1 lib/gherkin_lint/linter/unknown_variable.rb
gherkin_lint-0.3.0 lib/gherkin_lint/linter/unknown_variable.rb