Skip to content

Introduce AssertThatHasValue Refaster rule #1547

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

JulianBroudy
Copy link
Contributor

@JulianBroudy JulianBroudy commented Feb 14, 2025

Refaster:

assertThat(optional.orElseThrow()).isEqualTo(value);

to:

assertThat(optional).hasValue(value);

Suggested Commit Message 🎉

Introduce `AssertThatHasValue` Refaster rule (#1547)

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.

Ahh nice that you opened the PR 🚀 !

We can probably add one case to the Refaster rule above here AbstractOptionalAssertHasValue.

The build will probably fail as we also need to add tests 😄. Try running mvn clean install to see whether the build passes 😉.

@JulianBroudy
Copy link
Contributor Author

We can probably add one case to the Refaster rule above here AbstractOptionalAssertHasValue.

Maybe we can go over it together? Because IIUC we cannot add the following:
image

If we stay with the new function, should it maybe also return AbstractAssert<?, ?> instead of ObjectAssert<T>?

The build will probably fail as we also need to add tests 😄. Try running mvn clean install to see whether the build passes 😉.

On it!

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.

@JulianBroudy JulianBroudy requested a review from rickie February 18, 2025 08:14
@Stephan202 Stephan202 added this to the 0.21.0 milestone Feb 22, 2025
@Stephan202 Stephan202 changed the title Introduce optional.orElseThrow() assertion Introduce AssertThatHasValue Refaster rule Feb 22, 2025
@Stephan202 Stephan202 force-pushed the jbroudy/introduce-optional-orelsethrow-assert branch from bdd777a to f49d20d Compare February 22, 2025 13:04
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.

Congrats on your first contribution, @JulianBroudy! 🎉 Too bad this is a tricky one. 😬

I rebased and added a commit to show the issue. Normally for this kind of change we would use this commit message:

Introduce `AssertThatHasValue` Refaster rule (#1547)

But as this approach won't work, we'll have to consider an alternative: introducing an AssertThatHasValue Error Prone checker, just like we did for AssertJIsNull. This variant would be slightly more complex, but not by much. Are you up for that? I'm sure @rickie is up for providing some pointers :)

@@ -103,6 +104,19 @@ AbstractOptionalAssert<?, T> after(AbstractOptionalAssert<?, T> optionalAssert,
}
}

static final class AssertThatHasValue<T> {
@BeforeTemplate
AbstractStringAssert<?> before(Optional<String> optional, String value) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As-is, this rule will only match Optional<String> assertions, while we would like to match any Optional<T> assertion:

Suggested change
AbstractStringAssert<?> before(Optional<String> optional, String value) {
ObjectAssert<T> before(Optional<T> optional, T value) {

... but then we find that the tests fails, because of how Refaster works: the rule will only match the used assertThat overload, while there are many.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Stephan202, always up for anything!

We started with having it as generic and then went on to making it specific for each of String, Boolean, etc. Finally decided to stay only with String for now. I will take another look with @rickie.

@rickie rickie modified the milestones: 0.21.0, 0.22.0 Mar 19, 2025
@Stephan202 Stephan202 modified the milestones: 0.22.0, 0.23.0, 0.24.0 Apr 14, 2025
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