-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement assertj map refactors for containsKey and containsEntry (#925)
Implement assertj map refactors for containsKey and containsEntry
- Loading branch information
1 parent
2b6ea5c
commit 5d85979
Showing
7 changed files
with
323 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapContainsEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
.../src/main/java/com/palantir/baseline/refaster/AssertjMapContainsEntryWithDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ne-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapContainsKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...es/src/main/java/com/palantir/baseline/refaster/AssertjMapContainsKeyWithDescription.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...aster-rules/src/test/java/com/palantir/baseline/refaster/AssertjMapContainsEntryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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);", | ||
" }", | ||
"}"); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...efaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjMapContainsKeyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\");", | ||
" }", | ||
"}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |