From 2eda393c037769b33f267806a5899dc7b62423a6 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Mon, 9 Dec 2024 14:25:06 +0100 Subject: [PATCH] Introduce `PublisherProbeAssertWas{,Not}{Subscribed,Cancelled,Requested}` Refaster rules (#1423) While there, fix a typo in `README.md`. --- README.md | 2 +- .../refasterrules/ReactorRules.java | 85 +++++++++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 30 +++++++ .../refasterrules/ReactorRulesTestOutput.java | 30 +++++++ 4 files changed, 146 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 838f2556c2..1826bbe3a2 100644 --- a/README.md +++ b/README.md @@ -217,7 +217,7 @@ Other highly relevant commands: - `mvn fmt:format` formats the code using [`google-java-format`][google-java-format]. - [`./run-full-build.sh`][script-run-full-build] builds the project twice, - where the second pass validates compatbility with Picnic's [Error Prone + where the second pass validates compatibility with Picnic's [Error Prone fork][error-prone-fork-repo] and compliance of the code with any rules defined within this project. (Consider running this before [opening a pull request][contributing-pull-request], as the PR checks also perform this diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index 404f78305c..edbf788eed 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -1742,6 +1742,91 @@ PublisherProbe after() { } } + /** Prefer {@link PublisherProbe#assertWasSubscribed()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasSubscribed { + @BeforeTemplate + void before(PublisherProbe probe) { + Refaster.anyOf( + assertThat(probe.wasSubscribed()).isTrue(), + assertThat(probe.subscribeCount()).isNotNegative(), + assertThat(probe.subscribeCount()).isNotEqualTo(0), + assertThat(probe.subscribeCount()).isPositive()); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasSubscribed(); + } + } + + /** Prefer {@link PublisherProbe#assertWasNotSubscribed()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasNotSubscribed { + @BeforeTemplate + void before(PublisherProbe probe) { + Refaster.anyOf( + assertThat(probe.wasSubscribed()).isFalse(), + assertThat(probe.subscribeCount()).isEqualTo(0), + assertThat(probe.subscribeCount()).isNotPositive()); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasNotSubscribed(); + } + } + + /** Prefer {@link PublisherProbe#assertWasCancelled()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasCancelled { + @BeforeTemplate + void before(PublisherProbe probe) { + assertThat(probe.wasCancelled()).isTrue(); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasCancelled(); + } + } + + /** Prefer {@link PublisherProbe#assertWasNotCancelled()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasNotCancelled { + @BeforeTemplate + void before(PublisherProbe probe) { + assertThat(probe.wasCancelled()).isFalse(); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasNotCancelled(); + } + } + + /** Prefer {@link PublisherProbe#assertWasRequested()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasRequested { + @BeforeTemplate + void before(PublisherProbe probe) { + assertThat(probe.wasRequested()).isTrue(); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasRequested(); + } + } + + /** Prefer {@link PublisherProbe#assertWasNotRequested()} over more verbose alternatives. */ + static final class PublisherProbeAssertWasNotRequested { + @BeforeTemplate + void before(PublisherProbe probe) { + assertThat(probe.wasRequested()).isFalse(); + } + + @AfterTemplate + void after(PublisherProbe probe) { + probe.assertWasNotRequested(); + } + } + /** Prefer {@link Mono#as(Function)} when creating a {@link StepVerifier}. */ static final class StepVerifierFromMono { @BeforeTemplate diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index 80375ea4b4..ecfe450352 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -44,6 +44,7 @@ public ImmutableSet elidedTypesAndStaticImports() { List.class, ImmutableCollection.class, ImmutableMap.class, + assertThat(false), assertThat(0), maxBy(null), minBy(null), @@ -589,6 +590,35 @@ ImmutableSet> testPublisherProbeEmpty() { return ImmutableSet.of(PublisherProbe.of(Mono.empty()), PublisherProbe.of(Flux.empty())); } + void testPublisherProbeAssertWasSubscribed() { + assertThat(PublisherProbe.of(Mono.just(1)).wasSubscribed()).isTrue(); + assertThat(PublisherProbe.of(Mono.just(2)).subscribeCount()).isNotNegative(); + assertThat(PublisherProbe.of(Mono.just(3)).subscribeCount()).isNotEqualTo(0); + assertThat(PublisherProbe.of(Mono.just(4)).subscribeCount()).isPositive(); + } + + void testPublisherProbeAssertWasNotSubscribed() { + assertThat(PublisherProbe.of(Mono.just(1)).wasSubscribed()).isFalse(); + assertThat(PublisherProbe.of(Mono.just(2)).subscribeCount()).isEqualTo(0); + assertThat(PublisherProbe.of(Mono.just(3)).subscribeCount()).isNotPositive(); + } + + void testPublisherProbeAssertWasCancelled() { + assertThat(PublisherProbe.empty().wasCancelled()).isTrue(); + } + + void testPublisherProbeAssertWasNotCancelled() { + assertThat(PublisherProbe.empty().wasCancelled()).isFalse(); + } + + void testPublisherProbeAssertWasRequested() { + assertThat(PublisherProbe.empty().wasRequested()).isTrue(); + } + + void testPublisherProbeAssertWasNotRequested() { + assertThat(PublisherProbe.empty().wasRequested()).isFalse(); + } + ImmutableSet> testStepVerifierFromMono() { return ImmutableSet.of( StepVerifier.create(Mono.just(1)), Mono.just(2).flux().as(StepVerifier::create)); diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index 37f8f4f8eb..398fcb572a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -47,6 +47,7 @@ public ImmutableSet elidedTypesAndStaticImports() { List.class, ImmutableCollection.class, ImmutableMap.class, + assertThat(false), assertThat(0), maxBy(null), minBy(null), @@ -577,6 +578,35 @@ ImmutableSet> testPublisherProbeEmpty() { return ImmutableSet.of(PublisherProbe.empty(), PublisherProbe.empty()); } + void testPublisherProbeAssertWasSubscribed() { + PublisherProbe.of(Mono.just(1)).assertWasSubscribed(); + PublisherProbe.of(Mono.just(2)).assertWasSubscribed(); + PublisherProbe.of(Mono.just(3)).assertWasSubscribed(); + PublisherProbe.of(Mono.just(4)).assertWasSubscribed(); + } + + void testPublisherProbeAssertWasNotSubscribed() { + PublisherProbe.of(Mono.just(1)).assertWasNotSubscribed(); + PublisherProbe.of(Mono.just(2)).assertWasNotSubscribed(); + PublisherProbe.of(Mono.just(3)).assertWasNotSubscribed(); + } + + void testPublisherProbeAssertWasCancelled() { + PublisherProbe.empty().assertWasCancelled(); + } + + void testPublisherProbeAssertWasNotCancelled() { + PublisherProbe.empty().assertWasNotCancelled(); + } + + void testPublisherProbeAssertWasRequested() { + PublisherProbe.empty().assertWasRequested(); + } + + void testPublisherProbeAssertWasNotRequested() { + PublisherProbe.empty().assertWasNotRequested(); + } + ImmutableSet> testStepVerifierFromMono() { return ImmutableSet.of( Mono.just(1).as(StepVerifier::create), Mono.just(2).as(StepVerifier::create));