@Test public void simple() { Expression program = new NopExpression(); Program p = new ExpressionProgramBuilder().buildProgram(program); Assert.assertEquals("0: end\n", p.toString()); }
@Test public void sequence() { SeqExpression program = new SeqExpression(new VariableExpression("a"), new VariableExpression("b")); Program p = new ExpressionProgramBuilder().buildProgram(program); Assert.assertEquals("0: read a\n" + "1: read b\n" + "2: end\n", p.toString()); }
@Test public void whileExpression() { Expression program = new WhileExpression( new VariableExpression("a"), new AssignmentExpression("b", new NopExpression())); Program p = new ExpressionProgramBuilder().buildProgram(program); Assert.assertEquals( "0: read a\n" + "1: ifjump 3\n" + "2: write b\n" + "3: end\n", p.toString()); }
@Test public void ifExpression() { Expression program = new IfExpression( new NopExpression(), new AssignmentExpression("a", new NopExpression()), new AssignmentExpression("b", new NopExpression())); Program p = new ExpressionProgramBuilder().buildProgram(program); Assert.assertEquals( "0: ifjump 3\n" + "1: write a\n" + "2: jump 4\n" + "3: write b\n" + "4: end\n", p.toString()); }
@Test public void programWithTryFinally() { Program program = new SimpleProgramBuilder() .emitTry() .emitRead("x") .emitFinally() .emitRead("x") .emitEndTry() .buildProgram(); Assert.assertEquals( "0: try\n" + "1: read x\n" + "2: finally\n" + "3: read x\n" + "4: endTry\n" + "5: end\n", program.toString()); }
@Test public void simpleProgramBuilding() { Program program = new SimpleProgramBuilder().emitRead("x").emitWrite("y").buildProgram(); Assert.assertEquals("0: read x\n" + "1: write y\n" + "2: end\n", program.toString()); }