From dfded3e59238f9d5f30cb2b41eda8ca401a3b74e Mon Sep 17 00:00:00 2001 From: i22035 Date: Sun, 30 Jun 2024 13:20:58 +0200 Subject: [PATCH 1/2] Added ASTBuilder Tests --- .../java/ast/members/ConstructorNode.java | 5 - .../java/parser/astBuilder/ASTBuilder.java | 10 +- src/test/java/parser/AstBuilderTest.java | 106 ++++++++++++------ src/test/java/parser/Helper.java | 47 ++++++++ .../resources/input/javaCases/EmptyClass.java | 2 +- .../javaCases/EmptyClassWithConstructor.java | 5 + src/test/resources/input/javaCases/Field.java | 3 + .../javaCases/FieldWithAccessModifier.java | 3 + .../input/javaCases/MultipleClasses.java | 8 +- 9 files changed, 141 insertions(+), 48 deletions(-) create mode 100644 src/test/java/parser/Helper.java create mode 100644 src/test/resources/input/javaCases/EmptyClassWithConstructor.java create mode 100644 src/test/resources/input/javaCases/Field.java create mode 100644 src/test/resources/input/javaCases/FieldWithAccessModifier.java diff --git a/src/main/java/ast/members/ConstructorNode.java b/src/main/java/ast/members/ConstructorNode.java index a70ac85..d6a40c2 100644 --- a/src/main/java/ast/members/ConstructorNode.java +++ b/src/main/java/ast/members/ConstructorNode.java @@ -13,11 +13,6 @@ public class ConstructorNode extends MethodNode { public List parameters = new ArrayList<>(); public BlockNode block; - public ConstructorNode(AccessModifierNode accessType, String identifier) { - this.accessType = accessType; - this.identifier = identifier; - } - public ConstructorNode(String accessType, String identifier, BlockNode block) { this.accessType = new AccessModifierNode(accessType); this.identifier = identifier; diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 32137eb..55750a5 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -63,7 +63,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { } if(!hasConstructor){ - classNode.members.addFirst(new ConstructorNode(new AccessModifierNode("public"), ctx.Identifier().getText())); + BlockNode blockNode = new BlockNode(); + blockNode.addStatement(new ReturnNode(null)); + classNode.members.addFirst(new ConstructorNode("public", ctx.Identifier().getText(), blockNode)); } return classNode; @@ -111,7 +113,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { @Override public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { - return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); + if(ctx.AccessModifier() != null) { + return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); + } else { + return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); + } } @Override diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index d75901a..38a601c 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -5,8 +5,11 @@ import ast.ASTNode; import ast.ClassNode; import ast.ProgramNode; import ast.members.ConstructorNode; +import ast.members.FieldNode; import ast.members.MemberNode; import ast.type.AccessModifierNode; +import ast.type.type.BaseType; +import ast.type.type.TypeEnum; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; @@ -25,141 +28,176 @@ import static org.assertj.core.api.Assertions.assertThat; class AstBuilderTest { @Test + @DisplayName("Empty Class Test") public void emptyClassTest(){ - MemberNode constructor = new ConstructorNode(new AccessModifierNode("public"),"EmptyClass"); - ClassNode emptyClass = new ClassNode("public", "EmptyClass"); - emptyClass.addMember(constructor); - var expected = new ProgramNode(); + ClassNode emptyClass = Helper.generateEmptyClass("TestClass"); + ProgramNode expected = new ProgramNode(); expected.addClass(emptyClass); - CharStream testFile = null; - try { - testFile = CharStreams.fromFileName("src/test/resources/input/javaCases/EmptyClass.java"); - } catch (IOException e) { - throw new RuntimeException(e); - } - SimpleJavaLexer lexer = new SimpleJavaLexer(testFile); - CommonTokenStream tokenStream = new CommonTokenStream(lexer); - tokenStream.fill(); - SimpleJavaParser parser = new SimpleJavaParser(tokenStream); - ParseTree parseTree = parser.program(); - ASTBuilder astBuilder = new ASTBuilder(); - var actual = astBuilder.visit(parseTree); - + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java"); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } + @Test + @DisplayName("Multiple Empty Classes Test") + public void multipleEmptyClassesTest() { + ClassNode classNode1 = Helper.generateEmptyClass("TestClass1"); + ClassNode classNode2 = Helper.generateEmptyClass("TestClass2"); + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode1); + expected.addClass(classNode2); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } + + @Test + @DisplayName("Empty Class Test with Constructor") + public void emptyClassWithConstructorTest() { + ClassNode classNode = Helper.generateEmptyClass("TestClass"); + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } + + @Test + @DisplayName("Field Test") + public void fieldTest() { + ClassNode classNode = Helper.generateEmptyClass("TestClass"); + classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a")); + + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } + + @Test + @DisplayName("Field Test with Accessmodifier") + public void fieldTestWithModifier() { + ClassNode classNode = Helper.generateEmptyClass("TestClass"); + classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } @Test @DisplayName("Commments Ignore Test") public void commmentsIgnoreTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Paramerter Test") public void constructorParameterTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("This Dot Test") public void thisDotTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor This Dot Test") public void constructorThisDotTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Void Methoden Test") public void voidMethodenTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Method call Test") public void constructorMethodCallTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Constructor Method call Parameters Test") public void constructorMethodCallParametersTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Char Test") public void charTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Null Test") public void nullTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Self Reference Test") public void selfReferneceTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Variable Compare Test") public void variableCompareTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Variable Calculation Test") public void variableCalculationTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Main Method Test") public void mainMethodTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("While Test") public void whileTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("Do While Test") public void doWhileTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test @DisplayName("For Test") public void forTest(){ - assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } diff --git a/src/test/java/parser/Helper.java b/src/test/java/parser/Helper.java new file mode 100644 index 0000000..a679c8a --- /dev/null +++ b/src/test/java/parser/Helper.java @@ -0,0 +1,47 @@ +package parser; + +import ast.ASTNode; +import ast.ClassNode; +import ast.ProgramNode; +import ast.members.ConstructorNode; +import ast.members.MemberNode; +import ast.statements.BlockNode; +import ast.statements.ReturnNode; +import ast.type.AccessModifierNode; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.tree.ParseTree; +import parser.astBuilder.ASTBuilder; +import parser.generated.SimpleJavaLexer; +import parser.generated.SimpleJavaParser; + +import java.io.IOException; + +public class Helper { + public static ASTNode generateAST(String filePath) { + CharStream testFile = null; + try { + testFile = CharStreams.fromFileName(filePath); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(testFile); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + tokenStream.fill(); + SimpleJavaParser parser = new SimpleJavaParser(tokenStream); + ParseTree parseTree = parser.program(); + ASTBuilder astBuilder = new ASTBuilder(); + + return astBuilder.visit(parseTree); + } + + public static ClassNode generateEmptyClass(String className) { + BlockNode blockNode = new BlockNode(); + blockNode.addStatement(new ReturnNode(null)); + MemberNode constructor = new ConstructorNode("public",className, blockNode); + ClassNode classNode = new ClassNode("public", className); + classNode.addMember(constructor); + return classNode; + } +} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/EmptyClass.java b/src/test/resources/input/javaCases/EmptyClass.java index 393c29c..f2be03c 100644 --- a/src/test/resources/input/javaCases/EmptyClass.java +++ b/src/test/resources/input/javaCases/EmptyClass.java @@ -1 +1 @@ -class EmptyClass { } \ No newline at end of file +class TestClass {} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/EmptyClassWithConstructor.java b/src/test/resources/input/javaCases/EmptyClassWithConstructor.java new file mode 100644 index 0000000..0907f20 --- /dev/null +++ b/src/test/resources/input/javaCases/EmptyClassWithConstructor.java @@ -0,0 +1,5 @@ +public class TestClass { + public TestClass() { + + } +} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/Field.java b/src/test/resources/input/javaCases/Field.java new file mode 100644 index 0000000..082ad8b --- /dev/null +++ b/src/test/resources/input/javaCases/Field.java @@ -0,0 +1,3 @@ +public class TestClass { + int a; +} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/FieldWithAccessModifier.java b/src/test/resources/input/javaCases/FieldWithAccessModifier.java new file mode 100644 index 0000000..7cd041f --- /dev/null +++ b/src/test/resources/input/javaCases/FieldWithAccessModifier.java @@ -0,0 +1,3 @@ +public class TestClass { + public int a; +} \ No newline at end of file diff --git a/src/test/resources/input/javaCases/MultipleClasses.java b/src/test/resources/input/javaCases/MultipleClasses.java index 8f8e5c7..a560484 100644 --- a/src/test/resources/input/javaCases/MultipleClasses.java +++ b/src/test/resources/input/javaCases/MultipleClasses.java @@ -1,7 +1,3 @@ -class EmptyClass1{ +class TestClass1 {} -} - -class EmptyClass2{ - -} \ No newline at end of file +class TestClass2{} \ No newline at end of file From 2ae098898633803482f9610c3b521aa461759885 Mon Sep 17 00:00:00 2001 From: i22035 Date: Sun, 30 Jun 2024 22:17:52 +0200 Subject: [PATCH 2/2] Changed For Loop Structure --- src/main/java/parser/astBuilder/ASTBuilder.java | 11 +++++++++-- src/test/java/parser/AstBuilderTest.java | 14 ++++++++++++++ src/test/resources/input/javaCases/Increment.java | 12 ++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/input/javaCases/Increment.java diff --git a/src/main/java/parser/astBuilder/ASTBuilder.java b/src/main/java/parser/astBuilder/ASTBuilder.java index 55750a5..fcabdfa 100644 --- a/src/main/java/parser/astBuilder/ASTBuilder.java +++ b/src/main/java/parser/astBuilder/ASTBuilder.java @@ -207,10 +207,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor { crement = (IStatementNode) visit(ctx.statementExpression(i)); } - BlockNode forBlock = (BlockNode) visit(ctx.blockStatement()); + BlockNode forBlock = new BlockNode(); + + BlockNode forStatements = (BlockNode) visit(ctx.blockStatement()); + if(forStatements != null) { + forBlock.addStatement((IStatementNode) forStatements); + } if(crement != null){ - forBlock.addStatement((crement)); + BlockNode forCrement = new BlockNode(); + forCrement.addStatement((crement)); + forBlock.addStatement(forCrement); } WhileNode While = new WhileNode(condition, forBlock); diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index 38a601c..f239a41 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -200,6 +200,20 @@ class AstBuilderTest { //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } + //Noch nicht speziell Increment nur zum Development Testen per Debug + @Test + @DisplayName("Increment Test") + public void incrementTest(){ + ClassNode classNode = Helper.generateEmptyClass("TestClass"); + classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a")); + + ProgramNode expected = new ProgramNode(); + expected.addClass(classNode); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java"); + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + } + diff --git a/src/test/resources/input/javaCases/Increment.java b/src/test/resources/input/javaCases/Increment.java new file mode 100644 index 0000000..9560a9b --- /dev/null +++ b/src/test/resources/input/javaCases/Increment.java @@ -0,0 +1,12 @@ +public class Increment { + + public int test; + + public void increment(int p) { + test = p++; + + for(int i = 1; i<=10, i++) { + int a = 5; + } + } +}