Skip to content

Commit 25d7501

Browse files
committed
Optional matchers
1 parent 6bb90de commit 25d7501

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-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("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("present and matches ")
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("present");
24+
}
25+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.is;
6+
import static org.hamcrest.Matchers.not;
7+
import static org.hamcrest.optional.OptionalMatchers.emptyOptional;
8+
import static org.hamcrest.optional.OptionalMatchers.optionalWithValue;
9+
import static org.junit.Assert.assertEquals;
10+
import static org.junit.Assert.assertThrows;
11+
12+
import java.util.Optional;
13+
14+
import org.junit.Test;
15+
16+
public class OptionalMatchersTest {
17+
18+
@Test
19+
public void checkEmptyOptional() {
20+
assertThat(Optional.empty(), is(emptyOptional()));
21+
assertThat(Optional.of(1), not(emptyOptional()));
22+
}
23+
24+
@Test
25+
public void checkEmptyOptionalIsFailure() {
26+
AssertionError failure = assertThrows(AssertionError.class, () -> {
27+
assertThat(Optional.of(1), is(emptyOptional()));
28+
});
29+
assertEquals("\n" +
30+
"Expected: is empty\n" +
31+
" but: is Optional[1]", failure.getMessage());
32+
}
33+
34+
@Test
35+
public void checkEmptyOptionalFailure() {
36+
AssertionError failure = assertThrows(AssertionError.class, () -> {
37+
assertThat(Optional.of(1), emptyOptional());
38+
});
39+
assertEquals("\n" +
40+
"Expected: empty\n" +
41+
" but: is Optional[1]", failure.getMessage());
42+
}
43+
44+
@Test
45+
public void checkWithValue() {
46+
assertThat(Optional.empty(), not(optionalWithValue()));
47+
assertThat(Optional.of(1), is(optionalWithValue()));
48+
}
49+
50+
@Test
51+
public void checkWithMatchingValue() {
52+
assertThat(Optional.empty(), not(optionalWithValue(equalTo(1))));
53+
assertThat(Optional.of(1), is(optionalWithValue(equalTo(1))));
54+
assertThat(Optional.of(1), not(optionalWithValue(equalTo(1L))));
55+
}
56+
57+
@Test
58+
public void checkWithValueFailure() {
59+
AssertionError failure = assertThrows(AssertionError.class, () -> {
60+
assertThat(Optional.empty(), is(optionalWithValue()));
61+
});
62+
assertEquals("\n" +
63+
"Expected: is present\n" +
64+
" but: is Optional.empty", failure.getMessage());
65+
}
66+
67+
@Test
68+
public void checkWithMatchingValueFailure() {
69+
AssertionError failure = assertThrows(AssertionError.class, () -> {
70+
assertThat(Optional.empty(), is(optionalWithValue(equalTo(1))));
71+
});
72+
assertEquals("\n" +
73+
"Expected: is present and matches <1>\n" +
74+
" but: is Optional.empty", failure.getMessage());
75+
}
76+
}

0 commit comments

Comments
 (0)