Reflections: not running
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Lucas 2024-06-30 18:33:01 +02:00
parent 4e56760dd6
commit bea71838ac
5 changed files with 88 additions and 204 deletions

View File

@ -6,6 +6,11 @@
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="maven_central" />
<option name="name" value="Maven Central" />
<option name="url" value="https://repo.maven.apache.org/maven2/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />

13
pom.xml
View File

@ -44,6 +44,12 @@
<version>3.26.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -78,4 +84,11 @@
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>maven_central</id>
<name>Maven Central</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>
</project>

View File

@ -0,0 +1,70 @@
package main;
import ast.ASTNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import parser.astBuilder.ASTBuilder;
import parser.generated.SimpleJavaLexer;
import parser.generated.SimpleJavaParser;
import semantic.SemanticAnalyzer;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
public class E2E_Reflections_Test {
private CharStream mockInputCharStream;
private String outputDirectoryPath;
@BeforeEach
public void setUp() {
mockInputCharStream = mock(CharStream.class);
outputDirectoryPath = "path/to/output";
}
@Test
public void testCompileFile() throws Exception {
// Mock the dependencies
SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class);
CommonTokenStream mockTokenStream = mock(CommonTokenStream.class);
SimpleJavaParser mockParser = mock(SimpleJavaParser.class);
ParseTree mockParseTree = mock(ParseTree.class);
ASTBuilder mockASTBuilder = mock(ASTBuilder.class);
ASTNode mockASTNode = mock(ASTNode.class);
SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
ASTNode mockTypedAST = mock(ASTNode.class);
ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class);
// Mock the behavior
when(mockLexer.nextToken()).thenReturn(null);
when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
when(mockSemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
// Use reflection to invoke the compileFile method
Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
compileFileMethod.setAccessible(true);
compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
// Verify each step
verify(mockLexer, times(1)).nextToken();
verify(mockTokenStream, times(1)).getTokens();
verify(mockParser, times(1)).program();
verify(mockASTBuilder, times(1)).visit(mockParseTree);
verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
}
}

View File

@ -1,103 +0,0 @@
package semantic;
import ast.*;
import ast.block.BlockNode;
import ast.member.FieldNode;
import ast.member.MethodNode;
import ast.parameter.ParameterNode;
import ast.type.AccessModifierNode;
import ast.type.type.*;
public class Mocker {
public static ASTNode mockTwoSameFields(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
c.identifier = "testClass";
FieldNode f1 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
c.members.add(f1);
FieldNode f2 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
c.members.add(f2);
p.classes.add(c);
return p;
}
public static ASTNode mockSimpleMethod(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
//Parameter
ParameterNode parameterNode = new ParameterNode(new BaseType(TypeEnum.INT), "a");
methodNode.addParameter(parameterNode);
//Statements
//Block
methodNode.block = new BlockNode();
c.members.add(methodNode);
p.classes.add(c);
return p;
}
public static ASTNode mockTwoSameMethods(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
methodNode.block = new BlockNode();
methodNode.setType(new BaseType(TypeEnum.INT));
methodNode.setIdentifier("testMethod");
c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode();
methodNode1.block = new BlockNode();
methodNode1.setType(new BaseType(TypeEnum.INT));
methodNode1.setIdentifier("testMethod");
c.members.add(methodNode1);
p.classes.add(c);
return p;
}
public static ASTNode mockTwoDifferentMethods(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
methodNode.block = new BlockNode();
methodNode.setIdentifier("testMethod");
c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode();
methodNode1.block = new BlockNode();
methodNode1.setIdentifier("testMethod1");
c.members.add(methodNode1);
p.classes.add(c);
return p;
}
}

View File

@ -1,101 +0,0 @@
package semantic;
public class SemanticTest {
// @Test
// public void alreadyDeclaredLocalFieldVar() {
// ProgramNode programNode = new ProgramNode();
// List<ClassNode> classList = new ArrayList<>();
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
//
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
// ASTNode tast = semanticAnalyzer.generateTast(ast);
//
// MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
// classNode.members.add(memberNode2);
//
// classList.add(classNode);
// programNode.classes = classList;
//
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//
// assertEquals(1, SemanticAnalyzer.errors.size());
// assertInstanceOf(AlreadyDeclaredException.class, SemanticAnalyzer.errors.getFirst());
// assertNull(typedAst);
// }
//
// @Test
// public void shouldWorkWithNoError() {
// ProgramNode programNode = new ProgramNode();
// List<ClassNode> classList = new ArrayList<>();
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
//
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
// ASTNode tast = semanticAnalyzer.generateTast(ast);
//
// assertEquals(semanticAnalyzer.errors.size(), 0);
// assertNotNull(tast);
//
// MemberNode memberNode3 = getMemberNode(accessTypeNode);
// classNode.members.add(memberNode3);
//
// classList.add(classNode);
// programNode.classes = classList;
//
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
//
// assertEquals(0, SemanticAnalyzer.errors.size());
// assertEquals(programNode, typedAst);
// }
//
// /**
// * This method is used to create a MemberNode representing a method.
// * It first creates a list of ParameterNodes and adds a ParameterNode to it.
// * Then, it creates a ParameterListNode using the list of ParameterNodes.
// * After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method.
// * Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method,
// * the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode.
// *
// * @param accessTypeNode The AccessTypeNode representing the access type of the method.
// * @return The created MemberNode representing the method.
// */
//private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
// List<ParameterNode> parameterNodeList = new ArrayList<>();
// ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
// parameterNodeList.add(parameterNode1);
// ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
//
// List<StatementNode> statementNodeList = new ArrayList<>();
//
// StatementNode statementNode1 = getStatementNode();
// statementNodeList.add(statementNode1);
//
// return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
//}
//
// /**
// * This method is used to create a StatementNode for an assignment operation.
// * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
// * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
// * After that, it creates a LiteralNode to represent the integer value 1.
// * Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1',
// * and wraps this expression in an AssignmentStatementNode.
// *
// * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
// */
//private static StatementNode getStatementNode() {
// ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
// ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
//
// ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
//
// ExpressionNode expressionNodeRight = new LiteralNode(1);
//
// BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
//
// return new AssignmentStatementNode(expressionNode);
//}
}