Skip to content

Commit

Permalink
Drop or rewrite AssertJBigDecimalTemplates rules (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie authored Apr 11, 2022
1 parent 83670ae commit 2199acc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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(
Expand All @@ -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(
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,15 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(offset(0), withPercentage(0));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsEqualTo() {
ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsEqualByComparingTo() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isCloseTo(BigDecimal.ONE, offset(BigDecimal.ZERO)),
assertThat(BigDecimal.ZERO).isCloseTo(BigDecimal.ONE, withPercentage(0)));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotEqualTo() {
ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotEqualByComparingTo() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isNotCloseTo(BigDecimal.ONE, offset(BigDecimal.ZERO)),
assertThat(BigDecimal.ZERO).isNotCloseTo(BigDecimal.ONE, withPercentage(0)));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsZero() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isZero(),
assertThat(BigDecimal.ZERO).isEqualTo(0L),
assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ZERO));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotZero() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isNotZero(),
assertThat(BigDecimal.ZERO).isNotEqualTo(0L),
assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ZERO));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsOne() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isOne(),
assertThat(BigDecimal.ZERO).isEqualTo(1L),
assertThat(BigDecimal.ZERO).isEqualTo(BigDecimal.ONE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,15 @@ public ImmutableSet<?> elidedTypesAndStaticImports() {
return ImmutableSet.of(offset(0), withPercentage(0));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsEqualTo() {
ImmutableSet<AbstractBigDecimalAssert<?>> 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<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotEqualTo() {
ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotEqualByComparingTo() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ONE),
assertThat(BigDecimal.ZERO).isNotEqualTo(BigDecimal.ONE));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsZero() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isEqualTo(0),
assertThat(BigDecimal.ZERO).isEqualTo(0),
assertThat(BigDecimal.ZERO).isEqualTo(0));
}

ImmutableSet<AbstractBigDecimalAssert<?>> testAbstractBigDecimalAssertIsNotZero() {
return ImmutableSet.of(
assertThat(BigDecimal.ZERO).isNotEqualTo(0),
assertThat(BigDecimal.ZERO).isNotEqualTo(0),
assertThat(BigDecimal.ZERO).isNotEqualTo(0));
}

ImmutableSet<AbstractBigDecimalAssert<?>> 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));
}
}

0 comments on commit 2199acc

Please sign in to comment.