From 2199accedcaebfe0e67f8c229cbb9c39e712b92f Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Mon, 11 Apr 2022 10:08:15 +0200 Subject: [PATCH] Drop or rewrite `AssertJBigDecimalTemplates` rules (#30) --- .../AssertJBigDecimalTemplates.java | 68 +++++-------------- .../AssertJBigDecimalTemplatesTestInput.java | 25 +------ .../AssertJBigDecimalTemplatesTestOutput.java | 33 ++------- 3 files changed, 24 insertions(+), 102 deletions(-) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplates.java index f623524c52..aea7797008 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/AssertJBigDecimalTemplates.java @@ -8,13 +8,22 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import java.math.BigDecimal; import org.assertj.core.api.AbstractBigDecimalAssert; - -// XXX: If we add a rule which drops unnecessary `L` suffixes from literal longs, then the `0L`/`1L` -// cases below can go. +import org.assertj.core.api.BigDecimalAssert; + +/** + * Refaster templates related to AssertJ assertions over {@link BigDecimal}s. + * + *

Note that, contrary to collections of Refaster templates for other {@link + * org.assertj.core.api.NumberAssert} subtypes, these templates do not rewrite to/from {@link + * BigDecimalAssert#isEqualTo(Object)} and {@link BigDecimalAssert#isNotEqualTo(Object)}. This is + * because {@link BigDecimal#equals(Object)} considers not only the numeric value of compared + * instances, but also their scale. As a result various seemingly straightforward transformations + * would actually subtly change the assertion's semantics. + */ final class AssertJBigDecimalTemplates { private AssertJBigDecimalTemplates() {} - static final class AbstractBigDecimalAssertIsEqualTo { + static final class AbstractBigDecimalAssertIsEqualByComparingTo { @BeforeTemplate AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert, BigDecimal n) { return Refaster.anyOf( @@ -24,11 +33,11 @@ AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert, @AfterTemplate AbstractBigDecimalAssert after(AbstractBigDecimalAssert bigDecimalAssert, BigDecimal n) { - return bigDecimalAssert.isEqualTo(n); + return bigDecimalAssert.isEqualByComparingTo(n); } } - static final class AbstractBigDecimalAssertIsNotEqualTo { + static final class AbstractBigDecimalAssertIsNotEqualByComparingTo { @BeforeTemplate AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert, BigDecimal n) { return Refaster.anyOf( @@ -38,52 +47,7 @@ AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert, @AfterTemplate AbstractBigDecimalAssert after(AbstractBigDecimalAssert bigDecimalAssert, BigDecimal n) { - return bigDecimalAssert.isNotEqualTo(n); - } - } - - static final class AbstractBigDecimalAssertIsZero { - @BeforeTemplate - AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert) { - return Refaster.anyOf( - bigDecimalAssert.isZero(), - bigDecimalAssert.isEqualTo(0L), - bigDecimalAssert.isEqualTo(BigDecimal.ZERO)); - } - - @AfterTemplate - AbstractBigDecimalAssert after(AbstractBigDecimalAssert bigDecimalAssert) { - return bigDecimalAssert.isEqualTo(0); - } - } - - static final class AbstractBigDecimalAssertIsNotZero { - @BeforeTemplate - AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert) { - return Refaster.anyOf( - bigDecimalAssert.isNotZero(), - bigDecimalAssert.isNotEqualTo(0L), - bigDecimalAssert.isNotEqualTo(BigDecimal.ZERO)); - } - - @AfterTemplate - AbstractBigDecimalAssert after(AbstractBigDecimalAssert bigDecimalAssert) { - return bigDecimalAssert.isNotEqualTo(0); - } - } - - static final class AbstractBigDecimalAssertIsOne { - @BeforeTemplate - AbstractBigDecimalAssert before(AbstractBigDecimalAssert bigDecimalAssert) { - return Refaster.anyOf( - bigDecimalAssert.isOne(), - bigDecimalAssert.isEqualTo(1L), - bigDecimalAssert.isEqualTo(BigDecimal.ONE)); - } - - @AfterTemplate - AbstractBigDecimalAssert after(AbstractBigDecimalAssert bigDecimalAssert) { - return bigDecimalAssert.isEqualTo(1); + return bigDecimalAssert.isNotEqualByComparingTo(n); } } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestInput.java index 3749240b06..57d87c293a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestInput.java @@ -14,36 +14,15 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(offset(0), withPercentage(0)); } - ImmutableSet> testAbstractBigDecimalAssertIsEqualTo() { + ImmutableSet> testAbstractBigDecimalAssertIsEqualByComparingTo() { return ImmutableSet.of( assertThat(BigDecimal.ZERO).isCloseTo(BigDecimal.ONE, offset(BigDecimal.ZERO)), assertThat(BigDecimal.ZERO).isCloseTo(BigDecimal.ONE, withPercentage(0))); } - ImmutableSet> testAbstractBigDecimalAssertIsNotEqualTo() { + ImmutableSet> testAbstractBigDecimalAssertIsNotEqualByComparingTo() { return ImmutableSet.of( assertThat(BigDecimal.ZERO).isNotCloseTo(BigDecimal.ONE, offset(BigDecimal.ZERO)), assertThat(BigDecimal.ZERO).isNotCloseTo(BigDecimal.ONE, withPercentage(0))); } - - ImmutableSet> testAbstractBigDecimalAssertIsZero() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isZero(), - assertThat(BigDecimal.ZERO).isEqualTo(0L), - assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ZERO)); - } - - ImmutableSet> testAbstractBigDecimalAssertIsNotZero() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isNotZero(), - assertThat(BigDecimal.ZERO).isNotEqualTo(0L), - assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ZERO)); - } - - ImmutableSet> testAbstractBigDecimalAssertIsOne() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isOne(), - assertThat(BigDecimal.ZERO).isEqualTo(1L), - assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE)); - } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestOutput.java index 3172f16f0c..232626eea0 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/AssertJBigDecimalTemplatesTestOutput.java @@ -14,36 +14,15 @@ public ImmutableSet elidedTypesAndStaticImports() { return ImmutableSet.of(offset(0), withPercentage(0)); } - ImmutableSet> testAbstractBigDecimalAssertIsEqualTo() { + ImmutableSet> testAbstractBigDecimalAssertIsEqualByComparingTo() { return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE), - assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE)); + assertThat(BigDecimal.ZERO).isEqualByComparingTo(BigDecimal.ONE), + assertThat(BigDecimal.ZERO).isEqualByComparingTo(BigDecimal.ONE)); } - ImmutableSet> testAbstractBigDecimalAssertIsNotEqualTo() { + ImmutableSet> testAbstractBigDecimalAssertIsNotEqualByComparingTo() { return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ONE), - assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ONE)); - } - - ImmutableSet> testAbstractBigDecimalAssertIsZero() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isEqualTo(0), - assertThat(BigDecimal.ZERO).isEqualTo(0), - assertThat(BigDecimal.ZERO).isEqualTo(0)); - } - - ImmutableSet> testAbstractBigDecimalAssertIsNotZero() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isNotEqualTo(0), - assertThat(BigDecimal.ZERO).isNotEqualTo(0), - assertThat(BigDecimal.ZERO).isNotEqualTo(0)); - } - - ImmutableSet> testAbstractBigDecimalAssertIsOne() { - return ImmutableSet.of( - assertThat(BigDecimal.ZERO).isEqualTo(1), - assertThat(BigDecimal.ZERO).isEqualTo(1), - assertThat(BigDecimal.ZERO).isEqualTo(1)); + assertThat(BigDecimal.ZERO).isNotEqualByComparingTo(BigDecimal.ONE), + assertThat(BigDecimal.ZERO).isNotEqualByComparingTo(BigDecimal.ONE)); } }