Class: SunspotMatchers::BaseMatcher
- Inherits:
-
Object
- Object
- SunspotMatchers::BaseMatcher
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
#args ⇒ Object
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_params ⇒ Object
47
48
49
|
# File 'lib/sunspot_matchers/matchers.rb', line 47
def actual_params
@actual_params ||= query_params_for_search(actual_search)
end
|
#actual_search ⇒ Object
27
28
29
|
# File 'lib/sunspot_matchers/matchers.rb', line 27
def actual_search
search_tuple.last
end
|
#build_comparison_search ⇒ Object
#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_params ⇒ Object
51
52
53
|
# File 'lib/sunspot_matchers/matchers.rb', line 51
def comparison_params
@comparison_params ||= query_params_for_search(@comparison_search)
end
|
#differences ⇒ Object
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
|
#field ⇒ Object
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
55
56
57
|
# File 'lib/sunspot_matchers/matchers.rb', line 55
def match?
differences.empty?
end
|
#missing_param_error_message ⇒ Object
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_tuple ⇒ Object
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_types ⇒ Object
31
32
33
|
# File 'lib/sunspot_matchers/matchers.rb', line 31
def search_types
search_tuple.first
end
|
#unexpected_match_error_message ⇒ Object
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
35
36
37
|
# File 'lib/sunspot_matchers/matchers.rb', line 35
def wildcard?
@args && @args.last == any_param
end
|
#wildcard_matcher_for_keys ⇒ Object
122
123
124
|
# File 'lib/sunspot_matchers/matchers.rb', line 122
def wildcard_matcher_for_keys
{}
end
|