From fe6c9858a208a7fad730be73e9ae43f78ed0011d Mon Sep 17 00:00:00 2001 From: luca9913 Date: Mon, 17 Jul 2023 17:58:39 +0200 Subject: [PATCH] Finalized test cases for new jav-files --- .../syntaxtreegenerator/PatternMatching.ast | 120 ++++++++++++++++++ resources/syntaxtreegenerator/Switch.ast | 96 ++++++++++++++ .../syntaxtreegenerator/TestNewFeatures.java | 32 ++--- 3 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 resources/syntaxtreegenerator/PatternMatching.ast create mode 100644 resources/syntaxtreegenerator/Switch.ast diff --git a/resources/syntaxtreegenerator/PatternMatching.ast b/resources/syntaxtreegenerator/PatternMatching.ast new file mode 100644 index 00000000..41a24534 --- /dev/null +++ b/resources/syntaxtreegenerator/PatternMatching.ast @@ -0,0 +1,120 @@ +class Point { + + java.lang.Integer x; + java.lang.Integer y; +Point(java.lang.Integer x, java.lang.Integer y){ + super(()); + this.x = x; + this.y = y; + } + java.lang.Integer x(){ + return this.x; + } + + java.lang.Integer y(){ + return this.y; + } + + Point(java.lang.Integer x, java.lang.Integer y){ + super(()); + this.x = x; + this.y = y; + } + +}class Shape { + +}class ColoredPoint { + + Point pt; + java.lang.String color; +ColoredPoint(Point pt, java.lang.String color){ + super(()); + this.pt = pt; + this.color = color; + } + Point pt(){ + return this.pt; + } + + java.lang.String color(){ + return this.color; + } + + ColoredPoint(Point pt, java.lang.String color){ + super(()); + this.pt = pt; + this.color = color; + } + +}class Rectangle { + + ColoredPoint upperLeft; + ColoredPoint lowerRight; +Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight){ + super(()); + this.upperLeft = upperLeft; + this.lowerRight = lowerRight; + } + ColoredPoint upperLeft(){ + return this.upperLeft; + } + + ColoredPoint lowerRight(){ + return this.lowerRight; + } + + Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight){ + super(()); + this.upperLeft = upperLeft; + this.lowerRight = lowerRight; + } + +}class Color { + +Color(){ + super(()); + } + Color(){ + super(()); + } + +}class Blue { + +Blue(){ + super(()); + } + Blue(){ + super(()); + } + +}class Red { + +Red(){ + super(()); + } + Red(){ + super(()); + } + +}class PatternMatching { + +PatternMatching(){ + super(()); + } + void printColorOfUpperLeftPoint(Shape shape){ + switch(shape){ + case Rectangle(ColoredPoint(Point pt, java.lang.String color), ColoredPoint lowerRight): + System.out.println Signature: [TPH, TPH](x: op pt.x Signature: [TPH]() op / color: op color op / lowerRight: op lowerRight); + + default: + System.out.println Signature: [TPH, TPH](not a rectangle); + + }; + return; + } + + PatternMatching(){ + super(()); + } + +} \ No newline at end of file diff --git a/resources/syntaxtreegenerator/Switch.ast b/resources/syntaxtreegenerator/Switch.ast new file mode 100644 index 00000000..f6dfd8fb --- /dev/null +++ b/resources/syntaxtreegenerator/Switch.ast @@ -0,0 +1,96 @@ +class SwitchStatement { + +SwitchStatement(){ + super(()); + } + TPH switchStandard(){ + str = SwitchMe; + switch(str){ + case java.lang.String s: + return true; + + default: + return false; + + }; + } + + TPH switchInteger(){ + i = 5; + switch(i){ + case java.lang.Integer j: + case java.lang.String s: + i = 6; + break; + + default: + i = 0; + break; + + }; + return i op 0; + } + + TPH guardedPattern(){ + TPH i; + i = 1; + switch(i){ + case java.lang.Integer j: + return true; + + default: + return false; + + }; + } + + TPH recordPattern(java.lang.Object obj){ + switch(obj){ + case Coordinates(java.lang.Double lat, java.lang.Double lon): + return true; + + default: + return false; + + }; + } + + SwitchStatement(){ + super(()); + } + +}class SwitchExpression { + + java.lang.Integer x; + java.lang.Integer y; +SwitchExpression(java.lang.Integer x, java.lang.Integer y){ + super(()); + this.x = x; + this.y = y; + } + java.lang.Integer x(){ + return this.x; + } + + java.lang.Integer y(){ + return this.y; + } + + java.lang.Boolean switchStandard(TPH str){ + return switch(str){ + case java.lang.String s: + yield true; + + default: + yield false; + + }; + } + + SwitchExpression(java.lang.Integer x, java.lang.Integer y){ + super(()); + this.x = x; + this.y = y; + } + +} \ No newline at end of file diff --git a/src/test/java/syntaxtreegenerator/TestNewFeatures.java b/src/test/java/syntaxtreegenerator/TestNewFeatures.java index d6717e46..3bf0cfe8 100644 --- a/src/test/java/syntaxtreegenerator/TestNewFeatures.java +++ b/src/test/java/syntaxtreegenerator/TestNewFeatures.java @@ -42,10 +42,10 @@ public class TestNewFeatures { resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH"); System.out.println("Expected:\n" + new String(expectedAST)); System.out.println("Result:\n" + new String(resultingAST)); - assertEquals("Comparing expected and resulting AST for applyLambda.jav", expectedAST, resultingAST); + assertEquals("Comparing expected and resulting AST for Instanceof.jav", expectedAST, resultingAST); } catch (Exception exc) { exc.printStackTrace(); - fail("An error occured while generating the AST for applyLambda.jav"); + fail("An error occured while generating the AST for Instanceof.jav"); } } @@ -62,45 +62,47 @@ public class TestNewFeatures { resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH"); System.out.println("Expected:\n" + new String(expectedAST)); System.out.println("Result:\n" + new String(resultingAST)); - assertEquals("Comparing expected and resulting AST for applyLambda.jav", expectedAST, resultingAST); + assertEquals("Comparing expected and resulting AST for Record.jav", expectedAST, resultingAST); } catch (Exception exc) { exc.printStackTrace(); - fail("An error occured while generating the AST for applyLambda.jav"); + fail("An error occured while generating the AST for Record.jav"); } } @Test public void switchTest() { try { - /* - * FileInputStream fileIn = new FileInputStream(javFiles.get("Record")[1]); String expectedAST = new String(fileIn.readAllBytes()); fileIn.close(); expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH"); - */ + FileInputStream fileIn = new FileInputStream(javFiles.get("Switch")[1]); + String expectedAST = new String(fileIn.readAllBytes()); + fileIn.close(); + expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH"); File srcfile = javFiles.get("Switch")[0]; JavaTXCompiler compiler = new JavaTXCompiler(srcfile); String resultingAST = new String(ASTPrinter.print(compiler.sourceFiles.get(srcfile))); resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH"); - // System.out.println("Expected:\n" + new String(expectedAST)); + System.out.println("Expected:\n" + new String(expectedAST)); System.out.println("Result:\n" + new String(resultingAST)); - // assertEquals("Comparing expected and resulting AST for applyLambda.jav", expectedAST, resultingAST); + assertEquals("Comparing expected and resulting AST for Switch.jav", expectedAST, resultingAST); } catch (Exception exc) { exc.printStackTrace(); - fail("An error occured while generating the AST for applyLambda.jav"); + fail("An error occured while generating the AST for Switch.jav"); } } @Test public void patternMatching() { try { - /* - * FileInputStream fileIn = new FileInputStream(javFiles.get("PatternMatching")[1]); String expectedAST = new String(fileIn.readAllBytes()); fileIn.close(); expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH"); - */ + FileInputStream fileIn = new FileInputStream(javFiles.get("PatternMatching")[1]); + String expectedAST = new String(fileIn.readAllBytes()); + fileIn.close(); + expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH"); File srcfile = javFiles.get("PatternMatching")[0]; JavaTXCompiler compiler = new JavaTXCompiler(srcfile); String resultingAST = new String(ASTPrinter.print(compiler.sourceFiles.get(srcfile))); resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH"); - // System.out.println("Expected:\n" + new String(expectedAST)); + System.out.println("Expected:\n" + new String(expectedAST)); System.out.println("Result:\n" + new String(resultingAST)); - // assertEquals("Comparing expected and resulting AST for PatternMatching.jav", expectedAST, resultingAST); + assertEquals("Comparing expected and resulting AST for PatternMatching.jav", expectedAST, resultingAST); } catch (Exception exc) { exc.printStackTrace(); fail("An error occured while generating the AST for PatternMatching.jav");