Test update

This commit is contained in:
Julian Murek 2024-06-20 13:28:16 +02:00
parent 6585e67273
commit dcb564bd0d
3 changed files with 89 additions and 22 deletions

View File

@ -3,24 +3,25 @@ import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import org.junit.Test;
import java.io.File;
public class testAll {
AstComparer astComparer;
public testAll()
{
this.astComparer = new AstComparer("src/test/resources");
this.astComparer = new AstComparer("test/resources");
}
@Test
public void TestEmptyClass()
public void TestEmptyClass(){
Program ast = emptyClassAST.getEmptyProgramm();
String pathToCode = "basicClasses/emptyClass.java";
testAst(ast, pathToCode);
}
public void testAst(Program ast, String pathToCode)
{
System.out.println("Current working directory: " + new File(".").getAbsolutePath());
Program testEmptyClassAST = emptyClassAST.getEmptyProgramm();
try {
astComparer.astComparison("basicClasses/emptyClass.java", testEmptyClassAST);
astComparer.astComparison(pathToCode, ast);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -2,15 +2,82 @@ package Typecheck;
import ASTs.emptyClassAST;
import abstractSyntaxTree.Program;
import astGenerator.ASTGenerator;
import gen.DecafLexer;
import gen.DecafParser;
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.Test;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.fail;
public class TestAll {
@Test
public void TestEmptyClass() throws Exception {
Program emptyAst = emptyClassAST.getEmptyProgramm();
TypeChecker.assertTypeCheckResult(emptyAst, true);
public void testEmptyClass() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
testTypeCheck(emptyClassAst, true);
}
@Test
public void testEmptyClassWithConstructor(){
Program abstractSyntaxTree = getAst("test/resources/basicClasses/EmptyClassWithConstructor.java");
boolean expectedResult = true;
testTypeCheck(abstractSyntaxTree, expectedResult);
}
@Test
public void testFourClasses(){
Program abstractSyntaxTree = getAst("test/resources/basicClasses/FourClasses.java");
boolean expectedResult = true;
testTypeCheck(abstractSyntaxTree, expectedResult);
}
@Test
public void testFail(){
Program assignWrongType = getAst("test/resources/FailTests/AssignWrongType.java");
testTypeCheck(assignWrongType, false);
}
public void testTypeCheck(Program abstractSyntaxTree, boolean expectedResult){
try {
TypeChecker.assertTypeCheckResult(abstractSyntaxTree, expectedResult);
} catch (Exception e) {
fail();
}
}
private Program getAst (String pathToCode){
String content = "";
try {
System.out.println("Classpath: " + Path.of(pathToCode));
content = Files.readString(Path.of(pathToCode));
} catch (IOException e) {
System.out.println("File not found!");
fail();
}
CharStream codeCharStream = CharStreams.fromString(content);
DecafLexer lexer = new DecafLexer(codeCharStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill();
DecafParser parser = new DecafParser(tokens);
ParseTree tree = parser.program();
ASTGenerator generator = new ASTGenerator();
Program abstractSyntaxTree = (Program) generator.visit(tree);
return abstractSyntaxTree;
}
}

View File

@ -106,10 +106,11 @@ Int: "int"
Identifier: "getX"
OpenRoundBracket: "("
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Return: "return"
This: "this"
Dot: "."
identifier: "x"
Identifier: "x"
Semicolon: ";"
ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}"
@ -130,17 +131,15 @@ OpenCurlyBracket: "{"
This: "this"
Dot: "."
Identifier: "test"
Assign: "new"
Assign: "="
New: "new"
Identifier: "Test"
OpenRoundBracket: "("
Identifier: "i"
ClosedRoundBracket: ")"
Semicolon: ";"
ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}"
Class: "class"
Identifier: "Test3"
OpenCurlyBracket: "{"
@ -154,11 +153,11 @@ Identifier: "y"
Semicolon: ";"
AccessModifierPublic: "public"
Identifier: "Test3"
OpenRoundBrackets: "("
OpenRoundBracket: "("
Int: "int"
Identifier: "i"
ClosedRoundBrackets: ")"
OpenCurlyBrackets: "{"
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
This: "this"
Dot: "."
Identifier: "x"
@ -168,9 +167,9 @@ Semicolon: ";"
ClosedCurlyBracket: "}"
AccessModifierPublic: "public"
Int: "int"
Identifier: "getC"
OpenRoundBrackets: "("
ClosedRoundBrackets: ")"
Identifier: "getX"
OpenRoundBracket: "("
ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Return: "return"
This: "this"