Skip to content

Commit

Permalink
Implement assertj map refactors for containsKey and containsEntry (#925)
Browse files Browse the repository at this point in the history
Implement assertj map refactors for containsKey and containsEntry
  • Loading branch information
carterkozak authored and bulldozer-bot[bot] committed Oct 2, 2019
1 parent 2b6ea5c commit 5d85979
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 0 deletions.
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;
import java.util.Map;

public final class AssertjMapContainsEntry<K, V> {

@BeforeTemplate
void before(Map<K, V> things, K key, V expectedValue) {
assertThat(things.get(key)).isEqualTo(expectedValue);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, K key, V expectedValue) {
assertThat(things).containsEntry(key, expectedValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* (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 AssertjMapContainsEntryWithDescription<K, V> {

@BeforeTemplate
void before(Map<K, V> things, K key, V expectedValue, String description, @Repeated Object descriptionArgs) {
assertThat(things.get(key)).describedAs(description, descriptionArgs).isEqualTo(expectedValue);
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, K key, V expectedValue, String description, @Repeated Object descriptionArgs) {
assertThat(things).describedAs(description, descriptionArgs).containsEntry(key, expectedValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* (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 AssertjMapContainsKey<K, V> {

@BeforeTemplate
void before1(Map<K, V> things, K key) {
assertThat(things.containsKey(key)).isTrue();
}

@BeforeTemplate
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing
void before2(Map<K, V> things, K key) {
assertThat(things.keySet().contains(key)).isTrue();
}

@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
void after(Map<K, V> things, K key) {
assertThat(things).containsKey(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* (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 AssertjMapContainsKeyWithDescription<K, V> {

@BeforeTemplate
void before1(Map<K, V> things, K key, String description, @Repeated Object descriptionArgs) {
assertThat(things.containsKey(key)).describedAs(description, descriptionArgs).isTrue();
}

@BeforeTemplate
@SuppressWarnings("RedundantCollectionOperation") // It's what we're fixing
void before2(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).containsKey(key);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* (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.Assumptions.assumeThat;

import org.junit.Test;

public class AssertjMapContainsEntryTest {

@Test
public void simple() {
assumeThat(System.getProperty("java.specification.version"))
.describedAs("Refaster does not currently support fluent refactors on java 11")
.isEqualTo("1.8");
RefasterTestHelper
.forRefactoring(AssertjMapContainsEntry.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in, String key, Object expected) {",
" assertThat(in.get(key)).isEqualTo(expected);",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in, String key, Object expected) {",
" assertThat(in).containsEntry(key, expected);",
" }",
"}");
}

@Test
public void description() {
assumeThat(System.getProperty("java.specification.version"))
.describedAs("Refaster does not currently support fluent refactors on java 11")
.isEqualTo("1.8");
RefasterTestHelper
.forRefactoring(AssertjMapContainsEntryWithDescription.class)
.withInputLines(
"Test",
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in, String key, Object expected) {",
" assertThat(in.get(key)).describedAs(\"desc\").isEqualTo(expected);",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, Object> in, String key, Object expected) {",
" assertThat(in).describedAs(\"desc\").containsEntry(key, expected);",
" }",
"}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* (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 org.junit.Test;

public class AssertjMapContainsKeyTest {

@Test
public void 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) {",
" assertThat(in.keySet().contains(\"foo\")).isTrue();",
" assertThat(in.containsKey(\"foo\")).isTrue();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in).containsKey(\"foo\");",
" assertThat(in).containsKey(\"foo\");",
" }",
"}");
}

@Test
public void 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) {",
" assertThat(in.keySet().contains(\"foo\")).describedAs(\"desc\").isTrue();",
" assertThat(in.containsKey(\"foo\")).describedAs(\"desc\").isTrue();",
" }",
"}")
.hasOutputLines(
"import static org.assertj.core.api.Assertions.assertThat;",
"import java.util.Map;",
"public class Test {",
" void f(Map<String, String> in) {",
" assertThat(in).describedAs(\"desc\").containsKey(\"foo\");",
" assertThat(in).describedAs(\"desc\").containsKey(\"foo\");",
" }",
"}");
}
}
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-925.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Implement assertj map refactors for containsKey and containsEntry
links:
- https://github.com/palantir/gradle-baseline/pull/925

0 comments on commit 5d85979

Please sign in to comment.