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

Refaster rules for checks that maps do not contain a specific key #935

Merged
merged 3 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ void before2(Map<K, V> things, K key) {
assertThat(things.keySet().contains(key)).isTrue();
}

@BeforeTemplate
void before3(Map<K, V> things, K key) {
assertThat(things.get(key)).isNotNull();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, K key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ void before2(Map<K, V> things, K key, String description, @Repeated Object descr
assertThat(things.keySet().contains(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).isNotNull();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) {
Expand Down
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();
Copy link
Contributor

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 as HashMap or TreeMap where a key may be mapped to null (i.e. things.get(key) == null && things.containsKey(key) == true)

Copy link
Contributor

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#454

Copy link
Contributor Author

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.

}

@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();
Copy link
Contributor

Choose a reason for hiding this comment

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

If we had a standalone refaster that changed assertThat(!arbitaryBoolean).isFalse() to assertThat(arbitaryBoolean).isTrue(), we wouldn't need this case right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I can add that

Copy link
Contributor

Choose a reason for hiding this comment

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

If we're doing silly ones, might want assertThat(foo == null).isTrue() -> .isNull() too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@carterkozak carterkozak Oct 3, 2019

Choose a reason for hiding this comment

The 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
Expand Up @@ -21,52 +21,114 @@
public class AssertjMapContainsKeyTest {

@Test
public void simple() {
public void contains_simple() {
RefasterTestHelper
.forRefactoring(AssertjMapContainsKey.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" void f(Map<String, Object> in) {",
" assertThat(in.keySet().contains(\"foo\")).isTrue();",
" assertThat(in.containsKey(\"foo\")).isTrue();",
" assertThat(in.get(\"foo\")).isNotNull();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" void f(Map<String, Object> in) {",
" assertThat(in).containsKey(\"foo\");",
" assertThat(in).containsKey(\"foo\");",
" assertThat(in).containsKey(\"foo\");",
" }",
"}");
}

@Test
public void description() {
public void contains_description() {
RefasterTestHelper
.forRefactoring(AssertjMapContainsKeyWithDescription.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" void f(Map<String, Object> in) {",
" assertThat(in.keySet().contains(\"foo\")).describedAs(\"desc\").isTrue();",
" assertThat(in.containsKey(\"foo\")).describedAs(\"desc\").isTrue();",
" assertThat(in.get(\"foo\")).describedAs(\"desc\").isNotNull();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" void f(Map<String, Object> in) {",
" assertThat(in).describedAs(\"desc\").containsKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").containsKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").containsKey(\"foo\");",
" }",
"}");
}

@Test
public void notContain_simple() {
RefasterTestHelper
.forRefactoring(AssertjMapDoesNotContainKey.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in) {",
" assertThat(in.keySet().contains(\"foo\")).isFalse();",
" assertThat(!in.keySet().contains(\"foo\")).isTrue();",
" assertThat(in.containsKey(\"foo\")).isFalse();",
" assertThat(in.get(\"foo\")).isNull();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in) {",
" assertThat(in).doesNotContainKey(\"foo\");",
" assertThat(in).doesNotContainKey(\"foo\");",
" assertThat(in).doesNotContainKey(\"foo\");",
" assertThat(in).doesNotContainKey(\"foo\");",
" }",
"}");
}

@Test
public void notContain_description() {
RefasterTestHelper
.forRefactoring(AssertjMapDoesNotContainKeyWithDescription.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in) {",
" assertThat(in.keySet().contains(\"foo\")).describedAs(\"desc\").isFalse();",
" assertThat(!in.keySet().contains(\"foo\")).describedAs(\"desc\").isTrue();",
" assertThat(in.containsKey(\"foo\")).describedAs(\"desc\").isFalse();",
" assertThat(in.get(\"foo\")).describedAs(\"desc\").isNull();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in) {",
" assertThat(in).describedAs(\"desc\").doesNotContainKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").doesNotContainKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").doesNotContainKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").doesNotContainKey(\"foo\");",
" }",
"}");
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-935.v2.yml
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