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

Introduce JUnitToAssertJRules Refaster rule collection #417

Merged
merged 13 commits into from
Jan 2, 2023

Conversation

giall
Copy link
Contributor

@giall giall commented Dec 15, 2022

Related issue: #402

Left XXX comments on org.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

@giall giall force-pushed the giall/junit-to-assertj branch from db36cd7 to 45406fc Compare December 15, 2022 11:07
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@giall giall force-pushed the giall/junit-to-assertj branch from 45406fc to b8b093f Compare December 15, 2022 11:46
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@giall giall force-pushed the giall/junit-to-assertj branch from 7485fff to dc4122a Compare December 19, 2022 09:01
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie self-requested a review December 20, 2022 07:10
@rickie rickie force-pushed the giall/junit-to-assertj branch from dc4122a to 372dac8 Compare December 20, 2022 07:12
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a 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 {
Copy link
Member

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) {
Copy link
Member

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),
Copy link
Member

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.

@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie force-pushed the giall/junit-to-assertj branch from 58dd6e6 to 398689b Compare December 27, 2022 06:12
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

1 similar comment
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

Copy link
Member

@rickie rickie left a 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) {
Copy link
Member

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).

Copy link
Member

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.

Copy link
Member

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.)

@rickie rickie added this to the 0.7.0 milestone Dec 27, 2022
@rickie rickie force-pushed the giall/junit-to-assertj branch from 35652c5 to c949d47 Compare December 27, 2022 08:04
@github-actions
Copy link

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie requested a review from Stephan202 December 29, 2022 07:55
@rickie
Copy link
Member

rickie commented Dec 29, 2022

Note: we should add Javadoc to the Refaster rules that are added.

@Stephan202 Stephan202 force-pushed the giall/junit-to-assertj branch from c949d47 to 6caccb4 Compare January 1, 2023 12:58
Copy link
Member

@Stephan202 Stephan202 left a 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 {
Copy link
Member

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();
Copy link
Member

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.

Comment on lines 22 to 32
(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());
Copy link
Member

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 :)

Comment on lines 48 to 87
static final class FailWithMessage {
@BeforeTemplate
void before(String message) {
Assertions.fail(message);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(String message) {
fail(message);
}
}
Copy link
Member

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.

Comment on lines 96 to 98
Object actual = new Object();
Object expected = new Object();
assertSame(expected, actual);
Copy link
Member

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 :)

Comment on lines 155 to 150
void testAssertThatCodeDoesNotThrowAnyException() {
assertDoesNotThrow(() -> {});
}
Copy link
Member

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.

Comment on lines 61 to 113
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);
}
}
Copy link
Member

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.

Comment on lines 230 to 234
// 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`.
Copy link
Member

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.

Copy link
Member

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).

Copy link
Member

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 !

@github-actions
Copy link

github-actions bot commented Jan 1, 2023

Looks good. No mutations were possible for these changes.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed.

@rickie rickie changed the title Rewrite JUnit assertions to AssertJ Introduce JUnitToAssertJRules Refaster rule collection Jan 2, 2023
@rickie rickie merged commit e6e5071 into master Jan 2, 2023
@rickie rickie deleted the giall/junit-to-assertj branch January 2, 2023 07:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants