Skip to content

Commit

Permalink
Introduce `PublisherProbeAssertWas{,Not}{Subscribed,Cancelled,Request…
Browse files Browse the repository at this point in the history
…ed}` Refaster rules (#1423)

While there, fix a typo in `README.md`.
  • Loading branch information
werli authored Dec 9, 2024
1 parent 512a3be commit 2eda393
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,91 @@ PublisherProbe<T> after() {
}
}

/** Prefer {@link PublisherProbe#assertWasSubscribed()} over more verbose alternatives. */
static final class PublisherProbeAssertWasSubscribed<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
Refaster.anyOf(
assertThat(probe.wasSubscribed()).isTrue(),
assertThat(probe.subscribeCount()).isNotNegative(),
assertThat(probe.subscribeCount()).isNotEqualTo(0),
assertThat(probe.subscribeCount()).isPositive());
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasSubscribed();
}
}

/** Prefer {@link PublisherProbe#assertWasNotSubscribed()} over more verbose alternatives. */
static final class PublisherProbeAssertWasNotSubscribed<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
Refaster.anyOf(
assertThat(probe.wasSubscribed()).isFalse(),
assertThat(probe.subscribeCount()).isEqualTo(0),
assertThat(probe.subscribeCount()).isNotPositive());
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasNotSubscribed();
}
}

/** Prefer {@link PublisherProbe#assertWasCancelled()} over more verbose alternatives. */
static final class PublisherProbeAssertWasCancelled<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
assertThat(probe.wasCancelled()).isTrue();
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasCancelled();
}
}

/** Prefer {@link PublisherProbe#assertWasNotCancelled()} over more verbose alternatives. */
static final class PublisherProbeAssertWasNotCancelled<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
assertThat(probe.wasCancelled()).isFalse();
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasNotCancelled();
}
}

/** Prefer {@link PublisherProbe#assertWasRequested()} over more verbose alternatives. */
static final class PublisherProbeAssertWasRequested<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
assertThat(probe.wasRequested()).isTrue();
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasRequested();
}
}

/** Prefer {@link PublisherProbe#assertWasNotRequested()} over more verbose alternatives. */
static final class PublisherProbeAssertWasNotRequested<T> {
@BeforeTemplate
void before(PublisherProbe<T> probe) {
assertThat(probe.wasRequested()).isFalse();
}

@AfterTemplate
void after(PublisherProbe<T> probe) {
probe.assertWasNotRequested();
}
}

/** Prefer {@link Mono#as(Function)} when creating a {@link StepVerifier}. */
static final class StepVerifierFromMono<T> {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
List.class,
ImmutableCollection.class,
ImmutableMap.class,
assertThat(false),
assertThat(0),
maxBy(null),
minBy(null),
Expand Down Expand Up @@ -589,6 +590,35 @@ ImmutableSet<PublisherProbe<Void>> 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<StepVerifier.FirstStep<Integer>> testStepVerifierFromMono() {
return ImmutableSet.of(
StepVerifier.create(Mono.just(1)), Mono.just(2).flux().as(StepVerifier::create));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public ImmutableSet<Object> elidedTypesAndStaticImports() {
List.class,
ImmutableCollection.class,
ImmutableMap.class,
assertThat(false),
assertThat(0),
maxBy(null),
minBy(null),
Expand Down Expand Up @@ -577,6 +578,35 @@ ImmutableSet<PublisherProbe<Void>> 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<StepVerifier.FirstStep<Integer>> testStepVerifierFromMono() {
return ImmutableSet.of(
Mono.just(1).as(StepVerifier::create), Mono.just(2).as(StepVerifier::create));
Expand Down

0 comments on commit 2eda393

Please sign in to comment.