-
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
Improve and extend Refaster Map
rules
#337
Conversation
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
@OnlineDocumentation | ||
final class AssertJMapRules { | ||
private AssertJMapRules() {} | ||
|
||
// XXX: Reduce boilerplate using a `Matcher` that identifies "empty" instances. |
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 seem to recall that I already started working on this, but can't find the code right now. Will look more closely "later".
static final class AssertThatMapHasSize<K, V> { | ||
@BeforeTemplate | ||
AbstractAssert<?, ?> before(Map<K, V> map, int length) { | ||
return Refaster.anyOf( | ||
assertThat(map.size()).isEqualTo(length), | ||
assertThat(Refaster.anyOf(map.keySet(), map.values(), map.entrySet())).hasSize(length)); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
MapAssert<K, V> after(Map<K, V> map, int length) { | ||
return assertThat(map).hasSize(length); | ||
} | ||
} | ||
|
||
static final class AbstractMapAssertHasSameSizeAs<K, V> { | ||
@BeforeTemplate | ||
AbstractMapAssert<?, ?, K, V> before(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) { | ||
return mapAssert.hasSize(map.size()); | ||
} | ||
|
||
@AfterTemplate | ||
AbstractMapAssert<?, ?, K, V> after(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) { | ||
return mapAssert.hasSameSizeAs(map); | ||
} | ||
} |
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.
Part 1 of what I mentioned here.
/** Prefer {@link Map#size()} over more contrived alternatives. */ | ||
static final class MapSize<K, V> { | ||
@BeforeTemplate | ||
int before(Map<K, V> map) { | ||
return Refaster.anyOf(map.keySet(), map.values(), map.entrySet()).size(); | ||
} | ||
|
||
@AfterTemplate | ||
int after(Map<K, V> map) { | ||
return map.size(); | ||
} | ||
} |
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.
... and part 2.
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.
Quick Github-based review. The map-related rules LGTM, but the PR looks to also revert #327. Let's make sure we don't. 🙂
* Avoid unnecessary {@link Optional} to {@link Stream} conversion when filtering a value of the | ||
* former type. | ||
*/ | ||
static final class OptionalFilter<T> { |
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 suspect removing these two new Refaster rules in unintended? 🙂
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.
Nice catch! Likely an error while moving the code to a new branch (I initially wrote this on top of #332).
Note while reviewing, I find that doing some
|
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.
One probably unrelated comment, the whole set of refactoring + adding of new rules suits me!
Thanks :)
// same, so such a rule requires a `Matcher` that heuristically identifies `Stream` expressions | ||
// with deterministic order. | ||
// XXX: Consider whether to have a similar rule for `.findAny()`. For parallel streams it | ||
// wouldn't be quite the same.... |
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.
// wouldn't be quite the same.... | |
// wouldn't be quite the same... |
But did you even intend to drop the rest of the comment?
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 commit. (Forgot to make a comment and already pushed the improvement 😛).
Changes LGTM.
Tweaked suggested commit message.
@werli anything else from you side?
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
|
||
@OnlineDocumentation | ||
final class AssertJMapRules { | ||
private AssertJMapRules() {} | ||
|
||
// XXX: Reduce boilerplate using a `Matcher` that identifies "empty" instances. | ||
static final class AbstractMapAssertIsEmpty<K, V> { |
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 was about to propose Javadocs for these templates. However, for most AssertJ related rules we don't have that 👀.
Summary of changes: - Move relevant rules from `AssertJRules` to `AssertJMapRules` and introduce associated tests. - Extract relevant rules from `AssortedRules` to the new `MapRules` Refaster collection. - Add a few new rules to both `AssertJMapRules` and `MapRules`.
0b38be5
to
02397eb
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.
Haha if you see the last commit @Stephan202 I know what you'll think, and you are right; we should add something for this to automate it 😛.
I was still thinking about why it wasn't flagged that it was Assorted
instead of Map
in the tests 😂, anyway it's fixed now :).
Exactly 😉 Changes LGTM! |
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.
Pushed my small suggestion. 👍 Nice improvements. 🙂
// XXX: Implement a similar rule for `.findAny()`. For parallel streams this wouldn't be quite the | ||
// same, so such a rule requires a `Matcher` that heuristically identifies `Stream` expressions | ||
// with deterministic order. | ||
// XXX: Consider whether to have a similar rule for `.findAny()`. For parallel streams it | ||
// wouldn't be quite the same... |
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.
That's the last bit of unintended changes in this PR. 👀
See https://github.com/PicnicSupermarket/error-prone-support/pull/327/files#diff-35c5c43be02c59bf4ed5296465d36cc5adc02addcf0269f7264332cde0f0d779R171-R173
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.
Tnx!
Suggested commit message: