Test update

This commit is contained in:
Julian Murek 2024-07-01 13:13:07 +02:00
parent 21ece58cc9
commit 94ea539fab
5 changed files with 203 additions and 21 deletions

View File

@ -1,5 +1,5 @@
package AST;
import ASTs.emptyClassAST;
import ASTs.*;
import abstractSyntaxTree.Program;
import org.junit.Test;
@ -8,7 +8,7 @@ public class testAll {
AstComparer astComparer;
public testAll()
{
this.astComparer = new AstComparer("src/test/resources");
this.astComparer = new AstComparer("test/resources");
}
@Test
@ -20,10 +20,24 @@ public class testAll {
@Test
public void testEmptyClassWithConstructor(){
Program ast = emptyClassAST.getEmptyProgramm();
Program ast = EmptyClassWithConstructorAST.getProgram();
String pathToCode = "basicClasses/emptyClassWithConstructor.java";
testAst(ast, pathToCode);
}
@Test
public void testFakultaet(){
Program ast = FakultaetAST.getProgram();
String pathToCode = "basicClasses/Fakultaet.java";
testAst(ast, pathToCode);
}
@Test
public void testAssignWrongType(){
Program ast = AssignWrongTypeAST.getProgram();
String pathToCode = "failTests/AssignWrongType.java";
testAst(ast, pathToCode);
}
public void testAst(Program ast, String pathToCode)
{

View File

@ -0,0 +1,59 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BooleanConstantExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.Statement.LocalVarDecl;
import abstractSyntaxTree.Statement.ReturnStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class AssignWrongTypeAST {
public static Program getProgram(){
// TestMethod
//Expressions
// 0
IntConstantExpression intConstantExpression0 = new IntConstantExpression(1);
LocalVarDecl localVarDecl = new LocalVarDecl("int", "x", intConstantExpression0);
//1
LocalVarIdentifier localVarIdentifier0 = new LocalVarIdentifier("x");
BooleanConstantExpression booleanConstantExpression0 = new BooleanConstantExpression(true);
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", localVarIdentifier0, booleanConstantExpression0);
//2
ReturnStatement returnStatement0 = new ReturnStatement(localVarIdentifier0);
String classThatContainsMethod = "AssignWrongType";
String nameMethod0 = "test";
String returnType = "int";
ParameterList parameterList = new ParameterList(new ArrayList<>());
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(localVarDecl);
iStatementList.add(assignStatementExpression0);
iStatementList.add(returnStatement0);
BlockStatement codeBlock = new BlockStatement(iStatementList, null);
MethodDecl test = new MethodDecl(classThatContainsMethod, returnType, nameMethod0, parameterList, codeBlock);
String name = "AssignWrongType";
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(test);
boolean hasMain = false;
RefType assignWrongType = new RefType(name, fieldDeclList, methodDeclList, hasMain);
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(assignWrongType);
Program program = new Program(refTypeList);
return program;
}
}

View File

@ -3,7 +3,6 @@ package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
@ -12,23 +11,21 @@ import abstractSyntaxTree.Statement.IStatement;
import java.util.ArrayList;
import java.util.List;
public class emptyClassWithConstructorAST extends Program {
public emptyClassWithConstructorAST()
{
super(staticClasses());
}
private static List<RefType> staticClasses() {
public class EmptyClassWithConstructorAST {
public static Program getProgram() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
List<IStatement> emptyIStatementList = new ArrayList<>();
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, "void");
MethodDecl constructor = new MethodDecl("emptyClassWithConstructor", "null", "emptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, null);
MethodDecl constructor = new MethodDecl("EmptyClassWithConstructor", null, "EmptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(constructor);
RefType emptyClass = new RefType("emptyClass", fieldDeclList, methodDeclList, false);
RefType emptyClass = new RefType("EmptyClassWithConstructor", fieldDeclList, methodDeclList, false);
List<RefType> classes = new ArrayList<>();
classes.add(emptyClass);
return classes;
Program program = new Program(classes);
return program;
}
}

View File

@ -0,0 +1,106 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.*;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class FakultaetAST {
public static Program getProgram() {
String name = "Fakultaet";
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
methodDeclList.add(method1());
List<RefType> classes = new ArrayList<>();
RefType fakultaet = new RefType(name, fieldDeclList, methodDeclList, true);
classes.add(fakultaet);
Program program = new Program(classes);
return program;
}
private static MethodDecl method0() {
String classThatContainsMethod = "Fakultaet";
String name = "main";
ParameterList parameterList = new ParameterList(new ArrayList<>());
String returnType = "void";
List<IStatement> iStmtList = new ArrayList<>();
BlockStatement blockStatement = new BlockStatement(iStmtList, null);
return new MethodDecl(classThatContainsMethod, returnType, name, parameterList, blockStatement);
}
private static MethodDecl method1() {
String classThatContainsMethod = "Fakultaet";
String name = "fak";
// Parameters
Parameter param0 = new Parameter("int", "number");
List<Parameter> paramList = new ArrayList<>() {{
add(param0);
}};
ParameterList parameterListObj = new ParameterList(paramList);
String returnType = "int";
// Block Statement
List<IStatement> iStmtList0 = new ArrayList<>();
// If Statament
//condition
LocalVarIdentifier binExprIdentifier = new LocalVarIdentifier("number");
IntConstantExpression binExprIntConst = new IntConstantExpression(0);
BinaryExpression binExpr = new BinaryExpression("<", binExprIdentifier, binExprIntConst);
IntConstantExpression return0Expr0 = new IntConstantExpression(1);
ReturnStatement returnStatement0 = new ReturnStatement(return0Expr0);
List<IStatement> iStmtList1 = new ArrayList<>();
iStmtList1.add(returnStatement0);
BlockStatement ifStmt = new BlockStatement(iStmtList1, null);
IfStatement ifStatement = new IfStatement(binExpr, ifStmt);
iStmtList0.add(ifStatement);
// Expression 1
IntConstantExpression intConst0 = new IntConstantExpression(1);
LocalVarDecl localVarExpr0 = new LocalVarDecl("int", "factorial", intConst0);
iStmtList0.add(localVarExpr0);
// Expression 2
IntConstantExpression intConst1 = new IntConstantExpression(0);
LocalVarDecl localVarExpr1 = new LocalVarDecl("int", "i", intConst1);
iStmtList0.add(localVarExpr1);
//While Statement
BinaryExpression whileCondition = new BinaryExpression("<", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
BinaryExpression whileBinExpr = new BinaryExpression("*", new LocalVarIdentifier("factorial"), new LocalVarIdentifier("i"));
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", new LocalVarIdentifier("factorial"), whileBinExpr);
List<IStatement> whileBlockStmts = new ArrayList<>();
whileBlockStmts.add(assignStatementExpression0);
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, null);
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);
iStmtList0.add(whileStatement0);
// Return Statement
LocalVarIdentifier returnIdentifier = new LocalVarIdentifier("factorial");
ReturnStatement returnStatement = new ReturnStatement(returnIdentifier);
iStmtList0.add(returnStatement);
BlockStatement blockStatement = new BlockStatement(iStmtList0, null);
return new MethodDecl(classThatContainsMethod, returnType, name, parameterListObj, blockStatement);
}
}

View File

@ -1,6 +1,6 @@
package Typecheck;
import ASTs.emptyClassAST;
import ASTs.*;
import abstractSyntaxTree.Program;
import astGenerator.ASTGenerator;
import gen.DecafLexer;
@ -12,7 +12,6 @@ import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
@ -23,14 +22,14 @@ public class TestAll {
@Test
public void testEmptyClass() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
testTypeCheck(emptyClassAst, true);
testTypeCheck(emptyClassAst, false);
}
@Test
public void testEmptyClassWithConstructor(){
Program abstractSyntaxTree = getAst("test/resources/basicClasses/EmptyClassWithConstructor.java");
boolean expectedResult = true;
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram();
boolean expectedResult = false;
testTypeCheck(abstractSyntaxTree, expectedResult);
}
@ -42,11 +41,18 @@ public class TestAll {
}
@Test
public void testFail(){
Program assignWrongType = getAst("test/resources/FailTests/AssignWrongType.java");
public void testAssignWrongType(){
Program assignWrongType = AssignWrongTypeAST.getProgram();
testTypeCheck(assignWrongType, false);
}
@Test
public void testFakultaet(){
Program assignWrongType = FakultaetAST.getProgram();
testTypeCheck(assignWrongType, true);
}
public void testTypeCheck(Program abstractSyntaxTree, boolean expectedResult){
try {
TypeChecker.assertTypeCheckResult(abstractSyntaxTree, expectedResult);