From fe958130dba353440f53848d21c9c008b39fc740 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 31 Jan 2022 17:10:56 +0100 Subject: [PATCH 1/6] Introduce `AssertJMapTemplates` --- .../AssertJMapTemplates.java | 35 +++++++++++++++++++ .../ImmutableMapTemplates.java | 2 +- .../bugpatterns/RefasterCheckTest.java | 1 + .../AssertJMapTemplatesTestInput.java | 17 +++++++++ .../AssertJMapTemplatesTestOutput.java | 18 ++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJMapTemplates.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestOutput.java 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..b165bfdb12 --- /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 AbstractMapAssertContainsExactlyEntriesOf { + @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 AbstractMapAssertContainsEntry { + @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.containsExactly(Map.entry(key, value)); + } + } +} 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..4cf4570d29 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJMapTemplatesTestInput.java @@ -0,0 +1,17 @@ +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 testAbstractMapAssertContainsExactlyEntriesOf() { + return assertThat(ImmutableMap.of(1, 2, 3, 4)).isEqualTo(ImmutableMap.of(1, 2, 3, 4)); + } + + AbstractMapAssert testAbstractMapAssertContainsEntry() { + 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..2967f34d96 --- /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 java.util.Map; +import org.assertj.core.api.AbstractMapAssert; + +final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { + AbstractMapAssert testAbstractMapAssertContainsExactlyEntriesOf() { + return assertThat(ImmutableMap.of(1, 2, 3, 4)) + .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2, 3, 4)); + } + + AbstractMapAssert testAbstractMapAssertContainsEntry() { + return assertThat(ImmutableMap.of(1, 2)).containsExactly(Map.entry(1, 2)); + } +} From 409666b785adb8fd9ffb8843473db2ab9434a8cc Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 2 Feb 2022 08:59:29 +0100 Subject: [PATCH 2/6] Introduce templates `String#{matches,doesNotMatch}` --- .../AssertJStringTemplates.java | 26 +++++++++++++++++++ .../AssertJStringTemplatesTestInput.java | 8 ++++++ .../AssertJStringTemplatesTestOutput.java | 8 ++++++ 3 files changed, 42 insertions(+) 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..cdc2e6697d 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 @@ -61,4 +61,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(ImportPolicy.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(ImportPolicy.STATIC_IMPORT_ALWAYS) + AbstractAssert after(String string, String regex) { + return assertThat(string).doesNotMatch(regex); + } + } } 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(".*"); + } } From db944d0ab8855567116ad9926ebfa887b5985927 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 2 Feb 2022 09:22:48 +0100 Subject: [PATCH 3/6] Introduce templates for `NumberAssert{isOdd,IsEven}` This is for `byte`, `int`, `long` and `short`. --- .../AssertJNumberTemplates.java | 60 +++++++++++++++++++ .../AssertJNumberTemplatesTestInput.java | 16 +++++ .../AssertJNumberTemplatesTestOutput.java | 16 +++++ 3 files changed, 92 insertions(+) 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..c8b73c1ea6 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 org.assertj.core.api.Assertions.assertThat; + +import com.google.errorprone.refaster.ImportPolicy; 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,60 @@ AbstractBigDecimalAssert before(AbstractBigDecimalAssert numberAssert) { return numberAssert.isNotNegative(); } } + + static final class AssertThatIsOdd { + @BeforeTemplate + AbstractIntegerAssert before(byte number) { + return assertThat(number % 2).isEqualTo(1); + } + + @BeforeTemplate + AbstractIntegerAssert before(int number) { + return assertThat(number % 2).isEqualTo(1); + } + + @BeforeTemplate + AbstractLongAssert before(long number) { + return assertThat(number % 2).isEqualTo(1); + } + + @BeforeTemplate + AbstractIntegerAssert before(short number) { + return assertThat(number % 2).isEqualTo(1); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + NumberAssert after(Integer number) { + return assertThat(number).isOdd(); + } + } + + static final class AssertThatIsEven { + @BeforeTemplate + AbstractIntegerAssert before(byte number) { + return assertThat(number % 2).isEqualTo(0); + } + + @BeforeTemplate + AbstractIntegerAssert before(int number) { + return assertThat(number % 2).isEqualTo(0); + } + + @BeforeTemplate + AbstractLongAssert before(long number) { + return assertThat(number % 2).isEqualTo(0); + } + + @BeforeTemplate + AbstractIntegerAssert before(short number) { + return assertThat(number % 2).isEqualTo(0); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + NumberAssert after(Integer number) { + return assertThat(number).isEven(); + } + } } 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..62e3f43e33 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,20 @@ 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((Integer) 1 % 2).isEqualTo(1), + assertThat((long) 1 % 2).isEqualTo(1), + assertThat((short) 1 % 2).isEqualTo(1)); + } + + ImmutableSet> testAssertThatIsEven() { + return ImmutableSet.of( + assertThat((byte) 1 % 2).isEqualTo(0), + assertThat((Integer) 1 % 2).isEqualTo(0), + assertThat((long) 1 % 2).isEqualTo(0), + assertThat((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..b33f771d02 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,20 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { assertThat(BigInteger.ZERO).isNotNegative(), assertThat(BigDecimal.ZERO).isNotNegative()); } + + ImmutableSet> testAssertThatIsOdd() { + return ImmutableSet.of( + assertThat((byte) 1).isOdd(), + assertThat((Integer) 1).isOdd(), + assertThat((long) 1).isOdd(), + assertThat((short) 1).isOdd()); + } + + ImmutableSet> testAssertThatIsEven() { + return ImmutableSet.of( + assertThat((byte) 1).isEven(), + assertThat((Integer) 1).isEven(), + assertThat((long) 1).isEven(), + assertThat((short) 1).isEven()); + } } From 0d10eaaeb96b5d2da9f10a5db7c107d4beb611a1 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Wed, 2 Feb 2022 12:57:40 +0100 Subject: [PATCH 4/6] Tweaks --- .../errorprone/refastertemplates/AssertJMapTemplates.java | 4 ++-- .../errorprone/bugpatterns/AssertJMapTemplatesTestInput.java | 2 +- .../errorprone/bugpatterns/AssertJMapTemplatesTestOutput.java | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) 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 index b165bfdb12..5a54b59d1e 100644 --- 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 @@ -21,7 +21,7 @@ static final class AbstractMapAssertContainsExactlyEntriesOf { } } - static final class AbstractMapAssertContainsEntry { + static final class AbstractMapAssertContainsOnly { @BeforeTemplate AbstractMapAssert before(AbstractMapAssert mapAssert, K key, V value) { return mapAssert.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(key, value)); @@ -29,7 +29,7 @@ static final class AbstractMapAssertContainsEntry { @AfterTemplate AbstractMapAssert after(AbstractMapAssert mapAssert, K key, V value) { - return mapAssert.containsExactly(Map.entry(key, value)); + return mapAssert.containsOnly(Map.entry(key, value)); } } } 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 index 4cf4570d29..3739c50599 100644 --- 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 @@ -10,7 +10,7 @@ final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { return assertThat(ImmutableMap.of(1, 2, 3, 4)).isEqualTo(ImmutableMap.of(1, 2, 3, 4)); } - AbstractMapAssert testAbstractMapAssertContainsEntry() { + AbstractMapAssert testAbstractMapAssertContainsOnly() { 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 index 2967f34d96..d2fd29410f 100644 --- 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 @@ -12,7 +12,7 @@ final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2, 3, 4)); } - AbstractMapAssert testAbstractMapAssertContainsEntry() { - return assertThat(ImmutableMap.of(1, 2)).containsExactly(Map.entry(1, 2)); + AbstractMapAssert testAbstractMapAssertContainsOnly() { + return assertThat(ImmutableMap.of(1, 2)).containsOnly(Map.entry(1, 2)); } } From c2b3db76f5f4d99e9198ff3eac442742d3e60ba0 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 15 Feb 2022 10:53:56 +0100 Subject: [PATCH 5/6] Apply suggestions --- .../refastertemplates/AssertJMapTemplates.java | 6 +++--- .../AssertJMapTemplatesTestInput.java | 5 +++-- .../AssertJMapTemplatesTestOutput.java | 8 ++++---- .../AssertJNumberTemplatesTestInput.java | 16 ++++++++++++---- .../AssertJNumberTemplatesTestOutput.java | 16 ++++++++++++---- 5 files changed, 34 insertions(+), 17 deletions(-) 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 index 5a54b59d1e..566caa3265 100644 --- 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 @@ -9,7 +9,7 @@ final class AssertJMapTemplates { private AssertJMapTemplates() {} - static final class AbstractMapAssertContainsExactlyEntriesOf { + static final class AbstractMapAssertContainsExactlyInAnyOrderEntriesOf { @BeforeTemplate AbstractMapAssert before(AbstractMapAssert mapAssert, Map map) { return mapAssert.isEqualTo(map); @@ -21,7 +21,7 @@ static final class AbstractMapAssertContainsExactlyEntriesOf { } } - static final class AbstractMapAssertContainsOnly { + static final class AbstractMapAssertContainsExactlyEntriesOf { @BeforeTemplate AbstractMapAssert before(AbstractMapAssert mapAssert, K key, V value) { return mapAssert.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(key, value)); @@ -29,7 +29,7 @@ static final class AbstractMapAssertContainsOnly { @AfterTemplate AbstractMapAssert after(AbstractMapAssert mapAssert, K key, V value) { - return mapAssert.containsOnly(Map.entry(key, value)); + return mapAssert.containsExactlyEntriesOf(ImmutableMap.of(key, value)); } } } 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 index 3739c50599..108cf330eb 100644 --- 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 @@ -6,11 +6,12 @@ import org.assertj.core.api.AbstractMapAssert; final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { - AbstractMapAssert testAbstractMapAssertContainsExactlyEntriesOf() { + AbstractMapAssert + testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() { return assertThat(ImmutableMap.of(1, 2, 3, 4)).isEqualTo(ImmutableMap.of(1, 2, 3, 4)); } - AbstractMapAssert testAbstractMapAssertContainsOnly() { + 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 index d2fd29410f..cd25377325 100644 --- 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 @@ -3,16 +3,16 @@ import static org.assertj.core.api.Assertions.assertThat; import com.google.common.collect.ImmutableMap; -import java.util.Map; import org.assertj.core.api.AbstractMapAssert; final class AssertJMapTemplatesTest implements RefasterTemplateTestCase { - AbstractMapAssert testAbstractMapAssertContainsExactlyEntriesOf() { + AbstractMapAssert + testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() { return assertThat(ImmutableMap.of(1, 2, 3, 4)) .containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2, 3, 4)); } - AbstractMapAssert testAbstractMapAssertContainsOnly() { - return assertThat(ImmutableMap.of(1, 2)).containsOnly(Map.entry(1, 2)); + 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 62e3f43e33..3e0fe743f0 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 @@ -79,16 +79,24 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testAssertThatIsOdd() { return ImmutableSet.of( assertThat((byte) 1 % 2).isEqualTo(1), - assertThat((Integer) 1 % 2).isEqualTo(1), + assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(1), + assertThat((int) 1 % 2).isEqualTo(1), + assertThat(Integer.valueOf(1) % 2).isEqualTo(1), assertThat((long) 1 % 2).isEqualTo(1), - assertThat((short) 1 % 2).isEqualTo(1)); + assertThat(Long.valueOf(1L) % 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((Integer) 1 % 2).isEqualTo(0), + assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(0), + assertThat((int) 1 % 2).isEqualTo(0), + assertThat(Integer.valueOf(1) % 2).isEqualTo(0), assertThat((long) 1 % 2).isEqualTo(0), - assertThat((short) 1 % 2).isEqualTo(0)); + assertThat(Long.valueOf(1L) % 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 b33f771d02..b8a62004e7 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 @@ -79,16 +79,24 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { ImmutableSet> testAssertThatIsOdd() { return ImmutableSet.of( assertThat((byte) 1).isOdd(), - assertThat((Integer) 1).isOdd(), + assertThat(Byte.valueOf((byte) 1)).isOdd(), + assertThat((int) 1).isOdd(), + assertThat(Integer.valueOf(1)).isOdd(), assertThat((long) 1).isOdd(), - assertThat((short) 1).isOdd()); + assertThat(Long.valueOf(1L)).isOdd(), + assertThat((short) 1).isOdd(), + assertThat(Short.valueOf((short) 1)).isOdd()); } ImmutableSet> testAssertThatIsEven() { return ImmutableSet.of( assertThat((byte) 1).isEven(), - assertThat((Integer) 1).isEven(), + assertThat(Byte.valueOf((byte) 1)).isEven(), + assertThat((int) 1).isEven(), + assertThat(Integer.valueOf(1)).isEven(), assertThat((long) 1).isEven(), - assertThat((short) 1).isEven()); + assertThat(Long.valueOf(1L)).isEven(), + assertThat((short) 1).isEven(), + assertThat(Short.valueOf((short) 1)).isEven()); } } From 0e42b2cb2c22f87b8192e17e5fc4fa1969314aa5 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 10 Apr 2022 20:05:55 +0200 Subject: [PATCH 6/6] Suggestions --- .../AssertJNumberTemplates.java | 30 ++++--------------- .../AssertJStringTemplates.java | 5 ++-- .../AssertJNumberTemplatesTestInput.java | 12 ++++---- .../AssertJNumberTemplatesTestOutput.java | 12 ++++---- 4 files changed, 20 insertions(+), 39 deletions(-) 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 c8b73c1ea6..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,8 @@ 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; import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; @@ -227,11 +227,6 @@ AbstractBigDecimalAssert before(AbstractBigDecimalAssert numberAssert) { } static final class AssertThatIsOdd { - @BeforeTemplate - AbstractIntegerAssert before(byte number) { - return assertThat(number % 2).isEqualTo(1); - } - @BeforeTemplate AbstractIntegerAssert before(int number) { return assertThat(number % 2).isEqualTo(1); @@ -242,24 +237,14 @@ AbstractLongAssert before(long number) { return assertThat(number % 2).isEqualTo(1); } - @BeforeTemplate - AbstractIntegerAssert before(short number) { - return assertThat(number % 2).isEqualTo(1); - } - @AfterTemplate - @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) - NumberAssert after(Integer number) { + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + NumberAssert after(long number) { return assertThat(number).isOdd(); } } static final class AssertThatIsEven { - @BeforeTemplate - AbstractIntegerAssert before(byte number) { - return assertThat(number % 2).isEqualTo(0); - } - @BeforeTemplate AbstractIntegerAssert before(int number) { return assertThat(number % 2).isEqualTo(0); @@ -270,14 +255,9 @@ AbstractLongAssert before(long number) { return assertThat(number % 2).isEqualTo(0); } - @BeforeTemplate - AbstractIntegerAssert before(short number) { - return assertThat(number % 2).isEqualTo(0); - } - @AfterTemplate - @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) - NumberAssert after(Integer number) { + @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 cdc2e6697d..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; @@ -69,7 +70,7 @@ static final class AssertThatMatches { } @AfterTemplate - @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + @UseImportPolicy(STATIC_IMPORT_ALWAYS) AbstractAssert after(String string, String regex) { return assertThat(string).matches(regex); } @@ -82,7 +83,7 @@ static final class AssertThatDoesNotMatch { } @AfterTemplate - @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + @UseImportPolicy(STATIC_IMPORT_ALWAYS) AbstractAssert after(String string, String regex) { return assertThat(string).doesNotMatch(regex); } 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 3e0fe743f0..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 @@ -80,10 +80,10 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { return ImmutableSet.of( assertThat((byte) 1 % 2).isEqualTo(1), assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(1), - assertThat((int) 1 % 2).isEqualTo(1), + assertThat(1 % 2).isEqualTo(1), assertThat(Integer.valueOf(1) % 2).isEqualTo(1), - assertThat((long) 1 % 2).isEqualTo(1), - assertThat(Long.valueOf(1L) % 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)); } @@ -92,10 +92,10 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { return ImmutableSet.of( assertThat((byte) 1 % 2).isEqualTo(0), assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(0), - assertThat((int) 1 % 2).isEqualTo(0), + assertThat(1 % 2).isEqualTo(0), assertThat(Integer.valueOf(1) % 2).isEqualTo(0), - assertThat((long) 1 % 2).isEqualTo(0), - assertThat(Long.valueOf(1L) % 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 b8a62004e7..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 @@ -80,10 +80,10 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { return ImmutableSet.of( assertThat((byte) 1).isOdd(), assertThat(Byte.valueOf((byte) 1)).isOdd(), - assertThat((int) 1).isOdd(), + assertThat(1).isOdd(), assertThat(Integer.valueOf(1)).isOdd(), - assertThat((long) 1).isOdd(), - assertThat(Long.valueOf(1L)).isOdd(), + assertThat(1L).isOdd(), + assertThat(Long.valueOf(1)).isOdd(), assertThat((short) 1).isOdd(), assertThat(Short.valueOf((short) 1)).isOdd()); } @@ -92,10 +92,10 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase { return ImmutableSet.of( assertThat((byte) 1).isEven(), assertThat(Byte.valueOf((byte) 1)).isEven(), - assertThat((int) 1).isEven(), + assertThat(1).isEven(), assertThat(Integer.valueOf(1)).isEven(), - assertThat((long) 1).isEven(), - assertThat(Long.valueOf(1L)).isEven(), + assertThat(1L).isEven(), + assertThat(Long.valueOf(1)).isEven(), assertThat((short) 1).isEven(), assertThat(Short.valueOf((short) 1)).isEven()); }