Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PublisherProbeAssertWas{,Not}{Subscribed,Cancelled,Requested} Refaster rules #1423

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

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