openrewrite / openrewrite/rewrite
RewriteTest - pattern to validate recipes making non-content changes
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
consider a case like this:
@SuppressWarnings("DataFlowIssue")
@ExpectedToFail("fails because there's a reference change but no content diff but that's the point; would need to adjust RewriteTest")
@Test
void changeTypeAttributionImplicitUsage() {
rewriteRun(
spec -> spec.recipe(new ChangeType("java.util.List", "java.util.ArrayList", false)),
groovy(
"""
import java.util.Collections
class Test {
int zero = Collections.emptyList().size()
}
""",
spec -> spec.afterRecipe(cu -> {
J.VariableDeclarations varDecl = (J.VariableDeclarations) cu.getClasses().get(0).getBody().getStatements().get(0);
J.MethodInvocation sizeMi = (J.MethodInvocation) varDecl.getVariables().get(0).getInitializer();
assertThat(TypeUtils.isOfClassType(sizeMi.getMethodType().getDeclaringType(),
"java.util.ArrayList")).isTrue();
J.MethodInvocation emptyListMi = (J.MethodInvocation) sizeMi.getSelect();
assertThat(TypeUtils.isOfClassType(emptyListMi.getMethodType().getReturnType(),
"java.util.ArrayList")).isTrue();
})
)
);
}
The recipe makes changes to type attribution in the tree, but, that's not a "visible" change, so the rewriteRun logic auto-fails the test (due to the condition which checks for reference changes without content changes). It would be nice to have some mechanism to run tests like this.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in RewriteTest's rewriteRun logic, especially the condition that rejects reference changes without content changes, and compare it with the changeTypeAttributionImplicitUsage example. Define a test mechanism that permits assertions on type attribution when source text is unchanged, then run that test to verify it no longer auto-fails while still checking the attribution assertions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools, testing
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100