Skip to content

Commit

Permalink
Expanded Optional assertion api with hasValueSatisfying Condition (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
fiery-phoenix authored and joel-costigliola committed Oct 22, 2016
1 parent cc5ddcd commit bbe49e7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 16 deletions.
61 changes: 45 additions & 16 deletions src/main/java/org/assertj/core/api/AbstractOptionalAssert.java
Expand Up @@ -12,23 +12,23 @@
*/
package org.assertj.core.api;

import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.internal.ComparisonStrategy;
import org.assertj.core.internal.FieldByFieldComparator;
import org.assertj.core.internal.StandardComparisonStrategy;

import java.util.Comparator;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.OptionalShouldBeEmpty.shouldBeEmpty;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.OptionalShouldContain.shouldContain;
import static org.assertj.core.error.OptionalShouldContain.shouldContainSame;
import static org.assertj.core.error.OptionalShouldContainInstanceOf.shouldContainInstanceOf;

import java.util.Comparator;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;

import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.internal.ComparisonStrategy;
import org.assertj.core.internal.FieldByFieldComparator;
import org.assertj.core.internal.StandardComparisonStrategy;

/**
* Assertions for {@link java.util.Optional}.
*
Expand Down Expand Up @@ -60,8 +60,7 @@ protected AbstractOptionalAssert(Optional<T> actual, Class<?> selfType) {
* @return this assertion object.
*/
public S isPresent() {
isNotNull();
if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));
assertValueIsPresent();
return myself;
}

Expand Down Expand Up @@ -163,12 +162,38 @@ public S contains(T expectedValue) {
* @return this assertion object.
*/
public S hasValueSatisfying(Consumer<T> requirement) {
isNotNull();
if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));
assertValueIsPresent();
requirement.accept(actual.get());
return myself;
}

/**
* Verifies that the actual {@link Optional} contains a value which satisfies the given {@link Condition}.
* <p>
* Examples:
* <pre><code class='java'> Condition&lt;TolkienCharacter&gt isAnElf = new Condition&lt;&gt;(character -> character.getRace() == ELF, "an elf");
*
* TolkienCharacter legolas = new TolkienCharacter("Legolas", 1000, ELF);
* TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
*
* // assertion succeeds
* assertThat(Optional.of(legolas)).hasValueSatisfying(isAnElf);
*
* // assertion fails
* assertThat(Optional.of(frodo)).hasValueSatisfying(isAnElf);</code></pre>
*
* @param condition the given condition.
* @return this assertion object.
* @throws AssertionError if the actual {@link Optional} is null or empty.
* @throws NullPointerException if the given condition is {@code null}.
* @throws AssertionError if the actual value does not satisfy the given condition.
*/
public S hasValueSatisfying(Condition<? super T> condition) {
assertValueIsPresent();
conditions.assertIs(info, actual.get(), condition);
return myself;
}

/**
* Verifies that the actual {@link java.util.Optional} contains the given value (alias of {@link #contains(Object)}).
* </p>
Expand Down Expand Up @@ -205,8 +230,7 @@ public S hasValue(T expectedValue) {
* @return this assertion object.
*/
public S containsInstanceOf(Class<?> clazz) {
isNotNull();
if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));
assertValueIsPresent();
if (!clazz.isInstance(actual.get())) throwAssertionError(shouldContainInstanceOf(actual, clazz));
return myself;
}
Expand Down Expand Up @@ -375,4 +399,9 @@ private void checkNotNull(T expectedValue) {
if (expectedValue == null) throw new IllegalArgumentException("The expected value should not be <null>.");
}

private void assertValueIsPresent() {
isNotNull();
if (!actual.isPresent()) throwAssertionError(shouldBePresent(actual));
}

}
4 changes: 4 additions & 0 deletions src/test/java/org/assertj/core/api/TestCondition.java
Expand Up @@ -29,6 +29,10 @@ public TestCondition() {
super();
}

public TestCondition(boolean matches) {
this.matches = matches;
}

public TestCondition(String description) {
super(description);
}
Expand Down
@@ -0,0 +1,60 @@
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package org.assertj.core.api.optional;

import org.assertj.core.api.BaseTest;
import org.assertj.core.api.Condition;
import org.assertj.core.api.TestCondition;
import org.junit.Test;

import java.util.Optional;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.error.OptionalShouldBePresent.shouldBePresent;
import static org.assertj.core.error.ShouldBe.shouldBe;
import static org.assertj.core.util.FailureMessages.actualIsNull;

public class OptionalAssert_hasValueSatisfying_Condition_Test extends BaseTest {

private Condition<String> passingCondition = new TestCondition<>(true);
private Condition<String> notPassingCondition = new TestCondition<>();

@Test
public void should_fail_when_optional_is_null() {
thrown.expectAssertionError(actualIsNull());
assertThat((Optional<String>) null).hasValueSatisfying(passingCondition);
}

@Test
public void should_fail_when_optional_is_empty() {
thrown.expectAssertionError(shouldBePresent(Optional.empty()).create());
assertThat(Optional.<String>empty()).hasValueSatisfying(passingCondition);
}

@Test
public void should_fail_when_condition_is_null() {
thrown.expectNullPointerException("The condition to evaluate should not be null");
assertThat(Optional.of("something")).hasValueSatisfying((Condition) null);
}

@Test
public void should_pass_when_condition_is_met() {
assertThat(Optional.of("something")).hasValueSatisfying(passingCondition);
}

@Test
public void should_fail_when_condition_is_not_met() {
thrown.expectAssertionError(shouldBe("something", notPassingCondition).create());
assertThat(Optional.of("something")).hasValueSatisfying(notPassingCondition);
}
}
Expand Up @@ -23,6 +23,7 @@
import org.junit.Test;

public class OptionalAssert_hasValueSatisfying_Test extends BaseTest {

@Test
public void should_fail_when_optional_is_null() {
thrown.expectAssertionError(actualIsNull());
Expand Down

0 comments on commit bbe49e7

Please sign in to comment.