Move field set matcher to Matchers.
[Sone.git] / src / test / java / net / pterodactylus / sone / Matchers.java
index 8873278..9bd4cbb 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone;
 
+import static com.google.common.base.Objects.equal;
+import static com.google.common.collect.Iterators.size;
 import static java.util.Arrays.asList;
 import static java.util.regex.Pattern.compile;
 
@@ -26,6 +28,8 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
+import freenet.support.SimpleFieldSet;
+
 import com.google.common.base.Objects;
 import com.google.common.collect.Lists;
 import org.hamcrest.Description;
@@ -152,4 +156,27 @@ public class Matchers {
                };
        }
 
+       public static Matcher<SimpleFieldSet> matches(final SimpleFieldSet fieldSetToMatch) {
+               return new TypeSafeMatcher<SimpleFieldSet>() {
+                       @Override
+                       protected boolean matchesSafely(SimpleFieldSet fieldSet) {
+                               if (size(fieldSet.keyIterator()) != size(fieldSetToMatch.keyIterator())) {
+                                       return false;
+                               }
+                               for (Iterator<String> keys = fieldSetToMatch.keyIterator(); keys.hasNext(); ) {
+                                       String key = keys.next();
+                                       if (!equal(fieldSet.get(key), fieldSetToMatch.get(key))) {
+                                               return false;
+                                       }
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("is ").appendValue(fieldSetToMatch);
+                       }
+               };
+       }
+
 }