Skip to content

Commit

Permalink
Introduce VerifyOnlyElementInFlux Refaster rule (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannizotta authored Aug 14, 2023
1 parent 4af7b21 commit 7fb0e55
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
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,13 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNextMatches("qux"::equals));
}

StepVerifier.Step<?> testFluxAsStepVerifierExpectNext() {
return Flux.just(1)
.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(2));
}

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,10 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNext("qux"));
}

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

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

0 comments on commit 7fb0e55

Please sign in to comment.