public void inferTypes(TestCase test) { this.test = test; logger.debug("Inferring generic types"); // calculateExactTypes(); for (int i = test.size() - 1; i >= 0; i--) { Statement statement = test.getStatement(i); if (statement instanceof ConstructorStatement) { determineExactType((ConstructorStatement) statement); } } logger.debug("Resulting test: "+test.toCode()); }
/** * Add primitive statement at position * * @param test * @param old * @param position * @return * @throws ConstructionFailedException */ private VariableReference addPrimitive(TestCase test, PrimitiveStatement<?> old, int position) throws ConstructionFailedException { logger.debug("Adding primitive"); Statement st = old.clone(test); return test.addStatement(st, position); }
private boolean isTestUnstable() { return test!=null && test.isUnstable(); }
private void chopException(TestCase test, ExecutionResult result) { if (!result.noThrownExceptions()) { // No code including or after an exception should be in the pool Integer pos = result.getFirstPositionOfThrownException(); if (pos != null) { test.chop(pos); } else { test.chop(test.size() - 1); } } }
/** {@inheritDoc} */ @Override public String getTestString(int id, TestCase test, Map<Integer, Throwable> exceptions) { return test.toCode(exceptions); }
private void removeAssertionsAfterException(List<ExecutionResult> results) { for(ExecutionResult result : results) { if(result.noThrownExceptions()) continue; int exceptionPosition = result.getFirstPositionOfThrownException(); // TODO: Not clear how that can happen... if(result.test.size() > exceptionPosition) result.test.getStatement(exceptionPosition).removeAssertions(); } }
@Ignore @Test public void test3() { TestSuiteChromosome suite = generateSuite(ExampleInheritedClass.class); Assert.assertTrue(suite.size() > 0); for (TestCase test : suite.getTests()) { Assert.assertTrue("Test has no assertions: " + test.toCode(), test.hasAssertions()); } }
@Test public void testAssertionsIncludeContains() { //Properties.INLINE = false; EvoSuite evosuite = new EvoSuite(); String targetClass = ContainerExample.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 hasContainsAssertion = false; for(Assertion ass : test.getAssertions()) { if(ass instanceof ContainsAssertion) { hasContainsAssertion = true; } } Assert.assertTrue("Test has no contains assertions: " + test.toCode(), hasContainsAssertion); } } }
public static String getMethodIdentifier(ExecutionResult result, int exceptionPosition){ if (result.test.getStatement(exceptionPosition) instanceof MethodStatement) { MethodStatement ms = (MethodStatement) result.test.getStatement(exceptionPosition); Method method = ms.getMethod().getMethod(); return method.getName() + Type.getMethodDescriptor(method); } else if (result.test.getStatement(exceptionPosition) instanceof ConstructorStatement) { ConstructorStatement cs = (ConstructorStatement) result.test.getStatement(exceptionPosition); Constructor<?> constructor = cs.getConstructor().getConstructor(); return "<init>" + Type.getConstructorDescriptor(constructor); } return ""; }
@Test public void testCastAndBoxingInArray() { // short[] shortArray0 = new short[5]; // Long[] longArray0 = new Long[5]; // longArray0[0] = (Long) shortArray0[1]; <-- this gives a compile error TestCase tc = new DefaultTestCase(); ArrayStatement shortArrayStatement = new ArrayStatement(tc, short[].class, 5); tc.addStatement(shortArrayStatement); ArrayStatement longArrayStatement = new ArrayStatement(tc, Long[].class, 5); tc.addStatement(longArrayStatement); ArrayIndex longIndex = new ArrayIndex(tc, longArrayStatement.getArrayReference(), 0); ArrayIndex shortIndex = new ArrayIndex(tc, shortArrayStatement.getArrayReference(), 1); AssignmentStatement assignmentStatement = new AssignmentStatement(tc, longIndex, shortIndex); tc.addStatement(assignmentStatement); String code = tc.toCode(); System.out.println(tc); assertFalse(code.contains("longArray0[0] = (Long) shortArray0[1]")); }
@Ignore @Test public void test4() { TestSuiteChromosome suite = generateSuite(ExampleStaticVoidSetterClass.class); Assert.assertTrue(suite.size() > 0); for (TestCase test : suite.getTests()) { if (test.size() > 1) Assert.assertTrue("Test has no assertions: " + test.toCode(), test.hasAssertions()); } }
/** * {@inheritDoc} * * The size of a chromosome is the length of its test case */ @Override public int size() { return test.size(); }
@Test public void testTwoConstructorsDifferentClasses() { TestCase test = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("Foo", "<init>()"); test.addCoveredGoal(goal1); MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("Bar", "<init>()"); test.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); String generatedName = naming.getName(test); assertEquals("testCreatesBarAndCreatesFoo", generatedName); }
private VariableReference duplicateStatement(TestCase test, VariableReference owner) { Statement statement = test.getStatement(owner.getStPosition()); currentPosition++; VariableReference copy = test.addStatement(statement.clone(test), owner.getStPosition() + 1); if (!variableMapping.containsKey(owner.getStPosition())) { variableMapping.put(owner.getStPosition(), new HashSet<VariableReference>()); // variableMapping.get(owner.getStPosition()).add(owner); } variableMapping.get(owner.getStPosition()).add(copy); return copy; }
@Test public void testTwoTestsUniqueMethods() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString"); test1.addCoveredGoal(goal1); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "getSomeStuff"); test2.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testToString", naming.getName(test1)); assertEquals("testGetSomeStuff", naming.getName(test2)); }
@Test public void testInnerClassEnum() throws Throwable { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(EnumUser.class.getDeclaredConstructor(), EnumUser.class), 0, 0); EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, EnumInInnerClass.AnEnum.class); primitiveStatement.setValue(EnumInInnerClass.AnEnum.FOO); VariableReference enumObject = tc.addStatement(primitiveStatement); Method m = EnumUser.class.getDeclaredMethod("foo", EnumInInnerClass.AnEnum.class); GenericMethod gm = new GenericMethod(m, EnumUser.class); MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject)); tc.addStatement(ms); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception String code = visitor.getCode(); assertTrue(code.contains("= EnumInInnerClass.AnEnum.FOO")); }
/** * <p> * minimize * </p> * * @param test a {@link org.evosuite.testcase.TestChromosome} object. * @param objective a {@link org.evosuite.testcase.TestFitnessFunction} object. */ public void minimize(TestChromosome test, TestFitnessFunction objective) { this.objective = new TestMinimization(objective, test); test.test.accept(this); }
List<Assertion> assertions; Set<Integer> killed = new HashSet<Integer>(); assertions = test.getAssertions(); for (Assertion assertion : assertions) { for (Mutation m : executedMutants) {
protected void filterFailingAssertions(TestCase test) { // Make sure we are not keeping assertions influenced by static state // TODO: Need to handle statically initialized classes ExecutionResult result = runTest(test); Set<Assertion> invalidAssertions = new HashSet<Assertion>(); for(Assertion assertion : test.getAssertions()) { for(OutputTrace<?> outputTrace : result.getTraces()) { if(outputTrace.isDetectedBy(assertion)) { invalidAssertions.add(assertion); break; } } } logger.info("Removing {} nondeterministic assertions", invalidAssertions.size()); for(Assertion assertion : invalidAssertions) { test.removeAssertion(assertion); } }