diff --git a/src/test/java/semantic/EndToTypedAstTest.java b/src/test/java/semantic/EndToTypedAstTest.java index 6917d55..50bdfd2 100644 --- a/src/test/java/semantic/EndToTypedAstTest.java +++ b/src/test/java/semantic/EndToTypedAstTest.java @@ -210,78 +210,32 @@ public class EndToTypedAstTest { @Test public void correctRetType(){ + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/CorrectRetType.java"); - 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); + SemanticAnalyzer.generateTast(tast); assertTrue(SemanticAnalyzer.errors.isEmpty()); - } @Test public void retTypeMismatch(){ + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/RetTypeMismatch.java"); - 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); + SemanticAnalyzer.generateTast(tast); assertFalse(SemanticAnalyzer.errors.isEmpty()); - assertInstanceOf(TypeMismatchException.class, SemanticAnalyzer.errors.getFirst()); - + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException)); } @Test public void multipleRetType(){ + ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/MultipleRetTypes.java"); - 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); + SemanticAnalyzer.generateTast(tast); assertFalse(SemanticAnalyzer.errors.isEmpty()); - assertInstanceOf(MultipleReturnTypes.class, SemanticAnalyzer.errors.getFirst()); - + assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof MultipleReturnTypes)); } - // ------------------ Helpers ------------------ /** diff --git a/src/test/resources/input/singleFeatureSemanticTests/CorrectRetType.java b/src/test/resources/input/singleFeatureSemanticTests/CorrectRetType.java new file mode 100644 index 0000000..4c0a9f9 --- /dev/null +++ b/src/test/resources/input/singleFeatureSemanticTests/CorrectRetType.java @@ -0,0 +1,5 @@ +public class CorrectRetType { + public static int testMethod(int x){ + return x; + } +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/FieldAlreadyDecleared.java b/src/test/resources/input/singleFeatureSemanticTests/FieldAlreadyDecleared.java similarity index 100% rename from src/test/resources/input/johnsTests/FieldAlreadyDecleared.java rename to src/test/resources/input/singleFeatureSemanticTests/FieldAlreadyDecleared.java diff --git a/src/test/resources/input/johnsTests/FieldTests.java b/src/test/resources/input/singleFeatureSemanticTests/FieldTests.java similarity index 100% rename from src/test/resources/input/johnsTests/FieldTests.java rename to src/test/resources/input/singleFeatureSemanticTests/FieldTests.java diff --git a/src/test/resources/input/singleFeatureSemanticTests/MultipleRetTypes.java b/src/test/resources/input/singleFeatureSemanticTests/MultipleRetTypes.java new file mode 100644 index 0000000..1492d28 --- /dev/null +++ b/src/test/resources/input/singleFeatureSemanticTests/MultipleRetTypes.java @@ -0,0 +1,6 @@ +public class MultipleRetTypes { + public static int testMethod(int x, char c){ + return x; + return c; + } +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/NotDeclared.java b/src/test/resources/input/singleFeatureSemanticTests/NotDeclared.java similarity index 100% rename from src/test/resources/input/johnsTests/NotDeclared.java rename to src/test/resources/input/singleFeatureSemanticTests/NotDeclared.java diff --git a/src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java b/src/test/resources/input/singleFeatureSemanticTests/ParameterAlreadyDecleared.java similarity index 100% rename from src/test/resources/input/johnsTests/ParameterAlreadyDecleared.java rename to src/test/resources/input/singleFeatureSemanticTests/ParameterAlreadyDecleared.java diff --git a/src/test/resources/input/singleFeatureSemanticTests/RetTypeMismatch.java b/src/test/resources/input/singleFeatureSemanticTests/RetTypeMismatch.java new file mode 100644 index 0000000..994d51d --- /dev/null +++ b/src/test/resources/input/singleFeatureSemanticTests/RetTypeMismatch.java @@ -0,0 +1,5 @@ +public class Example { + public static int testMethod(char x){ + return x; + } +} \ No newline at end of file diff --git a/src/test/resources/input/johnsTests/TypeMismatchIntBool.java b/src/test/resources/input/singleFeatureSemanticTests/TypeMismatchIntBool.java similarity index 100% rename from src/test/resources/input/johnsTests/TypeMismatchIntBool.java rename to src/test/resources/input/singleFeatureSemanticTests/TypeMismatchIntBool.java diff --git a/src/test/resources/input/johnsTests/TypeMismatchRefType.java b/src/test/resources/input/singleFeatureSemanticTests/TypeMismatchRefType.java similarity index 100% rename from src/test/resources/input/johnsTests/TypeMismatchRefType.java rename to src/test/resources/input/singleFeatureSemanticTests/TypeMismatchRefType.java