kalle
14 years ago
10 changed files with 233 additions and 47 deletions
@ -0,0 +1,17 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import org.hamcrest.BaseMatcher; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
public abstract class CollectionMatcher<C extends Collection<?>> extends BaseMatcher<C> { |
||||
@SuppressWarnings("unchecked") |
||||
public boolean matches(Object item) { |
||||
if (!(item instanceof Collection)) { |
||||
return false; |
||||
} |
||||
return matchesSafely((C)item); |
||||
} |
||||
|
||||
protected abstract boolean matchesSafely(C collection); |
||||
} |
@ -0,0 +1,50 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import static org.hamcrest.core.IsEqual.equalTo; |
||||
|
||||
import org.hamcrest.Description; |
||||
import org.hamcrest.Factory; |
||||
import org.hamcrest.Matcher; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
/** |
||||
* Matches if collection size satisfies a nested matcher. |
||||
*/ |
||||
public class IsCollectionWithSize<E> extends CollectionMatcher<Collection<? extends E>> { |
||||
private final Matcher<? super Integer> sizeMatcher; |
||||
|
||||
public IsCollectionWithSize(Matcher<? super Integer> sizeMatcher) { |
||||
this.sizeMatcher = sizeMatcher; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matchesSafely(Collection<? extends E> item) { |
||||
return sizeMatcher.matches(item.size()); |
||||
} |
||||
|
||||
public void describeTo(Description description) { |
||||
description.appendText("a collection with size ") |
||||
.appendDescriptionOf(sizeMatcher); |
||||
} |
||||
|
||||
/** |
||||
* Does collection size satisfy a given matcher? |
||||
*/ |
||||
@Factory |
||||
public static <E> Matcher<? super Collection<? extends E>> hasSize(Matcher<? super Integer> size) { |
||||
return new IsCollectionWithSize<E>(size); |
||||
} |
||||
|
||||
/** |
||||
* This is a shortcut to the frequently used hasSize(equalTo(x)). |
||||
* |
||||
* For example, assertThat(hasSize(equal_to(x))) |
||||
* vs. assertThat(hasSize(x)) |
||||
*/ |
||||
@Factory |
||||
public static <E> Matcher<? super Collection<? extends E>> hasSize(int size) { |
||||
Matcher<? super Integer> matcher = equalTo(size); |
||||
return IsCollectionWithSize.<E>hasSize(matcher); |
||||
} |
||||
} |
@ -0,0 +1,30 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import org.hamcrest.Description; |
||||
import org.hamcrest.Factory; |
||||
import org.hamcrest.Matcher; |
||||
|
||||
import java.util.Collection; |
||||
|
||||
/** |
||||
* Tests if collection is empty. |
||||
*/ |
||||
public class IsEmptyCollection<E> extends CollectionMatcher<Collection<E>> { |
||||
|
||||
@Override |
||||
public boolean matchesSafely(Collection<E> item) { |
||||
return item.isEmpty(); |
||||
} |
||||
|
||||
public void describeTo(Description description) { |
||||
description.appendText("an empty collection"); |
||||
} |
||||
|
||||
/** |
||||
* Matches an empty collection. |
||||
*/ |
||||
@Factory |
||||
public static <E> Matcher<Collection<E>> empty() { |
||||
return new IsEmptyCollection<E>(); |
||||
} |
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import java.util.Map; |
||||
|
||||
import org.hamcrest.Description; |
||||
import org.hamcrest.Factory; |
||||
import org.hamcrest.Matcher; |
||||
import static org.hamcrest.core.IsEqual.equalTo; |
||||
|
||||
public class IsMapContainingKey<K> extends MapTypeSafeMatcher<Map<K,?>> { |
||||
private final Matcher<K> keyMatcher; |
||||
|
||||
public IsMapContainingKey(Matcher<K> keyMatcher) { |
||||
this.keyMatcher = keyMatcher; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matchesSafely(Map<K, ?> item) { |
||||
for (K key : item.keySet()) { |
||||
if (keyMatcher.matches(key)) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public void describeTo(Description description) { |
||||
description.appendText("map with key ") |
||||
.appendDescriptionOf(keyMatcher); |
||||
} |
||||
|
||||
@Factory |
||||
public static <K> Matcher<Map<K,?>> hasKey(K key) { |
||||
return hasKey(equalTo(key)); |
||||
} |
||||
|
||||
@Factory |
||||
public static <K> Matcher<Map<K,?>> hasKey(Matcher<K> keyMatcher) { |
||||
return new IsMapContainingKey<K>(keyMatcher); |
||||
} |
||||
} |
@ -0,0 +1,40 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import org.hamcrest.Description; |
||||
import org.hamcrest.Factory; |
||||
import org.hamcrest.Matcher; |
||||
import static org.hamcrest.core.IsEqual.equalTo; |
||||
import java.util.Map; |
||||
|
||||
public class IsMapContainingValue<V> extends MapTypeSafeMatcher<Map<?,V>>{ |
||||
private final Matcher<? super V> valueMatcher; |
||||
|
||||
public IsMapContainingValue(Matcher<? super V> valueMatcher) { |
||||
this.valueMatcher = valueMatcher; |
||||
} |
||||
|
||||
@Override |
||||
public boolean matchesSafely(Map<?, V> item) { |
||||
for (V value : item.values()) { |
||||
if (valueMatcher.matches(value)) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public void describeTo(Description description) { |
||||
description.appendText("map with value ") |
||||
.appendDescriptionOf(valueMatcher); |
||||
} |
||||
|
||||
@Factory |
||||
public static <V> Matcher<? super Map<?,V>> hasValue(V value) { |
||||
return IsMapContainingValue.<V>hasValue(equalTo(value)); |
||||
} |
||||
|
||||
@Factory |
||||
public static <V> Matcher<? super Map<?,V>> hasValue(Matcher<? super V> valueMatcher) { |
||||
return new IsMapContainingValue<V>(valueMatcher); |
||||
} |
||||
} |
@ -0,0 +1,14 @@
|
||||
package com.jayway.jsonassert.impl.matcher; |
||||
|
||||
import org.hamcrest.BaseMatcher; |
||||
|
||||
import java.util.Map; |
||||
|
||||
public abstract class MapTypeSafeMatcher<M extends Map<?, ?>> extends BaseMatcher<M> { |
||||
@SuppressWarnings("unchecked") |
||||
public boolean matches(Object item) { |
||||
return item instanceof Map && matchesSafely((M) item); |
||||
} |
||||
|
||||
protected abstract boolean matchesSafely(M map); |
||||
} |
Loading…
Reference in new issue