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

hydra.HydraTestBase Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
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.Type;
import hydra.core.TypeScheme;
import hydra.core.Unit;
import hydra.dsl.Terms;
import hydra.graph.Element;
import hydra.graph.Graph;
import hydra.graph.Primitive;
import hydra.lib.Libraries;
import hydra.tools.PrimitiveFunction;
import hydra.util.Opt;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import static hydra.Coders.*;
import static hydra.Flows.*;
import static org.junit.jupiter.api.Assertions.*;


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, Flows.UNIT);
    }

    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 types = 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());
        }

        Opt schema = Opt.empty();

        return new Graph(elements, environment, types, body, primitives, schema);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy