diff --git a/src/test/java/semantic/EndToTypedAstTest.java b/src/test/java/semantic/EndToTypedAstTest.java index a33b9d4..e25aec6 100644 --- a/src/test/java/semantic/EndToTypedAstTest.java +++ b/src/test/java/semantic/EndToTypedAstTest.java @@ -26,6 +26,10 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; +import semantic.exceptions.AlreadyDeclaredException; +import semantic.exceptions.MultipleReturnTypes; +import semantic.exceptions.NotDeclaredException; +import semantic.exceptions.TypeMismatchException; import static org.junit.jupiter.api.Assertions.*; @@ -152,6 +156,130 @@ public class EndToTypedAstTest { } } + /* + @Test + public void notDeclared() { + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/NotDeclared.java"); + + SemanticAnalyzer.generateTast(tast); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof NotDeclaredException)); + }*/ + + @Test + public void typeMismatchTest() { + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/TypeMismatchIntBool.java"); + + SemanticAnalyzer.generateTast(tast); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException)); + } + + @Test + public void parameterAlreadyDecleared() { + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java"); + + SemanticAnalyzer.generateTast(tast); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof AlreadyDeclaredException)); + } + + @Test + public void fieldAlreadyDecleared(){ + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/FieldAlreadyDecleared.java"); + SemanticAnalyzer.generateTast(tast); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof AlreadyDeclaredException)); + + } + + @Test + public void typeMismatchRefType(){ + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/TypeMismatchRefType.java"); + + SemanticAnalyzer.generateTast(tast); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException)); + } + + @Test + public void correctRetType(){ + + CharStream codeCharStream = null; + try { + codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/semantic/endToTAST/CorrectRetType.java")); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + + SimpleJavaParser parser = new SimpleJavaParser(tokenStream); + ParseTree parseTree = parser.program(); + + ASTBuilder astBuilder = new ASTBuilder(); + ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); + + ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree); + + assertTrue(SemanticAnalyzer.errors.isEmpty()); + + } + + @Test + public void retTypeMismatch(){ + + CharStream codeCharStream = null; + try { + codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/semantic/endToTAST/retTypeMismatch.java")); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + + SimpleJavaParser parser = new SimpleJavaParser(tokenStream); + ParseTree parseTree = parser.program(); + + ASTBuilder astBuilder = new ASTBuilder(); + ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); + + ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertInstanceOf(TypeMismatchException.class, SemanticAnalyzer.errors.getFirst()); + + } + + @Test + public void multipleRetType(){ + + CharStream codeCharStream = null; + try { + codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/semantic/endToTAST/MultipleRetTypes.java")); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + + SimpleJavaParser parser = new SimpleJavaParser(tokenStream); + ParseTree parseTree = parser.program(); + + ASTBuilder astBuilder = new ASTBuilder(); + ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); + + ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree); + + assertFalse(SemanticAnalyzer.errors.isEmpty()); + assertInstanceOf(MultipleReturnTypes.class, SemanticAnalyzer.errors.getFirst()); + + } // ------------------ Helpers ------------------ diff --git a/src/test/java/semantic/SemanticHelper.java b/src/test/java/semantic/SemanticHelper.java new file mode 100644 index 0000000..7b0c0ed --- /dev/null +++ b/src/test/java/semantic/SemanticHelper.java @@ -0,0 +1,32 @@ +package semantic; + +import ast.ASTNode; +import ast.ProgramNode; +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 parser.astBuilder.ASTBuilder; +import parser.generated.SimpleJavaLexer; +import parser.generated.SimpleJavaParser; + +import java.io.IOException; + +public class SemanticHelper { + public static ASTNode generateTypedASTFrom(String filePath) { + CharStream testFile = null; + try { + testFile = CharStreams.fromFileName(filePath); + } catch (IOException e) { + throw new RuntimeException(e); + } + SimpleJavaLexer lexer = new SimpleJavaLexer(testFile); + CommonTokenStream tokenStream = new CommonTokenStream(lexer); + tokenStream.fill(); + SimpleJavaParser parser = new SimpleJavaParser(tokenStream); + ParseTree parseTree = parser.program(); + ASTBuilder astBuilder = new ASTBuilder(); + + return (ProgramNode) astBuilder.visit(parseTree); + } +} diff --git a/src/test/resources/input/johnsTests/FieldAlreadyDecleared.java b/src/test/resources/input/johnsTests/FieldAlreadyDecleared.java new file mode 100644 index 0000000..2889707 --- /dev/null +++ b/src/test/resources/input/johnsTests/FieldAlreadyDecleared.java @@ -0,0 +1,4 @@ +public class FieldAlreadyDecleared { + public int a; + public int a; +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/NotDeclared.java b/src/test/resources/input/johnsTests/NotDeclared.java new file mode 100644 index 0000000..45f3333 --- /dev/null +++ b/src/test/resources/input/johnsTests/NotDeclared.java @@ -0,0 +1,5 @@ +public class NotDeclared { + public void Method() { + i = 10; + } +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java b/src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java new file mode 100644 index 0000000..b79c10a --- /dev/null +++ b/src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java @@ -0,0 +1,3 @@ +public class ParameterAlreadyDecleared { + public void Method(int a, int a) {} +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/TypeMismatchIntBool.java b/src/test/resources/input/johnsTests/TypeMismatchIntBool.java new file mode 100644 index 0000000..cc74248 --- /dev/null +++ b/src/test/resources/input/johnsTests/TypeMismatchIntBool.java @@ -0,0 +1,6 @@ +public class TypeMismatchIntBool { + public void Method() { + int intVariable; + intVariable = true; + } +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/TypeMismatchRefType.java b/src/test/resources/input/johnsTests/TypeMismatchRefType.java new file mode 100644 index 0000000..deacce3 --- /dev/null +++ b/src/test/resources/input/johnsTests/TypeMismatchRefType.java @@ -0,0 +1,13 @@ +public class TypeMismatchRefType { + public static int Methode(Class1 class1, Class2 class2){ + class1 = class2; + } +} + +public class Class1{ + public int a; +} + +public class Class2{ + public boolean a; +} \ No newline at end of file diff --git a/src/test/resources/input/typedAstFeatureTests/CorrectTest.java b/src/test/resources/input/typedAstFeatureTests/CorrectTest.java index 2a7e882..53a8f3c 100644 --- a/src/test/resources/input/typedAstFeatureTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeatureTests/CorrectTest.java @@ -1,7 +1,13 @@ +public class Klasse1 { + public int test; - public class Run { - public static void main(String[] args) { -// Test t = new Test(); -// System.out.println(t.test()); - } + public int test1() { + test = 5; + return 1; } + + public void test2() { + int testInt; + testInt = this.test1(); + } +} \ No newline at end of file