Add SimplifyHasSizeFromIsEqualToAssertion recipe#1034
Merged
Conversation
Handles literal-first AssertJ size assertions where the meaningful expression sits on the isEqualTo(...) side, e.g. assertThat(4).isEqualTo(coll.size()) -> assertThat(coll).hasSize(4). This is the always-safe "Case A" from the issue: the comparison is on a primitive int (size()/length()/array length), so reversing the assertion is behavior-preserving. Object-equality reversal (Case B) is intentionally left untouched since it depends on equals() symmetry. Fixes #1031
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's changed
Adds a focused recipe that simplifies literal-first AssertJ size assertions — where the meaningful expression sits on the
isEqualTo(...)side rather than insideassertThat(...):This is the always-safe Case A from the issue. The underlying comparison is on a primitive
int(Iterable.size(),Map.size(),CharSequence.length(), or array.length), so reversing the assertion intohasSize(...)is unconditionally behavior-preserving.Case B (arbitrary object equality, e.g.
assertThat("1").isEqualTo(list.get(0))) is intentionally left untouched, since reversing it would change whichequals()is invoked and is unsafe for asymmetric/brokenequalsimplementations.Guards
assertThat(...)holds a non-negativeintliteral (negativehasSizethrows at runtime, so reversal there would change behavior).isEqualToargument; otherint-returning methods (e.g.indexOf) are left alone.The recipe is wired into the
AssertJ best practicescomposite next toSimplifyHasSizeAssertion, andrecipes.csvwas regenerated via./gradlew recipeCsvGenerate.