-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add assertj refaster rules for map size asserts (#919)
Add assertj refaster rules for map size asserts
- Loading branch information
1 parent
5937e67
commit adbbfdd
Showing
8 changed files
with
454 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapHasSizeExactly.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,44 @@ | ||
/* | ||
* (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 AssertjMapHasSizeExactly<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things, int size) { | ||
assertThat(things.size() == size).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things, int size) { | ||
assertThat(things.size()).isEqualTo(size); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things, int size) { | ||
assertThat(things).hasSize(size); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...src/main/java/com/palantir/baseline/refaster/AssertjMapHasSizeExactlyWithDescription.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,44 @@ | ||
/* | ||
* (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 AssertjMapHasSizeExactlyWithDescription<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things, int size, String description) { | ||
assertThat(things.size() == size).describedAs(description).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things, int size, String description) { | ||
assertThat(things.size()).describedAs(description).isEqualTo(size); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things, int size, String description) { | ||
assertThat(things).describedAs(description).hasSize(size); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapIsEmpty.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,59 @@ | ||
/* | ||
* (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 AssertjMapIsEmpty<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things) { | ||
assertThat(things.size() == 0).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things) { | ||
assertThat(things.isEmpty()).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before3(Map<K, V> things) { | ||
assertThat(things.size()).isZero(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before4(Map<K, V> things) { | ||
assertThat(things.size()).isEqualTo(0); | ||
} | ||
|
||
@BeforeTemplate | ||
void before5(Map<K, V> things) { | ||
assertThat(things).hasSize(0); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things) { | ||
assertThat(things).isEmpty(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapIsEmpty2.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,44 @@ | ||
/* | ||
* (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 com.google.common.collect.ImmutableMap; | ||
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import java.util.Collections; | ||
import org.assertj.core.api.MapAssert; | ||
|
||
public final class AssertjMapIsEmpty2<A extends MapAssert<K, V>, K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(A in) { | ||
in.hasSize(0); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(A in) { | ||
in.isEqualTo(Refaster.anyOf( | ||
ImmutableMap.of(), | ||
Collections.emptyMap())); | ||
} | ||
|
||
@AfterTemplate | ||
void after(A in) { | ||
in.isEmpty(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...-rules/src/main/java/com/palantir/baseline/refaster/AssertjMapIsEmptyWithDescription.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,59 @@ | ||
/* | ||
* (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 AssertjMapIsEmptyWithDescription<K, V> { | ||
|
||
@BeforeTemplate | ||
void before1(Map<K, V> things, String description) { | ||
assertThat(things.size() == 0).describedAs(description).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before2(Map<K, V> things, String description) { | ||
assertThat(things.isEmpty()).describedAs(description).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before3(Map<K, V> things, String description) { | ||
assertThat(things.size()).describedAs(description).isZero(); | ||
} | ||
|
||
@BeforeTemplate | ||
void before4(Map<K, V> things, String description) { | ||
assertThat(things.size()).describedAs(description).isEqualTo(0); | ||
} | ||
|
||
@BeforeTemplate | ||
void before5(Map<K, V> things, String description) { | ||
assertThat(things).describedAs(description).hasSize(0); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
void after(Map<K, V> things, String description) { | ||
assertThat(things).describedAs(description).isEmpty(); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...ne-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjMapHasSizeTest.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 org.junit.Test; | ||
|
||
public class AssertjMapHasSizeTest { | ||
|
||
@Test | ||
public void exactSize_simple() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjMapHasSizeExactly.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.size() == 2).isTrue();", | ||
" assertThat(in.size()).isEqualTo(2);", | ||
" }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import java.util.Map;", | ||
"public class Test {", | ||
" void f(Map<String, String> in) {", | ||
" assertThat(in).hasSize(2);", | ||
" assertThat(in).hasSize(2);", | ||
" }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void exactSize_description() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjMapHasSizeExactlyWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableList;", | ||
"import java.util.Collections;", | ||
"import java.util.Map;", | ||
"public class Test {", | ||
" void f(Map<String, String> in) {", | ||
" assertThat(in.size() == 2).describedAs(\"desc\").isTrue();", | ||
" assertThat(in.size()).describedAs(\"desc\").isEqualTo(2);", | ||
" }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"import com.google.common.collect.ImmutableList;", | ||
"import java.util.Collections;", | ||
"import java.util.Map;", | ||
"public class Test {", | ||
" void f(Map<String, String> in) {", | ||
" assertThat(in).describedAs(\"desc\").hasSize(2);", | ||
" assertThat(in).describedAs(\"desc\").hasSize(2);", | ||
" }", | ||
"}"); | ||
} | ||
} |
Oops, something went wrong.