Skip to content

Commit

Permalink
misc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauma109 committed May 18, 2023
1 parent 6730466 commit 59fe6aa
Show file tree
Hide file tree
Showing 12 changed files with 718 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void accept(DeclarationVisitor visitor) {
@Override
public int hashCode() {
int result = super.hashCode();
return 31 * result + (type == null ? 0 : type.hashCode());
return 31 * result + Objects.hashCode(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public IntegerConstantExpression(int lineNumber, Type type, int value) {
}
}

public IntegerConstantExpression(int lineNumber, int value) {
this(lineNumber, PrimitiveTypeUtil.getPrimitiveTypeFromValue(value), value);
}

@Override
public int getIntegerValue() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ public NewArray(int lineNumber, Type type, BaseExpression dimensionExpressionLis
}

public boolean isEmptyNewArray() {
if (dimensionExpressionList instanceof IntegerConstantExpression) {
IntegerConstantExpression integerConstantExpression = (IntegerConstantExpression) dimensionExpressionList;
if (integerConstantExpression.getIntegerValue() == 0) {
return true;
}
}
return false;
return dimensionExpressionList instanceof IntegerConstantExpression ice && ice.getIntegerValue() == 0;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public NewExpression(int lineNumber, ObjectType type, String descriptor, boolean

public NewExpression(int lineNumber, ObjectType type, String descriptor, BodyDeclaration bodyDeclaration, boolean varArgs, boolean diamondPossible) {
super(lineNumber);
this.type = type;
this.setType(type);
this.descriptor = descriptor;
this.bodyDeclaration = bodyDeclaration;
this.diamondPossible = diamondPossible;
Expand All @@ -52,7 +52,7 @@ public Type getType() {
}

public void setType(ObjectType type) {
this.type = type;
setObjectType(type);
}

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

import org.jd.core.v1.model.javasyntax.declaration.ArrayVariableInitializer;
import org.jd.core.v1.model.javasyntax.declaration.ExpressionVariableInitializer;
import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.jd.core.v1.model.javasyntax.type.Type;
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.assertNotEquals;

public class ArrayVariableInitializerTest {
private static final int LINE_NUMBER = 1;
private static final Type TEST_TYPE = ObjectType.TYPE_STRING;

@Test
public void testArrayVariableInitializer() {
Expression expression1 = new IntegerConstantExpression(1, 42);
Expression expression2 = new StringConstantExpression(2, "Hello");

ExpressionVariableInitializer variableInitializer1 = new ExpressionVariableInitializer(expression1);
ExpressionVariableInitializer variableInitializer2 = new ExpressionVariableInitializer(expression2);

ArrayVariableInitializer arrayInitializer1 = new ArrayVariableInitializer(TEST_TYPE);
arrayInitializer1.add(variableInitializer1);
arrayInitializer1.add(variableInitializer2);

ArrayVariableInitializer arrayInitializer2 = new ArrayVariableInitializer(TEST_TYPE);
arrayInitializer2.add(variableInitializer1);
arrayInitializer2.add(variableInitializer2);

assertEquals(2, arrayInitializer1.size());
assertEquals(variableInitializer1, arrayInitializer1.get(0));
assertEquals(variableInitializer2, arrayInitializer1.get(1));
assertEquals(TEST_TYPE, arrayInitializer1.getType());
assertEquals(1, arrayInitializer1.getLineNumber());

assertEquals(arrayInitializer1, arrayInitializer2); // Same elements
assertEquals(arrayInitializer2, arrayInitializer1); // Reversed order
assertNotEquals(arrayInitializer1, null);
assertNotEquals(arrayInitializer1, "Not an ArrayVariableInitializer");
assertEquals(arrayInitializer1, arrayInitializer1);

TestVisitor visitor = new TestVisitor();
arrayInitializer1.accept(visitor);
assertEquals(1, visitor.getArrayVariableInitializerCount());

int hashCode1 = arrayInitializer1.hashCode();
int hashCode2 = arrayInitializer2.hashCode();
assertEquals(hashCode1, hashCode2);
}

@Test
public void testAcceptWithTestVisitor() {
TestVisitor visitor = new TestVisitor();

ArrayVariableInitializer arrayInitializer = new ArrayVariableInitializer(TEST_TYPE);
NewInitializedArray newInitializedArray = new NewInitializedArray(LINE_NUMBER, TEST_TYPE, arrayInitializer);

newInitializedArray.accept(visitor);

assertEquals(1, visitor.getNewInitializedArrayCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.jd.core.v1.model.javasyntax.expression;

import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.junit.Test;

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

public class BaseExpressionTest implements BaseExpression {

@Override
public void accept(ExpressionVisitor visitor) {}

@Test
public void test() {
assertFalse(isArrayExpression());
assertFalse(isBinaryOperatorExpression());
assertFalse(isBooleanExpression());
assertFalse(isCastExpression());
assertFalse(isConstructorInvocationExpression());
assertFalse(isDoubleConstantExpression());
assertFalse(isFieldReferenceExpression());
assertFalse(isFloatConstantExpression());
assertFalse(isIntegerConstantExpression());
assertFalse(isLengthExpression());
assertFalse(isLocalVariableReferenceExpression());
assertFalse(isLongConstantExpression());
assertFalse(isMethodInvocationExpression());
assertFalse(isNew());
assertFalse(isNewArray());
assertFalse(isNewExpression());
assertFalse(isNewInitializedArray());
assertFalse(isNullExpression());
assertFalse(isObjectTypeReferenceExpression());
assertFalse(isPostOperatorExpression());
assertFalse(isPreOperatorExpression());
assertFalse(isStringConstantExpression());
assertFalse(isSuperConstructorInvocationExpression());
assertFalse(isSuperExpression());
assertFalse(isTernaryOperatorExpression());
assertFalse(isThisExpression());

assertEquals(NoExpression.NO_EXPRESSION, getDimensionExpressionList());
assertEquals(NoExpression.NO_EXPRESSION, getParameters());
assertEquals(NoExpression.NO_EXPRESSION, getCondition());
assertEquals(NoExpression.NO_EXPRESSION, getExpression());
assertEquals(NoExpression.NO_EXPRESSION, getTrueExpression());
assertEquals(NoExpression.NO_EXPRESSION, getFalseExpression());
assertEquals(NoExpression.NO_EXPRESSION, getIndex());
assertEquals(NoExpression.NO_EXPRESSION, getLeftExpression());
assertEquals(NoExpression.NO_EXPRESSION, getRightExpression());

assertEquals("", getDescriptor());
assertEquals(0D, getDoubleValue(), 0.00001D);
assertEquals(0F, getFloatValue(), 0.00001F);
assertEquals(0, getIntegerValue());
assertEquals("", getInternalTypeName());
assertEquals(0L, getLongValue());
assertEquals("", getName());
assertEquals(ObjectType.TYPE_UNDEFINED_OBJECT, getObjectType());
assertEquals("", getOperator());
assertEquals("", getStringValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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.Type;
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 FieldReferenceExpressionTest {

@Test
public void testFieldReferenceExpression() {
Type type = ObjectType.TYPE_STRING;
Expression expression = new ThisExpression(2, type);
String internalTypeName = "java.lang.String";
String name = "length";
String descriptor = "I";

FieldReferenceExpression fieldReferenceExpression = new FieldReferenceExpression(type, expression, internalTypeName, name, descriptor);

// Getters
assertEquals(type, fieldReferenceExpression.getType());
assertEquals(expression, fieldReferenceExpression.getExpression());
assertEquals(internalTypeName, fieldReferenceExpression.getInternalTypeName());
assertEquals(name, fieldReferenceExpression.getName());
assertEquals(descriptor, fieldReferenceExpression.getDescriptor());
assertTrue(fieldReferenceExpression.isFieldReferenceExpression());
assertEquals("FieldReferenceExpression{type=" + type + ", expression=" + expression + ", name=" + name + ", descriptor=" + descriptor + "}", fieldReferenceExpression.toString());

// Setters
Expression newExpression = new FieldReferenceExpression(3, type, null, internalTypeName, name, descriptor);
fieldReferenceExpression.setExpression(newExpression);
fieldReferenceExpression.setName("newName");

assertEquals(newExpression, fieldReferenceExpression.getExpression());
assertEquals("newName", fieldReferenceExpression.getName());

// Accept method
TestVisitor testVisitor = new TestVisitor();
fieldReferenceExpression.accept(testVisitor);

assertEquals(1, testVisitor.getFieldReferenceExpressionCount());

// Copy method
FieldReferenceExpression copiedExpression = (FieldReferenceExpression) fieldReferenceExpression.copyTo(4);
assertEquals(4, copiedExpression.getLineNumber());
assertEquals(type, copiedExpression.getType());
assertEquals(newExpression, copiedExpression.getExpression());
assertEquals(internalTypeName, copiedExpression.getInternalTypeName());
assertEquals("newName", copiedExpression.getName());
assertEquals(descriptor, copiedExpression.getDescriptor());
assertTrue(copiedExpression.isFieldReferenceExpression());
assertEquals("FieldReferenceExpression{type=" + type + ", expression=" + newExpression + ", name=newName, descriptor=" + descriptor + "}", copiedExpression.toString());
}
}
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.Type;
import org.jd.core.v1.service.converter.classfiletojavasyntax.visitor.TestVisitor;
import org.jd.core.v1.model.javasyntax.type.ObjectType;
import org.junit.Test;

import static org.junit.Assert.*;

public class NewArrayTest {
private static final int LINE_NUMBER = 1;
private static final Type TEST_TYPE = ObjectType.TYPE_OBJECT;

@Test
public void testNewArray() {
BaseExpression dimensionExpressionList = new IntegerConstantExpression(LINE_NUMBER, 0);
NewArray newArray = new NewArray(LINE_NUMBER, TEST_TYPE, dimensionExpressionList);

assertTrue(newArray.isEmptyNewArray());
assertSame(dimensionExpressionList, newArray.getDimensionExpressionList());

BaseExpression newDimensionExpressionList = new IntegerConstantExpression(LINE_NUMBER, 1);
newArray.setDimensionExpressionList(newDimensionExpressionList);

assertFalse(newArray.isEmptyNewArray());
assertSame(newDimensionExpressionList, newArray.getDimensionExpressionList());

assertEquals(0, newArray.getPriority());
assertTrue(newArray.isNewArray());
assertTrue(newArray.isNew());

String expectedString = "NewArray{" + TEST_TYPE + "}";
assertEquals(expectedString, newArray.toString());

int newLineNumber = 2;
Expression copiedNewArray = newArray.copyTo(newLineNumber);

assertTrue(copiedNewArray instanceof NewArray);
assertEquals(newLineNumber, copiedNewArray.getLineNumber());
assertEquals(TEST_TYPE, ((NewArray) copiedNewArray).getType());
assertSame(newDimensionExpressionList, ((NewArray) copiedNewArray).getDimensionExpressionList());

TestVisitor testVisitor = new TestVisitor();
newArray.accept(testVisitor);
assertEquals(1, testVisitor.getNewArrayCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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.Type;
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.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class NewExpressionTest {

@Test
public void testNewExpression() {
int lineNumber = 1;
Type type = ObjectType.TYPE_STRING;
String descriptor = "Ljava/lang/String;";
boolean varArgs = true;
boolean diamondPossible = false;

NewExpression newExpression = new NewExpression(lineNumber, (ObjectType) type, descriptor, varArgs, diamondPossible);

// Getters
assertEquals(lineNumber, newExpression.getLineNumber());
assertEquals(type, newExpression.getObjectType());
assertEquals(type, newExpression.getType());
assertEquals(descriptor, newExpression.getDescriptor());
assertNull(newExpression.getParameters());
assertNull(newExpression.getBodyDeclaration());
assertTrue(newExpression.isVarArgs());
assertFalse(newExpression.isDiamondPossible());
assertTrue(newExpression.isNewExpression());
assertTrue(newExpression.isNew());
assertEquals("NewExpression{new " + type + "}", newExpression.toString());

// Setters
BaseExpression parameters = new IntegerConstantExpression(2, 42);
newExpression.setParameters(parameters);
newExpression.setDiamondPossible(true);
newExpression.setQualifier(new ThisExpression(3, type));

assertEquals(parameters, newExpression.getParameters());
assertTrue(newExpression.isDiamondPossible());
assertNotNull(newExpression.getQualifier());

// Accept method
TestVisitor testVisitor = new TestVisitor();
newExpression.accept(testVisitor);

assertEquals(1, testVisitor.getNewExpressionCount());

// Copy method
NewExpression copiedExpression = (NewExpression) newExpression.copyTo(4);
assertEquals(4, copiedExpression.getLineNumber());
assertEquals(type, copiedExpression.getType());
assertEquals(descriptor, copiedExpression.getDescriptor());
assertNull(copiedExpression.getParameters());
assertNull(copiedExpression.getBodyDeclaration());
assertTrue(copiedExpression.isVarArgs());
assertTrue(copiedExpression.isDiamondPossible());
assertTrue(copiedExpression.isNewExpression());
assertEquals("NewExpression{new " + type + "}", copiedExpression.toString());

// Priority
assertEquals(0, newExpression.getPriority());
}

}
Loading

0 comments on commit 59fe6aa

Please sign in to comment.