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

com.github.dakusui.pcond.fluent.MoreFluents Maven / Gradle / Ivy

package com.github.dakusui.pcond.fluent;

import com.github.dakusui.pcond.TestAssertions;
import com.github.dakusui.pcond.core.fluent.Fluent;
import com.github.dakusui.pcond.core.fluent.transformers.*;
import com.github.dakusui.pcond.core.printable.PrintableFunction;
import com.github.dakusui.pcond.core.printable.PrintablePredicate;
import com.github.dakusui.pcond.forms.Functions;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.github.dakusui.pcond.forms.Functions.elementAt;
import static com.github.dakusui.pcond.forms.Predicates.allOf;
import static com.github.dakusui.pcond.forms.Predicates.transform;
import static java.util.stream.Collectors.toList;

/**
 * An "entry-point" class to write a "more-fluent" style tests.
 * In order to avoid a conflict in `valueOf` method, this class is implemented as
 * a conventional class, not an `enum`.
 */
public class MoreFluents {
  private MoreFluents() {
  }

  /**
   * A function to provide a place-holder for `MoreFluent` style.
   * So far, no valid usage of this method in `MoreFluent` style and this method might be
   * dropped from future releases.
   *
   * @param  The type to which the place-holder is cast.
   * @return A place-holder variable that can be cast to any type.
   */
  public static  T value() {
    return Functions.value();
  }

  public static  void assertWhen(Statement statement) {
    TestAssertions.assertThat(statement.statementValue(), statement.statementPredicate());
  }

  public static void assertWhen(Statement... statements) {
    List values = Arrays.stream(statements).map(Statement::statementValue).collect(toList());
    TestAssertions.assertThat(values, createPredicateForAllOf(statements));
  }

  public static  void assumeWhen(Statement statement) {
    TestAssertions.assumeThat(statement.statementValue(), statement.statementPredicate());
  }

  public static void assumeWhen(Statement... statements) {
    List values = Arrays.stream(statements).map(Statement::statementValue).collect(toList());
    TestAssertions.assumeThat(values, createPredicateForAllOf(statements));
  }

  public static StringTransformer valueOf(String value) {
    return fluent(value).asString();
  }

  public static DoubleTransformer valueOf(double value) {
    return fluent(value).asDouble();
  }

  public static FloatTransformer valueOf(float value) {
    return fluent(value).asFloat();
  }

  public static LongTransformer valueOf(long value) {
    return fluent(value).asLong();
  }

  public static IntegerTransformer valueOf(int value) {
    return fluent(value).asInteger();
  }

  public static ShortTransformer valueOf(short value) {
    return fluent(value).asShort();
  }

  public static BooleanTransformer valueOf(boolean value) {
    return fluent(value).asBoolean();
  }

  public static  ObjectTransformer valueOf(T value) {
    return fluent(value).asObject();
  }

  public static  ListTransformer, E> valueOf(List value) {
    return fluent(value).asListOf(Fluents.value());
  }

  public static  StreamTransformer, E> valueOf(Stream value) {
    return fluent(value).asStreamOf(Fluents.value());
  }

  private static  Fluent fluent(T value) {
    return new Fluent<>("WHEN", value);
  }

  private static Predicate> createPredicateForAllOf(Statement[] statements) {
    AtomicInteger i = new AtomicInteger(0);
    @SuppressWarnings("unchecked") Predicate>[] predicates = Arrays.stream(statements)
        .map(e -> makeTrivial(transform(makeTrivial(elementAt(i.getAndIncrement()))).check((Predicate) e.statementPredicate())))
        .toArray(Predicate[]::new);
    return makeTrivial(allOf(predicates));
  }

  private static  Predicate makeTrivial(Predicate predicates) {
    return ((PrintablePredicate) predicates).makeTrivial();
  }

  private static  Function makeTrivial(Function predicates) {
    return ((PrintableFunction) predicates).makeTrivial();
  }

  @FunctionalInterface
  public interface Statement extends Predicate {
    default T statementValue() {
      throw new NoSuchElementException();
    }

    default Predicate statementPredicate() {
      return this;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy