Updatet Tests for Token-Generation

This commit is contained in:
KingJulian 2024-05-09 18:33:33 +02:00
parent 4f2599d053
commit 4c73ae6786
7 changed files with 268 additions and 265 deletions

View File

@ -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 {
}

View File

@ -1,42 +1,52 @@
package Tokens; package Tokens;
import org.antlr.v4.runtime.*; import gen.DecafLexer;
import java.io.IOException; 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.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class JavaLexerTest { 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 { public void testTokens(String sourceFilePath, String tokensFilePath) throws Exception {
String content = new String(Files.readAllBytes(Paths.get(sourcePath))); // Read the source file
JavaLexer lexer = new JavaLexer(CharStreams.fromString(content)); 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); CommonTokenStream tokens = new CommonTokenStream(lexer);
tokens.fill(); // Load all tokens tokens.fill();
List<Token> tokenList = tokens.getTokens();
List<Integer> expectedTokenTypes = loadExpectedTokens(tokensPath); // Read the expected tokens
List<Integer> actualTokenTypes = tokens.getTokens().stream() List<String> expectedTokens = Files.readAllLines(Paths.get(BASE_DIR + tokensFilePath));
.map(Token::getType)
.collect(Collectors.toList());
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<Integer> loadExpectedTokens(String tokenFilePath) throws IOException { // Compare tokens
List<Integer> tokenTypes = new ArrayList<>(); //assertEquals("Number of tokens does not match expected", expectedTokens.size(), tokenList.size());
List<String> lines = Files.readAllLines(Paths.get(tokenFilePath));
for (String line : lines) { for (int i = 0; i < tokenList.size(); i++) {
int tokenType; Token token = tokenList.get(i);
try { String tokenData = String.format("%s: \"%s\"", lexer.getVocabulary().getSymbolicName(token.getType()), token.getText());
tokenType = JavaLexer.class.getField(line).getInt(null); assertEquals("Token mismatch at index " + i, expectedTokens.get(i), tokenData);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException("Invalid token type in tokens file: " + line, e);
}
tokenTypes.add(tokenType);
} }
return tokenTypes;
} }
} }

View File

@ -1,6 +1,6 @@
class EmptyClassWithConstructor{ class EmptyClassWithConstructor {
public EmptyClassWithConstructor(){ public EmptyClassWithConstructor() {
} }

View File

@ -1,10 +1,11 @@
Class Class: "class"
Identifier Identifier: "EmptyClassWithConstructor"
OpenCurlyBracket OpenCurlyBracket: "{"
AccessModifier AccessModifierPublic: "public"
Identifier Identifier: "EmptyClassWithConstructor"
OpenRoundBracket OpenRoundBracket: "("
ClosedRoundBracket ClosedRoundBracket: ")"
OpenCurlyBracket OpenCurlyBracket: "{"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,207 +1,209 @@
Class Class: "class"
Identifier Identifier: "FourClasses"
OpenCurlyBracket OpenCurlyBracket: "{"
AccessModifier AccessModifierPublic: "public"
Int Int: "int"
Identifier Identifier: "main"
OpenRoundBracket OpenRoundBracket: "("
Int Int: "int"
Identifier Identifier: "i"
ClosedRoundBracket ClosedRoundBracket: ")"
OpenCurlyBracket OpenCurlyBracket: "{"
Identifier Identifier: "Test"
Identifier Identifier: "t"
Assign Assign: "="
New New: "new"
Identifier Identifier: "Test"
OpenRoundBracket OpenRoundBracket: "("
Identifier Identifier: "i"
ClosedRoundBracket ClosedRoundBracket: ")"
Semicolon Semicolon: ";"
Identifier Identifier: "Test2"
Identifier Identifier: "t2"
Assign Assign: "="
New New: "new"
Identifier Identifier: "Test2"
OpenRoundBracket OpenRoundBracket: "("
Identifier Identifier: "t"
Dot Dot: "."
Identifier Identifier: "y"
ClosedRoundBracket ClosedRoundBracket: ")"
Semicolon Semicolon: ";"
Return Return: "return"
Identifier Identifier: "t2"
Dot Dot: "."
Identifier Identifier: "test"
Dot Dot: "."
Identifier Identifier: "test3"
Dot Dot: "."
Identifier Identifier: "getX"
OpenRoundBracket OpenRoundBracket: "("
ClosedRoundBracket ClosedRoundBracket: ")"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
Class ClosedCurlyBracket: "}"
Identifier Class: "class"
OpenCurlyBracket Identifier: "Test"
AccessModifier OpenCurlyBracket: "{"
Int AccessModifierPublic: "public"
Identifier Int: "int"
Semicolon Identifier: "x"
AccessModifier Semicolon: ";"
Int AccessModifierPublic: "public"
Identifier Int: "int"
Semicolon Identifier: "y"
AccessModifier Semicolon: ";"
Int AccessModifierPublic: "public"
Identifier Identifier: "Test3"
Semicolon Identifier: "test3"
AccessModifier Semicolon: ";"
Identifier AccessModifierPublic: "public"
OpenRoundBracket Identifier: "Test"
Int OpenRoundBracket: "("
Identifier Int: "int"
ClosedRoundBracket Identifier: "i"
OpenCurlyBracket ClosedRoundBracket: ")"
This OpenCurlyBracket: "{"
Dot This: "this"
Identifier Dot: "."
Assign Identifier: "x"
Identifier Assign: "="
Semicolon Identifier: "i"
This Semicolon: ";"
Dot This: "this"
Identifier Dot: "."
Assign Identifier: "y"
IntValue Assign: "="
Semicolon IntValue: "10"
This Semicolon: ";"
Dot This: "this"
Identifier Dot: "."
Assign Identifier: "test3"
New Assign: "="
Identifier New: "new"
OpenRoundBracket Identifier: "Test3"
Identifier OpenRoundBracket: "("
DotOperator Identifier: "i"
IntValue DotOperator: "*"
ClosedRoundBracket IntValue: "2"
Semicolon ClosedRoundBracket: ")"
ClosedCurlyBracket Semicolon: ";"
AccessModifier ClosedCurlyBracket: "}"
Identifier AccessModifierPublic: "public"
Identifier Identifier: "Test3"
OpenRoundBracket Identifier: "getTest3"
ClosedRoundBracket OpenRoundBracket: "("
OpenCurlyBracket ClosedRoundBracket: ")"
Return OpenCurlyBracket: "{"
This Return: "return"
Dot This: "this"
Identifier Dot: "."
Semicolon Identifier: "test3"
ClosedCurlyBracket Semicolon: ";"
AccessModifier ClosedCurlyBracket: "}"
Int AccessModifierPublic: "public"
Identifier Int: "int"
OpenRoundBracket Identifier: "getX"
ClosedRoundBracket OpenRoundBracket: "("
OpenCurlyBracket ClosedRoundBracket: ")"
Return Return: "return"
This This: "this"
Dot Dot: "."
Identifier identifier: "x"
Semicolon Semicolon: ";"
ClosedCurlyBracket ClosedCurlyBracket: "}"
ClosedCurlyBracket ClosedCurlyBracket: "}"
Class Class: "class"
Identifier Identifier: "Test2"
OpenCurlyBracket OpenCurlyBracket: "{"
AccessModifier AccessModifierPublic: "public"
Identifier Identifier: "Test"
Identifier Identifier: "test"
Semicolon Semicolon: ";"
AccessModifier AccessModifierPublic: "public"
Identifier Identifier: "Test2"
OpenRoundBracket OpenRoundBracket: "("
Int Int: "int"
Identifier Identifier: "i"
ClosedRoundBracket ClosedRoundBracket: ")"
OpenCurlyBracket OpenCurlyBracket: "{"
This This: "this"
Dot Dot: "."
Identifier Identifier: "test"
Assign Assign: "new"
New Identifier: "Test"
Identifier OpenRoundBracket: "("
OpenRoundBrackets Identifier: "i"
Identifier ClosedRoundBracket: ")"
ClosedRoundBrackets Semicolon: ";"
Semicolon ClosedCurlyBracket: "}"
ClosedCurlyBracket
ClosedCurlyBracket
Class
Identifier
OpenCurlyBracket Class: "class"
AccessModifier Identifier: "Test3"
Int OpenCurlyBracket: "{"
Identifier AccessModifierPublic: "public"
Semicolon Int: "int"
AccessModifier Identifier: "x"
Int Semicolon: ";"
Identifier AccessModifierPublic: "public"
Semicolon Int: "int"
AccessModifier Identifier: "y"
Identifier Semicolon: ";"
OpenRoundBrackets AccessModifierPublic: "public"
Int Identifier: "Test3"
Identifier OpenRoundBrackets: "("
ClosedRoundBrackets Int: "int"
OpenCurlyBrackets Identifier: "i"
This ClosedRoundBrackets: ")"
Dot OpenCurlyBrackets: "{"
Identifier This: "this"
Assign Dot: "."
Identifier Identifier: "x"
Semicolon Assign: "="
ClosedCurlyBracket Identifier: "i"
AccessModifier Semicolon: ";"
Int ClosedCurlyBracket: "}"
Identifier AccessModifierPublic: "public"
OpenRoundBrackets Int: "int"
ClosedRoundBrackets Identifier: "getC"
OpenCurlyBracket OpenRoundBrackets: "("
Return ClosedRoundBrackets: ")"
This OpenCurlyBracket: "{"
Dot Return: "return"
Identifier This: "this"
Semicolon Dot: "."
ClosedCurlyBracket Identifier: "x"
AccessModifier Semicolon: ";"
Int ClosedCurlyBracket: "}"
Identifier AccessModifierPublic: "public"
OpenRoundBracket Int: "int"
ClosedRoundBracket Identifier: "getY"
OpenCurlyBracket OpenRoundBracket: "("
Return ClosedRoundBracket: ")"
This OpenCurlyBracket: "{"
Dot Return: "return"
Identifier This: "this"
Semicolon Dot: "."
ClosedCurlyBracket Identifier: "y"
AccessModifier Semicolon: ";"
Void ClosedCurlyBracket: "}"
Identifier AccessModifierPublic: "public"
OpenRoundBracket Void: "void"
Int Identifier: "setY"
Identifier OpenRoundBracket: "("
ClosedRoundBracket Int: "int"
OpenCurlyBracket Identifier: "y"
This ClosedRoundBracket: ")"
Dot OpenCurlyBracket: "{"
Identifier This: "this"
Assign Dot: "."
Identifier Identifier: "y"
Semicolon Assign: "="
ClosedCurlyBracket Identifier: "y"
ClosedCurlyBracket Semicolon: ";"
EOF ClosedCurlyBracket: "}"
ClosedCurlyBracket: "}"
EOF: "<EOF>"

View File

@ -1,5 +1,5 @@
Class Class: "class"
Identifier Identifier: "emptyClass"
OpenCurlyBracket OpenCurlyBracket: "{"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF EOF: "<EOF>"

View File

@ -1,6 +1,6 @@
AccessModifier AccessModifierPublic: "public"
Class Class: "class"
Identifier Identifier: "emptyClass"
OpenCurlyBracket OpenCurlyBracket: "{"
ClosedCurlyBracket ClosedCurlyBracket: "}"
EOF EOF: "<EOF>"