StellarInterpreter extends Interpreter { @Override public InterpreterResult interpret(final String input, InterpreterContext context) { InterpreterResult result = new InterpreterResult(SUCCESS, TEXT, ""); try { String[] expressions = input.split(System.lineSeparator()); for (String expression : expressions) { result = execute(expression); } } catch(Throwable t){ String message = getErrorMessage(Optional.of(t), input); result = new InterpreterResult(ERROR, TEXT, message); } return result; } StellarInterpreter(Properties properties); @Override void open(); @Override void close(); @Override InterpreterResult interpret(final String input, InterpreterContext context); @Override void cancel(InterpreterContext context); @Override FormType getFormType(); @Override int getProgress(InterpreterContext context); @Override List<InterpreterCompletion> completion(String buf, int cursor); StellarShellExecutor getExecutor();  }
@Test public void testExecuteStellar() { InterpreterResult result = interpreter.interpret("2 + 2", context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); assertEquals(1, result.message().size()); InterpreterResultMessage message = result.message().get(0); assertEquals("4", message.getData()); assertEquals(InterpreterResult.Type.TEXT, message.getType()); }
@Test public void testExecuteWithStellarList() { final String expected = "[1, 2, 3, 4, 5]"; InterpreterResult result = interpreter.interpret("[1,2,3,4,5]", context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); assertEquals(1, result.message().size()); InterpreterResultMessage message = result.message().get(0); assertEquals(expected, message.getData()); assertEquals(InterpreterResult.Type.TEXT, message.getType()); }
@Test public void testExecuteWithStellarMap() { final String expected = "{foo=2, key=val}"; InterpreterResult result = interpreter.interpret("{ 'foo':2, 'key':'val' }", context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); assertEquals(1, result.message().size()); InterpreterResultMessage message = result.message().get(0); assertEquals(expected, message.getData()); assertEquals(InterpreterResult.Type.TEXT, message.getType()); }
@Test public void testExecuteBadStellar() { InterpreterResult result = interpreter.interpret("2 + ", context); assertEquals(InterpreterResult.Code.ERROR, result.code()); assertEquals(1, result.message().size()); InterpreterResultMessage message = result.message().get(0); assertTrue(message.getData().length() > 0); assertEquals(InterpreterResult.Type.TEXT, message.getType()); }
@Test public void testExecuteNoop() { InterpreterResult result = interpreter.interpret("x", context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); assertEquals(1, result.message().size()); InterpreterResultMessage message = result.message().get(0); assertEquals(0, message.getData().length()); assertEquals(InterpreterResult.Type.TEXT, message.getType()); }