org.hamcrest.collection.IsArrayWithSize Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hamcrest-library Show documentation
Show all versions of hamcrest-library Show documentation
A library of Hamcrest matchers - deprecated, please use "hamcrest" instead
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 super Integer> 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 super Integer> 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