Search in sources :

Example 1 with TestCodeVisitor

use of org.evosuite.testcase.TestCodeVisitor in project evosuite by EvoSuite.

the class AbstractStatement method getCode.

/**
 * {@inheritDoc}
 */
@Override
public String getCode(Throwable exception) {
    TestCodeVisitor visitor = new TestCodeVisitor();
    visitor.setException(this, exception);
    visitor.visitStatement(this);
    String code = visitor.getCode();
    return code.substring(0, code.length() - 2);
}
Also used : TestCodeVisitor(org.evosuite.testcase.TestCodeVisitor)

Example 2 with TestCodeVisitor

use of org.evosuite.testcase.TestCodeVisitor in project evosuite by EvoSuite.

the class DifferenceFitnessFunction method getSyntacticSimilarity.

/**
 * Determine the syntactic distance between the string representation of two
 * test cases.
 *
 * @param individual
 * @return
 */
private double getSyntacticSimilarity(TestChromosome individual) {
    // The method toCode converts a test case to its JUnit representation
    String originalString = originalTest.toCode();
    // System.out.println("\n orgString: "+ originalString);
    // The length of test cases varies during the search
    // therefore the comparison needs to be on a test case
    // where we have removed all the irrelevant statements
    TestCaseMinimizer minimizer = new TestCaseMinimizer(originalCoveredGoals);
    TestChromosome copy = (TestChromosome) individual.clone();
    minimizer.minimize(copy);
    TestCodeVisitor visitor = new TestCodeVisitor();
    copy.getTestCase().accept(visitor);
    TestCodeVisitor workaroundVisitor = new TestCodeVisitor();
    // <-- new add, replace = original
    originalTest.accept(workaroundVisitor);
    Set<String> varNames = new HashSet<String>(visitor.getVariableNames());
    varNames.addAll(workaroundVisitor.getVariableNames());
    Set<String> classNames = new HashSet<String>(visitor.getClassNames());
    classNames.addAll(workaroundVisitor.getClassNames());
    // System.out.println(newString);
    String newString = visitor.getCode();
    if (!newString.isEmpty()) {
        if (loopNo == 0) {
            TokenSlicer ts1 = new TokenSlicer(originalString, varNames, classNames);
            originalTokenRecord = ts1.getTokenRecord();
            originalTypeRecord = ts1.getTypeRecord();
            loopNo++;
        }
        TokenSlicer ts2 = new TokenSlicer(newString, varNames, classNames);
        replaceTokenRecord = ts2.getTokenRecord();
        replaceTypeRecord = ts2.getTypeRecord();
        /*
			System.out.println("\noriginal:");
			for(int i=0; i<=originalTokenRecord.size();i++){
				System.out.print(originalTokenRecord.get(i)+ " ");
			}
			System.out.println("\n"+originalTokenRecord +"\n replace: ");
			for(int i=0; i<=replaceTypeRecord.size();i++){
				System.out.print(replaceTypeRecord.get(i)+ " ");
			}*/
        // System.out.println("\nreplaced test token:\n "+replaceTokenRecord);
        // System.out.println("replaced test type: \n"+replaceTypeRecord);
        // System.out.println("replaced test case: "+newString);
        nw = new NWAlgo(originalTokenRecord, replaceTokenRecord, originalTypeRecord, replaceTypeRecord);
        double nwDistance = nw.getDistance();
        return nwDistance;
    }
    return originalString.length() + 10;
}
Also used : TestCaseMinimizer(org.evosuite.testcase.TestCaseMinimizer) TestCodeVisitor(org.evosuite.testcase.TestCodeVisitor) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Aggregations

TestCodeVisitor (org.evosuite.testcase.TestCodeVisitor)2 HashSet (java.util.HashSet)1 TestCaseMinimizer (org.evosuite.testcase.TestCaseMinimizer)1 TestChromosome (org.evosuite.testcase.TestChromosome)1