aboutsummaryrefslogtreecommitdiff
path: root/library/src/org/hamcrest/collection
diff options
context:
space:
mode:
authorBrett Chabot <brettchabot@google.com>2014-06-11 16:08:22 -0700
committerBrett Chabot <brettchabot@google.com>2014-06-13 14:18:35 -0700
commitf5e9a2415ec42c425c2bb17db46f2a9649992d80 (patch)
tree51560c56cffa6e2e751396bf3ccea0124b90efc5 /library/src/org/hamcrest/collection
parentf79c5d901a0e83f829c2325f5304f2b2c87fac70 (diff)
downloadhamcrest-f5e9a2415ec42c425c2bb17db46f2a9649992d80.tar.gz
Add hamcrest 1.1 library and integration source
Change-Id: I98691c987d5845c1d6e05325971517eec7e6f8b5
Diffstat (limited to 'library/src/org/hamcrest/collection')
-rw-r--r--library/src/org/hamcrest/collection/IsArray.java64
-rw-r--r--library/src/org/hamcrest/collection/IsArrayContaining.java42
-rw-r--r--library/src/org/hamcrest/collection/IsCollectionContaining.java65
-rw-r--r--library/src/org/hamcrest/collection/IsIn.java45
-rw-r--r--library/src/org/hamcrest/collection/IsMapContaining.java70
-rw-r--r--library/src/org/hamcrest/collection/package.html7
6 files changed, 293 insertions, 0 deletions
diff --git a/library/src/org/hamcrest/collection/IsArray.java b/library/src/org/hamcrest/collection/IsArray.java
new file mode 100644
index 0000000..6a26ef1
--- /dev/null
+++ b/library/src/org/hamcrest/collection/IsArray.java
@@ -0,0 +1,64 @@
+package org.hamcrest.collection;
+
+import java.util.Arrays;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+public class IsArray<T> extends TypeSafeMatcher<T[]> {
+ private final Matcher<T>[] elementMatchers;
+
+ public IsArray(Matcher<T>[] elementMatchers) {
+ this.elementMatchers = elementMatchers.clone();
+ }
+
+ 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;
+ }
+
+ 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.
+ */
+ 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.
+ */
+ protected String descriptionSeparator() {
+ return ", ";
+ }
+
+ /**
+ * Returns the string that ends the description.
+ *
+ * Can be overridden in subclasses to customise how the matcher is
+ * described.
+ */
+ protected String descriptionEnd() {
+ return "]";
+ }
+
+ public static <T> IsArray<T> array(Matcher<T>... elementMatchers) {
+ return new IsArray<T>(elementMatchers);
+ }
+}
diff --git a/library/src/org/hamcrest/collection/IsArrayContaining.java b/library/src/org/hamcrest/collection/IsArrayContaining.java
new file mode 100644
index 0000000..76ddf9d
--- /dev/null
+++ b/library/src/org/hamcrest/collection/IsArrayContaining.java
@@ -0,0 +1,42 @@
+package org.hamcrest.collection;
+
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.TypeSafeMatcher;
+import static org.hamcrest.core.IsEqual.equalTo;
+
+public class IsArrayContaining<T> extends TypeSafeMatcher<T[]> {
+
+ private final Matcher<T> elementMatcher;
+
+ public IsArrayContaining(Matcher<T> elementMatcher) {
+ this.elementMatcher = elementMatcher;
+ }
+
+ public boolean matchesSafely(T[] array) {
+ for (T item : array) {
+ if (elementMatcher.matches(item)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void describeTo(Description description) {
+ description
+ .appendText("an array containing ")
+ .appendDescriptionOf(elementMatcher);
+ }
+
+ @Factory
+ public static <T> Matcher<T[]> hasItemInArray(Matcher<T> elementMatcher) {
+ return new IsArrayContaining<T>(elementMatcher);
+ }
+
+ @Factory
+ public static <T> Matcher<T[]> hasItemInArray(T element) {
+ return hasItemInArray(equalTo(element));
+ }
+
+}
diff --git a/library/src/org/hamcrest/collection/IsCollectionContaining.java b/library/src/org/hamcrest/collection/IsCollectionContaining.java
new file mode 100644
index 0000000..ba98630
--- /dev/null
+++ b/library/src/org/hamcrest/collection/IsCollectionContaining.java
@@ -0,0 +1,65 @@
+package org.hamcrest.collection;
+
+import static org.hamcrest.core.AllOf.allOf;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.Factory;
+import org.hamcrest.TypeSafeMatcher;
+import static org.hamcrest.core.IsEqual.equalTo;
+
+import java.util.Collection;
+import java.util.ArrayList;
+
+public class IsCollectionContaining<T> extends TypeSafeMatcher<Iterable<T>> {
+ private final Matcher<? extends T> elementMatcher;
+
+ public IsCollectionContaining(Matcher<? extends T> elementMatcher) {
+ this.elementMatcher = elementMatcher;
+ }
+
+ public boolean matchesSafely(Iterable<T> collection) {
+ for (T item : collection) {
+ if (elementMatcher.matches(item)){
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void describeTo(Description description) {
+ description
+ .appendText("a collection containing ")
+ .appendDescriptionOf(elementMatcher);
+ }
+
+ @Factory
+ public static <T> Matcher<Iterable<T>> hasItem(Matcher<? extends T> elementMatcher) {
+ return new IsCollectionContaining<T>(elementMatcher);
+ }
+
+ @Factory
+ public static <T> Matcher<Iterable<T>> hasItem(T element) {
+ return hasItem(equalTo(element));
+ }
+
+ @Factory
+ public static <T> Matcher<Iterable<T>> hasItems(Matcher<? extends T>... elementMatchers) {
+ Collection<Matcher<? extends Iterable<T>>> all
+ = new ArrayList<Matcher<? extends Iterable<T>>>(elementMatchers.length);
+ for (Matcher<? extends T> elementMatcher : elementMatchers) {
+ all.add(hasItem(elementMatcher));
+ }
+ return allOf(all);
+ }
+
+ @Factory
+ public static <T> Matcher<Iterable<T>> hasItems(T... elements) {
+ Collection<Matcher<? extends Iterable<T>>> all
+ = new ArrayList<Matcher<? extends Iterable<T>>>(elements.length);
+ for (T element : elements) {
+ all.add(hasItem(element));
+ }
+ return allOf(all);
+ }
+
+}
diff --git a/library/src/org/hamcrest/collection/IsIn.java b/library/src/org/hamcrest/collection/IsIn.java
new file mode 100644
index 0000000..0a7bbb5
--- /dev/null
+++ b/library/src/org/hamcrest/collection/IsIn.java
@@ -0,0 +1,45 @@
+package org.hamcrest.collection;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+
+public class IsIn<T> extends BaseMatcher<T> {
+ private final Collection<T> collection;
+
+ public IsIn(Collection<T> collection) {
+ this.collection = collection;
+ }
+
+ public IsIn(T[] elements) {
+ collection = Arrays.asList(elements);
+ }
+
+ public boolean matches(Object o) {
+ return collection.contains(o);
+ }
+
+ public void describeTo(Description buffer) {
+ buffer.appendText("one of ");
+ buffer.appendValueList("{", ", ", "}", collection);
+ }
+
+ @Factory
+ public static <T> Matcher<T> isIn(Collection<T> collection) {
+ return new IsIn<T>(collection);
+ }
+
+ @Factory
+ public static <T> Matcher<T> isIn(T[] elements) {
+ return new IsIn<T>(elements);
+ }
+
+ @Factory
+ public static <T> Matcher<T> isOneOf(T... elements) {
+ return isIn(elements);
+ }
+}
diff --git a/library/src/org/hamcrest/collection/IsMapContaining.java b/library/src/org/hamcrest/collection/IsMapContaining.java
new file mode 100644
index 0000000..74572dd
--- /dev/null
+++ b/library/src/org/hamcrest/collection/IsMapContaining.java
@@ -0,0 +1,70 @@
+package org.hamcrest.collection;
+
+import org.hamcrest.Description;
+import org.hamcrest.Factory;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.hamcrest.core.IsAnything;
+
+import static org.hamcrest.core.IsEqual.equalTo;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class IsMapContaining<K,V> extends TypeSafeMatcher<Map<K, V>> {
+
+ private final Matcher<K> keyMatcher;
+ private final Matcher<V> valueMatcher;
+
+ public IsMapContaining(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
+ this.keyMatcher = keyMatcher;
+ this.valueMatcher = valueMatcher;
+ }
+
+ public boolean matchesSafely(Map<K, V> map) {
+ for (Entry<K, V> entry : map.entrySet()) {
+ if (keyMatcher.matches(entry.getKey()) && valueMatcher.matches(entry.getValue())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public void describeTo(Description description) {
+ description.appendText("map containing [")
+ .appendDescriptionOf(keyMatcher)
+ .appendText("->")
+ .appendDescriptionOf(valueMatcher)
+ .appendText("]");
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasEntry(Matcher<K> keyMatcher, Matcher<V> valueMatcher) {
+ return new IsMapContaining<K,V>(keyMatcher, valueMatcher);
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasEntry(K key, V value) {
+ return hasEntry(equalTo(key), equalTo(value));
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasKey(Matcher<K> keyMatcher) {
+ return hasEntry(keyMatcher, IsAnything.<V>anything());
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasKey(K key) {
+ return hasKey(equalTo(key));
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasValue(Matcher<V> valueMatcher) {
+ return hasEntry(IsAnything.<K>anything(), valueMatcher);
+ }
+
+ @Factory
+ public static <K,V> Matcher<Map<K,V>> hasValue(V value) {
+ return hasValue(equalTo(value));
+ }
+}
diff --git a/library/src/org/hamcrest/collection/package.html b/library/src/org/hamcrest/collection/package.html
new file mode 100644
index 0000000..6248d8d
--- /dev/null
+++ b/library/src/org/hamcrest/collection/package.html
@@ -0,0 +1,7 @@
+<html>
+<head>
+</head>
+<body>
+ <p>Matchers of arrays and collections.</p>
+</body>
+</html>