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

JavaTemplateSemanticallyEqual fails to match AssertJ's assertThat("foo").isEqualTo("") #4423

Closed
timtebeek opened this issue Aug 16, 2024 · 0 comments · Fixed by #4427
Closed
Labels
bug Something isn't working

Comments

@timtebeek
Copy link
Contributor

What version of OpenRewrite are you using?

https://github.com/openrewrite/rewrite/releases/tag/v8.33.4

Problem statement

This recipe replicates what's generated from our Refaster template generator:

Based on a Refaster rule defined at Picnic

We might want to quickly double check the String passed in to JavaTemplate.builder.

What is the smallest, simplest way to reproduce the problem?

import org.junit.jupiter.api.Test;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.tree.J;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.test.RewriteTest.toRecipe;

class JavaTemplateMatcherTest implements RewriteTest {

    @Test
    @Issue("https://github.com/openrewrite/rewrite-templating/pull/91")
    void shouldMatchAbstracTStringAssertIsEqualToEmptyString() {
        rewriteRun(
          spec -> spec
            .parser(JavaParser.fromJavaVersion().classpath("assertj-core"))
            .recipe(toRecipe(() -> new JavaIsoVisitor<>() {
                // Mimics what we saw in rewrite-templating
                final JavaTemplate before = JavaTemplate
                  .builder("#{stringAssert:any(org.assertj.core.api.AbstractStringAssert<?>)}.isEqualTo(\"\");")
                  .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath()))
                  .build();

                @Override
                public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
                    J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
                    return before.matches(getCursor()) ? SearchResult.found(mi) : mi;
                }
            })),
          //language=java
          java(
            """
              import static org.assertj.core.api.Assertions.assertThat;
              class Foo {
                  void test() {
                      assertThat("foo").isEqualTo("");
                  }
              }
              """,
            """
              import static org.assertj.core.api.Assertions.assertThat;
              class Foo {
                  void test() {
                      /*~~>*/assertThat("foo").isEqualTo("");
                  }
              }
              """
          )
        );

    }
}

What is the full stack trace of any errors you encountered?

java.lang.AssertionError: Recipe was expected to make a change but made no changes.
@timtebeek timtebeek added the bug Something isn't working label Aug 16, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Aug 16, 2024
knutwannheden added a commit that referenced this issue Aug 18, 2024
Try to fix #4423 by correcting the type attribution for some generics and the corresponding type assignability check.

Fixes: #4423
timtebeek added a commit that referenced this issue Aug 21, 2024
* Fixes for type attribution and `TypeUtils#isAssignableTo()`

Try to fix #4423 by correcting the type attribution for some generics and the corresponding type assignability check.

Fixes: #4423

* Better type attribution for `Type.CapturedType`

* Replicate code changes to Java 8, 11 and 21

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
@github-project-automation github-project-automation bot moved this from Backlog to Done in OpenRewrite Aug 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant