-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bb90de
commit d0633a5
Showing
2 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
hamcrest/src/main/java/org/hamcrest/core/OptionalMatchers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.hamcrest.core; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* Matchers for {@link Optional}. | ||
*/ | ||
public class OptionalMatchers { | ||
|
||
/** | ||
* Matcher for {@link Optional} that expects that it presents. | ||
*/ | ||
public static <T> Matcher<Optional<T>> isPresent() { | ||
return new TypeSafeDiagnosingMatcher<Optional<T>>() { | ||
@Override | ||
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) { | ||
mismatchDescription.appendText("is " + value); | ||
return value.isPresent(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("to be present"); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Matcher for {@link Optional} that expects that it presents and applies passed <code>matcher</code> | ||
* | ||
* @param matcher matcher to validate present optional value | ||
*/ | ||
public static <T> Matcher<Optional<T>> isPresent(Matcher<T> matcher) { | ||
return new TypeSafeDiagnosingMatcher<Optional<T>>() { | ||
@Override | ||
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) { | ||
mismatchDescription.appendText("is " + value); | ||
return value.isPresent() && matcher.matches(value.get()); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("to be present and match ") | ||
.appendDescriptionOf(matcher); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Matcher that expects empty optional. | ||
*/ | ||
public static <T> Matcher<Optional<T>> isEmpty() { | ||
return new TypeSafeDiagnosingMatcher<Optional<T>>() { | ||
@Override | ||
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) { | ||
mismatchDescription.appendText("is " + value); | ||
return !value.isPresent(); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("to be empty"); | ||
} | ||
}; | ||
} | ||
|
||
private OptionalMatchers() { | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
hamcrest/src/test/java/org/hamcrest/core/OptionalMatchersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.hamcrest.core; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.hamcrest.core.OptionalMatchers.isEmpty; | ||
import static org.hamcrest.core.OptionalMatchers.isPresent; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.Test; | ||
|
||
public class OptionalMatchersTest { | ||
|
||
@Test | ||
public void checkEmpty() { | ||
assertThat(Optional.empty(), isEmpty()); | ||
assertThat(Optional.of(1), not(isEmpty())); | ||
} | ||
|
||
@Test | ||
public void checkEmptyFailure() { | ||
AssertionError failure = assertThrows(AssertionError.class, () -> { | ||
assertThat(Optional.of(1), isEmpty()); | ||
}); | ||
assertEquals("\n" + | ||
"Expected: to be empty\n" + | ||
" but: is Optional[1]", failure.getMessage()); | ||
} | ||
|
||
@Test | ||
public void checkPresent() { | ||
assertThat(Optional.empty(), not(isPresent())); | ||
assertThat(Optional.of(1), isPresent()); | ||
} | ||
|
||
@Test | ||
public void checkPresentMatcher() { | ||
assertThat(Optional.empty(), not(isPresent(equalTo(1)))); | ||
assertThat(Optional.of(1), isPresent(equalTo(1))); | ||
assertThat(Optional.of(1), not(isPresent(equalTo(1L)))); | ||
} | ||
|
||
@Test | ||
public void checkPresentFailure() { | ||
AssertionError failure = assertThrows(AssertionError.class, () -> { | ||
assertThat(Optional.empty(), isPresent()); | ||
}); | ||
assertEquals("\n" + | ||
"Expected: to be present\n" + | ||
" but: is Optional.empty", failure.getMessage()); | ||
} | ||
|
||
@Test | ||
public void checkPresentMatcherFailure() { | ||
AssertionError failure = assertThrows(AssertionError.class, () -> { | ||
assertThat(Optional.empty(), isPresent(equalTo(1))); | ||
}); | ||
assertEquals("\n" + | ||
"Expected: to be present and match <1>\n" + | ||
" but: is Optional.empty", failure.getMessage()); | ||
} | ||
} |