Skip to content

Commit

Permalink
tests for if/while/do-while
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 21, 2023
1 parent d96482d commit 7aaf1f3
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface BaseStatement extends Base<Statement> {

default boolean isBreakStatement() { return false; }
default boolean isContinueStatement() { return false; }
default boolean isDoWhileStatement() { return false; }
default boolean isExpressionStatement() { return false; }
default boolean isForStatement() { return false; }
default boolean isIfStatement() { return false; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public BaseStatement getStatements() {
return statements;
}

@Override
public boolean isDoWhileStatement() { return true; }

@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jd.core.v1.model.javasyntax.statement;

import org.jd.core.v1.model.javasyntax.expression.Expression;
import org.jd.core.v1.model.javasyntax.expression.IntegerConstantExpression;
import org.jd.core.v1.service.converter.classfiletojavasyntax.visitor.TestVisitor;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class DoWhileStatementTest {

@Test
public void testDoWhileStatement() {
// Test constructor
Expression condition = new IntegerConstantExpression(1, 10);
BaseStatement statements = new Statements();
DoWhileStatement doWhileStatement = new DoWhileStatement(condition, statements);

assertTrue(doWhileStatement.getCondition() instanceof IntegerConstantExpression);
assertEquals(10, doWhileStatement.getCondition().getIntegerValue());
assertTrue(doWhileStatement.getStatements() instanceof Statements);
assertTrue(doWhileStatement.isDoWhileStatement());

// Test the setCondition method
Expression newCondition = new IntegerConstantExpression(1, 20);
doWhileStatement.setCondition(newCondition);
assertEquals(20, doWhileStatement.getCondition().getIntegerValue());

// Test the accept method with a simple visitor
TestVisitor visitor = new TestVisitor();
doWhileStatement.accept(visitor);
assertEquals(1, visitor.getDoWhileStatementCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jd.core.v1.model.javasyntax.statement;

import org.jd.core.v1.model.javasyntax.expression.Expression;
import org.jd.core.v1.model.javasyntax.expression.IntegerConstantExpression;
import org.jd.core.v1.service.converter.classfiletojavasyntax.visitor.TestVisitor;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class IfStatementTest {

@Test
public void testIfStatement() {
// Test constructor
Expression condition = new IntegerConstantExpression(1, 10);
BaseStatement statements = new Statements();
IfStatement ifStatement = new IfStatement(condition, statements);

assertTrue(ifStatement.getCondition() instanceof IntegerConstantExpression);
assertEquals(10, ifStatement.getCondition().getIntegerValue());
assertTrue(ifStatement.getStatements() instanceof Statements);
assertTrue(ifStatement.isIfStatement());

// Test the setCondition method
Expression newCondition = new IntegerConstantExpression(1, 20);
ifStatement.setCondition(newCondition);
assertEquals(20, ifStatement.getCondition().getIntegerValue());

// Test the accept method with a simple visitor
TestVisitor visitor = new TestVisitor();
ifStatement.accept(visitor);
assertEquals(1, visitor.getIfStatementCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jd.core.v1.model.javasyntax.statement;

import org.jd.core.v1.model.javasyntax.expression.Expression;
import org.jd.core.v1.model.javasyntax.expression.IntegerConstantExpression;
import org.jd.core.v1.service.converter.classfiletojavasyntax.visitor.TestVisitor;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class WhileStatementTest {

@Test
public void testWhileStatement() {
// Test constructor
Expression condition = new IntegerConstantExpression(1, 10);
BaseStatement statements = new Statements();
WhileStatement whileStatement = new WhileStatement(condition, statements);

assertTrue(whileStatement.getCondition() instanceof IntegerConstantExpression);
assertEquals(10, whileStatement.getCondition().getIntegerValue());
assertTrue(whileStatement.getStatements() instanceof Statements);
assertTrue(whileStatement.isWhileStatement());

// Test the setCondition method
Expression newCondition = new IntegerConstantExpression(1, 20);
whileStatement.setCondition(newCondition);
assertEquals(20, whileStatement.getCondition().getIntegerValue());

// Test the accept method with a simple visitor
TestVisitor visitor = new TestVisitor();
whileStatement.accept(visitor);
assertEquals(1, visitor.getWhileStatementCount());
}
}

0 comments on commit 7aaf1f3

Please sign in to comment.