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

Improve and extend Refaster Map rules #337

Merged
merged 5 commits into from
Nov 9, 2022

Conversation

Stephan202
Copy link
Member

@Stephan202 Stephan202 commented Nov 5, 2022

Suggested commit message:

Improve and extend Refaster `Map` rules (#337)

Summary of changes:
- Move relevant rules from `AssertJRules` to `AssertJMapRules` and introduce 
  associated tests.
- Extract relevant rules from `AssortedRules` to the new `MapRules` Refaster 
  rule collection.
- Add a few new rules to both `AssertJMapRules` and `MapRules`.

@Stephan202 Stephan202 added this to the 0.6.0 milestone Nov 5, 2022
@Stephan202 Stephan202 requested review from Ptijohn and rickie November 5, 2022 15:00
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

@OnlineDocumentation
final class AssertJMapRules {
private AssertJMapRules() {}

// XXX: Reduce boilerplate using a `Matcher` that identifies "empty" instances.
Copy link
Member Author

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

Comment on lines +170 to +195
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);
}
}
Copy link
Member Author

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.

Comment on lines +44 to +55
/** 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();
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

... and part 2.

Copy link
Member

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

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? 🙂

Copy link
Member Author

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

@Ptijohn
Copy link
Contributor

Ptijohn commented Nov 7, 2022

Note while reviewing, I find that doing some vimdiffs on the test files helps a lot to review the rules introduced :)

vimdiff error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJMapRulesTestInput.java error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJMapRulesTestOutput.java

vimdiff error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestInput.java error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/MapRulesTestOutput.java

Copy link
Contributor

@Ptijohn Ptijohn left a 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....
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// wouldn't be quite the same....
// wouldn't be quite the same...

But did you even intend to drop the rest of the comment?

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

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

@rickie rickie self-requested a review November 8, 2022 09:11
Stephan202 and others added 4 commits November 8, 2022 10:14
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`.
@rickie rickie force-pushed the sschroevers/refaster-map-improvements branch from 0b38be5 to 02397eb Compare November 8, 2022 09:14
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.

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

@Stephan202
Copy link
Member Author

I know what you'll think, and you are right; we should add something for this to automate it stuck_out_tongue.

Exactly 😉

Changes LGTM!

Copy link
Member

@werli werli left a 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. 🙂

Comment on lines 171 to 172
// 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...
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Tnx!

@rickie rickie merged commit fcaa1f7 into master Nov 9, 2022
@rickie rickie deleted the sschroevers/refaster-map-improvements branch November 9, 2022 07:54
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.

4 participants