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

org.hamcrest.collection.IsArray Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version
package org.hamcrest.collection;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Arrays;

/**
 * Matcher for array whose elements satisfy a sequence of matchers.
 * The array size must equal the number of element matchers.
 */
public class IsArray extends TypeSafeMatcher {

    private final Matcher[] elementMatchers;

    public IsArray(Matcher[] elementMatchers) {
        this.elementMatchers = elementMatchers.clone();
    }

    @Override
    public boolean matchesSafely(T[] array) {
        if (array.length != elementMatchers.length) return false;

        for (int i = 0; i < array.length; i++) {
            if (!elementMatchers[i].matches(array[i])) return false;
        }

        return true;
    }

    @Override
    public void describeMismatchSafely(T[] actual, Description mismatchDescription) {
        if (actual.length != elementMatchers.length) {
            mismatchDescription.appendText("array length was ").appendValue(actual.length);
            return;
        }
        for (int i = 0; i < actual.length; i++) {
            if (!elementMatchers[i].matches(actual[i])) {
                mismatchDescription.appendText("element ").appendValue(i).appendText(" ");
                elementMatchers[i].describeMismatch(actual[i], mismatchDescription);
                return;
            }
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public void describeTo(Description description) {
        description.appendList(descriptionStart(), descriptionSeparator(), descriptionEnd(),
                               Arrays.asList(elementMatchers));
    }

    /**
     * Returns the string that starts the description.
     *
     * Can be overridden in subclasses to customise how the matcher is
     * described.
     *
     * @return The description prefix.
     */
    protected String descriptionStart() {
        return "[";
    }

    /**
     * Returns the string that separates the elements in the description.
     *
     * Can be overridden in subclasses to customise how the matcher is
     * described.
     *
     * @return The description separator.
     */
    protected String descriptionSeparator() {
        return ", ";
    }

    /**
     * Returns the string that ends the description.
     *
     * Can be overridden in subclasses to customise how the matcher is
     * described.
     *
     * @return The description suffix.
     */
    protected String descriptionEnd() {
        return "]";
    }

    /**
     * Creates a matcher that matches arrays whose elements are satisfied by the specified matchers.  Matches
     * positively only if the number of matchers specified is equal to the length of the examined array and
     * each matcher[i] is satisfied by array[i].
     * For example:
     * 
assertThat(new Integer[]{1,2,3}, is(array(equalTo(1), equalTo(2), equalTo(3))))
* * @param * the matcher type. * @param elementMatchers * the matchers that the elements of examined arrays should satisfy * @return The matcher. */ public static IsArray array(Matcher... elementMatchers) { return new IsArray<>(elementMatchers); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy