/** * {@inheritDoc} */ @Override public Set<Assertion> getAssertions(OutputTraceEntry other) { Set<Assertion> assertions = new HashSet<Assertion>(); if (other instanceof PrimitiveTraceEntry) { PrimitiveTraceEntry otherEntry = (PrimitiveTraceEntry) other; if (otherEntry != null && otherEntry.value != null && value != null && var.getStPosition() == otherEntry.var.getStPosition()) { if (!value.equals(otherEntry.value)) { double distance = ObjectDistanceCalculator.getObjectDistance(value, otherEntry.value); if (distance == 0) { return assertions; } PrimitiveAssertion assertion = new PrimitiveAssertion(); assertion.value = value; assertion.source = var; if (Properties.isRegression()) { assertion.setComment("// (Primitive) Original Value: " + value + " | Regression Value: " + otherEntry.value); } assertions.add(assertion); assert (assertion.isValid()); } } } return assertions; }
/** * {@inheritDoc} */ @Override public Set<Assertion> getAssertions() { Set<Assertion> assertions = new HashSet<Assertion>(); PrimitiveAssertion assertion = new PrimitiveAssertion(); assertion.source = var; assertion.value = value; assertions.add(assertion); assert (assertion.isValid()); return assertions; }
@Test public void testUnnecessaryDownCast() throws NoSuchMethodException { TestCaseBuilder builder = new TestCaseBuilder(); VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor()); VariableReference int0 = builder.appendIntPrimitive(42); VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getANumber", new Class<?>[] {int.class}), int0); num0.setType(Integer.class); // This would be set during execution VariableReference boolean0 = builder.appendMethod(var, DowncastExample.class.getMethod("testMe", new Class<?>[] {Number.class}), num0); PrimitiveAssertion assertion = new PrimitiveAssertion(); assertion.setSource(boolean0); assertion.setValue(false); DefaultTestCase test = builder.getDefaultTestCase(); test.getStatement(boolean0.getStPosition()).addAssertion(assertion); test.removeDownCasts(); assertEquals(Number.class, test.getStatement(2).getReturnClass()); }
@Test public void testAssertionsIncludeEnums() { EvoSuite evosuite = new EvoSuite(); String targetClass = ExampleReturningEnum.class.getCanonicalName(); String[] command = new String[] { "-generateSuite", "-class", targetClass, "-Dassertion_strategy=all" }; Object result = evosuite.parseCommandLine(command); GeneticAlgorithm<?> ga = getGAFromResult(result); TestSuiteChromosome suite = (TestSuiteChromosome) ga.getBestIndividual(); System.out.println(suite.toString()); Assert.assertTrue(suite.size() > 0); for (TestCase test : suite.getTests()) { boolean hasEnumAssertion = false; for(Assertion ass : test.getAssertions()) { if(ass instanceof PrimitiveAssertion) { Assert.assertTrue(((PrimitiveAssertion)ass).getValue().getClass().isEnum()); hasEnumAssertion = true; } } Assert.assertTrue("Test has no enum assertions: " + test.toCode(), hasEnumAssertion); } int goals = TestGenerationStrategy.getFitnessFactories().get(0).getCoverageGoals().size(); Assert.assertEquals("Wrong number of goals: ", 3, goals); Assert.assertEquals("Non-optimal coverage: ", 1d, suite.getCoverage(), 0.05); }
/** {@inheritDoc} */ @Override public Assertion copy(TestCase newTestCase, int offset) { PrimitiveAssertion s = new PrimitiveAssertion(); s.source = source.copy(newTestCase, offset); s.value = value; s.comment = comment; s.killedMutants.addAll(killedMutants); return s; }
@Test public void testNecessaryDownCast() throws NoSuchMethodException { TestCaseBuilder builder = new TestCaseBuilder(); VariableReference var = builder.appendConstructor(DowncastExample.class.getConstructor()); VariableReference int0 = builder.appendIntPrimitive(42); VariableReference num0 = builder.appendMethod(var, DowncastExample.class.getMethod("getANumber", new Class<?>[] {int.class}), int0); num0.setType(Integer.class); // This would be set during execution VariableReference boolean0 = builder.appendMethod(var, DowncastExample.class.getMethod("testWithInteger", new Class<?>[] {Integer.class}), num0); PrimitiveAssertion assertion = new PrimitiveAssertion(); assertion.setSource(boolean0); assertion.setValue(false); DefaultTestCase test = builder.getDefaultTestCase(); test.getStatement(boolean0.getStPosition()).addAssertion(assertion); test.removeDownCasts(); assertEquals(Integer.class, test.getStatement(2).getReturnClass()); }
for(Assertion ass : test.getAssertions()) { if(ass instanceof PrimitiveAssertion) { Assert.assertTrue(((PrimitiveAssertion)ass).getValue().getClass().isEnum()); hasEnumAssertion = true;