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

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... 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... matchers) { return Matcher.Disjunctive.create(true, asList(matchers)); } public static Matcher not(Matcher 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 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 predicate) { requireNonNull(predicate); return asBoolean(InternalUtils.function(predicate.toString(), predicate::test)); } public static AsBoolean asBoolean(Function function) { return new AsBoolean<>(function); } public static AsByte asByte(Function 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 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 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 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 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 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 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) Functions.cast(type)); } @SuppressWarnings("unchecked") public static , S extends AsComparable> S asComparable(Function 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 function) { return new AsString<>(requireNonNull(function)); } @SuppressWarnings({ "RedundantCast", "unchecked" }) public static AsString asString(String methodName, Object... args) { return asString((Function) Functions.invoke(methodName, args)); } public static > AsList asObjectList() { return asListOf(Object.class, Functions.collectionToList()); } public static AsList asObjectList(Function> function) { return asListOf(Object.class, function); } public static , E> AsList asListOf(Class type) { return asListOf(type, Functions.collectionToList()); } public static AsList asListOf(@SuppressWarnings("unused") Class type, Function> function) { return new AsList<>(function); } public static > SELF asObjectMap() { return asMapOf(Object.class, Object.class, InternalUtils.function("mapToMap", o -> new HashMap<>())); } public static > SELF asObjectMap(Function> function) { return asMapOf(Object.class, Object.class, function); } public static , K, V, SELF extends AsMap> SELF asMapOf(Class keyType, Class valueType) { return asMapOf(keyType, valueType, Functions.identity()); } @SuppressWarnings("unchecked") public static > SELF asMapOf(Class keyType, Class valueType, Function> function) { requireNonNull(keyType); requireNonNull(valueType); return (SELF) new AsMap(function); } public static Call call(String methodName, Object... args) { return Call.create(methodName, args); } public static Call call(Class klass, String methodName, Object... args) { return callOn(klass, methodName, args); } public static Call callOn(Object object, String methodName, Object... args) { return Call.createOn(object, methodName, args); } public static void assertThat(T actual, Matcher matcher) { assertThat("", actual, matcher); } public static void assumeThat(T actual, Matcher matcher) { assumeThat("", actual, matcher); } public static void requireThat(T actual, Matcher matcher) { requireThat("", actual, matcher); } public static void assertThat(String message, T value, Matcher matcher) { Session.perform( message, value, matcher, (msg, r, causes) -> new ComparisonFailure(msg, r.expectation(), r.mismatch()) ); } public static void assumeThat(String message, T value, Matcher matcher) { Session.perform( message, value, matcher, (msg, r, causes) -> new AssumptionViolatedException(composeComparisonText(msg, r)) ); } public static void requireThat(String message, T value, Matcher matcher) { Session.perform( message, value, matcher, (msg, r, causes) -> new ExecutionFailure(msg, r.expectation(), r.mismatch(), causes) ); } }