class SunspotMatchers::BaseMatcher
Attributes
args[RW]
Public Class Methods
new(actual, args)
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 5 def initialize(actual, args) @actual = actual @args = args build_comparison_search end
Public Instance Methods
actual_params()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 47 def actual_params @actual_params ||= query_params_for_search(actual_search) end
actual_search()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 27 def actual_search search_tuple.last end
build_comparison_search()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 51 def comparison_params @comparison_params ||= query_params_for_search(@comparison_search) end
differences()
click to toggle source
# 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()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 39 def field @args && @args.first end
filter_values(values)
click to toggle source
# 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?()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 55 def match? differences.empty? end
missing_param_error_message()
click to toggle source
# 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)
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 43 def query_params_for_search(search) search.instance_variable_get(:@query).to_params end
search_tuple()
click to toggle source
# 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()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 31 def search_types search_tuple.first end
unexpected_match_error_message()
click to toggle source
# 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?()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 35 def wildcard? @args && @args.last == any_param end
wildcard_matcher_for_keys()
click to toggle source
# File lib/sunspot_matchers/matchers.rb, line 122 def wildcard_matcher_for_keys {} end