|
| 1 | +package org.hamcrest.core; |
| 2 | + |
| 3 | +import static org.hamcrest.core.IsEqual.equalTo; |
| 4 | +import org.hamcrest.Factory; |
| 5 | +import org.hamcrest.Matcher; |
| 6 | +import org.hamcrest.BaseMatcher; |
| 7 | +import org.hamcrest.Description; |
| 8 | + |
| 9 | +/** |
| 10 | + * Decorates another Matcher, retaining the behavior but allowing tests |
| 11 | + * to be slightly more expressive. |
| 12 | + * |
| 13 | + * eg. assertThat(cheese, equalTo(smelly)) |
| 14 | + * vs assertThat(cheese, is(equalTo(smelly))) |
| 15 | + */ |
| 16 | +public class Is<T> extends BaseMatcher<T> { |
| 17 | + |
| 18 | + private final Matcher<T> matcher; |
| 19 | + |
| 20 | + public Is(Matcher<T> matcher) { |
| 21 | + this.matcher = matcher; |
| 22 | + } |
| 23 | + |
| 24 | + public boolean matches(Object arg) { |
| 25 | + return matcher.matches(arg); |
| 26 | + } |
| 27 | + |
| 28 | + public void describeTo(Description description) { |
| 29 | + description.appendText("is "); |
| 30 | + matcher.describeTo(description); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Decorates another Matcher, retaining the behavior but allowing tests |
| 35 | + * to be slightly more expressive. |
| 36 | + * |
| 37 | + * eg. assertThat(cheese, equalTo(smelly)) |
| 38 | + * vs assertThat(cheese, is(equalTo(smelly))) |
| 39 | + */ |
| 40 | + @Factory |
| 41 | + public static <T> Matcher<T> is(Matcher<T> matcher) { |
| 42 | + return new Is<T>(matcher); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * This is a shortcut to the frequently used is(equalTo(x)). |
| 47 | + * |
| 48 | + * eg. assertThat(cheese, is(equalTo(smelly))) |
| 49 | + * vs assertThat(cheese, is(smelly)) |
| 50 | + */ |
| 51 | + @Factory |
| 52 | + public static <T> Matcher<T> is(T value) { |
| 53 | + return is(equalTo(value)); |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
0 commit comments