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;
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<Token> tokenList = tokens.getTokens();
List<Integer> expectedTokenTypes = loadExpectedTokens(tokensPath);
List<Integer> actualTokenTypes = tokens.getTokens().stream()
.map(Token::getType)
.collect(Collectors.toList());
// Read the expected tokens
List<String> 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());
}*/
// 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);
}
}
private List<Integer> loadExpectedTokens(String tokenFilePath) throws IOException {
List<Integer> tokenTypes = new ArrayList<>();
List<String> 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);
}
return tokenTypes;
}
}

View File

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

View File

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

View File

@ -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
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: "<EOF>"

View File

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

View File

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