Tests, Structure, More #10

Merged
i22005 merged 15 commits from Tests into main 2024-06-21 16:16:54 +00:00
3 changed files with 60 additions and 34 deletions
Showing only changes of commit 1132ff015c - Show all commits

View File

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

View File

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

View File

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