-
Notifications
You must be signed in to change notification settings - Fork 134
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
Refaster rules for checks that maps do not contain a specific key #935
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.ImportPolicy; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
import java.util.Map; | ||
|
||
public final class AssertjMapDoesNotContainKey<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things, K key) { | ||
assertThat(things.containsKey(key)).isFalse(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things, K key) { | ||
assertThat(!things.containsKey(key)).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before3(Map<K, V> things, K key) { | ||
assertThat(things.get(key)).isNull(); | ||
} | ||
|
||
@BeforeTemplate | ||
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing | ||
void before4(Map<K, V> things, K key) { | ||
assertThat(things.keySet().contains(key)).isFalse(); | ||
} | ||
|
||
@BeforeTemplate | ||
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing | ||
void before5(Map<K, V> things, K key) { | ||
assertThat(!things.keySet().contains(key)).isTrue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we had a standalone refaster that changed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I can add that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're doing silly ones, might want There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed the negation checks from this class and added the general rule. |
||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things, K key) { | ||
assertThat(things).doesNotContainKey(key); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.ImportPolicy; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.Repeated; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
import java.util.Map; | ||
|
||
public final class AssertjMapDoesNotContainKeyWithDescription<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(things.containsKey(key)).describedAs(description, descriptionArgs).isFalse(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(!things.containsKey(key)).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before3(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(things.get(key)).describedAs(description, descriptionArgs).isNull(); | ||
} | ||
|
||
@BeforeTemplate | ||
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing | ||
void before4(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(things.keySet().contains(key)).describedAs(description, descriptionArgs).isFalse(); | ||
} | ||
|
||
@BeforeTemplate | ||
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing | ||
void before5(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(!things.keySet().contains(key)).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) { | ||
assertThat(things).describedAs(description, descriptionArgs).doesNotContainKey(key); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's worth writing a test that could use reflection to go through each file in the refaster package and verify there is a matching Foo and FooWithDescription class, each with the same number of methods? I feel like someone is going to forget at somepoint... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps, there are enough edge cases where the check with a description differs from the non-description version that it could be tricky (e.g. combining the optional isPresent and hasValue checks). I don't think we're at the point that it's worthwhile yet, especially considering the failure mode is that we don't automatically make something better. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Refaster rules for checks that maps do not contain a specific key | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/935 |
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'm not sure we necessarily want to refactor this one to
assertThat(things).doesNotContainKey(key)
as while this is true for some implementations of map (e.g.ConcurrentHashMap
,ImmutableMap
), but not necessarily for others such asHashMap
orTreeMap
where a key may be mapped tonull
(i.e.things.get(key) == null && things.containsKey(key) == true
)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.
To be fair, this did correctly refactor several places in tritium where we can properly use
doesNotContainKey
in palantir/tritium#454There 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've found one place in practice where it broke a test, but in all other cases it produced better asserts. When this is hit, we either want to refactor to:
assertThat(map).doesNotContainKey(key)
or
assertThat(map).containsEntry(key, null)
The two are semantically different, and it's uncommon that test authors actually mean to test that either the value is null or the map does not contain a key.