Class: SunspotMatchersTestunit::BaseMatcher
- Inherits:
-
Object
- Object
- SunspotMatchersTestunit::BaseMatcher
show all
- Defined in:
- lib/sunspot_matchers_testunit/matchers.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(session, args) ⇒ BaseMatcher
Returns a new instance of BaseMatcher.
7
8
9
10
11
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 7
def initialize(session, args)
@session = session
@args = args
build_comparison_search
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
5
6
7
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 5
def args
@args
end
|
Instance Method Details
#actual_params ⇒ Object
49
50
51
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 49
def actual_params
@actual_params ||= query_params_for_search(actual_search)
end
|
#actual_search ⇒ Object
29
30
31
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 29
def actual_search
search_tuple.last
end
|
#build_comparison_search ⇒ Object
#compare_key(key) ⇒ Object
82
83
84
85
86
87
88
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 82
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
107
108
109
110
111
112
113
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 107
def compare_multi_value(actual, comparison)
filter_values(comparison).reject do |value|
next false unless actual
value_matcher = Regexp.new(Regexp.escape(value))
actual.any?{ |actual_value| actual_value =~ value_matcher }
end
end
|
#compare_single_value(actual, comparison) ⇒ Object
98
99
100
101
102
103
104
105
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 98
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
90
91
92
93
94
95
96
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 90
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
53
54
55
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 53
def comparison_params
@comparison_params ||= query_params_for_search(@comparison_search)
end
|
#differences ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 74
def differences
keys_to_compare.inject({}) do |hsh, key|
result = compare_key(key)
hsh[key] = result unless result.empty?
hsh
end
end
|
#field ⇒ Object
41
42
43
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 41
def field
@args && @args.first
end
|
#filter_values(values) ⇒ Object
115
116
117
118
119
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 115
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
57
58
59
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 57
def match?
differences.empty?
end
|
#missing_param_error_message ⇒ Object
61
62
63
64
65
66
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 61
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
45
46
47
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 45
def query_params_for_search(search)
search.instance_variable_get(:@query).to_params
end
|
#search_tuple ⇒ Object
23
24
25
26
27
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 23
def search_tuple
search_tuple = @session.is_a?(Array) ? @session : @session.searches.last
raise 'no search found' unless search_tuple
search_tuple
end
|
#search_types ⇒ Object
33
34
35
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 33
def search_types
search_tuple.first
end
|
#unexpected_match_error_message ⇒ Object
68
69
70
71
72
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 68
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
37
38
39
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 37
def wildcard?
@args && @args.last == any_param
end
|
#wildcard_matcher_for_keys ⇒ Object
121
122
123
|
# File 'lib/sunspot_matchers_testunit/matchers.rb', line 121
def wildcard_matcher_for_keys
{}
end
|