All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hamcrest.core.Is Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package org.hamcrest.core;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
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))
* */ @Factory 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)))
* */ @Factory public static Matcher is(T value) { return is(equalTo(value)); } /** * A shortcut to the frequently used is(instanceOf(SomeClass.class)). *

* For example: *

assertThat(cheese, is(Cheddar.class))
* instead of: *
assertThat(cheese, is(instanceOf(Cheddar.class)))
* * @deprecated use isA(Class type) instead. */ @Factory @Deprecated public static Matcher is(Class type) { final Matcher typeMatcher = instanceOf(type); return is(typeMatcher); } /** * 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)))
* */ @Factory public static Matcher isA(Class type) { final Matcher typeMatcher = instanceOf(type); return is(typeMatcher); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy