Sha256: 0676383c3d35e54615de1d83530b4d8cec9998c88ea921c983b50c3de09893f5

Contents?: true

Size: 1.32 KB

Versions: 5

Compression:

Stored size: 1.32 KB

Contents

require 'chutney/linter'

module Chutney
  # 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, "Variable '<#{used_var}>' is unknown")
          end
        end
      end
    end

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

    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

    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? :tableHeader
        
        example[:tableHeader][:cells].map { |cell| cell[:value].strip }
      end.flatten
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
chutney-1.6.3 lib/chutney/linter/unknown_variable.rb
chutney-1.6.2 lib/chutney/linter/unknown_variable.rb
chutney-1.6.1 lib/chutney/linter/unknown_variable.rb
chutney-1.6.0 lib/chutney/linter/unknown_variable.rb
chutney-0.5.0 lib/chutney/linter/unknown_variable.rb