use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testLimit.
@Test
public void testLimit() throws Exception {
TestCase tc = new DefaultTestCase();
final int LIMIT_5 = 5;
Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = LIMIT_5;
final int LOOP_0 = 0, LOOP_3 = 3, LOOP_5 = 5, LOOP_7 = 7;
IntPrimitiveStatement x = new IntPrimitiveStatement(tc, LOOP_3);
VariableReference loop = tc.addStatement(x);
VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, true));
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("limit", Foo.class, int.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock, loop)));
// execute first time with default mock
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 3 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef));
// before re-executing, change loops to the limit
x.setValue(LOOP_5);
execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(LOOP_5 - LOOP_3, types.size());
for (Type t : types) {
Assert.assertEquals(boolean.class, t);
}
// add the 2 missing values
mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef));
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// before re-executing 3rd time, change loops above the limit
x.setValue(LOOP_7);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// decrease, but to the limit, so still no required change
x.setValue(LOOP_5);
execute(tc);
// no update should be required
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
// further decrease, but now we need to remove parameters
x.setValue(LOOP_3);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_3, mockStmt.getNumParameters());
// remove all
x.setValue(LOOP_0);
execute(tc);
// do update
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
types = mockStmt.updateMockedMethods();
// but no new types to add
Assert.assertEquals(0, types.size());
Assert.assertEquals(LOOP_0, mockStmt.getNumParameters());
}
use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_package.
@Test
public void testPackageLevel_differentPackage_instrumentation_package() throws Exception {
TestCase tc = new DefaultTestCase();
ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
InstrumentingClassLoader loader = new InstrumentingClassLoader();
Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
VariableReference ref = new VariableReferenceImpl(tc, example);
try {
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
fail();
} catch (java.lang.IllegalArgumentException e) {
// expected
}
// tc.addStatement(mockStmt);
// execute(tc);
}
use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.
the class PrimitiveExpression method copy.
/**
* {@inheritDoc}
*/
@Override
public Statement copy(TestCase newTestCase, int offset) {
VariableReference newRetVal = new VariableReferenceImpl(newTestCase, retval.getType());
VariableReference newLeftOperand = newTestCase.getStatement(leftOperand.getStPosition()).getReturnValue();
VariableReference newRightOperand = newTestCase.getStatement(rightOperand.getStPosition()).getReturnValue();
return new PrimitiveExpression(newTestCase, newRetVal, newLeftOperand, operator, newRightOperand);
// return new PrimitiveExpression(newTestCase, retval, leftOperand, operator, rightOperand);
}
use of org.evosuite.testcase.variable.VariableReferenceImpl 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.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.
the class FunctionalMockStatementTest method testAll_twice.
@Test
public void testAll_twice() throws Exception {
TestCase tc = new DefaultTestCase();
VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
VariableReference mock = tc.addStatement(mockStmt);
VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_twice", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
Assert.assertEquals(0, mockStmt.getNumParameters());
// execute first time with default mock
Scope scope = execute(tc);
Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
List<Type> types = mockStmt.updateMockedMethods();
Assert.assertEquals(14, types.size());
}
Aggregations