AssignmentCommand implements SpecialCommand { @Override public StellarResult execute(String input, StellarShellExecutor executor) { assert StellarAssignment.isAssignment(input); StellarAssignment assignment = StellarAssignment.from(input); String varName = assignment.getVariable(); String varExpr = assignment.getStatement(); StellarResult result = executor.execute(varExpr); if(result.isSuccess()) { Object value = null; if(result.getValue().isPresent()) { value = result.getValue().get(); } else if(result.isValueNull()) { value = null; } executor.assign(varName, value, Optional.of(varExpr)); return result; } else { return result; } }  @Override Function<String, Boolean> getMatcher(); @Override String getCommand(); @Override StellarResult execute(String input, StellarShellExecutor executor); static final String ASSIGNMENT_OP; }
@Test public void testAssignment() { StellarResult result = command.execute("x := 2 + 2", executor); assertTrue(result.isSuccess()); assertTrue(result.getValue().isPresent()); assertEquals(4, result.getValue().get()); assertEquals(4, executor.getState().get("x").getResult()); }
@Test public void testAssignments() { assertTrue(command.execute("x := 2 + 2", executor).isSuccess()); assertTrue(command.execute("y := 2 + x", executor).isSuccess()); assertTrue(command.execute("z := x + y", executor).isSuccess()); assertEquals(4, executor.getState().get("x").getResult()); assertEquals(6, executor.getState().get("y").getResult()); assertEquals(10, executor.getState().get("z").getResult()); }
@Test public void testReassignment() { assertTrue(command.execute("x := 2 + 2", executor).isSuccess()); assertTrue(command.execute("x := 5 + 5", executor).isSuccess()); assertEquals(10, executor.getState().get("x").getResult()); }
@Test public void testAssignmentOfEmptyVar() { StellarResult result = command.execute("x := z", executor); assertTrue(result.isSuccess()); assertTrue(result.isValueNull()); assertFalse(result.getValue().isPresent()); assertNull(executor.getState().get("x").getResult()); }
@Test public void testBadAssignmentExpr() { StellarResult result = command.execute("x := 2 + ", executor); assertTrue(result.isError()); assertTrue(result.getException().isPresent()); assertFalse(executor.getState().containsKey("x")); }
@Test public void testErrorMessageWhenAssignmentFails() { String stmt = "0/0"; StellarResult result = command.execute("x := " + stmt, executor); assertTrue(result.isError()); assertTrue(result.getException().isPresent()); assertEquals(ParseException.class, result.getException().get().getClass()); assertTrue(result.getException().get().getMessage().contains(stmt)); }
@Test public void testAssignNull() { StellarResult result = command.execute("x := NULL", executor); assertTrue(result.isSuccess()); assertTrue(result.isValueNull()); assertNull(executor.getState().get("x").getResult()); }
@Test public void testNoAssignmentExpr() { StellarResult result = command.execute("x := ", executor); assertTrue(result.isSuccess()); assertTrue(result.getValue().isPresent()); assertEquals("", executor.getState().get("x").getResult()); }
@Test public void testAssignmentWithVar() { executor.assign("x", 10, Optional.empty()); StellarResult result = command.execute("y := x + 2", executor); assertTrue(result.isSuccess()); assertTrue(result.getValue().isPresent()); assertEquals(12, result.getValue().get()); assertEquals(10, executor.getState().get("x").getResult()); assertEquals(12, executor.getState().get("y").getResult()); }
@Test public void testAssignmentWithOddWhitespace() { StellarResult result = command.execute(" x := 2 + 2", executor); assertTrue(result.isSuccess()); assertTrue(result.getValue().isPresent()); assertEquals(4, result.getValue().get()); assertEquals(4, executor.getState().get("x").getResult()); }