Add matchers for iterators.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Nov 2013 21:15:37 +0000 (22:15 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:26:00 +0000 (22:26 +0100)
src/test/java/net/pterodactylus/sone/Matchers.java

index 4bf7aa5..c08ffb1 100644 (file)
 
 package net.pterodactylus.sone;
 
 
 package net.pterodactylus.sone;
 
+import static java.util.Arrays.asList;
 import static java.util.regex.Pattern.compile;
 
 import static java.util.regex.Pattern.compile;
 
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import com.google.common.base.Objects;
+import com.google.common.collect.Lists;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.hamcrest.TypeSafeMatcher;
@@ -44,4 +51,65 @@ public class Matchers {
                };
        }
 
                };
        }
 
+       public static <T> Matcher<Iterator<T>> contains(T... items) {
+               return contains(asList(items));
+       }
+
+       public static <T> Matcher<Iterator<T>> contains(final Collection<T> items) {
+               return new TypeSafeMatcher<Iterator<T>>() {
+                       @Override
+                       protected boolean matchesSafely(Iterator<T> iterator) {
+                               for (T item : items) {
+                                       if (!iterator.hasNext()) {
+                                               return false;
+                                       }
+                                       T nextItem = iterator.next();
+                                       if (!Objects.equal(item, nextItem)) {
+                                               return false;
+                                       }
+                               }
+                               if (iterator.hasNext()) {
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("contains ").appendValue(items);
+                       }
+               };
+       }
+
+       public static <T> Matcher<Iterator<T>> containsInAnyOrder(T... items) {
+               return containsInAnyOrder(asList(items));
+       }
+
+       public static <T> Matcher<Iterator<T>> containsInAnyOrder(final Collection<T> items) {
+               return new TypeSafeMatcher<Iterator<T>>() {
+                       private final List<T> remainingItems = Lists.newArrayList(items);
+                       @Override
+                       protected boolean matchesSafely(Iterator<T> iterator) {
+                               while (iterator.hasNext()) {
+                                       T item = iterator.next();
+                                       if (remainingItems.isEmpty()) {
+                                               return false;
+                                       }
+                                       if (!remainingItems.remove(item)) {
+                                               return false;
+                                       }
+                               }
+                               if (!remainingItems.isEmpty()) {
+                                       return false;
+                               }
+                               return true;
+                       }
+
+                       @Override
+                       public void describeTo(Description description) {
+                               description.appendText("contains ").appendValue(items);
+                       }
+               };
+       }
+
 }
 }