liangzai-cool / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Implement IsEmptyMap, IsMapWithSize #131

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In hamcrest-java there are matchers IsEmptyCollection, IsCollectionWithSize.

I would like a similar pair to be implemented for java.util.Map, i.e. 
IsEmptyMap, IsMapWithSize.

A goal is to be able to test that a map is null or empty, like the following:
  anyOf(nullValue(),emptyMap())

Original issue reported on code.google.com by knst.kol...@gmail.com on 30 Nov 2010 at 9:50

GoogleCodeExporter commented 9 years ago
The Map contract defines that maps (of any concrete type) can be compared for 
equality.  So, isEmptyMap() is the same as equalTo(emptyMap()), where emptyMap 
is imported from java.util.Collections

IsMapWithSize is a reasonable matcher.

Original comment by nat.pr...@gmail.com on 1 Feb 2011 at 3:24

GoogleCodeExporter commented 9 years ago
If I understand, you're saying that IsEmptyMap shouldn't be a class, because 
there is a significant difference between the Map contract and the Collection 
contract. While the "empty map" method can be implemented trivially, the "empty 
collections" contract can't be trivially implemented because Collections 
encompass things like Lists, Sets, and Queues, which can't be compared to a 
"generic empty instance" in the same way maps can.

Despite this, it would still be a nice to have an "emptyMap()" utility method 
on org.hamcrest.Matchers. This utility method may just be a "one line method" 
but it's a pretty ugly method:

public static <U, V> Matcher<? super Map<U, V>> emptyMap() {
    return describedAs("an empty map", equalTo(Collections.<U, V> emptyMap()));
}

There's my best attempt; I've probably screwed it up myself, here, as I tend to 
take a trial-and-error approach with generics.

Original comment by aaron.pi...@gmail.com on 10 Feb 2011 at 4:08

GoogleCodeExporter commented 9 years ago
As an afterthought; it seems like IsEmptyCollection and IsEmptyMap can each be 
implemented trivially as derivations of IsCollectionWithSize and IsMapWithSize. 
I'm not sure if this approach is realistic, but it seems like it could cut down 
on "class clutter" and solve this problem in a somewhat elegant/symmetrical way.

Original comment by aaron.pi...@gmail.com on 10 Feb 2011 at 4:10

GoogleCodeExporter commented 9 years ago
The idea of comparing the map (as mapping) using equality is good, but I think 
that the issue is more related with the consistency and ease of use of the 
Hamcrest API, which would benefit from adding this simple method. 
Thanks for the alternative solution. 

Original comment by hanrisel...@gmail.com on 14 Jun 2011 at 8:09

GoogleCodeExporter commented 9 years ago
tagging

Original comment by t.denley on 12 May 2012 at 10:44

GoogleCodeExporter commented 9 years ago
I have added some new map size matchers in the following commit: 
https://github.com/hamcrest/JavaHamcrest/commit/749ef4be528e489139aec9d5ad401880
78dab389

This should give you access to:
is(aMapWithSize(3))
is(aMapWithSize(equalTo(3)))
is(anEmptyMap())

Original comment by t.denley on 29 Jul 2012 at 10:26

GoogleCodeExporter commented 9 years ago
Backport to the hamcrest-java-gwt branch

Original comment by dtrebbien@gmail.com on 28 Dec 2012 at 11:44

Attachments: