package org.hamcrest.core; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsInstanceOf.instanceOf; /** * Decorates another Matcher, retaining the behaviour but allowing tests * to be slightly more expressive. * * For example: assertThat(cheese, equalTo(smelly)) * vs. assertThat(cheese, is(equalTo(smelly))) */ public class Is extends BaseMatcher { private final Matcher matcher; public Is(Matcher matcher) { this.matcher = matcher; } @Override public boolean matches(Object arg) { return matcher.matches(arg); } @Override public void describeTo(Description description) { description.appendText("is ").appendDescriptionOf(matcher); } @Override public void describeMismatch(Object item, Description mismatchDescription) { matcher.describeMismatch(item, mismatchDescription); } /** * Decorates another Matcher, retaining its behaviour, but allowing tests * to be slightly more expressive. * For example: *
assertThat(cheese, is(equalTo(smelly)))
* instead of: *
assertThat(cheese, equalTo(smelly))
* */ public static Matcher is(Matcher matcher) { return new Is(matcher); } /** * A shortcut to the frequently used is(equalTo(x)). * For example: *
assertThat(cheese, is(smelly))
* instead of: *
assertThat(cheese, is(equalTo(smelly)))
* */ public static Matcher is(T value) { return is(equalTo(value)); } /** * A shortcut to the frequently used is(instanceOf(SomeClass.class)). * For example: *
assertThat(cheese, isA(Cheddar.class))
* instead of: *
assertThat(cheese, is(instanceOf(Cheddar.class)))
* */ public static Matcher isA(Class type) { final Matcher typeMatcher = instanceOf(type); return is(typeMatcher); } }