ScenarioParser extends ExtendedParser { public final void readStatement() throws ParserException { String command = readAlphabetic(); skipWhitespaces(); if (command.equals("var")) { String name = readAlphabetic(); skipWhitespaces(); check('='); skipWhitespaces(); float value = readExpression(); skipWhitespaces(); check(';'); super.variables.put(name, value); } else { ArrayList<Float> parameters = new ArrayList<>(); check('('); while (true) { skipWhitespaces(); parameters.add(readExpression()); skipWhitespaces(); if (peek() == ',') { check(','); skipWhitespaces(); } else { break; } } skipWhitespaces(); check(')'); skipWhitespaces(); check(';'); if (command.equals("delay")) { checkArgumentCount(parameters, 1); scenario.addPause(Math.round(parameters.get(0))); } else if (command.equals("setRepeat")) { checkArgumentCount(parameters, 1); scenario.setRepeat(Math.abs(parameters.get(0) - 1) < 0.000001); } else if (!commands.containsKey(command)) { die("Unknown command: " + command); } else { scenario.appendRunnable(commands.get(command).create(parameters)); } } } ScenarioParser(String toParse); final Scenario readScenario(); final void readStatement();  }
@Test public void testVariableAssignments() throws ParserException { ScenarioParser parser = new ScenarioParser("var x = 15;"); parser.readStatement(); assertEquals(15f, parser.variables.get("x"), 0.0001); parser = new ScenarioParser("var myVariable = -(3+3);"); parser.readStatement(); assertEquals(-6f, parser.variables.get("myVariable"), 0.0001); parser = new ScenarioParser("var var = -var;"); parser.variables.put("var", 42f); parser.readStatement(); assertEquals(-42f, parser.variables.get("var"), 0.0001); }
@Test(expected = ParserException.class) public void testUnknownVariableAssignment() throws ParserException { ScenarioParser parser = new ScenarioParser("var x = 15 + z;"); parser.readStatement(); }
@Test(expected = ParserException.class) public void testInvalidAssignmentSyntax() throws ParserException { ScenarioParser parser = new ScenarioParser("var x = 10 = 5;"); parser.readStatement(); }
@Test(expected = ParserException.class) public void testInvalidAssignmentNumber() throws ParserException { ScenarioParser parser = new ScenarioParser("var x = 12a3;"); parser.readStatement(); }