All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.amygdalum.testrecorder.runtime.AbstractIterableMatcher Maven / Gradle / Ivy

The newest version!
package net.amygdalum.testrecorder.runtime;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.hamcrest.TypeSafeMatcher;

public abstract class AbstractIterableMatcher extends TypeSafeMatcher {

	protected abstract Matcher bestMatcher();

	protected  List remainder(Iterator iterator) {
		List remainder = new ArrayList<>();
		while (iterator.hasNext()) {
			remainder.add(iterator.next());
		}
		return remainder;
	}

	protected Set toFoundSet(List elements) {
		Matcher matcher = bestMatcher();
		Set set = new LinkedHashSet<>();
		for (S element : elements) {
			String desc = descriptionOf(matcher, element);
			set.add(desc);
		}
		return set;
	}

	protected Set toExpectedSet(List> matchers) {
		Set set = new LinkedHashSet<>();
		for (Matcher matcher : matchers) {
			String desc = descriptionOf(matcher);
			set.add(desc);
		}
		return set;
	}

	protected  String descriptionOf(Matcher matcher) {
		StringDescription description = new StringDescription();
		matcher.describeTo(description);
		return description.toString();
	}

	protected  String descriptionOf(Matcher matcher, R value) {
		StringDescription description = new StringDescription();
		matcher.describeMismatch(value, description);
		return description.toString();
	}

}