Skip to content

Commit 246d63e

Browse files
committed
Optional matchers
1 parent 6bb90de commit 246d63e

File tree

5 files changed

+185
-0
lines changed

5 files changed

+185
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hamcrest.optional;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.TypeSafeDiagnosingMatcher;
5+
6+
import java.util.Optional;
7+
8+
/**
9+
* Matcher that expects empty {@link Optional}.
10+
11+
* @param <T>
12+
*/
13+
public class OptionalEmpty<T> extends TypeSafeDiagnosingMatcher<Optional<T>> {
14+
15+
@Override
16+
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) {
17+
mismatchDescription.appendText("is " + value);
18+
return !value.isPresent();
19+
}
20+
21+
@Override
22+
public void describeTo(Description description) {
23+
description.appendText("to be empty");
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.hamcrest.optional;
2+
3+
import org.hamcrest.Matcher;
4+
5+
import java.util.Optional;
6+
7+
/**
8+
* Matchers for {@link Optional}.
9+
*/
10+
public class OptionalMatchers {
11+
12+
/**
13+
* Matcher for {@link Optional} that expects that value is present.
14+
*/
15+
public static <T> Matcher<Optional<T>> optionalWithValue() {
16+
return new OptionalWithValue<>();
17+
}
18+
19+
/**
20+
* Matcher for {@link Optional} that expects that it presents and applies passed <code>matcher</code>
21+
*
22+
* @param matcher matcher to validate present optional value
23+
*/
24+
public static <T> Matcher<Optional<T>> optionalWithValue(Matcher<T> matcher) {
25+
return new OptionalWithMatchingValue<>(matcher);
26+
}
27+
28+
/**
29+
* Matcher that expects empty optional.
30+
*/
31+
public static <T> Matcher<Optional<T>> emptyOptional() {
32+
return new OptionalEmpty<>();
33+
}
34+
35+
private OptionalMatchers() {
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.hamcrest.optional;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeDiagnosingMatcher;
6+
7+
import java.util.Optional;
8+
9+
/**
10+
* Matcher for {@link Optional} that expects that value is present and matches the given matcher.
11+
*
12+
* @param <T>
13+
*/
14+
public class OptionalWithMatchingValue<T> extends TypeSafeDiagnosingMatcher<Optional<T>> {
15+
16+
private final Matcher<T> matcher;
17+
18+
public OptionalWithMatchingValue(Matcher<T> matcher) {
19+
this.matcher = matcher;
20+
}
21+
22+
@Override
23+
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) {
24+
mismatchDescription.appendText("is " + value);
25+
return value.isPresent() && matcher.matches(value.get());
26+
}
27+
28+
@Override
29+
public void describeTo(Description description) {
30+
description.appendText("to be present and match ")
31+
.appendDescriptionOf(matcher);
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hamcrest.optional;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.TypeSafeDiagnosingMatcher;
5+
6+
import java.util.Optional;
7+
8+
/**
9+
* Matcher for {@link Optional} that expects that value is present.
10+
*
11+
* @param <T>
12+
*/
13+
public class OptionalWithValue<T> extends TypeSafeDiagnosingMatcher<Optional<T>> {
14+
15+
@Override
16+
protected boolean matchesSafely(Optional<T> value, Description mismatchDescription) {
17+
mismatchDescription.appendText("is " + value);
18+
return value.isPresent();
19+
}
20+
21+
@Override
22+
public void describeTo(Description description) {
23+
description.appendText("to be present");
24+
}
25+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.hamcrest.optional;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.not;
6+
import static org.hamcrest.optional.OptionalMatchers.emptyOptional;
7+
import static org.hamcrest.optional.OptionalMatchers.optionalWithValue;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertThrows;
10+
11+
import java.util.Optional;
12+
13+
import org.junit.Test;
14+
15+
public class OptionalMatchersTest {
16+
17+
@Test
18+
public void checkEmptyOptional() {
19+
assertThat(Optional.empty(), emptyOptional());
20+
assertThat(Optional.of(1), not(emptyOptional()));
21+
}
22+
23+
@Test
24+
public void checkEmptyOptionalFailure() {
25+
AssertionError failure = assertThrows(AssertionError.class, () -> {
26+
assertThat(Optional.of(1), emptyOptional());
27+
});
28+
assertEquals("\n" +
29+
"Expected: to be empty\n" +
30+
" but: is Optional[1]", failure.getMessage());
31+
}
32+
33+
@Test
34+
public void checkWithValue() {
35+
assertThat(Optional.empty(), not(OptionalMatchers.optionalWithValue()));
36+
assertThat(Optional.of(1), OptionalMatchers.optionalWithValue());
37+
}
38+
39+
@Test
40+
public void checkWithMatchingValue() {
41+
assertThat(Optional.empty(), not(optionalWithValue(equalTo(1))));
42+
assertThat(Optional.of(1), optionalWithValue(equalTo(1)));
43+
assertThat(Optional.of(1), not(optionalWithValue(equalTo(1L))));
44+
}
45+
46+
@Test
47+
public void checkWithValueFailure() {
48+
AssertionError failure = assertThrows(AssertionError.class, () -> {
49+
assertThat(Optional.empty(), OptionalMatchers.optionalWithValue());
50+
});
51+
assertEquals("\n" +
52+
"Expected: to be present\n" +
53+
" but: is Optional.empty", failure.getMessage());
54+
}
55+
56+
@Test
57+
public void checkWithMatchingValueFailure() {
58+
AssertionError failure = assertThrows(AssertionError.class, () -> {
59+
assertThat(Optional.empty(), optionalWithValue(equalTo(1)));
60+
});
61+
assertEquals("\n" +
62+
"Expected: to be present and match <1>\n" +
63+
" but: is Optional.empty", failure.getMessage());
64+
}
65+
}

0 commit comments

Comments
 (0)