
com.github.tonivade.purecheck.PureCheck Maven / Gradle / Ivy
/*
* Copyright (c) 2020-2022, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purecheck;
import static com.github.tonivade.purefun.Precondition.checkNonEmpty;
import static com.github.tonivade.purefun.Precondition.checkNonNull;
import static com.github.tonivade.purefun.typeclasses.Instances.traverse;
import java.util.concurrent.Executor;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Witness;
import com.github.tonivade.purefun.concurrent.Future;
import com.github.tonivade.purefun.data.NonEmptyList;
import com.github.tonivade.purefun.data.Sequence;
import com.github.tonivade.purefun.data.SequenceOf;
import com.github.tonivade.purefun.data.Sequence_;
import com.github.tonivade.purefun.typeclasses.Parallel;
public abstract class PureCheck {
private final Parallel parallel;
private final String name;
private final NonEmptyList> suites;
protected PureCheck(Parallel parallel, String name, NonEmptyList> suites) {
this.parallel = checkNonNull(parallel);
this.name = checkNonEmpty(name);
this.suites = checkNonNull(suites);
}
public Kind> runK() {
var sequence = parallel.parSequence(traverse(Sequence_.class), suites.map(TestSuite::runK));
var results = parallel.monad().map(sequence, SequenceOf::narrowK);
return parallel.monad().map(results, xs -> new PureCheck.Report<>(name, xs));
}
public abstract Report run();
public abstract Future> parRun(Executor executor);
public Future> parRun() {
return parRun(Future.DEFAULT_EXECUTOR);
}
public static class Report {
private final String name;
private final Sequence> reports;
public Report(String name, Sequence> reports) {
this.name = checkNonEmpty(name);
this.reports = checkNonNull(reports);
}
public void assertion() {
try {
reports.forEach(TestSuite.Report::assertion);
} finally {
System.out.println(this);
}
}
@Override
public String toString() {
return reports.join("\n\n", "# " + name + "\n\n", "\n");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy