-
Notifications
You must be signed in to change notification settings - Fork 531
Fixed wildcard matching for elements with type information. #5833
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
Changes from all commits
24459b9
d675b76
e717136
13f0077
07d31e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ | |
| */ | ||
| package org.openrewrite.java; | ||
|
|
||
| import org.intellij.lang.annotations.Language; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
@@ -140,19 +141,63 @@ void matchesUnqualifiedJavaLangArguments() { | |
| void matchesArgumentsWithWildcards() { | ||
| assertTrue(argRegex("A foo(java.util.*)").matcher("java.util.Map").matches()); | ||
| assertTrue(argRegex("A foo(java..*)").matcher("java.util.Map").matches()); | ||
| assertTrue(argRegex("A foo(*.util.*)").matcher("java.util.Map").matches()); | ||
| assertTrue(argRegex("A foo(*..*)").matcher("java.util.Map").matches()); | ||
| } | ||
|
|
||
| @Test | ||
| void matchesExactlyOneWithWildcard() { | ||
| assertTrue(argRegex("A foo(*)").matcher("int").matches()); | ||
| assertTrue(argRegex("A foo(*)").matcher("java.lang.String").matches()); | ||
| assertTrue(argRegex("A foo(*, int)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(*,int)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(*, int)").matcher("double,int").matches()); | ||
| assertTrue(argRegex("A foo(*, int)").matcher("java.lang.String,int").matches()); | ||
| assertTrue(argRegex("A foo(*, String)").matcher("java.lang.String,java.lang.String").matches()); | ||
| assertTrue(argRegex("A foo(int, *)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(int, *)").matcher("int,double").matches()); | ||
| assertTrue(argRegex("A foo(int,*)").matcher("int,double").matches()); | ||
| assertTrue(argRegex("A foo(*, *)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(*,*)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(int, *, double)").matcher("int,int,double").matches()); | ||
| assertTrue(argRegex("A foo(int, *, double)").matcher("int,double,double").matches()); | ||
| assertTrue(argRegex("A foo(int,*,double)").matcher("int,double,double").matches()); | ||
|
|
||
| assertFalse(argRegex("A foo(*)").matcher("").matches()); | ||
| assertFalse(argRegex("A foo(*)").matcher("int,int").matches()); | ||
| assertFalse(argRegex("A foo(*, int)").matcher("int").matches()); | ||
| assertFalse(argRegex("A foo(*, int)").matcher("int,double").matches()); | ||
| assertFalse(argRegex("A foo(int, *)").matcher("int").matches()); | ||
| assertFalse(argRegex("A foo(int, *)").matcher("double,int").matches()); | ||
| assertFalse(argRegex("A foo(*, *)").matcher("").matches()); | ||
| assertFalse(argRegex("A foo(*, *)").matcher("int").matches()); | ||
| assertFalse(argRegex("A foo(int, *, double)").matcher("int,double").matches()); | ||
| assertFalse(argRegex("A foo(int, *, double)").matcher("double,int,double").matches()); | ||
| } | ||
|
|
||
| @Test | ||
| void matchesArgumentsWithDotDot() { | ||
| assertTrue(argRegex("A foo(.., int)").matcher("int").matches()); | ||
| assertTrue(argRegex("A foo(.., int)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(.., int)").matcher("double,int").matches()); | ||
| assertFalse(argRegex("A foo(.., int)").matcher("int,double").matches()); | ||
|
Jenson3210 marked this conversation as resolved.
|
||
|
|
||
| assertTrue(argRegex("A foo(int, ..)").matcher("int").matches()); | ||
| assertTrue(argRegex("A foo(int, ..)").matcher("int,int").matches()); | ||
| assertTrue(argRegex("A foo(int, ..)").matcher("int,double").matches()); | ||
| assertFalse(argRegex("A foo(int, ..)").matcher("double,int").matches()); | ||
|
|
||
| assertTrue(argRegex("A foo(int, .., double)").matcher("int,double").matches()); | ||
| assertTrue(argRegex("A foo(int, .., double)").matcher("int,int,double").matches()); | ||
| assertTrue(argRegex("A foo(int, .., double)").matcher("int,double,double").matches()); | ||
| assertFalse(argRegex("A foo(int, .., double)").matcher("double,int,double").matches()); | ||
|
|
||
| assertTrue(argRegex("A foo(..)").matcher("").matches()); | ||
| assertTrue(argRegex("A foo(..)").matcher("int").matches()); | ||
| assertTrue(argRegex("A foo(..)").matcher("int,int").matches()); | ||
|
|
||
| assertTrue(argRegex("A foo(.., int)").matcher("double,double,int").matches()); | ||
| assertTrue(argRegex("A foo(int, .., double)").matcher("int,double,java.lang.String,int,double").matches()); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -404,17 +449,42 @@ void matchUnknownTypesWildcardArguments() { | |
| void matchUnknownTypesSingleWildcardArgument() { | ||
| var mi = asMethodInvocation("Assert.assertTrue(Foo.bar(), \"message\");"); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, String)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, java.lang.String)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(String, *)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(double, *)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(java.lang.String, *)").matches(mi, true)); | ||
|
Comment on lines
+453
to
+455
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These three assertions surprise me. In this test the type of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that is how |
||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, *)").matches(mi, true)); | ||
| } | ||
|
|
||
| @Issue("https://github.com/openrewrite/rewrite/pull/5833") | ||
| @Test | ||
| void matchKnownTypesSingleWildcardArgument() { | ||
| var mi = asMethodInvocation("Assert.assertTrue(Foo.bar(), \"message\");", """ | ||
| class Foo { | ||
| static String bar() { | ||
| return "bar"; | ||
| } | ||
| } | ||
| """); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, String)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, java.lang.String)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(String, *)").matches(mi, true)); | ||
| assertFalse(new MethodMatcher("org.junit.Assert assertTrue(double, *)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(java.lang.String, *)").matches(mi, true)); | ||
| assertTrue(new MethodMatcher("org.junit.Assert assertTrue(*, *)").matches(mi, true)); | ||
| } | ||
|
|
||
| static J.MethodInvocation asMethodInvocation(String code) { | ||
| var cu = JavaParser.fromJavaVersion().build().parse( | ||
| String.format(""" | ||
| static J.MethodInvocation asMethodInvocation(String code, @Language("java") String... dependsOn) { | ||
| var cu = JavaParser.fromJavaVersion().dependsOn(dependsOn).build() | ||
| .parse( | ||
| """ | ||
| import org.junit.Assert; | ||
| class MyTest { | ||
| void test() { | ||
| %s | ||
| } | ||
| } | ||
| """, code) | ||
| """.formatted(code) | ||
| ) | ||
| .findFirst() | ||
| .map(J.CompilationUnit.class::cast) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.