package net.pterodactylus.sone;
+import static java.util.Arrays.asList;
import static java.util.regex.Pattern.compile;
+import java.io.IOException;
+import java.io.InputStream;
+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;
};
}
+ 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);
+ }
+ };
+ }
+
+ public static Matcher<InputStream> delivers(final byte[] data) {
+ return new TypeSafeMatcher<InputStream>() {
+ byte[] readData = new byte[data.length];
+
+ @Override
+ protected boolean matchesSafely(InputStream inputStream) {
+ int offset = 0;
+ try {
+ while (true) {
+ int r = inputStream.read();
+ if (r == -1) {
+ return offset == data.length;
+ }
+ if (offset == data.length) {
+ return false;
+ }
+ if (data[offset] != (readData[offset] = (byte) r)) {
+ return false;
+ }
+ offset++;
+ }
+ } catch (IOException ioe1) {
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendValue(data);
+ }
+
+ @Override
+ protected void describeMismatchSafely(InputStream item, Description mismatchDescription) {
+ mismatchDescription.appendValue(readData);
+ }
+ };
+ }
+
}