K
- the type of the key.V
- the type of the values.public static class MapMatchers.OrderedMapMatcher<K,V> extends org.hamcrest.TypeSafeDiagnosingMatcher<Map<K,V>>
public void makeSureElementsReturnedInDescendingOrder()
{
//This is a bad implementation as elements in ascending order.
final ImmutableSortedMap<Integer, Integer> sortedMap = ImmutableSortedMap.of(1, 1, 2, 2, 3, 3);
//This assertion will fail (correctly).
assertThat(sortedMap, OrderedMapMatcher.orderedMap(3, 3, 2, 2, 1, 1));
}
Some people will try to use the code below to make the above assertion. However, it will pass as the Map interface does not take order into account when doing equals.
public void makeSureElementsReturnedInDescendingOrder()
{
final LinkedHashMap<Integer, Integer> linked = Maps.newLinkedHashMap();
linked.put(3, 3);
linked.put(2, 2);
linked.put(1, 1);
//This is a bad implementation as elements are in ascending order.
final ImmutableSortedMap<Integer, Integer> sortedMap = ImmutableSortedMap.of(1, 1, 2, 2, 3, 3);
//This assertion will pass (incorrectly) even tough the sortedMap is not in ascending order.
assertThat(sortedMap, equalsTo(linked));
}
This is really only useful for Maps that
have a defined order like LinkedHashMap
, ImmutableMap
or SortedMap
instances.
Modifier and Type | Method and Description |
---|---|
void |
describeTo(org.hamcrest.Description description) |
protected boolean |
matchesSafely(Map<K,V> actual,
org.hamcrest.Description description) |
static <K,V> MapMatchers.OrderedMapMatcher<K,V> |
orderedMap(Iterable<com.atlassian.fugue.Pair<K,V>> pairs) |
static <K,V> MapMatchers.OrderedMapMatcher<K,V> |
orderedMap(K key1,
V value1) |
static <K,V> MapMatchers.OrderedMapMatcher<K,V> |
orderedMap(K key1,
V value1,
K key2,
V value2) |
static <K,V> MapMatchers.OrderedMapMatcher<K,V> |
orderedMap(K key1,
V value1,
K key2,
V value2,
K key3,
V value3) |
static <K,V> MapMatchers.OrderedMapMatcher<K,V> |
orderedMap(SortedMap<K,V> map) |
public static <K,V> MapMatchers.OrderedMapMatcher<K,V> orderedMap(SortedMap<K,V> map)
public static <K,V> MapMatchers.OrderedMapMatcher<K,V> orderedMap(Iterable<com.atlassian.fugue.Pair<K,V>> pairs)
public static <K,V> MapMatchers.OrderedMapMatcher<K,V> orderedMap(K key1, V value1)
public static <K,V> MapMatchers.OrderedMapMatcher<K,V> orderedMap(K key1, V value1, K key2, V value2)
public static <K,V> MapMatchers.OrderedMapMatcher<K,V> orderedMap(K key1, V value1, K key2, V value2, K key3, V value3)
protected boolean matchesSafely(Map<K,V> actual, org.hamcrest.Description description)
public void describeTo(org.hamcrest.Description description)
Copyright © 2002-2017 Atlassian. All Rights Reserved.