Sha256: dc4a659c35ae6bd18d7aeb95b822096d77bc433cd5e8b807e762bf373f237b85

Contents?: true

Size: 1.7 KB

Versions: 7

Compression:

Stored size: 1.7 KB

Contents

module Chutney
  # service class to lint for using outline
  class UseOutline < Linter
    def lint
      check_similarity(gather_scenarios(feature))
    end

    def check_similarity(scenarios)
      scenarios.product(scenarios) do |lhs, rhs|
        next if lhs == rhs
        next if lhs[:reference][:line] > rhs[:reference][:line]
        
        similarity = determine_similarity(lhs[:text], rhs[:text])
        next unless similarity >= 0.95
        
        similarity_pct = similarity.round(3) * 100
        
        add_issue(lhs, rhs, similarity_pct)
      end
    end
    
    def add_issue(lhs, rhs, pct)
      super(I18n.t('linters.use_outline', 
                   pct: pct,
                   lhs_name: lhs[:name],
                   lhs_line: lhs[:reference][:line],
                   rhs_name: rhs[:name],
                   rhs_line: rhs[:reference][:line]),
                  feature)
    end

    def determine_similarity(lhs, rhs)
      matcher = Amatch::Jaro.new(lhs)
      matcher.match(rhs)
    end

    def gather_scenarios(feature)
      scenarios = []
      return scenarios unless feature.include? :children
      
      feature[:children].each do |scenario|
        next unless scenario[:type] == :Scenario
        next unless scenario.include? :steps
        next if scenario[:steps].empty?
        
        scenarios.push generate_reference(feature, scenario)
      end
      scenarios
    end

    def generate_reference(feature, scenario)
      reference = {}
      reference[:reference] = location(feature, scenario, nil)
      reference[:name] = "#{scenario[:keyword]}: #{scenario[:name]}"
      reference[:text] = scenario[:steps].map { |step| render_step(step) }.join ' '
      reference
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

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