-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce IsCharacter
matcher and assorted test improvements
#237
Changes from 1 commit
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.NotMatches; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
import java.math.BigDecimal; | ||
import java.math.BigInteger; | ||
|
@@ -18,6 +19,7 @@ | |
import org.assertj.core.api.AbstractLongAssert; | ||
import org.assertj.core.api.AbstractShortAssert; | ||
import org.assertj.core.api.NumberAssert; | ||
import tech.picnic.errorprone.refaster.util.IsCharacter; | ||
|
||
final class AssertJNumberTemplates { | ||
private AssertJNumberTemplates() {} | ||
|
@@ -226,9 +228,14 @@ AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> numberAssert) { | |
} | ||
} | ||
|
||
/** | ||
* Prefer {@link AbstractLongAssert#isOdd()} over more contrived alternatives. | ||
* | ||
* <p>Note that for {@link Character}s this rewrite would lead to non-compilable code. | ||
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. It's not about being non-compilable, but that the alternative is not available. That said, I still feel like this can be left out: it's implied, and people doubting can just try to see what happens if 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. Ah correct, should've been more specific here. |
||
*/ | ||
static final class AssertThatIsOdd { | ||
@BeforeTemplate | ||
AbstractIntegerAssert<?> before(int number) { | ||
AbstractIntegerAssert<?> before(@NotMatches(IsCharacter.class) int number) { | ||
return assertThat(number % 2).isEqualTo(1); | ||
} | ||
|
||
|
@@ -244,9 +251,14 @@ AbstractLongAssert<?> before(long number) { | |
} | ||
} | ||
|
||
/** | ||
* Prefer {@link AbstractLongAssert#isEven()} over more contrived alternatives. | ||
* | ||
* <p>Note that for {@link Character}s this rewrite would lead to non-compilable code. | ||
*/ | ||
static final class AssertThatIsEven { | ||
@BeforeTemplate | ||
AbstractIntegerAssert<?> before(int number) { | ||
AbstractIntegerAssert<?> before(@NotMatches(IsCharacter.class) int number) { | ||
return assertThat(number % 2).isEqualTo(0); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||
package tech.picnic.errorprone.refaster.util; | ||||||
|
||||||
import static com.google.errorprone.matchers.Matchers.anyOf; | ||||||
import static com.google.errorprone.matchers.Matchers.isSameType; | ||||||
|
||||||
import com.google.errorprone.VisitorState; | ||||||
import com.google.errorprone.matchers.Matcher; | ||||||
import com.sun.source.tree.ExpressionTree; | ||||||
|
||||||
/** A matcher of {@link Character}-typed expressions. */ | ||||||
public final class IsCharacter implements Matcher<ExpressionTree> { | ||||||
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. Was doubting a bit about the name. We flag both 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. Shall we "generify" this? 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. The idea of a
This setup limits the expressiveness of specifying what Good question though 😉. 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. To be more explicit about the limitation we're facing here: we can't pass constructor parameters. |
||||||
private static final long serialVersionUID = 1L; | ||||||
private static final Matcher<ExpressionTree> DELEGATE = | ||||||
anyOf(isSameType(Character.class), isSameType(s -> s.getSymtab().charType)); | ||||||
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 suspect that the second case is more common. I.c.w. a predefined supplier we can do:
Suggested change
|
||||||
|
||||||
@Override | ||||||
public boolean matches(ExpressionTree tree, VisitorState state) { | ||||||
return DELEGATE.matches(tree, state); | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not just
AbstractLongAssert#isOdd()
. Would also argue that the original code isn't contrived, but rather that it yields a less informative error message.