-
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 JUnitToAssertJRules
Refaster rule collection
#417
Conversation
db36cd7
to
45406fc
Compare
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
45406fc
to
b8b093f
Compare
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
7485fff
to
dc4122a
Compare
Looks good. No mutations were possible for these changes. |
dc4122a
to
372dac8
Compare
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
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 a few commits and rebased.
I'm slightly in doubt whether we should add Javadocs here @Stephan202.
For ordering parameters, should we adhere to the order in which they occur in the before or after template? We kind of use both options.
As we base most thinks on the content of the after template, it could make sense to do the same for this. This would also make more sense given that you can have multiple before templates (this also holds for aftertemplates technically speaking, but we don't do that within Picnic currently).
Not completely done with reviewing the PR but flushing my comments and considerations :).
final class JUnitToAssertJRules { | ||
private JUnitToAssertJRules() {} | ||
|
||
static final class Fail { |
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 see you used the TestNGToAssertJ
class as input for the naming, which makes total sense. However that class is not completely up-to-date with our current naming scheme. Instead of using the before template we now use the content of the after template to decide on the name.
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
void after(boolean condition, Supplier<String> messageSupplier) { |
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 think that supplier
would also suffice here. It's also used in the withFailMessage
.
@Override | ||
public ImmutableSet<?> elidedTypesAndStaticImports() { | ||
return ImmutableSet.of( | ||
(Runnable) () -> assertDoesNotThrow(() -> null), |
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 don't need all these casts, just the first one it seems.
Looks good. No mutations were possible for these changes. |
58dd6e6
to
398689b
Compare
Looks good. No mutations were possible for these changes. |
1 similar comment
Looks good. No mutations were possible for these changes. |
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.
The changes LGTM. Thanks for the PR @giall 🚀 ! Let's get rid of those type of assertions 😄!
Suggested commit message:
Introduce `JUnitToAssertJRules` Refaster rule collection (#417)
|
||
static final class AssertThatWithFailMessageStringIsNotNull { | ||
@BeforeTemplate | ||
void before(Object object, String message) { |
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.
Further down we use actual
and expected
. In the before and after case the name of the parameter is called actual
as well. So propose to change it here and in some of the other templates :).
(I see it's used quite inconsistently though).
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.
Now did the same for the boolean
related ones to make it consistent within the file.
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 plan to automate the naming of the parameter names and the order in which they should be defined.)
35652c5
to
c949d47
Compare
Looks good. No mutations were possible for these changes. |
Note: we should add Javadoc to the Refaster rules that are added. |
c949d47
to
6caccb4
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. Very nice work!
Also tweaked the suggested commit message a bit.
final class JUnitToAssertJRules { | ||
private JUnitToAssertJRules() {} | ||
|
||
static final class ThrowNewAssertionError { |
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 wonder whether we should include New
here, but let's defer that decision to "the big automated rename".
@AfterTemplate | ||
@DoNotCall | ||
void after() { | ||
throw new AssertionError(); |
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.
JUnit throws a subtype. There are many such differences; will add some text about this in the Javadoc.
(Runnable) () -> assertDoesNotThrow(() -> null), | ||
() -> assertFalse(true), | ||
() -> assertInstanceOf(null, null), | ||
() -> assertNotNull(null), | ||
() -> assertNotSame(null, null), | ||
() -> assertNull(null), | ||
() -> assertSame(null, null), | ||
() -> assertThrows(null, null), | ||
() -> assertThrowsExactly(null, null), | ||
() -> assertTrue(true), | ||
() -> Assertions.fail()); |
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.
Will push a suggestion to make this more in line with other tests :)
static final class FailWithMessage { | ||
@BeforeTemplate | ||
void before(String message) { | ||
Assertions.fail(message); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
void after(String message) { | ||
fail(message); | ||
} | ||
} |
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: a number of these statements also return a value, and can thus be matched as expressions rather than statements. By not returning void
here we match strictly more cases. (Cause Refaster expression templates can also match statements, but not vice versa.)
Edit: but in practice this is only useful for the fail
methods, because with assertThat
the return type is fundamentally different.
Object actual = new Object(); | ||
Object expected = new Object(); | ||
assertSame(expected, actual); |
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 can also just use one-liners here :)
void testAssertThatCodeDoesNotThrowAnyException() { | ||
assertDoesNotThrow(() -> {}); | ||
} |
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 test both cases.
Edit: and when I do, the resultant code fails to compile with error: incompatible types: lambda body is not compatible with a void functional interface
for the conversion from assertDoesNotThrow(() -> "foo")
to assertThatCode(() -> "foo").doesNotThrowAnyException()
. This can be worked around by using something other than a constant expression, but is worth calling out in a comment.
static final class FailWithMessageAndThrowable { | ||
@BeforeTemplate | ||
void before(String message, Throwable throwable) { | ||
Assertions.fail(message, throwable); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
void after(String message, Throwable throwable) { | ||
fail(message, throwable); | ||
} | ||
} |
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.
There's also a Throwable
-only overload.
// XXX: Rewrite `org.junit.jupiter.api.Assertions.assertEquals`. | ||
// XXX: Rewrite `org.junit.jupiter.api.Assertions.assertArrayEquals`. | ||
// XXX: Rewrite `org.junit.jupiter.api.Assertions.assertIterableEquals`. | ||
// XXX: Rewrite `org.junit.jupiter.api.Assertions.assertLinesMatch`. | ||
// XXX: Rewrite `org.junit.jupiter.api.Assertions.assertNotEquals`. |
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.
This listing isn't exhaustive (especially when we consider the various overloads), and making it so doesn't seem worth the hassle, if done manually. Let's call out the incompleteness at the start of the class, and defer further work to another 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.
I started work on this idea here (heavy WIP, and some changes should be moved to separate PRs, but the gist is there).
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.
Wow, that branch is awesome! Looks like some nice changes @Stephan202 !
Looks good. No mutations were possible for these changes. |
JUnitToAssertJRules
Refaster rule collection
Related issue: #402
Left
XXX
comments onorg.junit.jupiter.api.Assertions
methods that do not (yet) have rules.Decided to leave the equality-related ones for a follow-up PR to reduce the scope a little bit, as those also need a bit more work (equality with deltas, etc.).
assertEquals
assertArrayEquals
assertIterableEquals
assertNotEquals
The following methods don't seem to have direct AssertJ equivalents, but could also be handled later.
assertLinesMatch
assertAll
assertTimeout
assertTimeoutPreemptively