Skip to content

Commit

Permalink
added LengthExpressionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 21, 2023
1 parent dd289c0 commit 9cb8402
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public class LengthExpression extends AbstractLineNumberExpression {
private Expression expression;

public LengthExpression(Expression expression) {
this.expression = expression;
this(UNKNOWN_LINE_NUMBER, expression);
}

public LengthExpression(int lineNumber, Expression expression) {
super(lineNumber);
this.expression = expression;
this.setExpression(expression);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jd.core.v1.model.javasyntax.expression;

import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.jd.core.v1.model.javasyntax.type.PrimitiveType;
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 LengthExpressionTest {

@Test
public void testLengthExpression() {
// Array expression
ObjectType type = (ObjectType) ObjectType.TYPE_PRIMITIVE_INT.createType(1);
String name = "array";
Expression expression = new LocalVariableReferenceExpression(type, name);
int lineNumber = 0;
Expression index = new IntegerConstantExpression(lineNumber, PrimitiveType.TYPE_INT, 0);
Expression arrayExpression = new ArrayExpression(lineNumber, expression, index);

LengthExpression lengthExpression = new LengthExpression(arrayExpression);

// Test getExpression
assertEquals(arrayExpression, lengthExpression.getExpression());

// Test getType
assertEquals(PrimitiveType.TYPE_INT, lengthExpression.getType());

// Test isLengthExpression
assertTrue(lengthExpression.isLengthExpression());

// Test toString
assertEquals("LengthExpression{ArrayExpression{LocalVariableReferenceExpression{type=ObjectType{I, dimension=1}, name=array}[IntegerConstantExpression{type=PrimitiveType{primitive=int}, value=0}]}}", lengthExpression.toString());

// Test copyTo
LengthExpression copiedExpression = (LengthExpression) lengthExpression.copyTo(20);
assertEquals(lengthExpression.getExpression(), copiedExpression.getExpression());
assertEquals(20, copiedExpression.getLineNumber());

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

0 comments on commit 9cb8402

Please sign in to comment.