Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jarek/7481: repr of scala unit object as MIMEContainer.HIDDEN #7694

Merged
merged 2 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ public void returnPrintln() throws Exception {
//when
TryResult result = evaluator().evaluate(seo, code);
//then
verifyReturnPrintlnStatement(result);
}

protected void verifyReturnPrintlnStatement(TryResult result) {
assertThat((String) result.result()).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.net.URL
import com.twosigma.beakerx.TryResult
import com.twosigma.beakerx.autocomplete.AutocompleteResult
import com.twosigma.beakerx.jvm.`object`.SimpleEvaluationObject
import com.twosigma.beakerx.mimetype.MIMEContainer

import scala.tools.jline_embedded.console.completer.Completer
import scala.tools.nsc.Settings
Expand Down Expand Up @@ -119,8 +120,12 @@ class ScalaEvaluatorGlue(val cl: ClassLoader, var cp: String, val replClassdir:
out.setOutputHandler()
interpreter.interpret(code) match {
case Success => {
val value = getOut.asInstanceOf[Object]
either = TryResult.createResult(value)
if (shouldHideResult()) {
either = TryResult.createResult(MIMEContainer.HIDDEN)
} else {
val value = getOut.asInstanceOf[Object]
either = TryResult.createResult(value)
}
}
case Incomplete => {
either = TryResult.createError("input is incomplete")
Expand All @@ -139,6 +144,11 @@ class ScalaEvaluatorGlue(val cl: ClassLoader, var cp: String, val replClassdir:
either
}

private def shouldHideResult() = {
val print = interpreter.lastRequest.lineRep.call("$print").toString.trim
print.equals("")
}

def autocomplete(buf: String, len: Integer): AutocompleteResult = {
val maybes = new java.util.ArrayList[CharSequence]
val offset = completer.complete(buf, len, maybes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
*/
package com.twosigma.beakerx.scala.evaluator;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.evaluator.BaseEvaluator;
import com.twosigma.beakerx.evaluator.EvaluatorBaseTest;
import com.twosigma.beakerx.evaluator.TempFolderFactory;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.scala.TestScalaEvaluator;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import static org.assertj.core.api.Assertions.assertThat;

public class ScalaBaseEvaluatorTest extends EvaluatorBaseTest {

private static BaseEvaluator evaluator;
Expand Down Expand Up @@ -67,4 +71,9 @@ protected String codeForPrintln() {
return "println(\"Hello\")";
}

@Override
protected void verifyReturnPrintlnStatement(TryResult tryResult) {
assertThat(((MIMEContainer) tryResult.result())).isEqualTo(MIMEContainer.HIDDEN);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.chart.xychart.Plot;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.EvaluatorParameters;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.kernel.PathToJar;
import com.twosigma.beakerx.scala.TestScalaEvaluator;
import com.twosigma.beakerx.scala.kernel.ScalaKernelMock;

import com.twosigma.beakerx.widget.DisplayableWidget;
import org.junit.After;
import org.junit.AfterClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2017 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.twosigma.beakerx.scala.evaluator;

import com.twosigma.beakerx.TryResult;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelManager;
import com.twosigma.beakerx.mimetype.MIMEContainer;
import com.twosigma.beakerx.scala.TestScalaEvaluator;
import com.twosigma.beakerx.scala.kernel.ScalaKernelMock;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import static com.twosigma.beakerx.MessageFactorTest.commMsg;
import static org.assertj.core.api.Assertions.assertThat;

public class ScalaReprTest {

private static ScalaEvaluator scalaEvaluator;

@BeforeClass
public static void setUpClass() throws Exception {
scalaEvaluator = TestScalaEvaluator.evaluator();
}

@Before
public void setUp() throws Exception {
ScalaKernelMock kernel = new ScalaKernelMock("id", scalaEvaluator);
KernelManager.register(kernel);
}

@After
public void tearDown() throws Exception {
KernelManager.register(null);
}

@AfterClass
public static void tearDownClass() throws Exception {
scalaEvaluator.exit();
}

@Test
public void unitObjectShouldBeRepresentedAsHIDDEN() {
//given
String code = "()";
SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
seo.setJupyterMessage(commMsg());
//when
TryResult evaluate = scalaEvaluator.evaluate(seo, code);
//then
assertThat(((MIMEContainer) evaluate.result())).isEqualTo(MIMEContainer.HIDDEN);
}

@Test
public void emptyStringShouldBeRepresentedAsEmptyString() {
//given
String code = "\"\"";
SimpleEvaluationObject seo = new SimpleEvaluationObject(code);
seo.setJupyterMessage(commMsg());
//when
TryResult evaluate = scalaEvaluator.evaluate(seo, code);
//then
assertThat(evaluate.result()).isEqualTo("");
}
}