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 VerifyOnlyElementInFlux Refaster rule #617

Merged
merged 5 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,23 @@ StepVerifier.Step<T> after(StepVerifier.Step<T> step, T object) {
}
}

/** Avoid list collection when verifying that a {@link Flux} emits exactly one value. */
// XXX: This rule assumes that the matched collector does not drop elements. Consider introducing
// a `@Matches(DoesNotDropElements.class)` or `@NotMatches(MayDropElements.class)` guard.
static final class FluxAsStepVerifierExpectNext<T, L extends List<T>> {
@BeforeTemplate
StepVerifier.Step<L> before(Flux<T> flux, Collector<? super T, ?, L> listCollector, T object) {
return flux.collect(listCollector)
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(object));
}

@AfterTemplate
StepVerifier.Step<T> after(Flux<T> flux, T object) {
return flux.as(StepVerifier::create).expectNext(object);
}
}

/** Prefer {@link StepVerifier.LastStep#verifyComplete()} over more verbose alternatives. */
static final class StepVerifierLastStepVerifyComplete {
@BeforeTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,18 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNextMatches("qux"::equals));
}

ImmutableSet<StepVerifier.Step<?>> testFluxAsStepVerifierExpectNext() {
return ImmutableSet.of(
Flux.just(1)
.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(2)),
Flux.just(3)
.collect(toCollection(ArrayList::new))
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(4)));
}
Copy link
Member

Choose a reason for hiding this comment

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

After pushing this I realized that perhaps for rule one test case would suffice, or at least be more consistent with what we do elsewhere. 🤷

Copy link
Member

Choose a reason for hiding this comment

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

I agree, I'll add a commit to drop the second test case.


Duration testStepVerifierLastStepVerifyComplete() {
return StepVerifier.create(Mono.empty()).expectComplete().verify();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,12 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNext("qux"));
}

ImmutableSet<StepVerifier.Step<?>> testFluxAsStepVerifierExpectNext() {
return ImmutableSet.of(
Flux.just(1).as(StepVerifier::create).expectNext(2),
Flux.just(3).as(StepVerifier::create).expectNext(4));
}

Duration testStepVerifierLastStepVerifyComplete() {
return StepVerifier.create(Mono.empty()).verifyComplete();
}
Expand Down