From f414e278bbee6f5733aede6bdcd61d077cfc966c Mon Sep 17 00:00:00 2001 From: Purplumbi504 Date: Tue, 2 Jul 2024 17:50:58 +0200 Subject: [PATCH] Adding Test Cases --- src/test/java/parser/AstBuilderTest.java | 51 ++++++++++++++----- .../input/javaCases/SelfReference.java | 4 +- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/test/java/parser/AstBuilderTest.java b/src/test/java/parser/AstBuilderTest.java index b163a2a..365f97c 100644 --- a/src/test/java/parser/AstBuilderTest.java +++ b/src/test/java/parser/AstBuilderTest.java @@ -14,29 +14,21 @@ import ast.members.MethodNode; import ast.parameters.ParameterNode; import ast.statementexpressions.AssignNode; import ast.statementexpressions.AssignableNode; +import ast.statementexpressions.NewDeclarationNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; +import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.BlockNode; -import ast.statements.IStatementNode; +import ast.statements.LocalVariableDeclarationNode; import ast.statements.ReturnNode; import ast.type.AccessModifierNode; import ast.type.EnumValueNode; import ast.type.ValueNode; import ast.type.type.BaseType; -import ast.type.type.ITypeNode; +import ast.type.type.ReferenceType; import ast.type.type.TypeEnum; -import com.fasterxml.jackson.annotation.JsonFormat; -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 org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import parser.astBuilder.ASTBuilder; -import parser.generated.SimpleJavaLexer; -import parser.generated.SimpleJavaParser; - -import java.io.IOException; -import java.lang.reflect.Member; import static org.assertj.core.api.Assertions.assertThat; @@ -343,7 +335,38 @@ class AstBuilderTest { @DisplayName("Self Reference Test") public void selfReferneceTest(){ - //assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); + ClassNode testClass = Helper.generateEmptyClass("TestClass"); + + MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("TestClass"),"testClass"); + + BlockNode testMethod1Block = new BlockNode(); + testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(new TargetNode(true), "testMethod2")))); + MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block); + + BlockNode testMethod2Block = new BlockNode(); + testMethod2Block.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE,"1")))); + MethodNode testMethod2 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod2", testMethod2Block); + + BlockNode testMethod3Block = new BlockNode(); + testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("TestClass"),"testClass1", "=", new UnaryNode(new NewDeclarationNode("TestClass")))); // Assing einfach "=" ? + MemberAccessNode methodAccess = new MemberAccessNode(false); + methodAccess.addIdentifier("testClass1"); + methodAccess.addIdentifier("testClass"); + TargetNode methodTarget = new TargetNode(methodAccess); + testMethod3Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(methodTarget,"testMethod1")))); + MethodNode testMethod3 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod3", testMethod3Block); + + testClass.addMember(testClassObject); + testClass.addMember(testMethod1); + testClass.addMember(testMethod2); + testClass.addMember(testMethod3); + + ProgramNode expected = new ProgramNode(); + expected.addClass(testClass); + + ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/SelfReference.java"); + + assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); } @Test diff --git a/src/test/resources/input/javaCases/SelfReference.java b/src/test/resources/input/javaCases/SelfReference.java index cbe6e25..a1709f5 100644 --- a/src/test/resources/input/javaCases/SelfReference.java +++ b/src/test/resources/input/javaCases/SelfReference.java @@ -3,14 +3,14 @@ class TestClass{ TestClass testClass; int testMethod1() { - return this.testMethod2() + return this.testMethod2(); } int testMethod2() { return 1; } - int testMehtod3(){ + int testMethod3(){ TestClass testClass1 = new TestClass(); return testClass1.testClass.testMethod1(); }