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

com.github.tonivade.purecheck.TestSpec Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2020-2024, Antonio Gabriel Muñoz Conejo 
 * Distributed under the terms of the MIT License
 */
package com.github.tonivade.purecheck;

import static com.github.tonivade.purefun.core.Precondition.checkNonNull;

import java.util.concurrent.Executor;

import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.concurrent.Future;
import com.github.tonivade.purefun.data.NonEmptyList;
import com.github.tonivade.purefun.typeclasses.Applicative;
import com.github.tonivade.purefun.typeclasses.FunctionK;
import com.github.tonivade.purefun.typeclasses.Instance;
import com.github.tonivade.purefun.typeclasses.Monad;
import com.github.tonivade.purefun.typeclasses.MonadDefer;
import com.github.tonivade.purefun.typeclasses.Parallel;
import com.github.tonivade.purefun.typeclasses.Runtime;

public abstract class TestSpec, E> {

  protected final TestFactory it;

  private final Runtime runtime;
  private final Applicative applicative;
  private final Monad monad;

  protected TestSpec(Instance instance) {
    this(instance.runtime(), instance.monadDefer(), instance.applicative());
  }

  protected TestSpec(Runtime runtime, MonadDefer monad, Applicative applicative) {
    this.runtime = checkNonNull(runtime);
    this.applicative = checkNonNull(applicative);
    this.monad = checkNonNull(monad);
    this.it = TestFactory.factory(monad);
  }

  @SafeVarargs
  protected final TestSuite suite(
      String name, TestCase test, TestCase... tests) {
    return new TestSuite<>(parallel(), name, NonEmptyList.of(test, tests)) {
      @Override
      public TestSuite.Report run() {
        return runtime.run(runK());
      }

      @Override
      public Future> parRun(Executor executor) {
        return runtime.parRun(runParK(), executor);
      }
    };
  }

  @SafeVarargs
  protected final PropertyTestSuite properties(
      String name, PropertyTestCase test, PropertyTestCase... tests) {
    return new PropertyTestSuite<>(parallel(), name, NonEmptyList.of(test, tests)) {
      @Override
      public PropertyTestSuite.Report run() {
        return runtime.run(runK());
      }

      @Override
      public Future> parRun(Executor executor) {
        return runtime.parRun(runParK(), executor);
      }
    };
  }

  @SafeVarargs
  protected final PureCheck pureCheck(
      String name, TestSuite suite, TestSuite... suites) {
    return new PureCheck<>(parallel(), name, NonEmptyList.of(suite, suites)) {
      @Override
      public PureCheck.Report run() {
        return runtime.run(runK());
      }

      @Override
      public Future> parRun(Executor executor) {
        return runtime.parRun(runParK(), executor);
      }
    };
  }

  private Parallel parallel() {
    return Parallel.of(monad, applicative, FunctionK.identity(), FunctionK.identity());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy