Skip to content

Commit

Permalink
Add OptionalBooleanMatcher.isNot() operator
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed May 6, 2020
1 parent c9c6305 commit c41ffaf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -31,7 +31,7 @@ public interface OptionalBooleanMatcher<R> extends BooleanMatcher<R>, PresentAbs
* empty, matching is equivalent to {@link #isAbsent()} otherwise standard
* {@link #is(boolean)} matching is used.
*
* @param optional argument to match with
* @param optional optional boolean to match with
* @throws NullPointerException if argument is null
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
Expand All @@ -40,6 +40,20 @@ default R is(Optional<Boolean> optional) {
return optional.map(this::is).orElseGet(this::isAbsent);
}

/**
* Do not match current optional boolean attribute. For empty
* optional matching is equivalent to {@link #isPresent()} (is not absent), otherwise standard
* {@link #isNot(boolean)} (boolean)} matching is used.
*
* @param optional optional boolean to match with
* @throws NullPointerException if argument is null
*/
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
default R isNot(Optional<Boolean> optional) {
Objects.requireNonNull(optional, "optional");
return optional.map(this::isNot).orElseGet(this::isPresent);
}

interface Self extends Template<Self, Void> {}

interface Template<R, P> extends OptionalBooleanMatcher<R>, WithMatcher<R, Self>, NotMatcher<R, Self>, Projection<P>, Aggregation.Count {}
Expand Down
Expand Up @@ -87,6 +87,22 @@ protected void optional() {
ids(holder.optional.is(Optional.empty())).isOf("id3");
ids(holder.optional.is(Optional.of(true))).isOf("id2");
ids(holder.optional.is(Optional.of(false))).isOf("id1");

// isNot for optional
ids(holder.optional.isNot(Optional.empty())).hasContentInAnyOrder("id1", "id2");
ids(holder.optional.isNot(Optional.of(false))).not().has("id1");
ids(holder.optional.isNot(Optional.of(true))).not().has("id2");
}

@Test
void nullableIsNot() {
repository.insert(generator.get().withId("id1").withValue(false).withNullable(false).withOptional(false));
repository.insert(generator.get().withId("id2").withValue(true).withNullable(true).withOptional(true));
repository.insert(generator.get().withId("id3").withValue(true).withNullable(null).withOptional(Optional.empty()));

ids(holder.nullable.isNot(Optional.empty())).hasContentInAnyOrder("id1", "id2");
ids(holder.nullable.isNot(Optional.of(false))).not().has("id1");
ids(holder.nullable.isNot(Optional.of(true))).not().has("id2");
}

@Test
Expand Down

0 comments on commit c41ffaf

Please sign in to comment.