Sha256: 14c2f2b6d75537bec540e5c60ed731c6b9aeba23cd3beaa8789c1f42d445ebfe

Contents?: true

Size: 1.61 KB

Versions: 14

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Reek
  module Spec
    #
    # Matches a +SmellWarning+ object agains a smell type and hash of attributes.
    #
    class SmellMatcher
      attr_reader :smell_warning

      COMPARABLE_ATTRIBUTES = [:message, :lines, :context, :source].freeze

      def initialize(smell_warning)
        @smell_warning = smell_warning
      end

      def matches?(klass, attributes = {})
        matches_smell_type?(klass) && matches_attributes?(attributes)
      end

      def matches_smell_type?(klass)
        smell_warning.smell_type == klass.to_s
      end

      def matches_attributes?(attributes)
        check_attributes_comparability(attributes)

        fields, params = attributes.
          partition { |key, _| COMPARABLE_ATTRIBUTES.include? key }.
          map(&:to_h)

        common_parameters_equal?(params) &&
          common_attributes_equal?(fields)
      end

      private

      def check_attributes_comparability(other_attributes)
        parameter_keys = other_attributes.keys - COMPARABLE_ATTRIBUTES
        extra_keys = parameter_keys - smell_warning.parameters.keys
        return if extra_keys.empty?

        raise ArgumentError, "The attribute '#{extra_keys.first}' is not available for comparison"
      end

      # :reek:FeatureEnvy
      def common_parameters_equal?(other_parameters)
        smell_warning.parameters.values_at(*other_parameters.keys) == other_parameters.values
      end

      def common_attributes_equal?(attributes)
        attributes.all? do |other_key, other_value|
          smell_warning.send(other_key) == other_value
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
reek-6.4.0 lib/reek/spec/smell_matcher.rb
reek-6.3.0 lib/reek/spec/smell_matcher.rb
reek-6.2.0 lib/reek/spec/smell_matcher.rb
reek-6.1.4 lib/reek/spec/smell_matcher.rb
reek-6.1.3 lib/reek/spec/smell_matcher.rb
reek-6.1.2 lib/reek/spec/smell_matcher.rb
reek-6.1.1 lib/reek/spec/smell_matcher.rb
reek-6.1.0 lib/reek/spec/smell_matcher.rb
reek-6.0.6 lib/reek/spec/smell_matcher.rb
reek-6.0.5 lib/reek/spec/smell_matcher.rb
reek-6.0.4 lib/reek/spec/smell_matcher.rb
reek-6.0.3 lib/reek/spec/smell_matcher.rb
reek-6.0.2 lib/reek/spec/smell_matcher.rb
reek-6.0.1 lib/reek/spec/smell_matcher.rb