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 all 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
@@ -0,0 +1,39 @@
/*
* (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;

public final class AssertjBooleanNegationIsFalse {

@BeforeTemplate
void before(boolean input) {
assertThat(!input).isFalse();
Copy link
Contributor

Choose a reason for hiding this comment

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

I am genuinely so curious to see if this catches anything!

}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(boolean input) {
assertThat(input).isTrue();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (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;

public final class AssertjBooleanNegationIsFalseWithDescription {

@BeforeTemplate
void before(boolean input, String description, @Repeated Object descriptionArgs) {
assertThat(!input).describedAs(description, descriptionArgs).isFalse();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(boolean input, String description, @Repeated Object descriptionArgs) {
assertThat(input).describedAs(description, descriptionArgs).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (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;

public final class AssertjBooleanNegationIsTrue {

@BeforeTemplate
void before(boolean input) {
assertThat(!input).isTrue();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(boolean input) {
assertThat(input).isFalse();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* (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;

public final class AssertjBooleanNegationIsTrueWithDescription {

@BeforeTemplate
void before(boolean input, String description, @Repeated Object descriptionArgs) {
assertThat(!input).describedAs(description, descriptionArgs).isTrue();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(boolean input, String description, @Repeated Object descriptionArgs) {
assertThat(input).describedAs(description, descriptionArgs).isFalse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* (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.Objects;

public final class AssertjIsNull {

@BeforeTemplate
void before1(Object input) {
assertThat(input == null).isTrue();
}

@BeforeTemplate
void before2(Object input) {
assertThat(input != null).isFalse();
}

@BeforeTemplate
void before3(Object input) {
assertThat(Objects.isNull(input)).isTrue();
}

@BeforeTemplate
void before4(Object input) {
assertThat(Objects.nonNull(input)).isFalse();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Object input) {
assertThat(input).isNull();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* (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.Objects;

public final class AssertjIsNullWithDescription {

@BeforeTemplate
void before1(Object input, String description, @Repeated Object descriptionArgs) {
assertThat(input == null).describedAs(description, descriptionArgs).isTrue();
}

@BeforeTemplate
void before2(Object input, String description, @Repeated Object descriptionArgs) {
assertThat(input != null).describedAs(description, descriptionArgs).isFalse();
}

@BeforeTemplate
void before3(Object input, String description, @Repeated Object descriptionArgs) {
assertThat(Objects.isNull(input)).describedAs(description, descriptionArgs).isTrue();
}

@BeforeTemplate
void before4(Object input, String description, @Repeated Object descriptionArgs) {
assertThat(Objects.nonNull(input)).describedAs(description, descriptionArgs).isFalse();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Object input, String description, @Repeated Object descriptionArgs) {
assertThat(input).describedAs(description, descriptionArgs).isNull();
}

}
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,50 @@
/*
* (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.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 before3(Map<K, V> things, K key) {
assertThat(things.keySet().contains(key)).isFalse();
}

@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,51 @@
/*
* (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.get(key)).describedAs(description, descriptionArgs).isNull();
}

@BeforeTemplate
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing
void before3(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) {
assertThat(things.keySet().contains(key)).describedAs(description, descriptionArgs).isFalse();
}

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

Loading