-
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 Refaster templates for AbstractComparableAssert
s
#225
Conversation
0c73ba6
to
b502fbf
Compare
return ImmutableSet.of( | ||
assertThat(1.0 == 2.0).isTrue(), | ||
assertThat(1.0 != 2.0).isFalse(), | ||
assertThat((byte) 1 == (byte) 2).isTrue(), | ||
assertThat(1F == 2F).isTrue(), | ||
assertThat(1 == 2).isTrue(), | ||
assertThat(1L == 2L).isTrue(), | ||
assertThat((short) 1 == (short) 2).isTrue()); |
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.
At the beginning, I added separate templates for different primitive types like short
, long
, int
, float
and byte
.
However, the double
templates render all the other ones obsolete.
So I removed them and added the primitive types here in the test case.
2346fee
to
264ee51
Compare
264ee51
to
9d7b2fc
Compare
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.
Rebased and added a commit. Also added some changes that should be moved to a separate PR. Not yet done reviewing, but need to head to the train 😄
final class AssertJComparableTemplates { | ||
private AssertJComparableTemplates() {} | ||
|
||
static final class AbstractComparableAssertActualIsEqualByComparingToExpected< |
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.
Generally we'd name this type AssertThatIsEqualByComparingTo
; will push a proposal :)
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.
Was struggling quite a bit about how to name stuff here. Consulted @rickie with it as well.
But the proposal looks good to me.
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.
Yes you are right, this part isn't easy.
I recall discussing a template where we would've had clashes if we didn't use the parameter names in the Refaster template name. Where possible we indeed like to simplify and omit the AbstractComparable
, actual
and expected
.
In the previous case that is mentioned here we would've probably needed this setup because of the clashes 😉.
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.
Just saying: an extra argument for picking up the task of auto-suggesting/fixing Refaster template names. I expect this'll be a topic for most/all upcoming Refaster pull requests ;)
ImmutableSet<AbstractAssert<?, ?>> testAbstractDoubleAssertActualIsEqualToExpected() { | ||
return ImmutableSet.of( | ||
assertThat(1.0 == 2.0).isTrue(), | ||
assertThat(1.0 != 2.0).isFalse(), | ||
assertThat((byte) 1 == (byte) 2).isTrue(), | ||
assertThat(1F == 2F).isTrue(), | ||
assertThat(1 == 2).isTrue(), | ||
assertThat(1L == 2L).isTrue(), | ||
assertThat((short) 1 == (short) 2).isTrue()); | ||
} |
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.
Very nice that it applies to other types as well. In fact, I see that we can slightly simplify another check 👀
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.
Added another commit :)
@@ -45,6 +49,26 @@ AbstractDoubleAssert<?> after(AbstractDoubleAssert<?> doubleAssert, double n) { | |||
} | |||
} | |||
|
|||
static final class AssertThatIsEqualTo { |
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.
Perhaps AssertJNumberTemplates
is a better location for these checks; that matches e.g. the AssertThatIsOdd
check. Will have a look.
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.
Added two more commits. I'll circle back to this PR "soon", but it's close to done. Nice!
@BeforeTemplate | ||
AbstractBooleanAssert<?> before(boolean actual, boolean expected) { | ||
return Refaster.anyOf( | ||
assertThat(actual == expected).isTrue(), assertThat(actual != expected).isFalse()); | ||
} |
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.
Here and below I added boolean
support, but those cases don't belong in this class. Still pondering another setup.
@BeforeTemplate | ||
AbstractBooleanAssert<?> before(double actual, double expected) { | ||
return Refaster.anyOf( | ||
assertThat(actual == expected).isTrue(), assertThat(actual != expected).isFalse()); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
AbstractBooleanAssert<?> after(boolean actual, boolean expected) { | ||
return assertThat(actual).isEqualTo(expected); | ||
} |
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.
These checks apply to primitive number types only, so arguably me moving them to AssertJNumberTemplates
isn't correct either. Perhaps we need a synthetic AssertJPrimitiveTemplates
class. TBD.
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.
AssertJPrimitiveTemplates
sounds like a good idea. I was thinking whether the convention is always clear as to when to put it in there, but yeah it is. So I'd say it's a good idea.
// XXX: This template also matches (unlikely) expressions of the form `assertThat(someChar % | ||
// 2).isEqualTo(1)`, for which the replacement AssertJ expression is invalid. |
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.
Here and below: TBD whether I'll move the extra changes to a separate PR, or (i.c.w. a suitable commit message) keep them in this PR.
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.
To be 100% correct we could add a Matcher
for that? I'd be up for creating that in refaster-support
. WDYT?
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.
Indeed a Matcher
could help here. It would be niche, but still. If you're up for this, we should first extract aa820c5 from this branch, as that will simplify the testing of such Matcher
s. I can open a PR for that ~later today.
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.
^ I just opened #232; feel free to move the relevant changes to a PR on top of that one :)
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.
Left some answers, the code looks good. Not yet approving because we need to settle on some things 😄.
Thanks for opening the PR @CoolTomatos 🚀 !
@BeforeTemplate | ||
AbstractBooleanAssert<?> before(double actual, double expected) { | ||
return Refaster.anyOf( | ||
assertThat(actual == expected).isTrue(), assertThat(actual != expected).isFalse()); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
AbstractBooleanAssert<?> after(boolean actual, boolean expected) { | ||
return assertThat(actual).isEqualTo(expected); | ||
} |
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.
AssertJPrimitiveTemplates
sounds like a good idea. I was thinking whether the convention is always clear as to when to put it in there, but yeah it is. So I'd say it's a good idea.
// XXX: This template also matches (unlikely) expressions of the form `assertThat(someChar % | ||
// 2).isEqualTo(1)`, for which the replacement AssertJ expression is invalid. |
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.
To be 100% correct we could add a Matcher
for that? I'd be up for creating that in refaster-support
. WDYT?
Added a commit to move the templates to |
Thanks for tagging, I think your PR is in very qualified hands, though 🙂 Let's get @CoolTomatos a shiny sticker to tell the world of this project 😄 |
That sounds like a good idea 😄. |
7885a2c
to
b4a1581
Compare
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.
Rebased and added a commit. @rickie will extract some changes from this PR, but it LGTM otherwise.
Suggested commit message:
Introduce `AssertJComparableTemplates` and `AssertJPrimitiveTemplates` (#225)
import org.assertj.core.api.AbstractAssert; | ||
import tech.picnic.errorprone.refaster.test.RefasterTemplateTestCase; | ||
|
||
final class AssertJStringTemplatesTest implements RefasterTemplateTestCase { |
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.
^ We should update the class name. (Another thing to automate 😅)
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.
Another thing to automate 😅
I filed #233 :)
@@ -28,6 +29,7 @@ final class RefasterTemplatesTest { | |||
AssertJMapTemplates.class, | |||
AssertJObjectTemplates.class, | |||
AssertJOptionalTemplates.class, | |||
AssertJPrimitivesTemplates.class, |
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.
Seems we favour singular here:
AssertJPrimitivesTemplates.class, | |
AssertJPrimitiveTemplates.class, |
b4a1581
to
61ae225
Compare
Filed #237 and changed the base for this branch to that one. Now the diff looks clean 🚀 ! |
I assume you don't want to review anymore @ferdinand-swoboda ? If not, we can remove the request for review. |
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.
Forgot to approve after moving out some changes.
The code looks nice, thanks for the contribution @CoolTomatos 🚀 !
Suggested commit message LGTM.
774f8b7
to
cb9fc83
Compare
61ae225
to
d4f9dc4
Compare
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.
Rebased. We'll merge once #237 is merged. Thx for the PR @CoolTomatos!
…e evaluated as booleans.
d4f9dc4
to
12518ec
Compare
Rebased; will merge once built. |
❗ This PR is on top of #237 ❗
With assertJ, sometimes comparisons are evaluated as booleans.
The introduced templates replace them with methods from the
AbstractComparableAssert
API.For example
will be then replaced with
Further more,
will first be replaced with
and then