Skip to content

Commit

Permalink
Optional matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
seregamorph committed Aug 26, 2024
1 parent 6bb90de commit d0633a5
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
73 changes: 73 additions & 0 deletions hamcrest/src/main/java/org/hamcrest/core/OptionalMatchers.java
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 hamcrest/src/test/java/org/hamcrest/core/OptionalMatchersTest.java
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());
}
}

0 comments on commit d0633a5

Please sign in to comment.