Skip to content

Commit

Permalink
Skip nested classes for document examples
Browse files Browse the repository at this point in the history
Since there's often more than one, and all got a `@DocumentExample`
  • Loading branch information
timtebeek committed Dec 8, 2023
1 parent d6fe177 commit 5306c1b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,29 @@ void test1() {
)
);
}

@Test
void skipNestedClasses() {
rewriteRun(
java(
"""
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.test.RewriteTest;
import static org.openrewrite.test.SourceSpecs.text;
class OuterClass implements RewriteTest {
@Nested
class InnerClass {
@Test
void test1() {
rewriteRun(text("before", "after"));
}
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
public class SelectRecipeExamples extends Recipe {

private static final String DOCUMENT_EXAMPLE_ANNOTATION_FQN = "org.openrewrite.DocumentExample";
private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api" +
".Test");
private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Test");
private static final AnnotationMatcher ISSUE_ANNOTATION_MATCHER = new AnnotationMatcher("@org.openrewrite.Issue");
private static final AnnotationMatcher DISABLED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter" +
".api.Disabled");
private static final AnnotationMatcher DISABLED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Disabled");
private static final AnnotationMatcher NESTED_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Nested");
private static final AnnotationMatcher DOCUMENT_EXAMPLE_ANNOTATION_MATCHER =
new AnnotationMatcher("@" + DOCUMENT_EXAMPLE_ANNOTATION_FQN);

Expand Down Expand Up @@ -101,6 +100,12 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
return method;
}

J.ClassDeclaration clazz = getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance).getValue();
boolean insideNestedClass = clazz != null && clazz.getLeadingAnnotations().stream().anyMatch(NESTED_ANNOTATION_MATCHER::matches);
if (insideNestedClass) {
return method;
}

// a good recipe example should have both before and after.
boolean isAGoodExample = new JavaIsoVisitor<AtomicBoolean>() {
@Override
Expand Down

0 comments on commit 5306c1b

Please sign in to comment.