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

com.pivotallabs.greatexpectations.matchers.IterableMatcher Maven / Gradle / Ivy

There is a newer version: 0.14
Show newest version
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;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy