So, let's have a list of strings and a function that takes a Hamcrest matcher and returns a result of the matches()
method of the provided matcher:
public boolean matchIt(final Matcher<? super List<String>> matcher) {
final List<String> lst = obtainListFromSomewhere();
return matcher.matches(lst);
}
So far so good. Now I can easily call:
matchIt(empty());
matchIt(anything());
matchIt(hasItem("item"));
matchIt(everyItem(equalToIgnoringCase("item")));
...since all of these factory static methods produce a matcher that fits the method signature Matcher<? super List<String>>
.
However, I believe a matcher that accepts an Iterable of objects should be accepted by the matchIt()
method as well:
matchIt(everyItem(anything()));
So I naively changed the matchIt()
method signature:
public boolean matchIt(final Matcher<? super List<? super String>> matcher);
But it doesn't work at all. Not only it doesn't accept everyItem(anything())
, it doesn't even accept the previously correct everyItem(equalToIgnoringCase("item"))
saying (1.7.0_05 compiler version):
actual argument Matcher<Iterable<String>> cannot be converted to Matcher<? super List<? super String>> by method invocation conversion
What the? So what's wrong here? Is it the matchIt()
method signature or is the everyItem()
Hamcrest signature designed wrongly? Or is it just the Java generics system being beyond repair? Thanks a lot for you comments!
EDIT
@rlegendi my intention here is to provide an interface for the client to add and execute predicates about the list. That's the matchIt()
method. Calling matchIt(anything())
makes sense in this scenario, the client wants to know whether the list is anything. Calling matchIt(empty())
means the client wants to know whether the list is empty. Vice versa for matchIt(everyItem(equalToIgnoringCase("item")))
and matchIt(hasItem("item"))
.
My goal here is to have a best matchIt()
method signature possible. The Matcher<? super List<String>>
works fine for all of the previous scenarios. However, I believe the client should be allowed to add Matcher<Iterable<Object>>
matchers as well (for example matchIt(everyItem(notNullValue())
makes perfect sense here, the client wants to know whether every String item of the list is not null).
However I can't find the right signature, matchIt(Matcher<? super List<? super String>>)
doesn't work for everyItem(notNullValue());
I use Hamcrest 1.3.
EDIT 2:
I believe I have found my root misunderstanding.
The everyItem(anything())
expression return an object of type Matcher<Iterable<Object>>
. So I can do easily Matcher<Iterable<Object>> m = everyItem(anything());
However what I don't understand is why I can't do Matcher<? super List<? super String>> m1 = m;
. It seems that Matcher<Iterable<Object>>
is not Matcher<? super List<? super String>>
but I don't get it why.
I can't even do Matcher<? super List<?>> m1 = m;
. Matcher<Iterable<Object>>
is not Matcher<? super List<?>>
? Why?
>` http://stackoverflow.com/questions/13289847/listlist-super-string-doesnt-work-as-expected
– Arend v. Reinersdorff Nov 13 '12 at 12:52