wrote tests for ScannerAndParser and more

This commit is contained in:
JonathanFleischmann 2024-05-09 00:10:23 +02:00
parent bbfdbd51dc
commit 4d58a4b1f2
13 changed files with 363 additions and 48 deletions

21
pom.xml
View File

@ -55,6 +55,27 @@
<version>4.13.1</version>
</dependency>
<!-- ANTLR end -->
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 end -->
</dependencies>
<build>

View File

@ -7,7 +7,6 @@ import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.typedclass.TypedClass;
import org.antlr.v4.runtime.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
@ -41,8 +40,18 @@ public class Compiler {
CommonTokenStream tokens = new CommonTokenStream(lexer);
DecafParser parser = new DecafParser(tokens);
DecafParser.ClassContext tree = parser.class_(); //Parsen
Class ast = ASTGenerator.generateAST(tree);
return ast;
return ASTGenerator.generateAST(tree);
}
public static Class generateASTFromFile(String sourcePath) {
ANTLRInputStream antlrInputStream;
try {
antlrInputStream = new ANTLRFileStream(sourcePath);
} catch (IOException e) {
System.out.println("Ungültiger Dateipfad D:");
throw new RuntimeException("Ungültiger Dateipfad D:");
}
return generateAST(antlrInputStream.toString());
}
public static TypedClass generateTypedASTFromAst(Class ast) {

View File

@ -111,7 +111,7 @@ public class AbstractSyntax_ClassWithConstructor {
"j"),
Operator.LT,
new FieldVarAccess(
false,
true,
null,
"x")
),

View File

@ -0,0 +1,128 @@
//public class ClassWithConstructorWithCodeInComments {
// int x;
// public ClassWithConstructorWithCodeInComments() {
// this.x = 10;
// int i;
// for (i = 0; i < 6; i = i + 1) {
// this.x = this.x * this.x;
//// int j;
//// for (j = 0; j < this.x; j += 1) {
//// this.x = this.x * this.x;
//// }
// }
// }
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorWithCodeInComments {
public static Class get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
Type.INT
)
);
List<Method> methods = List.of();
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithConstructorWithCodeInComments",
declarations,
methods,
constructors
);
}
private static List<Constructor> getConstructors() {
return List.of(getConstructor1());
}
private static Constructor getConstructor1() {
List<Parameter> parameters = List.of();
List<Declaration> localVariables = List.of(
new Declaration(
"i",
Type.INT
)
);
List<Statement> statementList = List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new IntLiteral(10)
),
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.LT,
new IntLiteral(6)
),
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new Binary(
new FieldVarAccess(
true,
null,
"x"),
Operator.MUL,
new FieldVarAccess(
true,
null,
"x")
)
)
)
)
)
);
Block block = new Block(
localVariables,
statementList
);
return new Constructor(
"ClassWithConstructorWithCodeInComments",
parameters,
block
);
}
}

View File

@ -1,6 +1,6 @@
//public class ClassWithConstructorWithParameters {
// int x;
// public classWithConstructorWithParameters(int startValue, int repetitions) {
// public ClassWithConstructorWithParameters(int startValue, int repetitions) {
// this.x = startValue;
// while (repetitions > 0) {
// int innerRepetitions;
@ -158,7 +158,7 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
)
);
return new Constructor(
"classWithConstructorWithParameters",
"ClassWithConstructorWithParameters",
parameters,
block
);

View File

@ -21,7 +21,7 @@ public class AbstractSyntax_ClassWithField {
List<Method> methods = List.of();
List<Constructor> constructors = List.of();
return new Class(
"ClassWithAssignment",
"ClassWithField",
declarations,
methods,
constructors

View File

@ -1,18 +0,0 @@
//class OnlyClass {
//}
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import java.util.List;
public class AbstractSyntax_OnlyClass {
public static Class get() {
return new Class(
"OnlyClass",
List.of(),
List.of(),
List.of()
);
}
}

View File

@ -0,0 +1,14 @@
public class ClassWithConstructorWithCodeInComments {
int x;
public ClassWithConstructorWithCodeInComments() {
this.x = 10;
int i;
for (i = 0; i < 6; i = i + 1) {
this.x = this.x * this.x;
// int j;
// for (j = 0; j < this.x; j += 1) {
// this.x = this.x * this.x;
// }
}
}
}

View File

@ -1,2 +0,0 @@
class OnlyClass {
}

View File

@ -0,0 +1,128 @@
//public class ClassWithConstructorWithCodeInComments {
// int x;
// public ClassWithConstructorWithCodeInComments() {
// this.x = 10;
// int i;
// for (i = 0; i < 6; i = i + 1) {
// this.x = this.x * this.x;
//// int j;
//// for (j = 0; j < this.x; j += 1) {
//// this.x = this.x * this.x;
//// }
// }
// }
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.*;
import de.maishai.typedast.Type;
import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructorWithCodeInComments {
// public static Class get() {
// List<Declaration> declarations = List.of(
// new Declaration(
// "x",
// Type.INT
// )
// );
// List<Method> methods = List.of();
// List<Constructor> constructors = getConstructors();
// return new Class(
// "ClassWithConstructorWithCodeInComments",
// declarations,
// methods,
// constructors
// );
// }
//
// private static List<Constructor> getConstructors() {
// return List.of(getConstructor1());
// }
//
// private static Constructor getConstructor1() {
// List<Parameter> parameters = List.of();
//
// List<Declaration> localVariables = List.of(
// new Declaration(
// "i",
// Type.INT
// )
// );
// List<Statement> statementList = List.of(
// new Assignment(
// new FieldVarAccess(
// true,
// null,
// "x"),
// new IntLiteral(10)
// ),
// new For(
// new Assignment(
// new FieldVarAccess(
// false,
// null,
// "i"),
// new IntLiteral(0)
// ),
// new Binary(
// new FieldVarAccess(
// false,
// null,
// "i"),
// Operator.LT,
// new IntLiteral(6)
// ),
// new Assignment(
// new FieldVarAccess(
// false,
// null,
// "i"),
// new Binary(
// new FieldVarAccess(
// false,
// null,
// "i"),
// Operator.ADD,
// new IntLiteral(1)
// )
// ),
// new Block(
// List.of(),
// List.of(
// new Assignment(
// new FieldVarAccess(
// true,
// null,
// "x"),
// new Binary(
// new FieldVarAccess(
// true,
// null,
// "x"),
// Operator.MUL,
// new FieldVarAccess(
// true,
// null,
// "x")
// )
// )
// )
// )
// )
//
// );
// Block block = new Block(
// localVariables,
// statementList
// );
//
// return new Constructor(
// "ClassWithConstructorWithCodeInComments",
// parameters,
// block
// );
// }
}

View File

@ -1,19 +0,0 @@
//class OnlyClass {
//}
import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAbstractSyntax_OnlyClass {
// public static TypedClass get() {
// TypedClass typedClass = new TypedClass();
// typedClass.setIsPublic(false);
// typedClass.setTypedId(new TypedId("OnlyClass"));
// typedClass.setTypedFields(List.of());
// typedClass.setTypedMethods(List.of());
// typedClass.setTypedMainMethod(null);
// typedClass.setTypedConstructors(List.of());
// return typedClass;
// }
}

View File

@ -1,5 +1,3 @@
public class E2ETests {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,56 @@
import de.maishai.Compiler;
import de.maishai.ast.records.Class;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScannerParserTests {
@Test
public void testPublicClass() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/PublicClass.java");
assertEquals(AbstractSyntax_PublicClass.get(), resultAst);
}
@Test
public void testClassWithField() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithField.java");
assertEquals(AbstractSyntax_ClassWithField.get(), resultAst);
}
@Test
public void testClassWithConstructor() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java");
assertEquals(AbstractSyntax_ClassWithConstructor.get(), resultAst);
}
@Test
public void testClassWithMethod() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethod.java");
assertEquals(AbstractSyntax_ClassWithMethod.get(), resultAst);
}
@Test
public void testClassWithConstructorWithCodeInComments() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithCodeInComments.java");
assertEquals(AbstractSyntax_ClassWithConstructorWithCodeInComments.get(), resultAst);
}
@Test
public void testClassWithConstructorWithParameters() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithParameters.java");
assertEquals(AbstractSyntax_ClassWithConstructorWithParameters.get(), resultAst);
}
@Test
public void testClassWithMethodAndField() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethodAndField.java");
assertEquals(AbstractSyntax_ClassWithMethodAndField.get(), resultAst);
}
// @Test
// public void testClassWithConstructorAndMethodCall() {
// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java");
// assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
// }
}