Sha256: ccbf08ef952c51800ecf33bec9c181014e581003e238b9d5a9ade173eabd1614

Contents?: true

Size: 1.28 KB

Versions: 9

Compression:

Stored size: 1.28 KB

Contents

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

    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

9 entries across 9 versions & 1 rubygems

Version Path
chutney-2.2.1 lib/chutney/linter/unknown_variable.rb
chutney-2.1.1 lib/chutney/linter/unknown_variable.rb
chutney-2.1.0 lib/chutney/linter/unknown_variable.rb
chutney-2.0.3.1 lib/chutney/linter/unknown_variable.rb
chutney-2.0.3 lib/chutney/linter/unknown_variable.rb
chutney-2.0.2 lib/chutney/linter/unknown_variable.rb
chutney-2.0.1 lib/chutney/linter/unknown_variable.rb
chutney-2.0.0 lib/chutney/linter/unknown_variable.rb
chutney-2.0.0.rc1 lib/chutney/linter/unknown_variable.rb