Class: Reek::Spec::SmellMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/reek/spec/smell_matcher.rb

Overview

Matches a SmellWarning object agains a smell type and hash of attributes.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(smell_warning) ⇒ SmellMatcher

Returns a new instance of SmellMatcher.



13
14
15
# File 'lib/reek/spec/smell_matcher.rb', line 13

def initialize(smell_warning)
  @smell_warning = smell_warning
end

Instance Attribute Details

#smell_warningObject (readonly)

Returns the value of attribute smell_warning.



9
10
11
# File 'lib/reek/spec/smell_matcher.rb', line 9

def smell_warning
  @smell_warning
end

Instance Method Details

#check_attributes_comparability(other_attributes) ⇒ Object (private)

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
# File 'lib/reek/spec/smell_matcher.rb', line 38

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

#common_attributes_equal?(attributes) ⇒ Boolean (private)

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/reek/spec/smell_matcher.rb', line 51

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

#common_parameters_equal?(other_parameters) ⇒ Boolean (private)

:reek:FeatureEnvy

Returns:

  • (Boolean)


47
48
49
# File 'lib/reek/spec/smell_matcher.rb', line 47

def common_parameters_equal?(other_parameters)
  smell_warning.parameters.values_at(*other_parameters.keys) == other_parameters.values
end

#matches?(klass, attributes = {}) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/reek/spec/smell_matcher.rb', line 17

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

#matches_attributes?(attributes) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
# File 'lib/reek/spec/smell_matcher.rb', line 25

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

#matches_smell_type?(klass) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/reek/spec/smell_matcher.rb', line 21

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