Changes in tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Lucas 2024-05-31 11:05:45 +02:00
parent 2a20a91d35
commit 1132ff015c
3 changed files with 60 additions and 34 deletions

View File

@ -120,6 +120,10 @@ public class Main {
}
public static void printAST(ASTNode node, int indent) {
if (node == null) {
System.out.println("null");
return;
}
String indentString = " ".repeat(indent * 2);
System.out.println(indentString + node.getClass().toString());

View File

@ -63,7 +63,7 @@ public class FailureTest {
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/EmptyClassExample.java"));
Main.parsefile(codeCharStream);
Main.parseFile(codeCharStream);
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}

View File

@ -1,6 +1,5 @@
package semantic;
import ast.*;
import ast.expression.BinaryExpressionNode;
import ast.expression.ExpressionNode;
@ -20,11 +19,10 @@ import ast.type.EnumTypeNode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import semantic.exeptions.AlreadyDeclearedException;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
public class SemanticTest {
@ -34,10 +32,9 @@ public class SemanticTest {
}
@Test
public void alreadyDeclaredLocalFieldVar(){
public void alreadyDeclaredLocalFieldVar() {
ProgramNode programNode = new ProgramNode();
List<ClassNode> classList = new ArrayList<ClassNode>();
List<ClassNode> classList = new ArrayList<>();
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
@ -53,16 +50,14 @@ public class SemanticTest {
ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
assertEquals(1, SemanticAnalyzer.errors.size());
assertEquals(true, SemanticAnalyzer.errors.get(0) instanceof AlreadyDeclearedException);
assertEquals(null, typedAst);
assertInstanceOf(AlreadyDeclearedException.class, SemanticAnalyzer.errors.getFirst());
assertNull(typedAst);
}
@Test
public void shouldWorkWithNoError(){
public void shouldWorkWithNoError() {
ProgramNode programNode = new ProgramNode();
List<ClassNode> classList = new ArrayList<ClassNode>();
List<ClassNode> classList = new ArrayList<>();
AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
@ -72,26 +67,7 @@ public class SemanticTest {
MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2");
classNode.members.add(memberNode2);
List<ParameterNode> parameterNodeList = new ArrayList<ParameterNode>();
ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
parameterNodeList.add(parameterNode1);
ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
List<StatementNode> statementNodeList = new ArrayList<StatementNode>();
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);
StatementNode statementNode1 = new AssignmentStatementNode(expressionNode);
statementNodeList.add(statementNode1);
MemberNode memberNode3 = new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2",parameterListNode, statementNodeList );
MemberNode memberNode3 = getMemberNode(accessTypeNode);
classNode.members.add(memberNode3);
classList.add(classNode);
@ -101,7 +77,53 @@ public class SemanticTest {
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);
}
}