Search in sources :

Example 31 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class EvaluatorBaseTest method shouldDivide16By2.

@Test
public void shouldDivide16By2() throws Exception {
    // given
    String code = codeForDivide16By2();
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    // when
    TryResult result = evaluator().evaluate(seo, code);
    // then
    assertThat(result.result().toString()).isEqualTo("8");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 32 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class KotlinCodeRunner method interpretResult.

private TryResult interpretResult(Object o) {
    TryResult either;
    if (o == null) {
        either = TryResult.createResult(null);
    } else if (o instanceof ReplEvalResult.UnitResult) {
        either = TryResult.createResult(null);
    } else if (o instanceof ReplEvalResult.ValueResult) {
        Object value = ((ReplEvalResult.ValueResult) o).getValue();
        either = TryResult.createResult(value);
    } else if (o instanceof ReplEvalResult.Error) {
        String message = ((ReplEvalResult.Error) o).getMessage();
        either = TryResult.createError(message);
    } else {
        either = TryResult.createError(o.toString());
    }
    return either;
}
Also used : ReplEvalResult(org.jetbrains.kotlin.cli.common.repl.ReplEvalResult) TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)

Example 33 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class ScalaEvaluatorTest method javaImports_shouldBeAdjustedForScala.

@Test
public void javaImports_shouldBeAdjustedForScala() throws Exception {
    // given
    Map<String, Object> paramMap = new HashMap<>();
    // This import tests both "static" removal and "object" escaping.
    List<String> imports = Arrays.asList("import static com.twosigma.beakerx.scala.evaluator.object.ImportTestHelper.staticMethod");
    paramMap.put(IMPORTS, imports);
    EvaluatorParameters kernelParameters = new EvaluatorParameters(paramMap);
    // when
    scalaEvaluator.setShellOptions(kernelParameters);
    String code = "val x = staticMethod()";
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    TryResult evaluate = scalaEvaluator.evaluate(seo, code);
    // then
    assertThat(evaluate.result()).isNull();
}
Also used : EvaluatorParameters(com.twosigma.beakerx.kernel.EvaluatorParameters) HashMap(java.util.HashMap) TryResult(com.twosigma.beakerx.TryResult) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 34 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class ScalaEvaluatorTest method evaluatePlot_shouldCreatePlotObject.

@Test
public void evaluatePlot_shouldCreatePlotObject() throws Exception {
    // given
    String code = "import com.twosigma.beakerx.chart.xychart.Plot;\n" + "val plot = new Plot();\n" + "plot.setTitle(\"test title\");";
    SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
    // when
    TryResult evaluate = scalaEvaluator.evaluate(seo, code);
    // then
    assertThat(evaluate.result() instanceof Plot).isTrue();
    assertThat(((Plot) evaluate.result()).getTitle()).isEqualTo("test title");
}
Also used : TryResult(com.twosigma.beakerx.TryResult) Plot(com.twosigma.beakerx.chart.xychart.Plot) SimpleEvaluationObject(com.twosigma.beakerx.jvm.object.SimpleEvaluationObject) Test(org.junit.Test)

Example 35 with TryResult

use of com.twosigma.beakerx.TryResult in project beakerx by twosigma.

the class JavaWorkerThread method call.

@Override
public TryResult call() throws Exception {
    NamespaceClient nc = null;
    TryResult r;
    try {
        nc = NamespaceClient.getBeaker(javaEvaluator.getSessionId());
        nc.setOutputObj(j.outputObject);
        r = javaEvaluator.executeTask(new JavaCodeRunner(javaEvaluator, j.outputObject, j));
    } catch (Throwable e) {
        e.printStackTrace();
        r = TryResult.createError(e.getLocalizedMessage());
    } finally {
        if (nc != null) {
            nc.setOutputObj(null);
            nc = null;
        }
    }
    return r;
}
Also used : NamespaceClient(com.twosigma.beakerx.NamespaceClient) TryResult(com.twosigma.beakerx.TryResult)

Aggregations

TryResult (com.twosigma.beakerx.TryResult)55 SimpleEvaluationObject (com.twosigma.beakerx.jvm.object.SimpleEvaluationObject)41 Test (org.junit.Test)30 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 NamespaceClient (com.twosigma.beakerx.NamespaceClient)5 PrintWriter (java.io.PrintWriter)5 StringWriter (java.io.StringWriter)5 KernelTest (com.twosigma.beakerx.KernelTest)4 EvaluatorParameters (com.twosigma.beakerx.kernel.EvaluatorParameters)3 PlainCode.createSimpleEvaluationObject (com.twosigma.beakerx.kernel.PlainCode.createSimpleEvaluationObject)3 MagicCommandOutput (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutput)3 HashMap (java.util.HashMap)3 Plot (com.twosigma.beakerx.chart.xychart.Plot)2 MagicCommandOutcomeItem (com.twosigma.beakerx.kernel.magic.command.outcome.MagicCommandOutcomeItem)2 Message (com.twosigma.beakerx.message.Message)2 ThreadMXBean (java.lang.management.ThreadMXBean)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 ReplEvalResult (org.jetbrains.kotlin.cli.common.repl.ReplEvalResult)2 Code (com.twosigma.beakerx.kernel.Code)1