-
Notifications
You must be signed in to change notification settings - Fork 377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java.util.Optional matchers #421
Conversation
Thanks for this, @seregamorph! I appreciate the time you've put into this PR. The code is clean and readable, and I think it will be a valuable addition to Hamcrest. If you don't mind, can I request some changes? Firstly, I'm not keen on the use of anonymous inner classes for published matchers in Hamcrest. It would be great if you re-write them as top level public classes that subclass the same Secondly, I'm not definite about this, but I think it would be nicer to put them in a separate sub-package (e.g. Thirdly (and finally), I think static factory method names could give a bit more context, to make them more readable when they've been staticly imported. e.g. change Putting those points together, we'd end up with something like this: import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.optional.OptionalMatchers.emptyOptional;
import static org.hamcrest.optional.OptionalMatchers.optionalWithValue;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
import org.junit.Test;
import java.util.Optional;
public class OptionalMatchersTest {
@Test
public void testEmptyOptional() {
Optional<Object> actual = Optional.empty();
// assertThat(actual, isEmpty());
assertThat(actual, is(emptyOptional()));
assertThat(actual, not(optionalWithValue()));
}
@Test
public void testOptionalWithValue() {
Optional<String> actual = Optional.of("Hello, world");
// assertThat(actual, isPresent());
assertThat(actual, not(emptyOptional()));
assertThat(actual, is(optionalWithValue()));
assertThat(actual, optionalWithValue("Hello, world"));
assertThat(actual, optionalWithValue(matchesPattern("Hell")));
}
} What do you think? |
246d63e
to
25d7501
Compare
Thank you for the review and comments. Done:
Also please note: I adjusted a bit the description message from These tests clarify the failure messages: AssertionError failure = assertThrows(AssertionError.class, () -> {
assertThat(Optional.of(1), is(emptyOptional()));
});
assertEquals("\n" +
"Expected: is empty\n" +
" but: is Optional[1]", failure.getMessage()); or without AssertionError failure = assertThrows(AssertionError.class, () -> {
assertThat(Optional.of(1), emptyOptional());
});
assertEquals("\n" +
"Expected: empty\n" +
" but: is Optional[1]", failure.getMessage()); |
Thanks again @seregamorph. Now that I can see the changes in context, I have a bit more feedback:
|
Done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
This change was waiting for releasing 3.0 with JDK 8 compatibility.
Matchers for
java.util.Optional
:OptionalMatchers.emptyOptional()
OptionalMatchers.optionalWithValue()
OptionalMatchers.optionalWithValue(value)
OptionalMatchers.optionalWithValue(Matcher)