Tests and Main

This commit is contained in:
Lucas 2024-05-14 15:47:49 +02:00
parent f781d8eeb6
commit dd424cda99
10 changed files with 94 additions and 15 deletions

2
.idea/misc.xml generated
View File

@ -40,7 +40,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -1,7 +1,6 @@
import ast.ASTNode;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.Token;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import org.antlr.v4.runtime.CharStream;
@ -11,7 +10,6 @@ import org.antlr.v4.runtime.CommonTokenStream;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import ast.ProgramNode;
import semantic.SemanticAnalyzer;
import bytecode.ByteCodeGenerator;
@ -23,12 +21,13 @@ public class Main {
public static void main(String[] args) throws Exception {
try {
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt"));
parseFile(codeCharStream);
parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
static void parsefile(CharStream codeCharStream) {
/* ------------------------- Scanner -> tokens ------------------------- */
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
@ -56,7 +55,6 @@ public class Main {
System.out.println(parseTree.toStringTree(parser));
printTree(parseTree, parser, 0);
System.out.println();
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(ast);
/* ------------------------- AST builder -> AST ------------------------- */
ASTBuilder astBuilder = new ASTBuilder();
@ -72,6 +70,7 @@ public class Main {
* ------------------------- Semantic Analyzer -> Tast -------------------------
*/
SemanticAnalyzer.generateTast(abstractSyntaxTree);
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Printing the Tast
System.out.println("Tast generated");
@ -81,7 +80,8 @@ public class Main {
* -------------------------
*/
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
byteCodeGenerator.generateByteCode(abstractSyntaxTree);
//byteCodeGenerator.generateByteCode(abstractSyntaxTree);
byteCodeGenerator.visit(typedAst);
System.out.println("Bytecode generated");
}
@ -126,6 +126,5 @@ public class Main {
// for (ASTNode child : node.) {
// printAST(child, indent + 1);
// }
byteCodeGenerator.visit(typedAst);
}
}

View File

@ -1,7 +1,12 @@
package ast;
public interface ASTNode {
//import java.util.List;
public interface ASTNode {
/**
* Please implement this method to return a list of children of each node.
*/
// public List<ASTNode> getChildren();
}

View File

@ -68,7 +68,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
public TypeCheckResult analyze(MethodNode methodNode) {
var valid = true;
currentLocalScope.pushScope();
// currentLocalScope.pushScope();
List<StatementNode> statements = methodNode.statements;
for (StatementNode statement : statements) {

View File

@ -1,6 +1,4 @@
package resources;
public class EmptyClassExample {
private class Inner {
}
}
} // -o für outout

View File

@ -0,0 +1,72 @@
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/failureTests/TestClass1.java",
"src/main/test/resources/failureTests/TestClass2.java",
"src/main/test/resources/failureTests/TestClass3.java",
"src/main/test/resources/failureTests/TestClass4.java",
"src/main/test/resources/failureTests/TestClass5.java",
"src/main/test/resources/failureTests/TestClass6.java",
"src/main/test/resources/failureTests/TestClass7.java",
"src/main/test/resources/failureTests/TestClass8.java",
"src/main/test/resources/failureTests/TestClass9.java",
"src/main/test/resources/failureTests/TestClass10.java",
"src/main/test/resources/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() {
// Get the system Java compiler
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// Assert that the compiler is available
assertNotNull(compiler, "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);
// 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());
// Assert that the compilation failed (i.e., the result is non-zero)
assertTrue(result != 0, "Expected compilation failure for " + fileName);
}
}
// 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/EmptyClassExample.java"));
Main.parsefile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
}

View File

@ -1,6 +1,11 @@
//import main.test.resources.EmptyClassExample.java
public class Tester {
public static void main(String[] args) {
// new EmptyClassExample();
new EmptyClassExample();
// cp mitgeben
}
}
// java -jar pfadtocompiler.jar EmptyClass.java
//mit bash scipt ode rmakefile test automatisieren
//mvn package
// javac tester // tester compilen
// java tester // tester ausführen

View File

View File

@ -1,4 +1,4 @@
// Syntax Error: Missing class body
public class TestClass4 {
public class TestClass4 {-
// Missing class body
}