Skip to content

Commit

Permalink
Survive when changed value isn't a number
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jun 7, 2022
1 parent 1a87a6d commit 1164679
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ private Object readValue() {
return this.value;
}

public void updateConstant(String text) {
this.value = Long.valueOf(text);
CONSTANTS_ARE_CONSTANTS.invalidate();
public boolean updateConstant(String text) {
try {
this.value = Long.valueOf(text);
CONSTANTS_ARE_CONSTANTS.invalidate();
return true;
} catch (NumberFormatException ex) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ private boolean updateNode(model.TextEdit edit, Node node) {
at.getEndLine() - 1 == edit.range().end().line() &&
at.getEndColumn() == edit.range().end().character()
) {
found = true;
integerNode.updateConstant(edit.text());
found = integerNode.updateConstant(edit.text());
}
}
for (Node n : NodeUtil.findNodeChildren(node)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.enso.polyglot.runtime.Runtime$Api$CreateContextRequest;
import org.enso.polyglot.runtime.Runtime$Api$CreateContextResponse;
import org.enso.polyglot.runtime.Runtime$Api$EditFileNotification;
import org.enso.polyglot.runtime.Runtime$Api$ExecutionFailed;
import org.enso.polyglot.runtime.Runtime$Api$InitializedNotification;
import org.enso.polyglot.runtime.Runtime$Api$MethodPointer;
import org.enso.polyglot.runtime.Runtime$Api$OpenFileNotification;
Expand Down Expand Up @@ -47,6 +48,17 @@ public void initializeContext() {

@Test
public void sendUpdatesWhenFunctionBodyIsChanged() {
sendUpdatesWhenFunctionBodyIsChanged("5", true);
}

@Test
public void sendNotANumberChange() {
sendUpdatesWhenFunctionBodyIsChanged("x", false);
var failed = context.receiveN(1, 10000);
assertTrue("Execution failed: " + failed, failed.head().payload() instanceof Runtime$Api$ExecutionFailed);
}

private void sendUpdatesWhenFunctionBodyIsChanged(String newText, boolean assertExecution) {
var contextId = UUID.randomUUID();
var requestId = UUID.randomUUID();
var moduleName = "Enso_Test.Test.Main";
Expand Down Expand Up @@ -142,19 +154,21 @@ public void sendUpdatesWhenFunctionBodyIsChanged() {
makeSeq(
new model.TextEdit(
new model.Range(new model.Position(3, 8), new model.Position(3, 9)),
"5"
newText
)
)
)
)
);
assertSameElements(context.receiveN(1, 10000),
context.executionComplete(contextId)
);
assertEquals(List.newBuilder().addOne("5"), context.consumeOut());
nodeCountingInstrument.assertNewNodes("No new nodes created", 0, 0);

assertEquals("Int node has been updated to 5 in the source", "5", intNode.getSourceSection().getCharacters().toString());
if (assertExecution) {
assertSameElements(context.receiveN(1, 10000),
context.executionComplete(contextId)
);
assertEquals(List.newBuilder().addOne(newText), context.consumeOut());
nodeCountingInstrument.assertNewNodes("No new nodes created", 0, 0);

assertEquals("Int node has been updated in the source", newText, intNode.getSourceSection().getCharacters().toString());
}
}

private IntegerLiteralNode findIntegerLiteralNode(Map<Class, java.util.List<Node>> nodes) {
Expand All @@ -168,7 +182,7 @@ private static void assertSameElements(List<Runtime$Api$Response> actual, Runtim
assertEquals("Same size: " + actual, seq.length, actual.size());
for (int i = 0; i < seq.length; i++) {
var real = actual.drop(i).head();
assertEquals("Check on #" + i, real, seq[i]);
assertEquals("Check on #" + i, seq[i], real);
}
}

Expand Down

0 comments on commit 1164679

Please sign in to comment.