Compare commits

..

No commits in common. "e7d4a83a1d611dd3f931369ad1bdeb2e9c812eb7" and "c8c12e4d9a9fb7af057f5c69dfe728629dafddb7" have entirely different histories.

3 changed files with 22 additions and 89 deletions

View File

@ -3,25 +3,24 @@ 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("test/resources");
this.astComparer = new AstComparer("src/test/resources");
}
@Test
public void TestEmptyClass(){
Program ast = emptyClassAST.getEmptyProgramm();
String pathToCode = "basicClasses/emptyClass.java";
testAst(ast, pathToCode);
}
public void testAst(Program ast, String pathToCode)
public void TestEmptyClass()
{
System.out.println("Current working directory: " + new File(".").getAbsolutePath());
Program testEmptyClassAST = emptyClassAST.getEmptyProgramm();
try {
astComparer.astComparison(pathToCode, ast);
astComparer.astComparison("basicClasses/emptyClass.java", testEmptyClassAST);
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -2,82 +2,15 @@ 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() {
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;
public void TestEmptyClass() throws Exception {
Program emptyAst = emptyClassAST.getEmptyProgramm();
TypeChecker.assertTypeCheckResult(emptyAst, true);
}
}

View File

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