From 764fbf45caad194718af5d58948531eb876e2688 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 29 Dec 2024 13:59:22 +0100 Subject: [PATCH 1/2] Introduce assorted `Iterator` and `Iterable` Refaster rules --- .../refasterrules/AssertJEnumerableRules.java | 101 ++++++++++++++++- .../refasterrules/AssertJIterableRules.java | 88 +++++++++++++++ .../refasterrules/AssertJIteratorRules.java | 43 +++++++ .../refasterrules/AssertJRules.java | 106 ------------------ .../refasterrules/RefasterRulesTest.java | 4 +- .../AssertJEnumerableRulesTestInput.java | 39 ++++++- .../AssertJEnumerableRulesTestOutput.java | 40 ++++++- .../AssertJIterableRulesTestInput.java | 35 ++++++ .../AssertJIterableRulesTestOutput.java | 34 ++++++ .../AssertJIteratorRulesTestInput.java | 17 +++ .../AssertJIteratorRulesTestOutput.java | 17 +++ 11 files changed, 402 insertions(+), 122 deletions(-) create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java create mode 100644 error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIteratorRules.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestOutput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestInput.java create mode 100644 error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestOutput.java diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJEnumerableRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJEnumerableRules.java index bc19fd72dc..b4318931d9 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJEnumerableRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJEnumerableRules.java @@ -5,6 +5,9 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; import java.util.Collection; +import org.assertj.core.api.AbstractIntegerAssert; +import org.assertj.core.api.AbstractIterableAssert; +import org.assertj.core.api.AbstractIterableSizeAssert; import org.assertj.core.api.EnumerableAssert; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; @@ -21,6 +24,11 @@ void before(EnumerableAssert enumAssert) { enumAssert.hasSizeLessThan(1)); } + @BeforeTemplate + void before(AbstractIterableAssert enumAssert) { + enumAssert.size().isNotPositive(); + } + @AfterTemplate void after(EnumerableAssert enumAssert) { enumAssert.isEmpty(); @@ -34,30 +42,113 @@ EnumerableAssert before(EnumerableAssert enumAssert) { enumAssert.hasSizeGreaterThan(0), enumAssert.hasSizeGreaterThanOrEqualTo(1)); } + @BeforeTemplate + AbstractIntegerAssert before(AbstractIterableAssert enumAssert) { + return Refaster.anyOf(enumAssert.size().isNotEqualTo(0), enumAssert.size().isPositive()); + } + @AfterTemplate EnumerableAssert after(EnumerableAssert enumAssert) { return enumAssert.isNotEmpty(); } } - static final class EnumerableAssertHasSameSizeAs { + static final class EnumerableAssertHasSize { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int size) { + return enumAssert.size().isEqualTo(size); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int size) { + return enumAssert.hasSize(size); + } + } + + static final class EnumerableAssertHasSizeLessThan { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int size) { + return enumAssert.size().isLessThan(size); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int size) { + return enumAssert.hasSizeLessThan(size); + } + } + + static final class EnumerableAssertHasSizeLessThanOrEqualTo { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int size) { + return enumAssert.size().isLessThanOrEqualTo(size); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int size) { + return enumAssert.hasSizeLessThanOrEqualTo(size); + } + } + + static final class EnumerableAssertHasSizeGreaterThan { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int size) { + return enumAssert.size().isGreaterThan(size); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int size) { + return enumAssert.hasSizeGreaterThan(size); + } + } + + static final class EnumerableAssertHasSizeGreaterThanOrEqualTo { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int size) { + return enumAssert.size().isGreaterThanOrEqualTo(size); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int size) { + return enumAssert.hasSizeGreaterThanOrEqualTo(size); + } + } + + static final class EnumerableAssertHasSizeBetween { + @BeforeTemplate + AbstractIterableSizeAssert before( + AbstractIterableAssert enumAssert, int lower, int upper) { + return enumAssert.size().isBetween(lower, upper); + } + + @AfterTemplate + EnumerableAssert after(EnumerableAssert enumAssert, int lower, int upper) { + return enumAssert.hasSizeBetween(lower, upper); + } + } + + static final class EnumerableAssertHasSameSizeAs { @BeforeTemplate - EnumerableAssert before(EnumerableAssert enumAssert, Iterable iterable) { + EnumerableAssert before(EnumerableAssert enumAssert, Iterable iterable) { return enumAssert.hasSize(Iterables.size(iterable)); } @BeforeTemplate - EnumerableAssert before(EnumerableAssert enumAssert, Collection iterable) { + EnumerableAssert before(EnumerableAssert enumAssert, Collection iterable) { return enumAssert.hasSize(iterable.size()); } @BeforeTemplate - EnumerableAssert before(EnumerableAssert enumAssert, T[] iterable) { + EnumerableAssert before(EnumerableAssert enumAssert, E[] iterable) { return enumAssert.hasSize(iterable.length); } @AfterTemplate - EnumerableAssert after(EnumerableAssert enumAssert, Iterable iterable) { + EnumerableAssert after(EnumerableAssert enumAssert, Iterable iterable) { return enumAssert.hasSameSizeAs(iterable); } } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java new file mode 100644 index 0000000000..7eb5ce2c1e --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java @@ -0,0 +1,88 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.Iterables; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.Collection; +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.AbstractIntegerAssert; +import org.assertj.core.api.IterableAssert; +import org.assertj.core.api.ObjectAssert; + +final class AssertJIterableRules { + private AssertJIterableRules() {} + + static final class AssertThatIterableIsEmpty { + @BeforeTemplate + void before(Iterable iterable) { + assertThat(iterable.iterator()).isExhausted(); + } + + @BeforeTemplate + void before(Collection iterable) { + assertThat(iterable.isEmpty()).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(Collection iterable) { + assertThat(iterable).isEmpty(); + } + } + + static final class AssertThatIterableIsNotEmpty { + @BeforeTemplate + AbstractAssert before(Iterable iterable) { + return assertThat(iterable.iterator()).hasNext(); + } + + @BeforeTemplate + AbstractAssert before(Collection iterable) { + return assertThat(iterable.isEmpty()).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + IterableAssert after(Iterable iterable) { + return assertThat(iterable).isNotEmpty(); + } + } + + static final class AssertThatIterableSize { + @BeforeTemplate + AbstractIntegerAssert before(Iterable iterable) { + return assertThat(Iterables.size(iterable)); + } + + @BeforeTemplate + AbstractIntegerAssert before(Collection iterable) { + return assertThat(iterable.size()); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractIntegerAssert after(Iterable iterable) { + return assertThat(iterable).size(); + } + } + + // XXX: In practice this rule isn't very useful, as it only matches invocations of + // `assertThat(E)`. In most cases a more specific overload of `assertThat` is invoked, in which + // case this rule won't match. Look into a more robust approach. + static final class AssertThatIterableHasOneElementEqualTo { + @BeforeTemplate + ObjectAssert before(Iterable iterable, E element) { + return assertThat(Iterables.getOnlyElement(iterable)).isEqualTo(element); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + IterableAssert after(Iterable iterable, E element) { + return assertThat(iterable).containsExactly(element); + } + } +} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIteratorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIteratorRules.java new file mode 100644 index 0000000000..2b2e503fcb --- /dev/null +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIteratorRules.java @@ -0,0 +1,43 @@ +package tech.picnic.errorprone.refasterrules; + +import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.Iterator; +import org.assertj.core.api.AbstractBooleanAssert; +import org.assertj.core.api.IteratorAssert; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; + +@OnlineDocumentation +final class AssertJIteratorRules { + private AssertJIteratorRules() {} + + static final class AssertThatHasNext { + @BeforeTemplate + AbstractBooleanAssert before(Iterator iterator) { + return assertThat(iterator.hasNext()).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + IteratorAssert after(Iterator iterator) { + return assertThat(iterator).hasNext(); + } + } + + static final class AssertThatIsExhausted { + @BeforeTemplate + AbstractBooleanAssert before(Iterator iterator) { + return assertThat(iterator.hasNext()).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + IteratorAssert after(Iterator iterator) { + return assertThat(iterator).isExhausted(); + } + } +} diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJRules.java index c325dcec7f..c45c912c26 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJRules.java @@ -3,7 +3,6 @@ import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; import static org.assertj.core.api.Assertions.assertThat; -import com.google.common.collect.Iterables; import com.google.common.collect.Multiset; import com.google.errorprone.refaster.Refaster; import com.google.errorprone.refaster.annotation.AfterTemplate; @@ -12,7 +11,6 @@ import com.google.errorprone.refaster.annotation.NotMatches; import com.google.errorprone.refaster.annotation.Repeated; import com.google.errorprone.refaster.annotation.UseImportPolicy; -import java.util.Collection; import java.util.List; import java.util.Map; import java.util.OptionalDouble; @@ -22,9 +20,7 @@ import java.util.function.Predicate; import java.util.stream.Collector; import java.util.stream.Stream; -import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractCollectionAssert; -import org.assertj.core.api.AbstractComparableAssert; import org.assertj.core.api.AbstractDoubleAssert; import org.assertj.core.api.AbstractIntegerAssert; import org.assertj.core.api.AbstractLongAssert; @@ -254,108 +250,6 @@ ObjectEnumerableAssert after(Set set, T element) { } } - // - // Iterable - // - - static final class AssertThatIterableIsEmpty { - @BeforeTemplate - void before(Iterable iterable) { - Refaster.anyOf( - assertThat(iterable).hasSize(0), - assertThat(iterable.iterator().hasNext()).isFalse(), - assertThat(iterable.iterator()).isExhausted(), - assertThat(Iterables.size(iterable)).isEqualTo(0L), - assertThat(Iterables.size(iterable)).isNotPositive()); - } - - @BeforeTemplate - void before(Collection iterable) { - Refaster.anyOf( - assertThat(iterable.isEmpty()).isTrue(), - assertThat(iterable.size()).isEqualTo(0L), - assertThat(iterable.size()).isNotPositive()); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - void after(Collection iterable) { - assertThat(iterable).isEmpty(); - } - } - - static final class AssertThatIterableIsNotEmpty { - @BeforeTemplate - AbstractAssert before(Iterable iterable) { - return Refaster.anyOf( - assertThat(iterable.iterator().hasNext()).isTrue(), - assertThat(Iterables.size(iterable)).isNotEqualTo(0), - assertThat(Iterables.size(iterable)).isPositive()); - } - - @BeforeTemplate - AbstractAssert before(Collection iterable) { - return Refaster.anyOf( - assertThat(iterable.isEmpty()).isFalse(), - assertThat(iterable.size()).isNotEqualTo(0), - assertThat(iterable.size()).isPositive()); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - IterableAssert after(Iterable iterable) { - return assertThat(iterable).isNotEmpty(); - } - } - - static final class AssertThatIterableHasSize { - @BeforeTemplate - AbstractIntegerAssert before(Iterable iterable, int length) { - return assertThat(Iterables.size(iterable)).isEqualTo(length); - } - - @BeforeTemplate - AbstractIntegerAssert before(Collection iterable, int length) { - return assertThat(iterable.size()).isEqualTo(length); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - IterableAssert after(Iterable iterable, int length) { - return assertThat(iterable).hasSize(length); - } - } - - static final class AssertThatIterableHasOneElementEqualTo { - @BeforeTemplate - ObjectAssert before(Iterable iterable, T element) { - return assertThat(Iterables.getOnlyElement(iterable)).isEqualTo(element); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - IterableAssert after(Iterable iterable, T element) { - return assertThat(iterable).containsExactly(element); - } - } - - // XXX: This overload is here because `assertThat` has an overload for `Comparable` types. - // Unfortunately this still doesn't convince Refaster to match this rule in the context of - // Comparable types. Figure out why! Note that this also affects the `AssertThatOptional` rule. - static final class AssertThatIterableHasOneComparableElementEqualTo< - S extends Comparable, T extends S> { - @BeforeTemplate - AbstractComparableAssert before(Iterable iterable, T element) { - return assertThat(Iterables.getOnlyElement(iterable)).isEqualTo(element); - } - - @AfterTemplate - @UseImportPolicy(STATIC_IMPORT_ALWAYS) - IterableAssert after(Iterable iterable, T element) { - return assertThat(iterable).containsExactly(element); - } - } - // // List // diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java index d7b47d307b..0d7046f918 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/refasterrules/RefasterRulesTest.java @@ -23,6 +23,8 @@ final class RefasterRulesTest { AssertJEnumerableRules.class, AssertJFloatRules.class, AssertJIntegerRules.class, + AssertJIterableRules.class, + AssertJIteratorRules.class, AssertJLongRules.class, AssertJMapRules.class, AssertJNumberRules.class, @@ -43,7 +45,6 @@ final class RefasterRulesTest { DoubleStreamRules.class, EqualityRules.class, FileRules.class, - InputStreamRules.class, ImmutableEnumSetRules.class, ImmutableListRules.class, ImmutableListMultimapRules.class, @@ -55,6 +56,7 @@ final class RefasterRulesTest { ImmutableSortedMultisetRules.class, ImmutableSortedSetRules.class, ImmutableTableRules.class, + InputStreamRules.class, IntStreamRules.class, JUnitRules.class, JUnitToAssertJRules.class, diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestInput.java index bab44b4e7e..f2e603e46b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestInput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.EnumerableAssert; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; @@ -14,14 +15,42 @@ public ImmutableSet elidedTypesAndStaticImports() { } void testEnumerableAssertIsEmpty() { - assertThat(ImmutableSet.of()).hasSize(0); - assertThat(ImmutableSet.of()).hasSizeLessThanOrEqualTo(0); - assertThat(ImmutableSet.of()).hasSizeLessThan(1); + assertThat(ImmutableSet.of(1)).hasSize(0); + assertThat(ImmutableSet.of(2)).hasSizeLessThanOrEqualTo(0); + assertThat(ImmutableSet.of(3)).hasSizeLessThan(1); + assertThat(ImmutableSet.of(4)).size().isNotPositive(); } - ImmutableSet> testEnumerableAssertIsNotEmpty() { + ImmutableSet> testEnumerableAssertIsNotEmpty() { return ImmutableSet.of( - assertThat("foo").hasSizeGreaterThan(0), assertThat("bar").hasSizeGreaterThanOrEqualTo(1)); + assertThat(ImmutableSet.of(1)).hasSizeGreaterThan(0), + assertThat(ImmutableSet.of(2)).hasSizeGreaterThanOrEqualTo(1), + assertThat(ImmutableSet.of(3)).size().isNotEqualTo(0), + assertThat(ImmutableSet.of(4)).size().isPositive()); + } + + AbstractAssert testEnumerableAssertHasSize() { + return assertThat(ImmutableSet.of(1)).size().isEqualTo(2); + } + + AbstractAssert testEnumerableAssertHasSizeLessThan() { + return assertThat(ImmutableSet.of(1)).size().isLessThan(2); + } + + AbstractAssert testEnumerableAssertHasSizeLessThanOrEqualTo() { + return assertThat(ImmutableSet.of(1)).size().isLessThanOrEqualTo(2); + } + + AbstractAssert testEnumerableAssertHasSizeGreaterThan() { + return assertThat(ImmutableSet.of(1)).size().isGreaterThan(2); + } + + AbstractAssert testEnumerableAssertHasSizeGreaterThanOrEqualTo() { + return assertThat(ImmutableSet.of(1)).size().isGreaterThanOrEqualTo(2); + } + + AbstractAssert testEnumerableAssertHasSizeBetween() { + return assertThat(ImmutableSet.of(1)).size().isBetween(2, 3); } ImmutableSet> testEnumerableAssertHasSameSizeAs() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestOutput.java index 892f2f69d8..93f7a685c9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJEnumerableRulesTestOutput.java @@ -4,6 +4,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; +import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.EnumerableAssert; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; @@ -14,13 +15,42 @@ public ImmutableSet elidedTypesAndStaticImports() { } void testEnumerableAssertIsEmpty() { - assertThat(ImmutableSet.of()).isEmpty(); - assertThat(ImmutableSet.of()).isEmpty(); - assertThat(ImmutableSet.of()).isEmpty(); + assertThat(ImmutableSet.of(1)).isEmpty(); + assertThat(ImmutableSet.of(2)).isEmpty(); + assertThat(ImmutableSet.of(3)).isEmpty(); + assertThat(ImmutableSet.of(4)).isEmpty(); } - ImmutableSet> testEnumerableAssertIsNotEmpty() { - return ImmutableSet.of(assertThat("foo").isNotEmpty(), assertThat("bar").isNotEmpty()); + ImmutableSet> testEnumerableAssertIsNotEmpty() { + return ImmutableSet.of( + assertThat(ImmutableSet.of(1)).isNotEmpty(), + assertThat(ImmutableSet.of(2)).isNotEmpty(), + assertThat(ImmutableSet.of(3)).isNotEmpty(), + assertThat(ImmutableSet.of(4)).isNotEmpty()); + } + + AbstractAssert testEnumerableAssertHasSize() { + return assertThat(ImmutableSet.of(1)).hasSize(2); + } + + AbstractAssert testEnumerableAssertHasSizeLessThan() { + return assertThat(ImmutableSet.of(1)).hasSizeLessThan(2); + } + + AbstractAssert testEnumerableAssertHasSizeLessThanOrEqualTo() { + return assertThat(ImmutableSet.of(1)).hasSizeLessThanOrEqualTo(2); + } + + AbstractAssert testEnumerableAssertHasSizeGreaterThan() { + return assertThat(ImmutableSet.of(1)).hasSizeGreaterThan(2); + } + + AbstractAssert testEnumerableAssertHasSizeGreaterThanOrEqualTo() { + return assertThat(ImmutableSet.of(1)).hasSizeGreaterThanOrEqualTo(2); + } + + AbstractAssert testEnumerableAssertHasSizeBetween() { + return assertThat(ImmutableSet.of(1)).hasSizeBetween(2, 3); } ImmutableSet> testEnumerableAssertHasSameSizeAs() { diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestInput.java new file mode 100644 index 0000000000..9e3b3fb145 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestInput.java @@ -0,0 +1,35 @@ +package tech.picnic.errorprone.refasterrules; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.AbstractIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class AssertJIterableRulesTest implements RefasterRuleCollectionTestCase { + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Iterables.class); + } + + void testAssertThatIterableIsEmpty() { + assertThat(ImmutableSet.of(1).iterator()).isExhausted(); + assertThat(ImmutableSet.of(2).isEmpty()).isTrue(); + } + + ImmutableSet> testAssertThatIterableIsNotEmpty() { + return ImmutableSet.of( + assertThat(ImmutableSet.of(1).iterator()).hasNext(), + assertThat(ImmutableSet.of(2).isEmpty()).isFalse()); + } + + ImmutableSet> testAssertThatIterableSize() { + return ImmutableSet.of( + assertThat(Iterables.size(ImmutableSet.of(1))), assertThat(ImmutableSet.of(2).size())); + } + + AbstractAssert testAssertThatIterableHasOneElementEqualTo() { + return assertThat(Iterables.getOnlyElement(ImmutableSet.of(new Object()))).isEqualTo("foo"); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestOutput.java new file mode 100644 index 0000000000..e4757b4f48 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIterableRulesTestOutput.java @@ -0,0 +1,34 @@ +package tech.picnic.errorprone.refasterrules; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.AbstractIntegerAssert; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class AssertJIterableRulesTest implements RefasterRuleCollectionTestCase { + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Iterables.class); + } + + void testAssertThatIterableIsEmpty() { + assertThat(ImmutableSet.of(1)).isEmpty(); + assertThat(ImmutableSet.of(2)).isEmpty(); + } + + ImmutableSet> testAssertThatIterableIsNotEmpty() { + return ImmutableSet.of( + assertThat(ImmutableSet.of(1)).isNotEmpty(), assertThat(ImmutableSet.of(2)).isNotEmpty()); + } + + ImmutableSet> testAssertThatIterableSize() { + return ImmutableSet.of( + assertThat(ImmutableSet.of(1)).size(), assertThat(ImmutableSet.of(2)).size()); + } + + AbstractAssert testAssertThatIterableHasOneElementEqualTo() { + return assertThat(ImmutableSet.of(new Object())).containsExactly("foo"); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestInput.java new file mode 100644 index 0000000000..fbbb2e5874 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestInput.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refasterrules; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableSet; +import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class AssertJIteratorRulesTest implements RefasterRuleCollectionTestCase { + AbstractAssert testAssertThatHasNext() { + return assertThat(ImmutableSet.of().iterator().hasNext()).isTrue(); + } + + AbstractAssert testAssertThatIsExhausted() { + return assertThat(ImmutableSet.of().iterator().hasNext()).isFalse(); + } +} diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestOutput.java new file mode 100644 index 0000000000..e616358b13 --- /dev/null +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJIteratorRulesTestOutput.java @@ -0,0 +1,17 @@ +package tech.picnic.errorprone.refasterrules; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableSet; +import org.assertj.core.api.AbstractAssert; +import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; + +final class AssertJIteratorRulesTest implements RefasterRuleCollectionTestCase { + AbstractAssert testAssertThatHasNext() { + return assertThat(ImmutableSet.of().iterator()).hasNext(); + } + + AbstractAssert testAssertThatIsExhausted() { + return assertThat(ImmutableSet.of().iterator()).isExhausted(); + } +} From 7e5c40b20807ccf59284ca05221343f3349ff573 Mon Sep 17 00:00:00 2001 From: Rick Ossendrijver Date: Tue, 7 Jan 2025 17:58:07 +0100 Subject: [PATCH 2/2] Add `@OnlineDocumentation` annotation --- .../picnic/errorprone/refasterrules/AssertJIterableRules.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java index 7eb5ce2c1e..70bc5ed1e6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJIterableRules.java @@ -12,7 +12,9 @@ import org.assertj.core.api.AbstractIntegerAssert; import org.assertj.core.api.IterableAssert; import org.assertj.core.api.ObjectAssert; +import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; +@OnlineDocumentation final class AssertJIterableRules { private AssertJIterableRules() {}