summaryrefslogtreecommitdiffstats
path: root/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2014-10-04 12:52:32 -0400
committerTavian Barnes <tavianator@tavianator.com>2014-10-04 12:52:32 -0400
commit3fd5777a58f59218e3229c9ea11d816ecc6ae367 (patch)
tree53f937505806e010d2bc3134fa5507d0ff78d32c /sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
parentb0bb30831a334ff30e3e6a13790bbc324db89de5 (diff)
parent8e24a47b69f940c31ac95451b4584b95de7c8670 (diff)
downloadsangria-3fd5777a58f59218e3229c9ea11d816ecc6ae367.tar.xz
Merge branch 'test'
Diffstat (limited to 'sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java')
-rw-r--r--sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java42
1 files changed, 42 insertions, 0 deletions
diff --git a/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
new file mode 100644
index 0000000..36eea97
--- /dev/null
+++ b/sangria-test/src/main/java/com/tavianator/sangria/test/BestPracticesMatcher.java
@@ -0,0 +1,42 @@
+package com.tavianator.sangria.test;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.CreationException;
+import com.google.inject.Guice;
+import com.google.inject.Module;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+
+/**
+ * Matcher that checks whether a {@link Module} follows Guice best practices.
+ *
+ * @author Tavian Barnes (tavianator@tavianator.com)
+ * @version 1.2
+ * @since 1.2
+ */
+final class BestPracticesMatcher extends TypeSafeDiagnosingMatcher<Module> {
+ private static final class EnforcerModule extends AbstractModule {
+ @Override
+ protected void configure() {
+ binder().requireAtInjectOnConstructors();
+ binder().requireExactBindingAnnotations();
+ binder().requireExplicitBindings();
+ }
+ }
+
+ @Override
+ protected boolean matchesSafely(Module item, Description mismatchDescription) {
+ try {
+ Guice.createInjector(item, new EnforcerModule());
+ return true;
+ } catch (CreationException e) {
+ mismatchDescription.appendValue(e);
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("a Module following Guice best practices");
+ }
+}