Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mutation coverage fix within lambdas #1362

Merged
merged 6 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ExcludedAnnotationInterceptor implements MutationInterceptor {

Expand All @@ -24,7 +26,6 @@ public class ExcludedAnnotationInterceptor implements MutationInterceptor {
private boolean skipClass;
private Predicate<MutationDetails> annotatedMethodMatcher;


ExcludedAnnotationInterceptor(List<String> configuredAnnotations) {
this.configuredAnnotations = configuredAnnotations;
}
Expand All @@ -39,17 +40,56 @@ public void begin(ClassTree clazz) {
this.skipClass = clazz.annotations().stream()
.anyMatch(avoidedAnnotation());
if (!this.skipClass) {
final List<Predicate<MutationDetails>> methods = clazz.methods().stream()
// 1. Collect methods with avoided annotations
final List<MethodTree> avoidedMethods = clazz.methods().stream()
.filter(hasAvoidedAnnotation())
.collect(Collectors.toList());

final Set<String> avoidedMethodNames = avoidedMethods.stream()
.map(method -> method.rawNode().name)
.collect(Collectors.toSet());

// 2. Collect lambda methods with being inside avoided methods
final List<MethodTree> lambdaMethods = clazz.methods().stream()
.filter(MethodTree::isGeneratedLambdaMethod)
.filter(lambdaMethod -> {
String lambdaName = lambdaMethod.rawNode().name; // e.g., lambda$fooWithLambdas$0
String enclosingMethodName = extractEnclosingMethodName(lambdaName);
Copy link
Owner

@hcoles hcoles Nov 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is based solely on method name. If you were to add the other suggested tests they would fail as all lambdas in all the overloads of a method would be filtered if any one of the overloads was annotated.

The method signature must also be taken into account.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the tests. Also, the logic should be adjusted to overridden methods. Let me know if there's something I miss...Thanks

Copy link
Contributor Author

@see-quick see-quick Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm interesting that the build is failing after 1m but locally my build is successful without any problems.

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for pitest-parent dev-SNAPSHOT:
[INFO] 
[INFO] pitest-parent ...................................... SUCCESS [  0.365 s]
[INFO] Build Config ....................................... SUCCESS [  0.295 s]
[INFO] pitest ............................................. SUCCESS [  6.108 s]
[INFO] pitest-entry ....................................... SUCCESS [ 51.068 s]
[INFO] pitest-html-report ................................. SUCCESS [  0.782 s]
[INFO] pitest-aggregator .................................. SUCCESS [  0.795 s]
[INFO] pitest-maven ....................................... SUCCESS [  5.872 s]
[INFO] pitest-command-line ................................ SUCCESS [  0.637 s]
[INFO] pitest-ant ......................................... SUCCESS [  0.626 s]
[INFO] pitest-java8-verification .......................... SUCCESS [  3.993 s]
[INFO] pitest-maven-verification .......................... SUCCESS [02:09 min]
[INFO] pitest-modern-verification ......................... SUCCESS [  0.670 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:20 min
[INFO] Finished at: 2024-11-08T10:39:43+01:00
[INFO] ------------------------------------------------------------------------

with mvn clean install
and

Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /opt/homebrew/Cellar/maven/3.9.9/libexec
Java version: 17.0.12, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk@17/17.0.12/libexec/openjdk.jdk/Contents/Home
Default locale: en_SK, platform encoding: UTF-8

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think overrides need to be considered, just overloads within the same class. Although it is possible to navigate the class hierarchy from the bytecode, it becomes quite expensive when there are a lot of mutants.

Unhelpfully, I used the word override when I meant overload earlier when describing the test cases.

The failing test is likely because the suite runs a matrix of different java versions. Sometimes the bytecode generated differs substantially with version. Tests need to be careful what they assert on in case they are making implicit assumptions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated for overloads. Tested also with my code and it seems to work fine. Build locally is passing now I have to figure it out to fix it also upstream :D


return avoidedMethodNames.contains(enclosingMethodName);
})
.collect(Collectors.toList());

// 3. Merge the two lists into a single list and cast MethodTree to Predicate<MutationDetails>
final List<Predicate<MutationDetails>> mutationPredicates = Stream.concat(avoidedMethods.stream(), lambdaMethods.stream())
.map(AnalysisFunctions.matchMutationsInMethod())
.collect(Collectors.toList());
this.annotatedMethodMatcher = Prelude.or(methods);

this.annotatedMethodMatcher = Prelude.or(mutationPredicates);
}
}

/**
* TODO: maybe move to MethodTree class?? WDYT?
* Extracts the enclosing method name from a lambda method's name.
* Assumes lambda methods follow the naming convention: lambda$enclosingMethodName$number
*
* @param lambdaName The name of the lambda method (e.g., "lambda$fooWithLambdas$0")
* @return The name of the enclosing method (e.g., "fooWithLambdas")
*/
private String extractEnclosingMethodName(String lambdaName) {
int firstDollar = lambdaName.indexOf('$');
int secondDollar = lambdaName.indexOf('$', firstDollar + 1);

if (firstDollar != -1 && secondDollar != -1) {
return lambdaName.substring(firstDollar + 1, secondDollar);
}
return lambdaName;
}

private Predicate<MethodTree> hasAvoidedAnnotation() {
return a -> a.annotations().stream()
.anyMatch(avoidedAnnotation());
return methodTree ->
methodTree.annotations().stream().anyMatch(avoidedAnnotation());
}

private Predicate<AnnotationNode> avoidedAnnotation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ public void shouldFilterMethodsWithGeneratedAnnotation() {
assertThat(actual.iterator().next().getId().getLocation().getMethodName()).isEqualTo("bar");
}

@Test
public void shouldFilterMethodsWithGeneratedAnnotationAndLambdasInside() {
final List<MutationDetails> mutations = this.mutator.findMutations(ClassName.fromClass(ClassAnnotatedWithGeneratedWithLambdas.class));
final Collection<MutationDetails> actual = runWithTestee(mutations, ClassAnnotatedWithGeneratedWithLambdas.class);
assertThat(actual).hasSize(3);

for (MutationDetails mutationDetails : actual) {
assertThat(mutationDetails.getId().getLocation().getMethodName()).isIn("barWithLambdas", "lambda$barWithLambdas$2", "lambda$barWithLambdas$3");
}
}

@Test
public void shouldHandleOverloadedMethods() {
final List<MutationDetails> mutations = this.mutator.findMutations(ClassName.fromClass(OverloadedMethods.class));
final Collection<MutationDetails> actual = runWithTestee(mutations, OverloadedMethods.class);
// Assume only one overloaded version is annotated
assertThat(actual).hasSize(2); // Assuming three methods: two overloaded (one annotated) and one regular
}

private Collection<MutationDetails> runWithTestee(
Collection<MutationDetails> input, Class<?> clazz) {
this.testee.begin(treeFor(clazz));
Expand All @@ -82,7 +101,6 @@ ClassTree treeFor(Class<?> clazz) {
return ClassTree.fromBytes(source.getBytes(clazz.getName()).get());
}


}

class UnAnnotated {
Expand Down Expand Up @@ -120,4 +138,48 @@ public void bar() {

}

class OverloadedMethods {
public void foo(int x) {
System.out.println("mutate me");
}

@TestGeneratedAnnotation
public void foo(String x) {
System.out.println("don't mutate me");
}

public void bar() {
System.out.println("mutate me");
}
}


class ClassAnnotatedWithGeneratedWithLambdas {

@TestGeneratedAnnotation
public void fooWithLambdas() {
System.out.println("don't mutate me");

Runnable runnable = () -> {
System.out.println("don't mutate me also in lambdas");

Runnable anotherOne = () -> {
System.out.println("don't mutate me also recursive lambdas");
};
};
}

public void barWithLambdas() {
System.out.println("mutate me");

Runnable runnable = () -> {
System.out.println("mutate me also in lambdas");

Runnable anotherOne = () -> {
System.out.println("mutate me also recursive lambdas");
};
};
}
}


Loading