
com.github.dakusui.crest.Crest Maven / Gradle / Ivy
package com.github.dakusui.crest;
import com.github.dakusui.crest.core.*;
import com.github.dakusui.crest.matcherbuilders.*;
import com.github.dakusui.crest.matcherbuilders.primitives.*;
import com.github.dakusui.crest.utils.printable.Functions;
import org.junit.AssumptionViolatedException;
import org.junit.ComparisonFailure;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import static com.github.dakusui.crest.core.InternalUtils.composeComparisonText;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
/**
* A facade class of 'thincrest'.
*/
public enum Crest {
;
/**
* A bit better version of CoreMatchers.allOf.
* For example:
* assertThat("myValue", allOf(startsWith("my"), containsString("Val")))
*
* @param matchers Child matchers.
* @param Type of the value to be matched with the returned matcher.
* @return A matcher that matches when all of given {@code matchers} match.
*/
@SuppressWarnings("unchecked")
@SafeVarargs
public static Matcher allOf(Matcher super T>... matchers) {
return Matcher.Conjunctive.create(true, asList(matchers));
}
/**
* A bit better version of CoreMatchers.anyOf.
* For example:
* assertThat("myValue", anyOf(startsWith("my"), containsString("Val")))
*
* @param matchers Child matchers.
* @param Type of the value to be matched with the returned matcher.
* @return A matcher that matches when any of given {@code matchers} matches.
*/
@SuppressWarnings("unchecked")
@SafeVarargs
public static Matcher anyOf(Matcher super T>... matchers) {
return Matcher.Disjunctive.create(true, asList(matchers));
}
public static Matcher not(Matcher super T> matcher) {
return Matcher.Negative.create(matcher);
}
@SuppressWarnings("unchecked")
public static Matcher noneOf(Matcher... matcher) {
return new Matcher.Composite.Base(true, Arrays.asList(matcher)) {
@Override
public String name() {
return "noneOf";
}
@Override
protected boolean first() {
return true;
}
@Override
protected boolean op(boolean current, boolean next) {
return current && !next;
}
};
}
public static AsObject asObject() {
return new AsObject<>(Functions.identity());
}
public static AsObject asObject(String methodName, Object... args) {
return new AsObject<>(Functions.invoke(methodName, args));
}
public static AsObject asObject(Function super I, ? extends O> function) {
return new AsObject<>(function);
}
public static AsBoolean asBoolean() {
return asBoolean(Functions.identity());
}
public static AsBoolean asBoolean(String methodName, Object... args) {
return asBoolean(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Boolean.class)));
}
public static AsBoolean asBoolean(Predicate super I> predicate) {
requireNonNull(predicate);
return asBoolean(InternalUtils.function(predicate.toString(), predicate::test));
}
public static AsBoolean asBoolean(Function super I, Boolean> function) {
return new AsBoolean<>(function);
}
public static AsByte asByte(Function super I, Byte> function) {
return new AsByte<>(function);
}
public static AsByte asByte(String methodName, Object... args) {
return asByte(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Byte.class)));
}
public static AsByte asByte() {
return asByte(Functions.identity());
}
public static AsChar asChar(Function super I, Character> function) {
return new AsChar<>(function);
}
public static AsChar asChar(String methodName, Object... args) {
return asChar(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Character.class)));
}
public static AsChar asChar() {
return asChar(Functions.identity());
}
public static AsShort asShort(Function super I, Short> function) {
return new AsShort<>(function);
}
public static AsShort asShort(String methodName, Object... args) {
return asShort(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Short.class)));
}
public static AsShort asShort() {
return asShort(Functions.identity());
}
public static AsInteger asInteger(Function super I, Integer> function) {
return new AsInteger<>(function);
}
public static AsInteger asInteger(String methodName, Object... args) {
return asInteger(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Integer.class)));
}
public static AsInteger asInteger() {
return asInteger(Functions.identity());
}
public static AsLong asLong(Function super I, Long> function) {
return new AsLong<>(function);
}
public static AsLong asLong(String methodName, Object... args) {
return asLong(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Long.class)));
}
public static AsLong asLong() {
return asLong(Functions.identity());
}
public static AsFloat asFloat(Function super I, Float> function) {
return new AsFloat<>(function);
}
public static AsFloat asFloat(String methodName, Object... args) {
return asFloat(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Float.class)));
}
public static AsFloat asFloat() {
return asFloat(Functions.identity());
}
public static AsDouble asDouble(Function super I, Double> function) {
return new AsDouble<>(function);
}
public static AsDouble asDouble(String methodName, Object... args) {
return asDouble(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(Double.class)));
}
public static AsDouble asDouble() {
return asDouble(Functions.identity());
}
/*
* Casts a given object into the given comparable type
*/
public static , S extends AsComparable>
S asComparableOf(Class type) {
return asComparable((Function super I, ? extends I>) Functions.cast(type));
}
@SuppressWarnings("unchecked")
public static , S extends AsComparable>
S asComparable(Function super I, ? extends T> function) {
return (S) new AsComparable<>(function);
}
@SuppressWarnings("unchecked")
public static , S extends AsComparable>
S asComparableOf(Class type, String methodName, Object... args) {
return (S) asComparable(Functions.invoke(methodName, (Object[]) args).andThen(Functions.cast(type)));
}
public static AsString asString() {
return asString(Functions.stringify());
}
public static AsString asString(Function super I, ? extends String> function) {
return new AsString<>(requireNonNull(function));
}
@SuppressWarnings({ "RedundantCast", "unchecked" })
public static AsString asString(String methodName, Object... args) {
return asString((Function super I, ? extends String>) Functions.invoke(methodName, args));
}
public static > AsList super I, ?> asObjectList() {
return asListOf(Object.class, Functions.collectionToList());
}
public static AsList super I, ?> asObjectList(Function super I, ? extends List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy