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

com.github.valid8j.metamor.MetamorphicTestCaseFactoryWithPreformer Maven / Gradle / Ivy

Go to download

Java Library Providing Uniformed Programming Experiences across DbC, Value Checking, and Test Assertions

The newest version!
package com.github.valid8j.metamor;


import com.github.valid8j.metamor.internals.InternalUtils;
import com.github.valid8j.pcond.forms.Printables;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Predicate;

import static java.util.Objects.requireNonNull;

public interface MetamorphicTestCaseFactoryWithPreformer extends MetamorphicTestCaseFactory {
  Function, P> metamorphicPreformer();

  Function, R> metamorphicReducer();

  @Override
  default Function>, R> metamorphicTransformer() {
    return metamorphicPreformerStage().andThen(metamorphicReducer());
  }

  default Function>, Dataset

> metamorphicPreformerStage() { return InternalUtils.createObservableProcessingPipeline( "preform", metamorphicPreformerToIoContextCallback(metamorphicPreformer()), inputResolverSequenceFactory().count(), ioVariableNameFormatter(), ioVariableName()); } default Function, P>, Function, P>> metamorphicPreformerToIoContextCallback(Function, P> preformer) { return Printables.function( () -> " " + preformer, c -> Printables.function( () -> String.format("preform[%s]:%s", c, preformer), preformer)); } IntFunction ioVariableNameFormatter(); class Impl implements MetamorphicTestCaseFactoryWithPreformer { private final Function fut; private final InputResolver.Sequence.Factory inputResolverSequenceFactory; private final Function, P> preformer; private final Function, R> reducer; private final Predicate checker; private final String ioVariableName; private final String inputVariableName; public Impl(Function fut, InputResolver.Sequence.Factory inputResolverSequenceFactory, Function, P> preformer, Function, R> reducer, Predicate checker, String inputVariableName, String ioVariableName) { this.fut = requireNonNull(fut); this.inputResolverSequenceFactory = inputResolverSequenceFactory; this.preformer = requireNonNull(preformer); this.reducer = requireNonNull(reducer); this.checker = requireNonNull(checker); this.inputVariableName = requireNonNull(inputVariableName); this.ioVariableName = requireNonNull(ioVariableName); } @Override public Function fut() { return this.fut; } @Override public InputResolver.Sequence.Factory inputResolverSequenceFactory() { return this.inputResolverSequenceFactory; } @Override public Function, P> metamorphicPreformer() { return this.preformer; } @Override public Function, R> metamorphicReducer() { return Printables.function(() -> "reduce:" + reducer, this.reducer); } @Override public Predicate metamorphicChecker() { return this.checker; } @Override public String inputVariableName() { return this.inputVariableName; } @Override public IntFunction inputVariableNameFormatter() { return i -> this.inputVariableName + "[" + i + "]"; } @Override public String ioVariableName() { return this.ioVariableName; } @Override public IntFunction ioVariableNameFormatter() { return i -> this.ioVariableName + "[" + i + "]"; } } class Builder extends BuilderBase, X, I, O, R> { private Function, R> reducer; private Function, P> preformer; public Builder() { } @SuppressWarnings("unchecked") @Override public , XX> BB sourceValueType(XX sourceType) { return (BB) this., XX>newBuilderWithSpecifiedSourceType(Builder::new); } @Override public Builder preformer(Function, PP> preformer) { Builder ret = this.withPreformer(); ret.preformer = preformer; return ret; } public Builder preform(Function preformer) { Function, P> currentPreformer = this.preformer; if (currentPreformer == null) throw new IllegalStateException(); Builder ret = this.withPreformer(); ret.preformer = currentPreformer.andThen(preformer); return ret; } public Builder preform(String name, Function preformer) { return this.preform(Printables.function(name, preformer)); } public Builder reducer(Function, R> reducer) { this.reducer = requireNonNull(reducer); return this; } public Builder reduce(Function, R> reducer) { return this.reducer(reducer); } public Builder reduce(String reducerName, Function, R> reducer) { return this.reduce(Printables.function(reducerName, reducer)); } public Builder propositionFactory(Function, Proposition> pf) { return this ., Proposition>newBuilderWithSpecifiedRelationType(Builder::new) .preformer(this.preformer) .reducer(pf) .checker(PropositionPredicate.INSTANCE); } public MetamorphicTestCaseFactory proposition(Function propositionFormatter, Predicate> p) { return this.propositionFactory( Proposition.Factory.create( p, propositionFormatter, i -> this.outputVariableName + "[" + i + "]", this.inputResolverSequenceFactoryProvider.count())) .build(); } public MetamorphicTestCaseFactory proposition(String propositionName, Predicate> p) { return this.proposition(args -> MessageFormat.format(propositionName, Arrays.stream(args).map(Objects::toString).toArray()), p); } public MetamorphicTestCaseFactoryWithPreformer build() { return new Impl<>(fut, inputResolverSequenceFactoryProvider.get(), preformer, reducer, checker, inputVariableName, ioVariableName); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy