Compare commits
No commits in common. "e7d4a83a1d611dd3f931369ad1bdeb2e9c812eb7" and "c8c12e4d9a9fb7af057f5c69dfe728629dafddb7" have entirely different histories.
e7d4a83a1d
...
c8c12e4d9a
@ -3,25 +3,24 @@ 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("test/resources");
|
this.astComparer = new AstComparer("src/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(pathToCode, ast);
|
astComparer.astComparison("basicClasses/emptyClass.java", testEmptyClassAST);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
@ -2,82 +2,15 @@ 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() {
|
public void TestEmptyClass() throws Exception {
|
||||||
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
|
Program emptyAst = emptyClassAST.getEmptyProgramm();
|
||||||
testTypeCheck(emptyClassAst, true);
|
TypeChecker.assertTypeCheckResult(emptyAst, 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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,11 +106,10 @@ 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: "}"
|
||||||
@ -131,15 +130,17 @@ OpenCurlyBracket: "{"
|
|||||||
This: "this"
|
This: "this"
|
||||||
Dot: "."
|
Dot: "."
|
||||||
Identifier: "test"
|
Identifier: "test"
|
||||||
Assign: "="
|
Assign: "new"
|
||||||
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: "{"
|
||||||
@ -153,11 +154,11 @@ Identifier: "y"
|
|||||||
Semicolon: ";"
|
Semicolon: ";"
|
||||||
AccessModifierPublic: "public"
|
AccessModifierPublic: "public"
|
||||||
Identifier: "Test3"
|
Identifier: "Test3"
|
||||||
OpenRoundBracket: "("
|
OpenRoundBrackets: "("
|
||||||
Int: "int"
|
Int: "int"
|
||||||
Identifier: "i"
|
Identifier: "i"
|
||||||
ClosedRoundBracket: ")"
|
ClosedRoundBrackets: ")"
|
||||||
OpenCurlyBracket: "{"
|
OpenCurlyBrackets: "{"
|
||||||
This: "this"
|
This: "this"
|
||||||
Dot: "."
|
Dot: "."
|
||||||
Identifier: "x"
|
Identifier: "x"
|
||||||
@ -167,9 +168,9 @@ Semicolon: ";"
|
|||||||
ClosedCurlyBracket: "}"
|
ClosedCurlyBracket: "}"
|
||||||
AccessModifierPublic: "public"
|
AccessModifierPublic: "public"
|
||||||
Int: "int"
|
Int: "int"
|
||||||
Identifier: "getX"
|
Identifier: "getC"
|
||||||
OpenRoundBracket: "("
|
OpenRoundBrackets: "("
|
||||||
ClosedRoundBracket: ")"
|
ClosedRoundBrackets: ")"
|
||||||
OpenCurlyBracket: "{"
|
OpenCurlyBracket: "{"
|
||||||
Return: "return"
|
Return: "return"
|
||||||
This: "this"
|
This: "this"
|
||||||
|
Loading…
Reference in New Issue
Block a user