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

com.github.dakusui.crest.matcherbuilders.Crest Maven / Gradle / Ivy

package com.github.dakusui.crest.matcherbuilders;

import com.github.dakusui.crest.core.Assertion;
import com.github.dakusui.crest.core.Matcher;
import com.github.dakusui.crest.core.Printable;
import com.github.dakusui.crest.functions.CrestFunctions;
import com.github.dakusui.crest.matcherbuilders.primitives.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

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")))
*/ @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")))
*/ @SuppressWarnings("unchecked") @SafeVarargs public static Matcher anyOf(Matcher... matchers) { return Matcher.Disjunctive.create(true, asList(matchers)); } public static > AsObject asObject() { return new AsObject<>(CrestFunctions.identity()); } public static > AsObject asObject(String methodName, Object... args) { return new AsObject<>(CrestFunctions.invoke(methodName, args)); } public static > AsObject asObject(Function function) { return new AsObject<>(function); } public static AsBoolean asBoolean() { return asBoolean(CrestFunctions.identity()); } public static AsBoolean asBoolean(String methodName, Object... args) { return asBoolean(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Boolean.class))); } public static AsBoolean asBoolean(Predicate predicate) { requireNonNull(predicate); return asBoolean(Printable.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(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Byte.class))); } public static AsByte asByte() { return asByte(CrestFunctions.identity()); } public static AsChar asChar(Function function) { return new AsChar<>(function); } public static AsChar asChar(String methodName, Object... args) { return asChar(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Character.class))); } public static AsChar asChar() { return asChar(CrestFunctions.identity()); } public static AsShort asShort(Function function) { return new AsShort<>(function); } public static AsShort asShort(String methodName, Object... args) { return asShort(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Short.class))); } public static AsShort asShort() { return asShort(CrestFunctions.identity()); } public static AsInteger asInteger(Function function) { return new AsInteger<>(function); } public static AsInteger asInteger(String methodName, Object... args) { return asInteger(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Integer.class))); } public static AsInteger asInteger() { return asInteger(CrestFunctions.identity()); } public static AsLong asLong(Function function) { return new AsLong<>(function); } public static AsLong asLong(String methodName, Object... args) { return asLong(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Long.class))); } public static AsLong asLong() { return asLong(CrestFunctions.identity()); } public static AsFloat asFloat(Function function) { return new AsFloat<>(function); } public static AsFloat asFloat(String methodName, Object... args) { return asFloat(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Float.class))); } public static AsFloat asFloat() { return asFloat(CrestFunctions.identity()); } public static AsDouble asDouble(Function function) { return new AsDouble<>(function); } public static AsDouble asDouble(String methodName, Object... args) { return asDouble(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(Double.class))); } public static AsDouble asDouble() { return asDouble(CrestFunctions.identity()); } /* * Casts a given object into the given comparable type */ public static , S extends AsComparable> S asComparableOf(Class type) { return asComparable(CrestFunctions.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(CrestFunctions.invoke(methodName, (Object[]) args).andThen(CrestFunctions.cast(type))); } public static AsString asString() { return asString(CrestFunctions.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) CrestFunctions.invoke(methodName, args)); } public static , E> AsStream asStream() { return new AsStream<>(CrestFunctions.stream()); } public static > AsList asObjectList() { return asListOf(Object.class, CrestFunctions.collectionToList()); } public static AsList asObjectList(Function> function) { return asListOf(Object.class, function); } public static , E> AsList asListOf(Class type) { return asListOf(type, CrestFunctions.collectionToList()); } public static AsList asListOf(@SuppressWarnings("unused") Class type, Function> function) { return new AsList<>(function); } public static > SELF asMap() { return asMapOf(Object.class, Object.class, Printable.function("mapToMap", o -> new HashMap<>())); } public static > SELF asMap(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, CrestFunctions.identity()); } @SuppressWarnings("unchecked") public static > SELF asMapOf(Class keyType, Class valueType, Function> function) { requireNonNull(keyType); requireNonNull(valueType); return (SELF) new AsMap(function); } public static void assertThat(T actual, Matcher matcher) { assertThat("", actual, matcher); } public static void assertThat(String message, T actual, Matcher matcher) { Assertion.assertThat(message, actual, matcher); } }