More Tests, Structure, etc. Huge Changes #14
@ -15,17 +15,28 @@ compile-raupenpiler:
|
||||
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
|
||||
|
||||
test-javac:
|
||||
# gleich wie bei raupenpiler, kann ich ohne funktionierenden Compiler nicht testen
|
||||
|
||||
|
||||
test-raupenpiler:
|
||||
# move the compiled class to the test/main folder
|
||||
mv ../main/resources/output/CompilerInput.class .java/main/
|
||||
cd .java/main/
|
||||
# compile the test class
|
||||
javac .java/main.EndToEndTester.java
|
||||
# run the test class
|
||||
java .java/main.EndToEndTester
|
||||
|
||||
|
||||
|
||||
clean:
|
||||
# clean output folders
|
||||
rm -f ../main/resources/output/*.class
|
||||
rm -f ./resources/output/javac/*.class
|
||||
rm -f ./resources/output/raupenpiler/*.class
|
||||
rm -f ./java/*.class
|
||||
rm -f ../main/resources/output/*.class
|
||||
# clean logs
|
||||
rm -f ../main/resources/logs/*.log
|
||||
# clean test/main folders from .class files for End-to-End tests
|
||||
rm -f ./java/main/*.class
|
||||
# clean javac output from featureTests
|
||||
rm -f ./resources/input/featureTests/*.class
|
||||
|
||||
|
@ -78,8 +78,8 @@ Compiled Classfile
|
||||
|
||||
wenn beides erfolgreich
|
||||
|
||||
- Ergebnis vom eigenen Compiler mithilfe von main.TestCompilerOutput ausführen
|
||||
- (Ergebnis von javac mithilfe von main.TestCompilerOutput ausführen)
|
||||
- Ergebnis vom eigenen Compiler mithilfe von main.EndToEndTester ausführen
|
||||
- (Ergebnis von javac mithilfe von main.EndToEndTester ausführen)
|
||||
|
||||
### Andis Tipps:
|
||||
|
||||
@ -89,4 +89,5 @@ wenn beides erfolgreich
|
||||
- mvn package
|
||||
- javac tester // tester compilen
|
||||
- java tester // tester ausführen
|
||||
- -> tester ist in unserem Fall main.TestCompilerOutput.java
|
||||
- -> tester ist in unserem Fall main.EndToEndTester.java
|
||||
- -> Hab ich alles umgesetzt
|
@ -3,18 +3,18 @@ package main;
|
||||
/**
|
||||
* This class is used to test the output of the compiler.
|
||||
*
|
||||
* <p>Im gleichen Ordner wie diese Datei (TestCompilerOutput.java) muss die selbst kompilierte CompilerInput.class Datei sein.
|
||||
* <br><strong>Hinweis:</strong> Diese muss man also vom Ordner <code> main/resources/output </code> in diesen Ordner hier (test/java/main) rein kopieren. (bis es eine bessere Lösung gibt)</p>
|
||||
* <p>Im gleichen Ordner wie diese Datei (EndToEndTester.java) muss die selbst kompilierte CompilerInput.class Datei sein.
|
||||
* <br><strong>Hinweis:</strong> Diese muss man also vom Ordner <code> main/resources/output </code> in diesen Ordner hier (test/java/main) rein kopieren. (bis es eine bessere Lösung gibt -> bin grad in der Make dran das alles hier automatisch zu machen)</p>
|
||||
*
|
||||
* <p>Die selbst kompilierte .class Datei wird dann hier drin geladen und eine Instanz von ihr erstellt, es können auch Methoden aufgerufen werden.
|
||||
* <p>Diese TestCompilerOutput.java Datei wird dann in <code> \src\test\java> </code> mit <code>javac .\main.TestCompilerOutput.java</code> kompiliert und mit <code>java main.TestCompilerOutput</code> ausgeführt.
|
||||
* <p>Diese EndToEndTester.java Datei wird dann in <code> \src\test\java> </code> mit <code>javac .\main.EndToEndTester.java</code> kompiliert und mit <code>java main.EndToEndTester</code> ausgeführt.
|
||||
* Wenn unser Compiler funktioniert, sollten keine Errors kommen (sondern nur die Ausgaben, die wir in der CompilerInput.java Datei gemacht haben,
|
||||
* oder Methoden, die wir hier aufrufen).</p>
|
||||
*
|
||||
* <p><strong>PROBLEM:</strong> Hier kommen Errors, was eigentlich heißt, dass der Compiler nicht funktioniert, der Test sollte eigentlich passen.
|
||||
* <br><strong>DENN:</strong> Wenn ich statt unserem CompilerInput.class die CompilerInput.class von javac verwende (aus <code> src/test/resources/output/javac </code>), dann funktioniert es.</p>
|
||||
*/
|
||||
public class TestCompilerOutput {
|
||||
public class EndToEndTester {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
// Try to load the class named "CompilerInput"
|
@ -3,71 +3,45 @@ package main;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class FailureTest {
|
||||
private static final List<String> TEST_FILES = Arrays.asList(
|
||||
"src/main/test/resources/input/failureTests/TestClass1.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass2.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass3.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass4.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass5.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass6.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass7.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass8.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass9.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass10.java",
|
||||
"src/main/test/resources/input/failureTests/TestClass11.java"
|
||||
);
|
||||
|
||||
/**
|
||||
* This test method checks if invalid Java files fail to compile as expected.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files fail to compile.
|
||||
*/
|
||||
@Test
|
||||
public void invalidJavaFilesTest() {
|
||||
public void areTestFilesActuallyFailTest() {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(compiler, "Java Compiler is not available");
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
// Iterate over the test files
|
||||
for (String fileName : TEST_FILES) {
|
||||
// Create a File object for the current file
|
||||
File file = new File(fileName);
|
||||
String directoryPath = "src/test/resources/input/failureTests";
|
||||
File folder = new File(directoryPath);
|
||||
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = compiler.run(null, null, null, file.getPath());
|
||||
if (folder.isDirectory()) {
|
||||
File[] files = folder.listFiles((dir, name) -> name.endsWith(".java"));
|
||||
|
||||
// Assert that the compilation failed (i.e., the result is non-zero)
|
||||
assertTrue(result != 0, "Expected compilation failure for " + fileName);
|
||||
}
|
||||
}
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// schmeißt John Fehler, wenn namen doppelt sind?
|
||||
// Input: ParseTree mit genanntem Fehler
|
||||
// Output: Fehlermeldung
|
||||
@Test
|
||||
void typedASTTest() throws IOException {
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/main/EmptyClassExample.java"));
|
||||
Main.compileFile(codeCharStream, "src/main/test/resources/output");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
// Assert that the compilation failed (i.e., the result is non-zero)
|
||||
assertTrue(result != 0, "Expected compilation failure for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directory.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("The provided path is not a directory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
46
src/test/java/main/FeatureTest.java
Normal file
46
src/test/java/main/FeatureTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package main;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class FeatureTest {
|
||||
/**
|
||||
* This test method checks if valid Java files compile successfully.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files compile without errors.
|
||||
*/
|
||||
@Test
|
||||
public void areTestFilesActuallyValid() {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
String directoryPath = "src/test/resources/input/featureTests";
|
||||
File folder = new File(directoryPath);
|
||||
|
||||
if (folder.isDirectory()) {
|
||||
File[] files = folder.listFiles((dir, name) -> name.endsWith(".java"));
|
||||
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directory.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("The provided path is not a directory.");
|
||||
}
|
||||
}
|
||||
}
|
@ -7,14 +7,14 @@ import org.antlr.v4.runtime.CharStreams;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* run: mvn test
|
||||
* run every test: mvn test
|
||||
* Nutzen dieser Klasse: Eigentlich nicht vorhanden, in der Main gibts nichts zu testen
|
||||
*/
|
||||
public class MainTest {
|
||||
@Test
|
||||
void testEmptyClass() {
|
||||
void test() {
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/CompilerInput.java"));
|
||||
|
103
src/test/java/parser/AstBuilderTest.java
Normal file
103
src/test/java/parser/AstBuilderTest.java
Normal file
@ -0,0 +1,103 @@
|
||||
package parser;
|
||||
|
||||
import ast.ClassNode;
|
||||
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 org.junit.jupiter.api.Test;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class AstBuilderTest {
|
||||
|
||||
@Test
|
||||
public void astBuilderTest() {
|
||||
// ---------------- Leere Klasse nachgebaut ----------------
|
||||
|
||||
ProgramNode expectedASTEmptyClass = new ProgramNode();
|
||||
|
||||
// public class Name {}
|
||||
ClassNode nameClass = new ClassNode("public", "Name");
|
||||
|
||||
expectedASTEmptyClass.addClass(nameClass);
|
||||
|
||||
|
||||
// ---------------- Leere Klasse erzeugt ----------------
|
||||
|
||||
// init
|
||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
|
||||
/* Parser -> Parsetree */
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
||||
|
||||
/* AST builder -> AST */
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
||||
|
||||
|
||||
// ---------------- Vergleichen ----------------
|
||||
|
||||
String expectedASTasString = expectedASTEmptyClass.toString();
|
||||
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
||||
|
||||
// Wie vergleiche ich das?
|
||||
assertEquals(expectedASTasString, actualASTasString);
|
||||
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ---------------- Alter CompilerInput nachgebaut ----------------
|
||||
// ProgramNode startNode = new ProgramNode();
|
||||
// public class CompilerInput {}
|
||||
// ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
||||
// public int a;
|
||||
// compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
||||
// public static int testMethod(char x) { return 0; }
|
||||
/* compilerInputClass.addMember(
|
||||
new MethodNode(
|
||||
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||
new BaseTypeNode(EnumTypeNode.INT),
|
||||
"testMethod",
|
||||
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"))),
|
||||
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||
));
|
||||
|
||||
ClassNode testClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "Test");
|
||||
testClass.addMember(
|
||||
new MethodNode(
|
||||
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||
new BaseTypeNode(EnumTypeNode.INT),
|
||||
"testMethod",
|
||||
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"), new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "a"))),
|
||||
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||
)
|
||||
);
|
||||
|
||||
*/
|
||||
|
||||
//compilerInputClass.addClass(testClass);
|
||||
|
||||
// startNode.addClass(compilerInputClass);
|
||||
// startNode.addClass(testClass);
|
||||
}
|
@ -1,16 +1,9 @@
|
||||
package parser;
|
||||
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import ast.member.FieldNode;
|
||||
import ast.member.MethodNode;
|
||||
import ast.parameter.ParameterNode;
|
||||
import ast.statement.ReturnStatementNode;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
@ -19,41 +12,13 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
import java.util.*;
|
||||
|
||||
public class ParserTest {
|
||||
/*
|
||||
@BeforeEach
|
||||
public void init() { // noch nicht benötigt
|
||||
String inputFilePath = "src/main/resources/input/CompilerInput.java";
|
||||
String outputDirectoryPath = "src/main/resources/output";
|
||||
}
|
||||
|
||||
/**
|
||||
* This test method is used to test the scanner functionality of the SimpleJavaLexer.
|
||||
* It creates a CharStream from a string representing a simple Java class declaration,
|
||||
* and uses the SimpleJavaLexer to tokenize this input.
|
||||
* It then compares the actual tokens and their types produced by the lexer to the expected tokens and their types.
|
||||
*/
|
||||
@Test
|
||||
public void scannerTest() {
|
||||
// Create a CharStream from a string representing a simple Java class declaration
|
||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||
|
||||
// Use the SimpleJavaLexer to tokenize the input
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
|
||||
// Prepare the expected results
|
||||
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
||||
List<String> expectedTokenTypes = Arrays.asList("AccessModifier", "Class", "Identifier", "OpenCurlyBracket", "ClosedCurlyBracket", "EOF");
|
||||
List<Token> actualTokens = tokenStream.getTokens();
|
||||
|
||||
// Compare the actual tokens and their types to the expected tokens and their types
|
||||
assertEquals(expectedTokens.size(), actualTokens.size());
|
||||
for (int i = 0; i < expectedTokens.size(); i++) {
|
||||
assertEquals(expectedTokens.get(i), actualTokens.get(i).getText());
|
||||
assertEquals(expectedTokenTypes.get(i), SimpleJavaLexer.VOCABULARY.getSymbolicName(actualTokens.get(i).getType()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void parserTest() {
|
||||
@ -83,80 +48,6 @@ public class ParserTest {
|
||||
// assertEquals(expectedTreeStructure, actualTreeStructure);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void astBuilderTest() {
|
||||
// TODO: Implement this test method
|
||||
|
||||
// ---------------- Alter CompilerInput nachgebaut ----------------
|
||||
// ProgramNode startNode = new ProgramNode();
|
||||
// public class CompilerInput {}
|
||||
// ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
||||
// public int a;
|
||||
// compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
||||
// public static int testMethod(char x) { return 0; }
|
||||
/* compilerInputClass.addMember(
|
||||
new MethodNode(
|
||||
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||
new BaseTypeNode(EnumTypeNode.INT),
|
||||
"testMethod",
|
||||
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"))),
|
||||
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||
));
|
||||
|
||||
ClassNode testClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "Test");
|
||||
testClass.addMember(
|
||||
new MethodNode(
|
||||
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||
new BaseTypeNode(EnumTypeNode.INT),
|
||||
"testMethod",
|
||||
new ParameterListNode(List.of(new ParameterNode(new BaseTypeNode(EnumTypeNode.CHAR), "x"), new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "a"))),
|
||||
List.of(new ReturnStatementNode(new LiteralNode(0)))
|
||||
)
|
||||
);
|
||||
|
||||
*/
|
||||
|
||||
//compilerInputClass.addClass(testClass);
|
||||
|
||||
// startNode.addClass(compilerInputClass);
|
||||
// startNode.addClass(testClass);
|
||||
|
||||
// ---------------- Leere Klasse nachgebaut ----------------
|
||||
|
||||
ProgramNode expectedASTEmptyClass = new ProgramNode();
|
||||
|
||||
// public class Name {}
|
||||
ClassNode nameClass = new ClassNode("public", "Name");
|
||||
|
||||
expectedASTEmptyClass.addClass(nameClass);
|
||||
|
||||
|
||||
// ---------------- Leere Klasse erzeugt ----------------
|
||||
|
||||
// init
|
||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
|
||||
/* Parser -> Parsetree */
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
||||
|
||||
/* AST builder -> AST */
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
||||
|
||||
|
||||
// ---------------- Vergleichen ----------------
|
||||
|
||||
String expectedASTasString = expectedASTEmptyClass.toString();
|
||||
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
||||
|
||||
// Wie vergleiche ich das?
|
||||
assertEquals(expectedASTasString, actualASTasString);
|
||||
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
||||
}
|
||||
|
||||
|
||||
// Helpers Variante 2.1
|
||||
|
45
src/test/java/parser/ScannerTest.java
Normal file
45
src/test/java/parser/ScannerTest.java
Normal file
@ -0,0 +1,45 @@
|
||||
package parser;
|
||||
|
||||
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.jupiter.api.Test;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class ScannerTest {
|
||||
|
||||
/**
|
||||
* This test method is used to test the scanner functionality of the SimpleJavaLexer.
|
||||
* It creates a CharStream from a string representing a simple Java class declaration,
|
||||
* and uses the SimpleJavaLexer to tokenize this input.
|
||||
* It then compares the actual tokens and their types produced by the lexer to the expected tokens and their types.
|
||||
*/
|
||||
@Test
|
||||
public void scannerTest() {
|
||||
// Create a CharStream from a string representing a simple Java class declaration
|
||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||
|
||||
// Use the SimpleJavaLexer to tokenize the input
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
|
||||
// Prepare the expected results
|
||||
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
||||
List<String> expectedTokenTypes = Arrays.asList("AccessModifier", "Class", "Identifier", "OpenCurlyBracket", "ClosedCurlyBracket", "EOF");
|
||||
List<Token> actualTokens = tokenStream.getTokens();
|
||||
|
||||
// Compare the actual tokens and their types to the expected tokens and their types
|
||||
assertEquals(expectedTokens.size(), actualTokens.size());
|
||||
for (int i = 0; i < expectedTokens.size(); i++) {
|
||||
assertEquals(expectedTokens.get(i), actualTokens.get(i).getText());
|
||||
assertEquals(expectedTokenTypes.get(i), SimpleJavaLexer.VOCABULARY.getSymbolicName(actualTokens.get(i).getType()));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user