Skip to content

Commit cf9ed64

Browse files
committed
JShellWrapper changed logic to know when the code is has a syntax error + tests
1 parent 2626520 commit cf9ed64

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

JShellWrapper/src/main/java/JShellWrapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ private void ok(PrintStream processOut) {
5959
private EvalResult eval(JShell shell, String code, AtomicBoolean hasStopped) {
6060
List<SnippetEvent> resultEvents = new ArrayList<>();
6161
JShellEvalAbortion abortion = null;
62-
do {
62+
while(!code.isEmpty()) {
6363
var completion = shell.sourceCodeAnalysis().analyzeCompletion(clean(code));
64-
if(completion.completeness() == SourceCodeAnalysis.Completeness.DEFINITELY_INCOMPLETE) {
64+
if(!completion.completeness().isComplete()) {
6565
abortion = new JShellEvalAbortion(code, completion.remaining(), new JShellEvalAbortionCause.SyntaxErrorAbortionCause());
6666
break;
6767
}
@@ -76,7 +76,7 @@ private EvalResult eval(JShell shell, String code, AtomicBoolean hasStopped) {
7676
break;
7777
}
7878
code = completion.remaining();
79-
} while(!code.isEmpty());
79+
}
8080
return new EvalResult(resultEvents, abortion);
8181
}
8282
private JShellEvalAbortionCause handleEvents(JShell shell, List<SnippetEvent> evalEvents, List<SnippetEvent> resultEvents) {

JShellWrapper/src/test/java/JShellWrapperTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void testRejected() {
205205
}
206206
@Test
207207
void testSyntaxError() {
208+
// DEFINITELY_INCOMPLETE
208209
evalTest("""
209210
eval
210211
1
@@ -217,6 +218,31 @@ void testSyntaxError() {
217218
print(\\n
218219
false
219220
""");
221+
// CONSIDERED_INCOMPLETE
222+
evalTest("""
223+
eval
224+
1
225+
while(true)""",
226+
"""
227+
OK
228+
0
229+
SYNTAX_ERROR
230+
while(true)
231+
while(true)\\n
232+
false
233+
""");
234+
evalTest("""
235+
eval
236+
1
237+
for(int i = 0; i < 10; i++)""",
238+
"""
239+
OK
240+
0
241+
SYNTAX_ERROR
242+
for(int i = 0; i < 10; i++)
243+
for(int i = 0; i < 10; i++)\\n
244+
false
245+
""");
220246
}
221247
@Test
222248
void testRejectedAndMultiples() {

0 commit comments

Comments
 (0)