
com.github.npathai.hamcrestopt.OptionalMatchers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-optional Show documentation
Show all versions of hamcrest-optional Show documentation
A set of Hamcrest matchers for java.util.Optional.
The newest version!
package com.github.npathai.hamcrestopt;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import java.util.Optional;
import static org.hamcrest.core.IsEqual.equalTo;
/**
* Provides a set of Hamcrest matchers for {@code java.util.Optional}:
*
* - {@link #isEmpty()} - matches when the examined {@code Optional}
* contains no value.
* - {@link #isPresent()} - matches when the examined {@code Optional}
* contains a value.
* - {@link #hasValue(Object)} - matches when the examined
* {@code Optional} contains a value that is logically equal to the
* {@code operand}.
* - {@link #hasValue(Matcher)} - matches when the examined
* {@code Optional} contains a value that satisfies the specified matcher.
*
*
*
* @author npathai
*/
public class OptionalMatchers {
/**
* Creates a matcher that matches when the examined {@code Optional}
* contains a value.
*
* Optional<String> optionalObject = Optional.of("dummy value");
* assertThat(optionalObject, isPresent());
*
*
* @return a matcher that matches when the examined {@code Optional}
* contains a value.
*/
public static Matcher> isPresent() {
return new PresenceMatcher();
}
private static class PresenceMatcher extends TypeSafeMatcher> {
public void describeTo(Description description) {
description.appendText("is ");
}
@Override
protected boolean matchesSafely(Optional> item) {
return item.isPresent();
}
@Override
protected void describeMismatchSafely(Optional> item, Description mismatchDescription) {
mismatchDescription.appendText("was ");
}
}
/**
* Creates a matcher that matches when the examined {@code Optional}
* contains no value.
*
* Optional<String> optionalObject = Optional.empty();
* assertThat(optionalObject, isEmpty());
*
*
* @return a matcher that matches when the examined {@code Optional}
* contains no value.
*/
public static Matcher> isEmpty() {
return new EmptyMatcher();
}
private static class EmptyMatcher extends TypeSafeMatcher> {
public void describeTo(Description description) {
description.appendText("is ");
}
@Override
protected boolean matchesSafely(Optional> item) {
return !item.isPresent();
}
@Override
protected void describeMismatchSafely(Optional> item, Description mismatchDescription) {
mismatchDescription.appendText("had value ");
mismatchDescription.appendValue(item.get());
}
}
/**
* Creates a matcher that matches when the examined {@code Optional}
* contains a value that is logically equal to the {@code operand}, as
* determined by calling the {@code equals} method on the value.
*
* Optional<String> optionalInt = Optional.of("dummy value");
* assertThat(optionalInt, hasValue("dummy value"));
*
*
* @param operand the object that any examined {@code Optional} value
* should equal
* @param the class of the value.
* @return a matcher that matches when the examined {@code Optional}
* contains a value that is logically equal to the {@code operand}.
*/
public static Matcher> hasValue(T operand) {
return new HasValue<>(equalTo(operand));
}
/**
* Creates a matcher that matches when the examined {@code Optional}
* contains a value that satisfies the specified matcher.
*
* Optional<String> optionalObject = Optional.of("dummy value");
* assertThat(optionalObject, hasValue(startsWith("dummy")));
*
*
* @param matcher a matcher for the value of the examined {@code Optional}.
* @param the class of the value.
* @return a matcher that matches when the examined {@code Optional}
* contains a value that satisfies the specified matcher.
*/
public static Matcher> hasValue(Matcher super T> matcher) {
return new HasValue<>(matcher);
}
private static class HasValue extends TypeSafeMatcher> {
private Matcher super T> matcher;
public HasValue(Matcher super T> matcher) {
this.matcher = matcher;
}
@Override
public void describeTo(Description description) {
description.appendText("has value that is ");
matcher.describeTo(description);
}
@Override
protected boolean matchesSafely(Optional item) {
return item.isPresent() && matcher.matches(item.get());
}
@Override
protected void describeMismatchSafely(Optional item, Description mismatchDescription) {
if (item.isPresent()) {
mismatchDescription.appendText("value ");
matcher.describeMismatch(item.get(), mismatchDescription);
} else {
mismatchDescription.appendText("was ");
}
}
}
//This is an utility class that must not be instantiated.
private OptionalMatchers() {
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy