Skip to content

Commit

Permalink
Suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed May 9, 2023
1 parent 787b325 commit 5cad15f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,28 @@ StepVerifier.Step<T> after(StepVerifier.Step<T> step, T object) {
}
}

/** Avoid collecting when verifying only the given element is in the given {@link Flux}. */
static final class FluxAsStepVerifierExpectNext<T> {
@BeforeTemplate
StepVerifier.Step<ImmutableList<T>> before(Flux<T> flux, T object) {
return flux.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(object));
}

@BeforeTemplate
StepVerifier.Step<ImmutableSet<T>> before2(Flux<T> flux, T object) {
return flux.collect(toImmutableSet())
.as(StepVerifier::create)
.assertNext(set -> assertThat(set).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 Expand Up @@ -1397,20 +1419,4 @@ Duration after(StepVerifier.LastStep step, Duration duration) {
return step.verifyTimeout(duration);
}
}

/** Avoid collecting when verifying only the given element is in the given {@link Flux}. */
static final class VerifyOnlyElementInFlux<T> {
@BeforeTemplate
Duration before(Flux<T> flux, T object) {
return flux.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(object))
.verifyComplete();
}

@AfterTemplate
Duration after(Flux<T> flux, T object) {
return flux.as(StepVerifier::create).expectNext(object).verifyComplete();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.picnic.errorprone.refasterrules;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.MoreCollectors.toOptional;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Function.identity;
Expand Down Expand Up @@ -414,6 +415,20 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNextMatches("qux"::equals));
}

ImmutableSet<Duration> testFluxAsStepVerifierExpectNext() {
return ImmutableSet.of(
Flux.just(1)
.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(2))
.verifyComplete(),
Flux.just(3)
.collect(toImmutableSet())
.as(StepVerifier::create)
.assertNext(set -> assertThat(set).containsExactly(4))
.verifyComplete());
}

Duration testStepVerifierLastStepVerifyComplete() {
return StepVerifier.create(Mono.empty()).expectComplete().verify();
}
Expand Down Expand Up @@ -446,12 +461,4 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return StepVerifier.create(Mono.empty()).expectTimeout(Duration.ZERO).verify();
}

Duration testVerifyOnlyElementInFlux() {
return Flux.just(1)
.collect(toImmutableList())
.as(StepVerifier::create)
.assertNext(list -> assertThat(list).containsExactly(1))
.verifyComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,12 @@ ImmutableSet<StepVerifier.Step<String>> testStepVerifierStepExpectNext() {
StepVerifier.create(Mono.just("baz")).expectNext("qux"));
}

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

Duration testStepVerifierLastStepVerifyComplete() {
return StepVerifier.create(Mono.empty()).verifyComplete();
}
Expand Down Expand Up @@ -435,8 +441,4 @@ Duration testStepVerifierLastStepVerifyErrorMessage() {
Duration testStepVerifierLastStepVerifyTimeout() {
return StepVerifier.create(Mono.empty()).verifyTimeout(Duration.ZERO);
}

Duration testVerifyOnlyElementInFlux() {
return Flux.just(1).as(StepVerifier::create).expectNext(1).verifyComplete();
}
}

0 comments on commit 5cad15f

Please sign in to comment.