madrob commented on a change in pull request #117:
URL: https://github.com/apache/solr/pull/117#discussion_r628253601



##########
File path: solr/core/src/test/org/apache/solr/hamcrest/QueryMatchers.java
##########
@@ -0,0 +1,184 @@
+package org.apache.solr.hamcrest;
+
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.BoostQuery;
+import org.apache.lucene.search.DisjunctionMaxQuery;
+import org.apache.lucene.search.PhraseQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.containsInAnyOrder;
+import static org.hamcrest.Matchers.is;
+
+public class QueryMatchers {
+  private QueryMatchers() {}
+
+  public static Matcher<Query> termQuery(String field, String text) {
+    // TODO Use a better matcher for more descriptive results?
+    return is(new TermQuery(new Term(field, text)));
+  }
+
+  public static Matcher<Query> boostQuery(String field, String text, float 
boost) {
+    return boostQuery(termQuery(field, text), boost);
+  }
+
+  public static Matcher<Query> boostQuery(Matcher<? extends Query> query, 
float boost) {
+    return new TypeSafeDiagnosingMatcher<Query>() {
+      @Override
+      protected boolean matchesSafely(Query item, Description 
mismatchDescription) {
+        if (item instanceof BoostQuery) {
+          BoostQuery bq = (BoostQuery) item;
+          boolean match = true;
+          mismatchDescription.appendText("was a BoostQuery ");
+          if (!query.matches(bq.getQuery())) {
+            match = false;
+            mismatchDescription.appendText("with" + bq.getQuery());
+          }
+          if (boost != bq.getBoost()) {
+            match = false;
+            mismatchDescription.appendText("with boost " + bq.getBoost());
+          }
+          return match;
+        } else {
+          classMismatch(mismatchDescription, item);
+          return false;
+        }
+      }
+
+      @Override
+      public void describeTo(Description description) {
+        description.appendText("a BoostQuery 
").appendDescriptionOf(query).appendText("^" + boost);
+      }
+    };
+  }
+
+  public static Matcher<Query> phraseQuery(String field, String... terms) {
+    // TODO Use a better matcher for more descriptive results?
+    return is(new PhraseQuery(field, terms));
+  }
+
+  public static Matcher<BooleanClause> booleanClause(Matcher<? extends Query> 
query) {
+    return booleanClause(query, BooleanClause.Occur.SHOULD);
+  }
+
+  public static Matcher<BooleanClause> booleanClause(Matcher<? extends Query> 
query, BooleanClause.Occur occur) {
+    return new TypeSafeDiagnosingMatcher<BooleanClause>() {
+      @Override
+      protected boolean matchesSafely(BooleanClause item, Description 
mismatchDescription) {
+        boolean match = true;
+        mismatchDescription.appendText("was a BooleanClause ");
+        if (occur != item.getOccur()) {
+          match = false;
+          mismatchDescription.appendText("that " + item.getOccur().name() + " 
occur ");
+        }
+        if (!query.matches(item.getQuery())) {
+          match = false;
+          mismatchDescription.appendText("with" + item.getQuery());
+        }
+        return match;
+      }
+
+      @Override
+      public void describeTo(Description description) {
+        description.appendText("a BooleanClause that " + occur.name() + " 
occur with ")
+            .appendDescriptionOf(query);
+      }
+    };
+  }
+
+  // TODO Figure out Varargs

Review comment:
       The problem is that Varargs and Generic types in Matcher<T> don't play 
nicely together




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to