From 4c73ae678628f65739b64a6553120db3769eab00 Mon Sep 17 00:00:00 2001 From: KingJulian Date: Thu, 9 May 2024 18:33:33 +0200 Subject: [PATCH] Updatet Tests for Token-Generation --- src/test/java/TestSuite.java | 10 - src/test/java/Tokens/JavaLexerTest.java | 60 +-- .../EmptyClassWithConstructor.java | 4 +- .../EmptyClassWithConstructor.tokens | 21 +- .../resources/basicClasses/FourClasses.tokens | 416 +++++++++--------- .../resources/basicClasses/emptyClass.tokens | 10 +- .../basicClasses/publicEmptyClass.tokens | 12 +- 7 files changed, 268 insertions(+), 265 deletions(-) delete mode 100644 src/test/java/TestSuite.java diff --git a/src/test/java/TestSuite.java b/src/test/java/TestSuite.java deleted file mode 100644 index 82821d8..0000000 --- a/src/test/java/TestSuite.java +++ /dev/null @@ -1,10 +0,0 @@ -import org.junit.platform.suite.api.SelectClasses; -import org.junit.platform.suite.api.Suite; -import org.junit.platform.suite.api.SuiteDisplayName; - -@SelectClasses({AST.TestRunner.class, TAST.TestRunner.class, CodeGen.TestRunner.class, All.TestRunner.class}) - -@Suite -@SuiteDisplayName("House of Compiler") -public class TestSuite { -} diff --git a/src/test/java/Tokens/JavaLexerTest.java b/src/test/java/Tokens/JavaLexerTest.java index edff369..12c5ea6 100644 --- a/src/test/java/Tokens/JavaLexerTest.java +++ b/src/test/java/Tokens/JavaLexerTest.java @@ -1,42 +1,52 @@ package Tokens; -import org.antlr.v4.runtime.*; -import java.io.IOException; +import gen.DecafLexer; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.Token; +import org.junit.Test; + import java.nio.file.Files; import java.nio.file.Paths; -import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; public class JavaLexerTest { + // Method to test tokens + private static String BASE_DIR; + public JavaLexerTest(String base_directory){ + BASE_DIR = base_directory + "/"; + } - public void testFileTokens(String sourcePath, String tokensPath) throws IOException { - String content = new String(Files.readAllBytes(Paths.get(sourcePath))); - JavaLexer lexer = new JavaLexer(CharStreams.fromString(content)); + public void testTokens(String sourceFilePath, String tokensFilePath) throws Exception { + // Read the source file + String content = new String(Files.readAllBytes(Paths.get(BASE_DIR + sourceFilePath))); + CharStream codeCharStream = CharStreams.fromString(content); + + // Tokenize the source + DecafLexer lexer = new DecafLexer(codeCharStream); CommonTokenStream tokens = new CommonTokenStream(lexer); - tokens.fill(); // Load all tokens + tokens.fill(); + List tokenList = tokens.getTokens(); - List expectedTokenTypes = loadExpectedTokens(tokensPath); - List actualTokenTypes = tokens.getTokens().stream() - .map(Token::getType) - .collect(Collectors.toList()); + // Read the expected tokens + List expectedTokens = Files.readAllLines(Paths.get(BASE_DIR + tokensFilePath)); - assertEquals(expectedTokenTypes, actualTokenTypes.subList(0, actualTokenTypes.size())); - } + /* (int i = 0; i < tokenList.size(); i++) { + Token token = tokenList.get(i); + System.out.println("Token[" + i + "]: " + lexer.getVocabulary().getSymbolicName(token.getType()) + ": " + token.getText()); + }*/ - private List loadExpectedTokens(String tokenFilePath) throws IOException { - List tokenTypes = new ArrayList<>(); - List lines = Files.readAllLines(Paths.get(tokenFilePath)); - for (String line : lines) { - int tokenType; - try { - tokenType = JavaLexer.class.getField(line).getInt(null); - } catch (NoSuchFieldException | IllegalAccessException e) { - throw new RuntimeException("Invalid token type in tokens file: " + line, e); - } - tokenTypes.add(tokenType); + // Compare tokens + //assertEquals("Number of tokens does not match expected", expectedTokens.size(), tokenList.size()); + + for (int i = 0; i < tokenList.size(); i++) { + Token token = tokenList.get(i); + String tokenData = String.format("%s: \"%s\"", lexer.getVocabulary().getSymbolicName(token.getType()), token.getText()); + assertEquals("Token mismatch at index " + i, expectedTokens.get(i), tokenData); } - return tokenTypes; } + } diff --git a/src/test/resources/basicClasses/EmptyClassWithConstructor.java b/src/test/resources/basicClasses/EmptyClassWithConstructor.java index 257b084..5017643 100644 --- a/src/test/resources/basicClasses/EmptyClassWithConstructor.java +++ b/src/test/resources/basicClasses/EmptyClassWithConstructor.java @@ -1,6 +1,6 @@ -class EmptyClassWithConstructor{ +class EmptyClassWithConstructor { - public EmptyClassWithConstructor(){ + public EmptyClassWithConstructor() { } diff --git a/src/test/resources/basicClasses/EmptyClassWithConstructor.tokens b/src/test/resources/basicClasses/EmptyClassWithConstructor.tokens index 7f8d465..d135433 100644 --- a/src/test/resources/basicClasses/EmptyClassWithConstructor.tokens +++ b/src/test/resources/basicClasses/EmptyClassWithConstructor.tokens @@ -1,10 +1,11 @@ -Class -Identifier -OpenCurlyBracket -AccessModifier -Identifier -OpenRoundBracket -ClosedRoundBracket -OpenCurlyBracket -ClosedCurlyBracket -EOF \ No newline at end of file +Class: "class" +Identifier: "EmptyClassWithConstructor" +OpenCurlyBracket: "{" +AccessModifierPublic: "public" +Identifier: "EmptyClassWithConstructor" +OpenRoundBracket: "(" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +ClosedCurlyBracket: "}" +ClosedCurlyBracket: "}" +EOF: "" \ No newline at end of file diff --git a/src/test/resources/basicClasses/FourClasses.tokens b/src/test/resources/basicClasses/FourClasses.tokens index f01b082..62fbdb4 100644 --- a/src/test/resources/basicClasses/FourClasses.tokens +++ b/src/test/resources/basicClasses/FourClasses.tokens @@ -1,207 +1,209 @@ -Class -Identifier -OpenCurlyBracket -AccessModifier -Int -Identifier -OpenRoundBracket -Int -Identifier -ClosedRoundBracket -OpenCurlyBracket -Identifier -Identifier -Assign -New -Identifier -OpenRoundBracket -Identifier -ClosedRoundBracket -Semicolon -Identifier -Identifier -Assign -New -Identifier -OpenRoundBracket -Identifier -Dot -Identifier -ClosedRoundBracket -Semicolon -Return -Identifier -Dot -Identifier -Dot -Identifier -Dot -Identifier -OpenRoundBracket -ClosedRoundBracket -Semicolon -ClosedCurlyBracket -Class -Identifier -OpenCurlyBracket -AccessModifier -Int -Identifier -Semicolon -AccessModifier -Int -Identifier -Semicolon -AccessModifier -Int -Identifier -Semicolon -AccessModifier -Identifier -OpenRoundBracket -Int -Identifier -ClosedRoundBracket -OpenCurlyBracket -This -Dot -Identifier -Assign -Identifier -Semicolon -This -Dot -Identifier -Assign -IntValue -Semicolon -This -Dot -Identifier -Assign -New -Identifier -OpenRoundBracket -Identifier -DotOperator -IntValue -ClosedRoundBracket -Semicolon -ClosedCurlyBracket -AccessModifier -Identifier -Identifier -OpenRoundBracket -ClosedRoundBracket -OpenCurlyBracket -Return -This -Dot -Identifier -Semicolon -ClosedCurlyBracket -AccessModifier -Int -Identifier -OpenRoundBracket -ClosedRoundBracket -OpenCurlyBracket -Return -This -Dot -Identifier -Semicolon -ClosedCurlyBracket -ClosedCurlyBracket -Class -Identifier -OpenCurlyBracket -AccessModifier -Identifier -Identifier -Semicolon -AccessModifier -Identifier -OpenRoundBracket -Int -Identifier -ClosedRoundBracket -OpenCurlyBracket -This -Dot -Identifier -Assign -New -Identifier -OpenRoundBrackets -Identifier -ClosedRoundBrackets -Semicolon -ClosedCurlyBracket -ClosedCurlyBracket -Class -Identifier -OpenCurlyBracket -AccessModifier -Int -Identifier -Semicolon -AccessModifier -Int -Identifier -Semicolon -AccessModifier -Identifier -OpenRoundBrackets -Int -Identifier -ClosedRoundBrackets -OpenCurlyBrackets -This -Dot -Identifier -Assign -Identifier -Semicolon -ClosedCurlyBracket -AccessModifier -Int -Identifier -OpenRoundBrackets -ClosedRoundBrackets -OpenCurlyBracket -Return -This -Dot -Identifier -Semicolon -ClosedCurlyBracket -AccessModifier -Int -Identifier -OpenRoundBracket -ClosedRoundBracket -OpenCurlyBracket -Return -This -Dot -Identifier -Semicolon -ClosedCurlyBracket -AccessModifier -Void -Identifier -OpenRoundBracket -Int -Identifier -ClosedRoundBracket -OpenCurlyBracket -This -Dot -Identifier -Assign -Identifier -Semicolon -ClosedCurlyBracket -ClosedCurlyBracket -EOF \ No newline at end of file +Class: "class" +Identifier: "FourClasses" +OpenCurlyBracket: "{" +AccessModifierPublic: "public" +Int: "int" +Identifier: "main" +OpenRoundBracket: "(" +Int: "int" +Identifier: "i" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +Identifier: "Test" +Identifier: "t" +Assign: "=" +New: "new" +Identifier: "Test" +OpenRoundBracket: "(" +Identifier: "i" +ClosedRoundBracket: ")" +Semicolon: ";" +Identifier: "Test2" +Identifier: "t2" +Assign: "=" +New: "new" +Identifier: "Test2" +OpenRoundBracket: "(" +Identifier: "t" +Dot: "." +Identifier: "y" +ClosedRoundBracket: ")" +Semicolon: ";" +Return: "return" +Identifier: "t2" +Dot: "." +Identifier: "test" +Dot: "." +Identifier: "test3" +Dot: "." +Identifier: "getX" +OpenRoundBracket: "(" +ClosedRoundBracket: ")" +Semicolon: ";" +ClosedCurlyBracket: "}" +ClosedCurlyBracket: "}" +Class: "class" +Identifier: "Test" +OpenCurlyBracket: "{" +AccessModifierPublic: "public" +Int: "int" +Identifier: "x" +Semicolon: ";" +AccessModifierPublic: "public" +Int: "int" +Identifier: "y" +Semicolon: ";" +AccessModifierPublic: "public" +Identifier: "Test3" +Identifier: "test3" +Semicolon: ";" +AccessModifierPublic: "public" +Identifier: "Test" +OpenRoundBracket: "(" +Int: "int" +Identifier: "i" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +This: "this" +Dot: "." +Identifier: "x" +Assign: "=" +Identifier: "i" +Semicolon: ";" +This: "this" +Dot: "." +Identifier: "y" +Assign: "=" +IntValue: "10" +Semicolon: ";" +This: "this" +Dot: "." +Identifier: "test3" +Assign: "=" +New: "new" +Identifier: "Test3" +OpenRoundBracket: "(" +Identifier: "i" +DotOperator: "*" +IntValue: "2" +ClosedRoundBracket: ")" +Semicolon: ";" +ClosedCurlyBracket: "}" +AccessModifierPublic: "public" +Identifier: "Test3" +Identifier: "getTest3" +OpenRoundBracket: "(" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +Return: "return" +This: "this" +Dot: "." +Identifier: "test3" +Semicolon: ";" +ClosedCurlyBracket: "}" +AccessModifierPublic: "public" +Int: "int" +Identifier: "getX" +OpenRoundBracket: "(" +ClosedRoundBracket: ")" +Return: "return" +This: "this" +Dot: "." +identifier: "x" +Semicolon: ";" +ClosedCurlyBracket: "}" +ClosedCurlyBracket: "}" +Class: "class" +Identifier: "Test2" +OpenCurlyBracket: "{" +AccessModifierPublic: "public" +Identifier: "Test" +Identifier: "test" +Semicolon: ";" +AccessModifierPublic: "public" +Identifier: "Test2" +OpenRoundBracket: "(" +Int: "int" +Identifier: "i" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +This: "this" +Dot: "." +Identifier: "test" +Assign: "new" +Identifier: "Test" +OpenRoundBracket: "(" +Identifier: "i" +ClosedRoundBracket: ")" +Semicolon: ";" +ClosedCurlyBracket: "}" + + + + +Class: "class" +Identifier: "Test3" +OpenCurlyBracket: "{" +AccessModifierPublic: "public" +Int: "int" +Identifier: "x" +Semicolon: ";" +AccessModifierPublic: "public" +Int: "int" +Identifier: "y" +Semicolon: ";" +AccessModifierPublic: "public" +Identifier: "Test3" +OpenRoundBrackets: "(" +Int: "int" +Identifier: "i" +ClosedRoundBrackets: ")" +OpenCurlyBrackets: "{" +This: "this" +Dot: "." +Identifier: "x" +Assign: "=" +Identifier: "i" +Semicolon: ";" +ClosedCurlyBracket: "}" +AccessModifierPublic: "public" +Int: "int" +Identifier: "getC" +OpenRoundBrackets: "(" +ClosedRoundBrackets: ")" +OpenCurlyBracket: "{" +Return: "return" +This: "this" +Dot: "." +Identifier: "x" +Semicolon: ";" +ClosedCurlyBracket: "}" +AccessModifierPublic: "public" +Int: "int" +Identifier: "getY" +OpenRoundBracket: "(" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +Return: "return" +This: "this" +Dot: "." +Identifier: "y" +Semicolon: ";" +ClosedCurlyBracket: "}" +AccessModifierPublic: "public" +Void: "void" +Identifier: "setY" +OpenRoundBracket: "(" +Int: "int" +Identifier: "y" +ClosedRoundBracket: ")" +OpenCurlyBracket: "{" +This: "this" +Dot: "." +Identifier: "y" +Assign: "=" +Identifier: "y" +Semicolon: ";" +ClosedCurlyBracket: "}" +ClosedCurlyBracket: "}" +EOF: "" \ No newline at end of file diff --git a/src/test/resources/basicClasses/emptyClass.tokens b/src/test/resources/basicClasses/emptyClass.tokens index 95fa1ab..7c4b3eb 100644 --- a/src/test/resources/basicClasses/emptyClass.tokens +++ b/src/test/resources/basicClasses/emptyClass.tokens @@ -1,5 +1,5 @@ -Class -Identifier -OpenCurlyBracket -ClosedCurlyBracket -EOF \ No newline at end of file +Class: "class" +Identifier: "emptyClass" +OpenCurlyBracket: "{" +ClosedCurlyBracket: "}" +EOF: "" \ No newline at end of file diff --git a/src/test/resources/basicClasses/publicEmptyClass.tokens b/src/test/resources/basicClasses/publicEmptyClass.tokens index 72a68ed..7a768f1 100644 --- a/src/test/resources/basicClasses/publicEmptyClass.tokens +++ b/src/test/resources/basicClasses/publicEmptyClass.tokens @@ -1,6 +1,6 @@ -AccessModifier -Class -Identifier -OpenCurlyBracket -ClosedCurlyBracket -EOF \ No newline at end of file +AccessModifierPublic: "public" +Class: "class" +Identifier: "emptyClass" +OpenCurlyBracket: "{" +ClosedCurlyBracket: "}" +EOF: "" \ No newline at end of file