Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
Version 2.0.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph Palermo committed Apr 23, 2013
1 parent e448852 commit d4798f8
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 159 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
pkg/*
*.gem
.bundle
.idea
10 changes: 5 additions & 5 deletions Gemfile.lock
@@ -1,16 +1,16 @@
PATH
remote: .
specs:
sunspot_matchers (2.0.0.pre.120417)
sunspot_matchers (2.0.0)

GEM
remote: http://rubygems.org/
specs:
builder (3.0.0)
builder (3.2.0)
diff-lcs (1.1.2)
pr_geohash (1.0.0)
rake (0.8.7)
rsolr (1.0.8)
rsolr (1.0.9)
builder (>= 2.1.2)
rspec (2.4.0)
rspec-core (~> 2.4.0)
Expand All @@ -20,7 +20,7 @@ GEM
rspec-expectations (2.4.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.4.0)
sunspot (2.0.0.pre.120417)
sunspot (2.0.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)

Expand All @@ -31,5 +31,5 @@ DEPENDENCIES
bundler (>= 1.0.0)
rake
rspec (~> 2.4.0)
sunspot (~> 2.0.0.pre.120417)
sunspot (~> 2.0.0)
sunspot_matchers!
40 changes: 22 additions & 18 deletions README.markdown
Expand Up @@ -70,11 +70,15 @@ have a dozen `with` restrictions on a search and still write an expectation on a

Negative expectations also work correctly. `should_not` will fail if the search actually includes the expectation.

With all matchers, you can specify a `Proc` as the second argument, and perform multi statement expectations inside the
Proc. Keep in mind, that only the expectation type specified in the first argument will actually be checked. So if
you specify `keywords` and `with` restrictions in the same Proc, but you said `have_search_params(:keywords, ...`
With all matchers, you can pass a block and perform multi statement expectations inside the
block. Keep in mind, that only the expectation type specified as the argument will actually be checked. So if
you specify `keywords` and `with` restrictions in the same block, but you said `have_search_params(:keywords, ...`
the `with` restrictions are simply ignored.

Without creative usage of parentheses you can not use do...end blocks to pass multi statement expectations to the matcher
though. Because do...end have such a low binding precedent, the block is passed to the wrong method. Curly syntax blocks
will work though {...}, or you can pass a Proc as the last argument.

### wildcard matching

keywords, with, without, and order_by support wildcard expectations using the `any_param` parameter:
Expand Down Expand Up @@ -109,7 +113,7 @@ You can match against a with restriction:

Sunspot.session.should have_search_params(:with, :author_name, 'Mark Twain')

Complex conditions can be matched by using a Proc instead of a value. Be aware that order does matter, not for
Complex conditions can be matched by using a block instead of a value. Be aware that order does matter, not for
the actual results that would come out of Solr, but the matcher will fail of the order of `with` restrictions is
different.

Expand All @@ -120,12 +124,12 @@ different.
end
end

Sunspot.session.should have_search_params(:with, Proc.new {
Sunspot.session.should have_search_params(:with) {
any_of do
with :category_ids, 1
with :category_ids, 2
end
})
}

### :without

Expand All @@ -149,7 +153,7 @@ You can also specify only page or per_page, both are not required.

### :order_by

Expectations on multiple orderings are supported using using the Proc format mentioned above.
Expectations on multiple orderings are supported using using the block format mentioned above.

Sunspot.search(Post) do
order_by :published_at, :desc
Expand All @@ -174,10 +178,10 @@ Faceting where a query is excluded:
facet(:category_ids, :exclude => category_filter)
end

Sunspot.session.should have_search_params(:facet, Proc.new {
Sunspot.session.should have_search_params(:facet) {
category_filter = with(:category_ids, 2)
facet(:category_ids, :exclude => category_filter)
})
}

Query faceting:

Expand All @@ -192,7 +196,7 @@ Query faceting:
end
end

Sunspot.session.should have_search_params(:facet, Proc.new {
Sunspot.session.should have_search_params(:facet) {
facet(:average_rating) do
row(1.0..2.0) do
with(:average_rating, 1.0..2.0)
Expand All @@ -201,7 +205,7 @@ Query faceting:
with(:average_rating, 2.0..3.0)
end
end
})
}

### :boost

Expand All @@ -213,11 +217,11 @@ Field boost matching:
end
end

Sunspot.session.should have_search_params(:boost, Proc.new {
Sunspot.session.should have_search_params(:boost) {
keywords 'great pizza' do
boost_fields :body => 2.0
end
})
}

Boost query matching:

Expand All @@ -229,13 +233,13 @@ Boost query matching:
end
end

Sunspot.session.should have_search_params(:boost, Proc.new {
Sunspot.session.should have_search_params(:boost) {
keywords 'great pizza' do
boost(2.0) do
with :blog_id, 4
end
end
})
}

Boost function matching:

Expand All @@ -245,11 +249,11 @@ Boost function matching:
end
end

Sunspot.session.should have_search_params(:boost, Proc.new {
Sunspot.session.should have_search_params(:boost) {
keywords 'great pizza' do
boost(function { sum(:average_rating, product(:popularity, 10)) })
end
})
}

# Assertions

Expand All @@ -270,7 +274,7 @@ These are used like:
Sunspot.search(Post) do
with :category_ids, 1..3
end
assert_has_search_params Sunspot.session, :with, Proc.new {
assert_has_search_params Sunspot.session, :with {
with :category_ids, 1..3
}

Expand Down
8 changes: 4 additions & 4 deletions lib/sunspot_matchers/matchers.rb
Expand Up @@ -123,9 +123,9 @@ def wildcard_matcher_for_keys
end

class HaveSearchParams
def initialize(method, *args)
def initialize(method, *args, &block)
@method = method
@args = args
@args = [*args, block].compact
end

def matches?(actual)
Expand Down Expand Up @@ -164,8 +164,8 @@ def get_matcher
end
end

def have_search_params(method, *args)
HaveSearchParams.new(method, *args)
def have_search_params(method, *args, &block)
HaveSearchParams.new(method, *args, &block)
end

class WithMatcher < BaseMatcher
Expand Down
12 changes: 6 additions & 6 deletions lib/sunspot_matchers/test_helper.rb
Expand Up @@ -6,10 +6,10 @@ module SunspotMatchers
class HaveSearchParamsForSession
include Test::Unit::Assertions

def initialize(session, method, *args)
def initialize(session, method, *args, &block)
@session = session
@method = method
@args = args
@args = [*args, block].compact
end

def get_matcher
Expand Down Expand Up @@ -63,15 +63,15 @@ def failure_message_for_should_not
end
end
module TestHelper
def assert_has_search_params(session, *method_and_args)
def assert_has_search_params(session, *method_and_args, &block)
method, *args = method_and_args
matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
matcher = HaveSearchParamsForSession.new(session, method, *args, &block).get_matcher
assert matcher.match?, matcher.missing_param_error_message
end

def assert_has_no_search_params(session, *method_and_args)
def assert_has_no_search_params(session, *method_and_args, &block)
method, *args = method_and_args
matcher = HaveSearchParamsForSession.new(session, method, *args).get_matcher
matcher = HaveSearchParamsForSession.new(session, method, *args, &block).get_matcher
assert !matcher.match?, matcher.unexpected_match_error_message
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sunspot_matchers/version.rb
@@ -1,3 +1,3 @@
module SunspotMatchers
VERSION = "2.0.0.pre.120417"
VERSION = "2.0.0.0"
end

0 comments on commit d4798f8

Please sign in to comment.