diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJMapTemplates.java new file mode 100644 index 0000000000..566caa3265 --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJMapTemplates.java @@ -0,0 +1,35 @@ +package tech.picnic.errorprone.refastertemplates; + +import com.google.common.collect.ImmutableMap; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Map; +import org.assertj.core.api.AbstractMapAssert; + +final class AssertJMapTemplates { + private AssertJMapTemplates() {} + + static final class AbstractMapAssertContainsExactlyInAnyOrderEntriesOf { + @BeforeTemplate + AbstractMapAssert before(AbstractMapAssert mapAssert, Map map) { + return mapAssert.isEqualTo(map); + } + + @AfterTemplate + AbstractMapAssert after(AbstractMapAssert mapAssert, Map map) { + return mapAssert.containsExactlyInAnyOrderEntriesOf(map); + } + } + + static final class AbstractMapAssertContainsExactlyEntriesOf { + @BeforeTemplate + AbstractMapAssert before(AbstractMapAssert mapAssert, K key, V value) { + return mapAssert.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(key, value)); + } + + @AfterTemplate + AbstractMapAssert after(AbstractMapAssert mapAssert, K key, V value) { + return mapAssert.containsExactlyEntriesOf(ImmutableMap.of(key, value)); + } + } +} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplates.java index 05ee76169a..7f01fba802 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJNumberTemplates.java @@ -1,8 +1,12 @@ package tech.picnic.errorprone.refastertemplates; +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static org.assertj.core.api.Assertions.assertThat; + import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; import java.math.BigDecimal; import java.math.BigInteger; import org.assertj.core.api.AbstractBigDecimalAssert; @@ -221,4 +225,40 @@ AbstractBigDecimalAssert before(AbstractBigDecimalAssert numberAssert) { return numberAssert.isNotNegative(); } } + + static final class AssertThatIsOdd { + @BeforeTemplate + AbstractIntegerAssert before(int number) { + return assertThat(number % 2).isEqualTo(1); + } + + @BeforeTemplate + AbstractLongAssert before(long number) { + return assertThat(number % 2).isEqualTo(1); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + NumberAssert after(long number) { + return assertThat(number).isOdd(); + } + } + + static final class AssertThatIsEven { + @BeforeTemplate + AbstractIntegerAssert before(int number) { + return assertThat(number % 2).isEqualTo(0); + } + + @BeforeTemplate + AbstractLongAssert before(long number) { + return assertThat(number % 2).isEqualTo(0); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + NumberAssert after(long number) { + return assertThat(number).isEven(); + } + } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJStringTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJStringTemplates.java index 30679581e6..4b1a3041cd 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJStringTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJStringTemplates.java @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refastertemplates; +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; import static org.assertj.core.api.Assertions.assertThat; import com.google.errorprone.refaster.ImportPolicy; @@ -61,4 +62,30 @@ static final class AssertThatStringIsNotEmpty { return assertThat(string).isNotEmpty(); } } + + static final class AssertThatMatches { + @BeforeTemplate + AbstractAssert before(String string, String regex) { + return assertThat(string.matches(regex)).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractAssert after(String string, String regex) { + return assertThat(string).matches(regex); + } + } + + static final class AssertThatDoesNotMatch { + @BeforeTemplate + AbstractAssert before(String string, String regex) { + return assertThat(string.matches(regex)).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractAssert after(String string, String regex) { + return assertThat(string).doesNotMatch(regex); + } + } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java index 9c307a8e3a..19b841be61 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/ImmutableMapTemplates.java @@ -55,7 +55,7 @@ ImmutableMap after() { /** * Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives and - * alternatives that don't communicate the immutability of the resulting map at the type level.. + * alternatives that don't communicate the immutability of the resulting map at the type level. */ // XXX: One can define variants for more than one key-value pair, but at some point the builder // actually produces nicer code. So it's not clear we should add Refaster templates for those diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java index 4238d22212..c19513ad76 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/RefasterCheckTest.java @@ -34,6 +34,7 @@ public final class RefasterCheckTest { "AssertJInteger", "AssertJLong", "AssertJNumber", + "AssertJMap", "AssertJObject", "AssertJOptional", "AssertJShort", diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestInput.java new file mode 100644 index 0000000000..108cf330eb --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestInput.java @@ -0,0 +1,18 @@ +package tech.picnic.errorprone.bugpatterns; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableMap; +import org.assertj.core.api.AbstractMapAssert; + +final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { + AbstractMapAssert + testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() { + return assertThat(ImmutableMap.of(1, 2, 3, 4)).isEqualTo(ImmutableMap.of(1, 2, 3, 4)); + } + + AbstractMapAssert testAbstractMapAssertContainsExactlyEntriesOf() { + return assertThat(ImmutableMap.of(1, 2)) + .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2)); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestOutput.java new file mode 100644 index 0000000000..cd25377325 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestOutput.java @@ -0,0 +1,18 @@ +package tech.picnic.errorprone.bugpatterns; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableMap; +import org.assertj.core.api.AbstractMapAssert; + +final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { + AbstractMapAssert + testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() { + return assertThat(ImmutableMap.of(1, 2, 3, 4)) + .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2, 3, 4)); + } + + AbstractMapAssert testAbstractMapAssertContainsExactlyEntriesOf() { + return assertThat(ImmutableMap.of(1, 2)).containsExactlyEntriesOf(ImmutableMap.of(1, 2)); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestInput.java index 1838f8c6f0..d91f9fc229 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestInput.java @@ -75,4 +75,28 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { assertThat(BigInteger.ZERO).isGreaterThan(BigInteger.valueOf(-1)), assertThat(BigDecimal.ZERO).isGreaterThanOrEqualTo(BigDecimal.ZERO)); } + + ImmutableSet> testAssertThatIsOdd() { + return ImmutableSet.of( + assertThat((byte) 1 % 2).isEqualTo(1), + assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(1), + assertThat(1 % 2).isEqualTo(1), + assertThat(Integer.valueOf(1) % 2).isEqualTo(1), + assertThat(1L % 2).isEqualTo(1), + assertThat(Long.valueOf(1) % 2).isEqualTo(1), + assertThat((short) 1 % 2).isEqualTo(1), + assertThat(Short.valueOf((short) 1) % 2).isEqualTo(1)); + } + + ImmutableSet> testAssertThatIsEven() { + return ImmutableSet.of( + assertThat((byte) 1 % 2).isEqualTo(0), + assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(0), + assertThat(1 % 2).isEqualTo(0), + assertThat(Integer.valueOf(1) % 2).isEqualTo(0), + assertThat(1L % 2).isEqualTo(0), + assertThat(Long.valueOf(1) % 2).isEqualTo(0), + assertThat((short) 1 % 2).isEqualTo(0), + assertThat(Short.valueOf((short) 1) % 2).isEqualTo(0)); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestOutput.java index 5b6eaee6d9..56f8d3eef4 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJNumberTemplatesTestOutput.java @@ -75,4 +75,28 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { assertThat(BigInteger.ZERO).isNotNegative(), assertThat(BigDecimal.ZERO).isNotNegative()); } + + ImmutableSet> testAssertThatIsOdd() { + return ImmutableSet.of( + assertThat((byte) 1).isOdd(), + assertThat(Byte.valueOf((byte) 1)).isOdd(), + assertThat(1).isOdd(), + assertThat(Integer.valueOf(1)).isOdd(), + assertThat(1L).isOdd(), + assertThat(Long.valueOf(1)).isOdd(), + assertThat((short) 1).isOdd(), + assertThat(Short.valueOf((short) 1)).isOdd()); + } + + ImmutableSet> testAssertThatIsEven() { + return ImmutableSet.of( + assertThat((byte) 1).isEven(), + assertThat(Byte.valueOf((byte) 1)).isEven(), + assertThat(1).isEven(), + assertThat(Integer.valueOf(1)).isEven(), + assertThat(1L).isEven(), + assertThat(Long.valueOf(1)).isEven(), + assertThat((short) 1).isEven(), + assertThat(Short.valueOf((short) 1)).isEven()); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestInput.java index eefa445372..7b0b70c4af 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestInput.java @@ -21,4 +21,12 @@ AbstractStringAssert testAbstractStringAssertStringIsNotEmpty() { AbstractAssert testAssertThatStringIsNotEmpty() { return assertThat("foo".isEmpty()).isFalse(); } + + AbstractAssert testAssertThatMatches() { + return assertThat("foo".matches(".*")).isTrue(); + } + + AbstractAssert testAssertThatDoesNotMatch() { + return assertThat("foo".matches(".*")).isFalse(); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestOutput.java index b470da2988..16328ba5bc 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJStringTemplatesTestOutput.java @@ -21,4 +21,12 @@ AbstractStringAssert testAbstractStringAssertStringIsNotEmpty() { AbstractAssert testAssertThatStringIsNotEmpty() { return assertThat("foo").isNotEmpty(); } + + AbstractAssert testAssertThatMatches() { + return assertThat("foo").matches(".*"); + } + + AbstractAssert testAssertThatDoesNotMatch() { + return assertThat("foo").doesNotMatch(".*"); + } }