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 1 commit
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 (isUnitObject()) {
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 isUnitObject() = {
val print = interpreter.lastRequest.lineRep.call("$print").toString.trim
print.equals("")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method name isUnitObject doesn't match what it actually does. Maybe isPrintStringEmpty? (and then use isEmpty instead of equals("")). Or just compare the value to () and ignore the interpreter's notion of how things should be printed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by isUnitObject I want to express whether the interpreter returns Unit object or not.
I used val print = interpreter.lastRequest.lineRep.call (" $ print "). toString.trim print.equals ("") because I have not found a better way to do this.
The question is whether my solution meets the requirements and doesn't provide errors.
There is a test that checks if Unit object is represented by MIMEContainer.HIDDEN https://github.com/twosigma/beakerx/pull/7694/files#diff-7a177fbeb8a50f57cde772923fc6e1ccR60
if you know use cases which we should handle or you see problems with this solution, please let us know, your input is welcome.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just did some exploration and the behavior of the interpreter is surprising. Calling $result when the input is "()" returns null instead of a boxed Unit, so looking at the result value alone is not a solution. I am persuaded that it makes sense to use $print to decide whether to hide the result. That said, I still think the name isUnitObject is misleading, and would suggest changing it to shouldHideResult. For a specific example, the input "(): Any" gives a result that is a unit object (with a wider type), but the interpreter does not suppress the output in that case.


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