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

ext.test4j.hamcrest.core.CombinableMatcher Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package ext.test4j.hamcrest.core;

import ext.test4j.hamcrest.BaseMatcher;
import ext.test4j.hamcrest.Description;
import ext.test4j.hamcrest.Factory;
import ext.test4j.hamcrest.Matcher;

import java.util.ArrayList;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class CombinableMatcher extends BaseMatcher {
	private final Matcher matcher;

	public CombinableMatcher(Matcher matcher) {
		this.matcher = matcher;
	}

	public boolean matches(Object item) {
		return matcher.matches(item);
	}

	public void describeTo(Description description) {
		description.appendDescriptionOf(matcher);
	}

	public CombinableMatcher and(Matcher other) {
		return new CombinableMatcher(new AllOf(templatedListWith(other)));
	}

	public CombinableMatcher or(Matcher other) {
		return new CombinableMatcher(new AnyOf(templatedListWith(other)));
	}

	private ArrayList templatedListWith(Matcher other) {
		ArrayList matchers = new ArrayList();
		matchers.add(matcher);
		matchers.add(other);
		return matchers;
	}

	/**
	 * This is useful for fluently combining matchers that must both pass. For
	 * example:
	 * 
	 * 
	 * assertThat(string, both(containsString("a")).and(containsString("b")));
	 * 
*/ @Factory public static CombinableMatcher both(Matcher matcher) { return new CombinableMatcher(matcher); } /** * This is useful for fluently combining matchers where either may pass, for * example: * *
	 * assertThat(string, both(containsString("a")).and(containsString("b")));
	 * 
*/ @Factory public static CombinableMatcher either(Matcher matcher) { return new CombinableMatcher(matcher); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy