| @@ -11,7 +11,7 @@ public class ExpressionPrinter extends Visitor<String> { | |||||
| } | } | ||||
| public String visitAddition(Addition addition) { | public String visitAddition(Addition addition) { | ||||
| return "(" + visit(addition.leftHandSide) + " + " + visit(addition.rightHandSide) + ")"; | |||||
| return visit(addition.leftHandSide) + " + " + visit(addition.rightHandSide); | |||||
| } | } | ||||
| public String visitIdentifier(Identifier identifier) { | public String visitIdentifier(Identifier identifier) { | ||||
| @@ -23,7 +23,7 @@ public class ExpressionPrinter extends Visitor<String> { | |||||
| } | } | ||||
| public String visitSubtraction(Subtraction subtraction) { | public String visitSubtraction(Subtraction subtraction) { | ||||
| return "(" + visit(subtraction.leftHandSide) + " - " + visit(subtraction.rightHandSide) + ")"; | |||||
| return visit(subtraction.leftHandSide) + " - " + visit(subtraction.rightHandSide); | |||||
| } | } | ||||
| public String getValue() { | public String getValue() { | ||||
| @@ -0,0 +1,49 @@ | |||||
| package printer; | |||||
| import expression.*; | |||||
| import org.junit.Test; | |||||
| import static org.junit.Assert.*; | |||||
| /** | |||||
| * Created by nils on 15.01.2017. | |||||
| */ | |||||
| public class ExpressionPrinterTest { | |||||
| final String identifierCode = "a"; | |||||
| final Identifier identifier = new Identifier(identifierCode); | |||||
| final String integerCode = "42"; | |||||
| final Int integer = new Int(42); | |||||
| final String additionCode = "a + 42"; | |||||
| final Addition addition = new Addition(new Identifier("a"), new Int(42)); | |||||
| final String subtractionCode = "1 - a"; | |||||
| final Subtraction subtraction = new Subtraction(new Int(1), new Identifier("a")); | |||||
| @Test | |||||
| public void testVisitAddition() { | |||||
| ExpressionPrinter printer = new ExpressionPrinter(addition); | |||||
| assertEquals(additionCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitIdentifier() { | |||||
| ExpressionPrinter printer = new ExpressionPrinter(identifier); | |||||
| assertEquals(identifierCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitInt() { | |||||
| ExpressionPrinter printer = new ExpressionPrinter(integer); | |||||
| assertEquals(integerCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitSubtraction() { | |||||
| ExpressionPrinter printer = new ExpressionPrinter(subtraction); | |||||
| assertEquals(subtractionCode, printer.getValue()); | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,58 @@ | |||||
| package printer; | |||||
| import expression.Addition; | |||||
| import expression.Identifier; | |||||
| import expression.Int; | |||||
| import expression.Subtraction; | |||||
| import org.junit.Test; | |||||
| import program.*; | |||||
| import static org.junit.Assert.*; | |||||
| public class ProgramPrinterTest { | |||||
| final String loopCode = "while (a) { r := r + b ; a := a - 1 }"; | |||||
| final Loop loop = new Loop(new Identifier("a"), new Composition(new Assignment(new Identifier("r"), new Addition(new Identifier("r"), new Identifier("b"))), new Assignment(new Identifier("a"), new Subtraction(new Identifier("a"), new Int(1))))); | |||||
| final String assignmentCode = "a := 2"; | |||||
| final Assignment assignment = new Assignment(new Identifier("a"), new Int(2)); | |||||
| final String compositionCode = assignmentCode + " ; b := bar"; | |||||
| final Composition composition = new Composition(assignment, new Assignment(new Identifier("b"), new Identifier("bar"))); | |||||
| final String conditionalCode = "if (foo - bar) then { x := 5 } else { x := x }"; | |||||
| final Conditional conditional = new Conditional(new Subtraction(new Identifier("foo"), new Identifier("bar")), new Assignment(new Identifier("x"), new Int(5)), new Assignment(new Identifier("x"), new Identifier("x"))); | |||||
| final String programCode = compositionCode + " ; " + loopCode + " ; " + conditionalCode; | |||||
| final Program program = new Composition(new Composition(composition, loop), conditional); | |||||
| @Test | |||||
| public void testVisitAssignment() { | |||||
| ProgramPrinter printer = new ProgramPrinter(assignment); | |||||
| assertEquals(assignmentCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitComposition() { | |||||
| ProgramPrinter printer = new ProgramPrinter(composition); | |||||
| assertEquals(compositionCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitConditional() { | |||||
| ProgramPrinter printer = new ProgramPrinter(conditional); | |||||
| assertEquals(conditionalCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitLoop() { | |||||
| ProgramPrinter printer = new ProgramPrinter(loop); | |||||
| assertEquals(loopCode, printer.getValue()); | |||||
| } | |||||
| @Test | |||||
| public void testVisitProgram() { | |||||
| ProgramPrinter printer = new ProgramPrinter(program); | |||||
| assertEquals(programCode, printer.getValue()); | |||||
| } | |||||
| } | |||||