Skip to content

RewriteTest - pattern to validate recipes making non-content changes #3058

Open
@nmck257

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttest providedAlready replicated with a unit test, using JUnit pioneer's ExpectedToFail

    Type

    No type

    Projects

    • Status

      Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions