Fixed If Else Statements und If Else Tests

This commit is contained in:
i22035 2024-07-03 21:24:19 +02:00
parent ca539add98
commit d19748766f
38 changed files with 123 additions and 13 deletions

View File

@ -1,11 +0,0 @@
package ast.statement;
import ast.statements.IStatementNode;
import java.util.List;
public class BlockStatementNode {
List<IStatementNode> statements;
public BlockStatementNode(List<IStatementNode> statements) {this.statements = statements;}
}

View File

@ -281,8 +281,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
@Override
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
IfElseNode ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()),
(ElseNode) visit(ctx.elseStatement()));
IfElseNode ifElseStatementNode;
if(ctx.elseStatement() != null) {
ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()),
(ElseNode) visit(ctx.elseStatement()));
} else {
ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()), null);
}
for (SimpleJavaParser.ElseIfStatementContext elseIf : ctx.elseIfStatement()){
ifElseStatementNode.addElseIfStatement(((IfNode) visit(elseIf)));

View File

@ -333,6 +333,92 @@ class AstBuilderTest {
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("If Test")
public void ifTest() {
BlockNode block = new BlockNode();
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
BlockNode ifBlock = new BlockNode();
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
block.addStatement(new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), null));
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "If", block);
ClassNode class1 = new ClassNode("public", "If");
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST(directoryPath + "If.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("If Else Test")
public void ifElseTest() {
BlockNode block = new BlockNode();
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
BlockNode ifBlock = new BlockNode();
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
BlockNode elseBlock = new BlockNode();
elseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
block.addStatement(new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), new ElseNode(elseBlock)));
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "IfElse", block);
ClassNode class1 = new ClassNode("public", "IfElse");
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST(directoryPath + "IfElse.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("If Else If ElseTest")
public void ifElseIfElseTest() {
BlockNode block = new BlockNode();
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
BlockNode ifBlock = new BlockNode();
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
BlockNode elseBlock = new BlockNode();
elseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
IfElseNode ifElseStatement = new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), new ElseNode(elseBlock));
BlockNode ifElseBlock = new BlockNode();
ifElseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
ifElseStatement.addElseIfStatement(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "4"))), ifElseBlock));
block.addStatement(ifElseStatement);
block.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "IfElseIfElse", block);
ClassNode class1 = new ClassNode("public", "IfElseIfElse");
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST(directoryPath + "IfElseIfElse.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test
@DisplayName("Self Reference Test")
public void selfReferneceTest(){

View File

@ -0,0 +1,8 @@
public class If {
public If() {
int intValue = 5;
if(intValue == 5) {
intValue--;
}
}
}

View File

@ -0,0 +1,10 @@
public class IfElse {
public IfElse() {
int intValue = 5;
if(intValue == 5) {
intValue--;
} else {
intValue++;
}
}
}

View File

@ -0,0 +1,12 @@
public class IfElseIfElse {
public IfElseIfElse() {
int intValue = 5;
if(intValue == 5) {
intValue--;
} else if(intValue ==4) {
intValue++;
} else {
intValue++;
}
}
}