com.pivotallabs.greatexpectations.matchers.IterableMatcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of great-expectations Show documentation
Show all versions of great-expectations Show documentation
Jasmine-style expect() for Java.
package com.pivotallabs.greatexpectations.matchers;
import com.pivotallabs.greatexpectations.MatcherOf;
import java.util.ArrayList;
import java.util.List;
@MatcherOf(value = Iterable.class, directObject = true)
public class IterableMatcher, X, M extends IterableMatcher> extends ObjectMatcher {
public boolean toContain(X... expectedItems) {
List items = items();
for (X expectedItem : expectedItems) {
if (!items.contains(expectedItem)) return false;
}
return true;
}
public boolean toContainInOrder(X... expectedItems) {
int expectedIndex = 0;
for (X x : actual) {
if (x.equals(expectedItems[expectedIndex])) {
expectedIndex++;
}
if (expectedIndex == expectedItems.length) {
return true;
}
}
return false;
}
public boolean toContainExactly(X... expectedItems) {
int i = 0;
for (X x : actual) {
if (i >= expectedItems.length || !x.equals(expectedItems[i++])) {
return false;
}
}
return i == expectedItems.length;
}
public boolean toBeEmpty() {
return !actual.iterator().hasNext();
}
private List items() {
List list = new ArrayList();
for (X x : actual) {
list.add(x);
}
return list;
}
}