From 8b3b07e32caeddbf84a6120cba459a1fda701af2 Mon Sep 17 00:00:00 2001 From: luca9913 Date: Wed, 28 Jun 2023 22:10:08 +0200 Subject: [PATCH] Added test case Record(.jav) in TestNewFeatures --- resources/syntaxtreegenerator/Record.ast | 47 +++++++++++++++++++ .../syntaxtreegenerator/javFiles/Record.jav | 5 ++ .../syntaxtree/visual/ASTPrinter.java | 2 +- .../syntaxtree/visual/OutputGenerator.java | 1 - .../syntaxtreegenerator/TestNewFeatures.java | 20 ++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 resources/syntaxtreegenerator/Record.ast create mode 100644 resources/syntaxtreegenerator/javFiles/Record.jav diff --git a/resources/syntaxtreegenerator/Record.ast b/resources/syntaxtreegenerator/Record.ast new file mode 100644 index 00000000..26d95878 --- /dev/null +++ b/resources/syntaxtreegenerator/Record.ast @@ -0,0 +1,47 @@ +class Point { + + TPH x; + TPH y; +Point(TPH x, TPH y){ + super(()); + this.x = x; + this.y = y; + } + TPH x(){ + return this.x; + } + + TPH y(){ + return this.y; + } + + Point(TPH x, TPH y){ + super(()); + this.x = x; + this.y = y; + } + +}class Line { + + TPH pt1; + TPH pt2; +Line(TPH pt1, TPH pt2){ + super(()); + this.pt1 = pt1; + this.pt2 = pt2; + } + TPH pt1(){ + return this.pt1; + } + + TPH pt2(){ + return this.pt2; + } + + Line(TPH pt1, TPH pt2){ + super(()); + this.pt1 = pt1; + this.pt2 = pt2; + } + +} \ No newline at end of file diff --git a/resources/syntaxtreegenerator/javFiles/Record.jav b/resources/syntaxtreegenerator/javFiles/Record.jav new file mode 100644 index 00000000..31edc83d --- /dev/null +++ b/resources/syntaxtreegenerator/javFiles/Record.jav @@ -0,0 +1,5 @@ +// Simple records +record Point(x, y){ } + +//Combination of records +record Line(pt1, pt2){} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/ASTPrinter.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/ASTPrinter.java index 9d04b298..0562d05b 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/ASTPrinter.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/ASTPrinter.java @@ -4,7 +4,7 @@ import de.dhbwstuttgart.syntaxtree.*; public class ASTPrinter { - public static String print(SourceFile toPrint){ + public static String print(SourceFile toPrint) { StringBuilder output = new StringBuilder(); new OutputGenerator(output).visit(toPrint); return output.toString(); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java index 69465821..3d427d23 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/visual/OutputGenerator.java @@ -7,7 +7,6 @@ import de.dhbwstuttgart.syntaxtree.type.*; import java.lang.reflect.Modifier; import java.util.Iterator; -import java.util.Optional; public class OutputGenerator implements ASTVisitor { private static final String TAB = " "; diff --git a/src/test/java/syntaxtreegenerator/TestNewFeatures.java b/src/test/java/syntaxtreegenerator/TestNewFeatures.java index 5008c55d..3c6f13e3 100644 --- a/src/test/java/syntaxtreegenerator/TestNewFeatures.java +++ b/src/test/java/syntaxtreegenerator/TestNewFeatures.java @@ -48,6 +48,26 @@ public class TestNewFeatures { fail("An error occured while generating the AST for applyLambda.jav"); } } + + @Test + public void recordTest() { + try { + FileInputStream fileIn = new FileInputStream(javFiles.get("Record")[1]); + String expectedAST = new String(fileIn.readAllBytes()); + fileIn.close(); + expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH"); + File srcfile = javFiles.get("Record")[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("Result:\n" + new String(resultingAST)); + assertEquals("Comparing expected and resulting AST for applyLambda.jav", expectedAST, resultingAST); + } catch (Exception exc) { + exc.printStackTrace(); + fail("An error occured while generating the AST for applyLambda.jav"); + } + } } class JavFilter implements FileFilter {