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

hydra.HydraTestBase Maven / Gradle / Ivy

package hydra;

import hydra.compute.Coder;
import hydra.compute.Flow;
import hydra.compute.FlowState;
import hydra.core.Name;
import hydra.core.Term;
import hydra.core.Unit;
import hydra.dsl.Terms;
import hydra.graph.AnnotationClass;
import hydra.graph.Element;
import hydra.graph.Graph;
import hydra.graph.Primitive;
import hydra.lib.Libraries;
import hydra.tools.PrimitiveFunction;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;

import static hydra.Coders.roundTrip;
import static hydra.Flows.EMPTY_TRACE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;


public class HydraTestBase {

    protected static  void assertFails(Flow flow, S initialState) {
        FlowState result = flow.value.apply(initialState).apply(EMPTY_TRACE);
        assertTrue(!result.value.isPresent());
        //assertTrue(result.trace.messages.size() > 1);
    }

    protected static  void assertFails(Flow flow) {
        assertFails(flow, null);
    }

    protected static  void assertRoundTripIsNoop(Coder coder, V1 initialValue) {
        assertRoundTripIsNoop(coder, new Unit(), initialValue);
    }

    protected static  void assertRoundTripIsNoop(Coder coder,
                                                            S initialState,
                                                            V1 initialValue) {
        assertSucceedsWith(initialValue, roundTrip(coder, initialValue), initialState);
    }

    protected static  void assertRoundTripFails(Coder coder, V1 initialValue) {
        assertFails(roundTrip(coder, initialValue));
    }

    protected static  void assertRoundTripFails(Coder coder,
                                                           S initialState,
                                                           V1 initialValue) {
        assertFails(roundTrip(coder, initialValue), initialState);
    }

    protected static  void assertSucceedsWith(X expected, Flow flow) {
        assertSucceedsWith(expected, flow, new Unit());
    }

    protected static  void assertSucceedsWith(X expected, Flow flow, S initialState) {
        checkFlow(flow, initialState, x -> assertEquals(expected, x));
    }

    protected static  void checkFlow(Flow flow, S initialState, Consumer consumer) {
        FlowState result = flow.value.apply(initialState).apply(EMPTY_TRACE);
        assertTrue(result.value.isPresent(), "Flow failed: " + result.trace.messages);
        consumer.accept(result.value.get());
    }

    protected static  void checkFlow(Flow flow, Consumer consumer) {
        checkFlow(flow, new Unit(), consumer);
    }

    protected static  Graph emptyGraph() {
        Map> elements = Collections.emptyMap();
        Map>> environment = Collections.emptyMap();
        Term body = Terms.string("empty graph");

        Map> primitives = new HashMap<>();
        for (PrimitiveFunction prim : Libraries.standardPrimitives()) {
            primitives.put(prim.name(), prim.toNative());
        }

        AnnotationClass annotations = null;
        Optional> schema = Optional.empty();

        return new Graph<>(elements, environment, body, primitives, annotations, schema);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy