Added ASTBuilder Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
dccd34db73
commit
dfded3e592
@ -13,11 +13,6 @@ public class ConstructorNode extends MethodNode {
|
||||
public List<ParameterNode> 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;
|
||||
|
@ -63,7 +63,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
}
|
||||
|
||||
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<ASTNode> {
|
||||
|
||||
@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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
47
src/test/java/parser/Helper.java
Normal file
47
src/test/java/parser/Helper.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -1 +1 @@
|
||||
class EmptyClass { }
|
||||
class TestClass {}
|
@ -0,0 +1,5 @@
|
||||
public class TestClass {
|
||||
public TestClass() {
|
||||
|
||||
}
|
||||
}
|
3
src/test/resources/input/javaCases/Field.java
Normal file
3
src/test/resources/input/javaCases/Field.java
Normal file
@ -0,0 +1,3 @@
|
||||
public class TestClass {
|
||||
int a;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
public class TestClass {
|
||||
public int a;
|
||||
}
|
@ -1,7 +1,3 @@
|
||||
class EmptyClass1{
|
||||
class TestClass1 {}
|
||||
|
||||
}
|
||||
|
||||
class EmptyClass2{
|
||||
|
||||
}
|
||||
class TestClass2{}
|
Loading…
Reference in New Issue
Block a user