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 abstractSyntaxTree.Program;
import org.junit.Test; import org.junit.Test;
import java.io.File;
public class testAll { public class testAll {
AstComparer astComparer; AstComparer astComparer;
public testAll() public testAll()
{ {
this.astComparer = new AstComparer("src/test/resources"); this.astComparer = new AstComparer("test/resources");
} }
@Test @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 { try {
astComparer.astComparison("basicClasses/emptyClass.java", testEmptyClassAST); astComparer.astComparison(pathToCode, ast);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -2,15 +2,82 @@ package Typecheck;
import ASTs.emptyClassAST; import ASTs.emptyClassAST;
import abstractSyntaxTree.Program; 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 org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.fail;
public class TestAll { public class TestAll {
@Test @Test
public void TestEmptyClass() throws Exception { public void testEmptyClass() {
Program emptyAst = emptyClassAST.getEmptyProgramm(); Program emptyClassAst = emptyClassAST.getEmptyProgramm();
TypeChecker.assertTypeCheckResult(emptyAst, true); 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" Identifier: "getX"
OpenRoundBracket: "(" OpenRoundBracket: "("
ClosedRoundBracket: ")" ClosedRoundBracket: ")"
OpenCurlyBracket: "{"
Return: "return" Return: "return"
This: "this" This: "this"
Dot: "." Dot: "."
identifier: "x" Identifier: "x"
Semicolon: ";" Semicolon: ";"
ClosedCurlyBracket: "}" ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}" ClosedCurlyBracket: "}"
@ -130,17 +131,15 @@ OpenCurlyBracket: "{"
This: "this" This: "this"
Dot: "." Dot: "."
Identifier: "test" Identifier: "test"
Assign: "new" Assign: "="
New: "new"
Identifier: "Test" Identifier: "Test"
OpenRoundBracket: "(" OpenRoundBracket: "("
Identifier: "i" Identifier: "i"
ClosedRoundBracket: ")" ClosedRoundBracket: ")"
Semicolon: ";" Semicolon: ";"
ClosedCurlyBracket: "}" ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}"
Class: "class" Class: "class"
Identifier: "Test3" Identifier: "Test3"
OpenCurlyBracket: "{" OpenCurlyBracket: "{"
@ -154,11 +153,11 @@ Identifier: "y"
Semicolon: ";" Semicolon: ";"
AccessModifierPublic: "public" AccessModifierPublic: "public"
Identifier: "Test3" Identifier: "Test3"
OpenRoundBrackets: "(" OpenRoundBracket: "("
Int: "int" Int: "int"
Identifier: "i" Identifier: "i"
ClosedRoundBrackets: ")" ClosedRoundBracket: ")"
OpenCurlyBrackets: "{" OpenCurlyBracket: "{"
This: "this" This: "this"
Dot: "." Dot: "."
Identifier: "x" Identifier: "x"
@ -168,9 +167,9 @@ Semicolon: ";"
ClosedCurlyBracket: "}" ClosedCurlyBracket: "}"
AccessModifierPublic: "public" AccessModifierPublic: "public"
Int: "int" Int: "int"
Identifier: "getC" Identifier: "getX"
OpenRoundBrackets: "(" OpenRoundBracket: "("
ClosedRoundBrackets: ")" ClosedRoundBracket: ")"
OpenCurlyBracket: "{" OpenCurlyBracket: "{"
Return: "return" Return: "return"
This: "this" This: "this"