use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.
the class EqualsSymmetricContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
VariableReference a = variables.get(0);
VariableReference b = variables.get(1);
try {
Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
GenericMethod method = new GenericMethod(equalsMethod, a.getGenericClass());
// Create x = a.equals(b)
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] { b }));
VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
// Create y = b.equals(a);
Statement st2 = new MethodStatement(test, method, b, Arrays.asList(new VariableReference[] { a }));
VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
Statement newStatement = test.getStatement(y.getStPosition());
// Create assertEquals(x, y)
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(newStatement);
assertion.setSource(x);
assertion.setDest(y);
assertion.setValue(true);
newStatement.addAssertion(assertion);
newStatement.addComment("Violates contract a.equals(b) <-> b.equals(a)");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.
the class EqualsHashcodeContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
VariableReference a = variables.get(0);
VariableReference b = variables.get(1);
try {
Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
GenericMethod genericEqualsMethod = new GenericMethod(equalsMethod, a.getGenericClass());
GenericMethod genericHashCodeMethod = new GenericMethod(hashCodeMethod, a.getGenericClass());
// Create x = a.equals(b)
Statement st1 = new MethodStatement(test, genericEqualsMethod, a, Arrays.asList(new VariableReference[] { b }));
VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
// Create y = a.hashCode();
Statement st2 = new MethodStatement(test, genericHashCodeMethod, a, Arrays.asList(new VariableReference[] {}));
VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
// Create z = b.hashCode();
Statement st3 = new MethodStatement(test, genericHashCodeMethod, b, Arrays.asList(new VariableReference[] {}));
VariableReference z = test.addStatement(st3, statement.getPosition() + 3);
// Create w = z == z
VariableReference w = new VariableReferenceImpl(test, boolean.class);
PrimitiveExpression exp = new PrimitiveExpression(test, w, y, Operator.EQUALS, z);
w = test.addStatement(exp, statement.getPosition() + 4);
Statement newStatement = test.getStatement(w.getStPosition());
// Create assertEquals(x, w)
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(newStatement);
assertion.setSource(x);
assertion.setDest(w);
assertion.setValue(true);
newStatement.addAssertion(assertion);
newStatement.addComment("Violates contract equals - hashcode");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.
the class EqualsContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(statement);
assertion.setSource(variables.get(0));
assertion.setDest(variables.get(0));
assertion.setValue(true);
statement.addAssertion(assertion);
statement.addComment("Violates contract a.equals(a)");
}
use of org.evosuite.assertion.EqualsAssertion in project evosuite by EvoSuite.
the class EqualsNullContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(statement);
VariableReference var = variables.get(0);
assertion.setSource(var);
assertion.setDest(new ConstantValue(statement.getTestCase(), Object.class));
// assertion.setDest(new NullReference(statement.getTestCase(), var.getType()));
assertion.setValue(false);
statement.addAssertion(assertion);
statement.addComment("Violates contract equals(null)");
}
Aggregations