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

org.whaka.asserts.UberMatchers Maven / Gradle / Ivy

package org.whaka.asserts;

import java.util.Collection;
import java.util.Objects;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.regex.Pattern;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.whaka.asserts.matcher.ComparisonMatcher;
import org.whaka.asserts.matcher.ConsistencyMatcher;
import org.whaka.asserts.matcher.FunctionalMatcher;
import org.whaka.asserts.matcher.RegexpMatcher;
import org.whaka.asserts.matcher.ThrowableMatcher;
import org.whaka.util.UberCollections;
import org.whaka.util.reflection.comparison.ComparisonPerformer;
import org.whaka.util.reflection.comparison.ComparisonPerformers;

import com.google.common.base.Preconditions;


/**
 * Class provides static factory methods for custom Hamcrest matchers provided by the library.
 * 
 * @see NumberMatchers
 */
public final class UberMatchers {

	private UberMatchers() {
	}
	
	/**
	 * Create a matcher that will check that tested item and specified value are equal
	 * according to the specified {@link ComparisonPerformer}.
	 * 
	 * @see #deeplyEqualTo(Object)
	 * @see #reflectivelyEqualTo(Object)
	 * @see ComparisonMatcher
	 * @see ComparisonPerformers
	 */
	public static  Matcher equalTo(T item, ComparisonPerformer performer) {
		return new ComparisonMatcher<>(item, performer);
	}
	
	/**
	 * Create a matcher that will check that tested item and specified value are equal
	 * according to the {@link ComparisonPerformers#DEEP_EQUALS} performer.
	 * 
	 * @see #equalTo(Object, ComparisonPerformer)
	 * @see #reflectivelyEqualTo(Object)
	 * @see ComparisonMatcher
	 * @see ComparisonPerformers
	 */
	public static  Matcher deeplyEqualTo(T item) {
		return equalTo(item, ComparisonPerformers.DEEP_EQUALS);
	}
	
	/**
	 * Create a matcher that will check that tested item and specified value are equal
	 * according to the {@link ComparisonPerformers#REFLECTIVE_EQUALS} performer.
	 * 
	 * @see #equalTo(Object, ComparisonPerformer)
	 * @see #deeplyEqualTo(Object)
	 * @see ComparisonMatcher
	 * @see ComparisonPerformers
	 */
	public static  Matcher reflectivelyEqualTo(T item) {
		return equalTo(item, ComparisonPerformers.REFLECTIVE_EQUALS);
	}
	
	/**
	 * Create a matcher that will check that tested item and specified value are either both consistently matched,
	 * or both consistently not matched by the specified matcher.
	 * 
	 * @see #nullConsistentWith(Object)
	 * @see ConsistencyMatcher
	 */
	public static  Matcher consistentWith(T value, Matcher matcher) {
		return new ConsistencyMatcher(value, matcher);
	}
	
	/**
	 * 

Create a matcher that will check that tested item and specified value are either both consistently nulls, * or both consistently not-nulls. * *

Equal to {@link #consistentWith(Object, Matcher)} with {@link Matchers#nullValue()} as delegate. * * @see ConsistencyMatcher */ public static Matcher nullConsistentWith(Object value) { return consistentWith(value, Matchers.nullValue()); } /** * Create a matcher that will check that string representation of any object * matches specified regexp pattern. * * @throws NullPointerException if specified string is null * * @see #matches(Pattern) * @see RegexpMatcher */ public static Matcher matches(String pattern) { return RegexpMatcher.create(pattern); } /** * Create a matcher that will check that string representation of any object * matches specified pattern. * * @throws NullPointerException if specified pattern is null * * @see #matches(String) * @see RegexpMatcher */ public static Matcher matches(Pattern pattern) { return RegexpMatcher.create(pattern); } /** * Create a matcher that will check that an exception is an instance of the specified type. * And will set asserted throwable as cause of the result. * * @see #notExpected() * @see ThrowableMatcher */ public static Matcher throwableOfType(Class type) { return ThrowableMatcher.throwableOfType(type); } /** * Create a matcher that will check that an exception is null. * And will set asserted throwable as cause of the result. * * @see #throwableOfType(Class) * @see ThrowableMatcher */ public static Matcher notExpected() { return ThrowableMatcher.notExpected(); } /** *

Create a matcher that will check that a collection contains the specified item. * Specified predicate is used to check elements equality. * *

Note: matcher will throw an NPE if matched against a null value! * * @throws NullPointerException if specified predicate is null */ public static Matcher> hasItem(T item, BiPredicate matcher) { Objects.requireNonNull(matcher, "Predicate cannot be null!"); return new FunctionalMatcher>( Collection.class, c -> UberCollections.contains(c, item, matcher), d -> d.appendText("has item ").appendValue(item).appendText(" with a predicate")); } /** * Equal to {@link #hasAnyItem(Collection, BiPredicate)} with {@link Objects#deepEquals(Object, Object)} * used as a predicate. * *

Note: matcher will throw an NPE if matched against a null value! * * @throws NullPointerException if specified collection is null * @throws IllegalArgumentException if specified collection is empty */ public static Matcher> hasAnyItem(Collection items) { return hasAnyItem(items, Objects::deepEquals); } /** *

Create a matcher that will check that a collection contains any one of the specified item. * Specified predicate is used to check elements equality. * *

Note: matcher will throw an NPE if matched against a null value! * * @throws NullPointerException if specified collection or specified predicate is null * @throws IllegalArgumentException if specified collection is empty * * @see #hasAnyItem(Collection) */ public static Matcher> hasAnyItem(Collection items, BiPredicate matcher) { Objects.requireNonNull(items, "Items collection cannot be null!"); Objects.requireNonNull(matcher, "Predicate cannot be null!"); Preconditions.checkArgument(!items.isEmpty(), "Items cannot be empty!"); return new FunctionalMatcher>( Collection.class, c -> UberCollections.containsAny(c, items, matcher), d -> d.appendText("has any one of ").appendValue(items).appendText(" with a predicate")); } /** * Create a matcher that will check that an item is contained in the specified collection. * Specified predicate is used to check elements equality. * * @throws NullPointerException if specified collection or predicate is null */ public static Matcher isIn(Collection col, BiPredicate matcher) { Objects.requireNonNull(col, "Collection cannot be null!"); Objects.requireNonNull(matcher, "Predicate cannot be null!"); return new FunctionalMatcher( Object.class, t -> UberCollections.contains(col, t, matcher), d -> d.appendText("one of ").appendValue(col).appendText(" with a predicate")); } /** * Create a matcher that will apply specified function to the received value and then delegate result * to the specified matcher delegate. * * @throws NullPointerException if specified matcher or function is null */ public static Matcher convert(Matcher m, Function f) { Objects.requireNonNull(m, "Delegate matcher cannot be null!"); Objects.requireNonNull(f, "Converting function cannot be null!"); return new FunctionalMatcher( Object.class, v -> m.matches(f.apply(v)), d -> m.describeTo(d)); } }