Class: SunspotMatchers::BaseMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot_matchers/matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actual, args) ⇒ BaseMatcher

Returns a new instance of BaseMatcher.



5
6
7
8
9
# File 'lib/sunspot_matchers/matchers.rb', line 5

def initialize(actual, args)
  @actual = actual
  @args = args
  build_comparison_search
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/sunspot_matchers/matchers.rb', line 3

def args
  @args
end

Instance Method Details

#actual_paramsObject



47
48
49
# File 'lib/sunspot_matchers/matchers.rb', line 47

def actual_params
  @actual_params ||= query_params_for_search(actual_search)
end

#actual_searchObject



27
28
29
# File 'lib/sunspot_matchers/matchers.rb', line 27

def actual_search
  search_tuple.last
end

#build_comparison_searchObject



11
12
13
14
15
16
17
18
19
# File 'lib/sunspot_matchers/matchers.rb', line 11

def build_comparison_search
  @comparison_search = if(@args.last.is_a?(Proc))
    SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types, &args.last)
  else
    SunspotMatchers::SunspotSessionSpy.new(nil).build_search(search_types) do
      send(search_method, *args)
    end
  end
end

#compare_key(key) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/sunspot_matchers/matchers.rb', line 80

def compare_key(key)
  if(actual_params[key].is_a?(Array) || comparison_params[key].is_a?(Array))
    compare_multi_value(actual_params[key], comparison_params[key])
  else
    compare_single_value(actual_params[key], comparison_matcher_for_key(key))
  end
end

#compare_multi_value(actual, comparison) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/sunspot_matchers/matchers.rb', line 105

def compare_multi_value(actual, comparison)
  actual = [actual] unless actual.is_a?(Array)
  comparison = [comparison] unless comparison.is_a?(Array)
  filter_values(comparison).reject do |value|
    next false unless actual
    tag_matcher = Regexp.new('{!tag=\w+}')
    value_matcher = Regexp.new("^(#{tag_matcher})?#{Regexp.escape(value)}")
    actual.any?{ |actual_value| actual_value =~ value_matcher }
  end
end

#compare_single_value(actual, comparison) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/sunspot_matchers/matchers.rb', line 96

def compare_single_value(actual, comparison)
  if comparison.is_a?(Regexp)
    return [] if comparison =~ actual
    return [comparison.source]
  end
  return [comparison] unless actual == comparison
  []
end

#comparison_matcher_for_key(key) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/sunspot_matchers/matchers.rb', line 88

def comparison_matcher_for_key(key)
  if wildcard? && wildcard_matcher_for_keys.has_key?(key)
    wildcard_matcher_for_keys[key]
  else
    comparison_params[key]
  end
end

#comparison_paramsObject



51
52
53
# File 'lib/sunspot_matchers/matchers.rb', line 51

def comparison_params
  @comparison_params ||= query_params_for_search(@comparison_search)
end

#differencesObject



72
73
74
75
76
77
78
# File 'lib/sunspot_matchers/matchers.rb', line 72

def differences
  keys_to_compare.inject({}) do |hsh, key|
    result = compare_key(key)
    hsh[key] = result unless result.empty?
    hsh
  end
end

#fieldObject



39
40
41
# File 'lib/sunspot_matchers/matchers.rb', line 39

def field
  @args && @args.first
end

#filter_values(values) ⇒ Object



116
117
118
119
120
# File 'lib/sunspot_matchers/matchers.rb', line 116

def filter_values(values)
  return values unless wildcard?
  field_matcher = Regexp.new(field.to_s)
  values.select{ |value| field_matcher =~ value }.collect{|value| value.gsub(/:.*/, '')}
end

#match?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/sunspot_matchers/matchers.rb', line 55

def match?
  differences.empty?
end

#missing_param_error_messageObject



59
60
61
62
63
64
# File 'lib/sunspot_matchers/matchers.rb', line 59

def missing_param_error_message
  missing_params = differences
  actual_values = missing_params.keys.collect {|key| "#{key} => #{actual_params[key]}"}
  missing_values = missing_params.collect{ |key, value| "#{key} => #{value}"}
  "expected search params: #{actual_values.join(' and ')} to match expected: #{missing_values.join(' and ')}"
end

#query_params_for_search(search) ⇒ Object



43
44
45
# File 'lib/sunspot_matchers/matchers.rb', line 43

def query_params_for_search(search)
  search.instance_variable_get(:@query).to_params
end

#search_tupleObject



21
22
23
24
25
# File 'lib/sunspot_matchers/matchers.rb', line 21

def search_tuple
  search_tuple = @actual.is_a?(Array) ? @actual : @actual.searches.last
  raise 'no search found' unless search_tuple
  search_tuple
end

#search_typesObject



31
32
33
# File 'lib/sunspot_matchers/matchers.rb', line 31

def search_types
  search_tuple.first
end

#unexpected_match_error_messageObject



66
67
68
69
70
# File 'lib/sunspot_matchers/matchers.rb', line 66

def unexpected_match_error_message
  actual_values = keys_to_compare.collect {|key| "#{key} => #{actual_params[key]}"}
  comparison_values = keys_to_compare.collect {|key| "#{key} => #{comparison_params[key]}"}
  "expected search params: #{actual_values.join(' and ')} NOT to match expected: #{comparison_values.join(' and ')}"
end

#wildcard?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/sunspot_matchers/matchers.rb', line 35

def wildcard?
  @args && @args.last == any_param
end

#wildcard_matcher_for_keysObject



122
123
124
# File 'lib/sunspot_matchers/matchers.rb', line 122

def wildcard_matcher_for_keys
  {}
end