liangzai-cool / hamcrest

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

hasItem matcher containing equalTo matcher won't compile #143

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Using hamcrest 1.3.RC2 and JDK 1.6.0_24, the following doesn't compile:
  Iterable<String> i = null;
  assertThat(i, hasItem(equalTo("foo")));

The error:
cannot find symbol method 
assertThat(java.lang.Iterable<java.lang.String>,org.hamcrest.Matcher<java.lang.I
terable<? super java.lang.Object>>)

Replace Iterable<String> with Iterable<Object>, and it works:
  Iterable<Object> i = null;
  assertThat(i, hasItem(equalTo("foo")));

I can't figure out why it thinks the type is Matcher<Iterable<? super Object>>. 
It should resolve to Matcher<Iterable<? super String>> unless the source that I 
have for 1.3.RC2 doesn't match the binaries. This worked in 1.1, but fails from 
1.2 on.

Original issue reported on code.google.com by zzant...@gmail.com on 16 Mar 2011 at 4:43

GoogleCodeExporter commented 9 years ago
Perhaps stranger still is that extracting the matcher to a local variable makes 
it work fine. It's terribly ugly this way, though:
  Iterable<String> i = null;
  Matcher<Iterable<? super String>> matcher = hasItem(equalTo("foo"));
  assertThat(i, matcher);

Original comment by zzant...@gmail.com on 16 Mar 2011 at 4:53

GoogleCodeExporter commented 9 years ago
interesting thing is that "contains" matcher works. I've never had courage to 
investigate the difference though =)

Original comment by ata...@gmail.com on 31 Mar 2011 at 11:05

GoogleCodeExporter commented 9 years ago
The signature of contains is different than hasItem:
public static <T> org.hamcrest.Matcher<java.lang.Iterable<? super T>> 
hasItem(org.hamcrest.Matcher<? super T> p0)
vs
public static <E> org.hamcrest.Matcher<java.lang.Iterable<? extends E>> 
contains(org.hamcrest.Matcher<? super E> p0)

Notably, the former returns a Matcher<Iterable<? super T>>, where the latter 
returns a Matcher<Iterable<? extends E>>. The generics are where things are 
broken. I'm not sure, but I think "? extends E" makes more sense in that 
context anyway.

Original comment by zzant...@gmail.com on 31 Mar 2011 at 2:53

GoogleCodeExporter commented 9 years ago
Bug added against JDK6:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7035413

(The bug database is *really* slow sometimes.)

Original comment by zzant...@gmail.com on 11 Apr 2011 at 3:08

GoogleCodeExporter commented 9 years ago
tagging

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