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

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

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

import org.hamcrest.Factory;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

import static org.hamcrest.core.DescribedAs.describedAs;
import static org.hamcrest.core.IsEqual.equalTo;

/**
 * Matches if array size satisfies a nested matcher.
 */
public class IsArrayWithSize extends FeatureMatcher {
    public IsArrayWithSize(Matcher sizeMatcher) {
        super(sizeMatcher, "an array with size","array size");
    }

    @Override
    protected Integer featureValueOf(E[] actual) {
      return actual.length;
    }

    /**
     * Creates a matcher for arrays that matches when the length of the array
     * satisfies the specified matcher.
     * 

* For example: *

assertThat(new String[]{"foo", "bar"}, arrayWithSize(equalTo(2)))
* * @param sizeMatcher * a matcher for the length of an examined array */ @Factory public static Matcher arrayWithSize(Matcher sizeMatcher) { return new IsArrayWithSize(sizeMatcher); } /** * Creates a matcher for arrays that matches when the length of the array * equals the specified size. *

* For example: *

assertThat(new String[]{"foo", "bar"}, arrayWithSize(2))
* * @param size * the length that an examined array must have for a positive match */ @Factory public static Matcher arrayWithSize(int size) { return arrayWithSize(equalTo(size)); } /** * Creates a matcher for arrays that matches when the length of the array * is zero. *

* For example: *

assertThat(new String[0], emptyArray())
* */ @Factory public static Matcher emptyArray() { Matcher isEmpty = arrayWithSize(0); return describedAs("an empty array", isEmpty); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy