Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does IsIterableContainingInOrder Hamcrest matcher fail to work with arrays?
    text
    copied!<p>I am obviously confused about how to use Hamcrest's <code>IsIterableContainingInOrder</code> to verify List equality beyond just using <code>.equals()</code>. I'd like to see Hamcrest's helpful messages in my reports.</p> <p>Why are the tests below failing to even compile? Some of them are more counter-intuitive than others, at least for me. I get, I think, the general principle that the type parameter will be inferred to what I pass to a method with a varargs signature so it will consider arrays of T as varargs of T and so will generate a Matcher based on T and not on array of T, iterable of T, or anything like that.</p> <p>I could use an explanation, please, as to why some of the lines that are most intuitive actually fail to even compile.</p> <p>Particularly:</p> <ul> <li>3 and 4 show the asymmetry of array/List</li> <li>5 and 6 show that I can look in a List and not in an array (?)</li> </ul> <p>The compiler warning on the line marked as such is even more mysterious to me.</p> <hr> <pre><code>@org.junit.Test public void testTest() { String string1 = "A"; String string2 = "B"; String string3 = "C"; List&lt;String&gt; list1 = Lists.newArrayList(string1, string2, string3); List&lt;String&gt; list2 = Lists.newArrayList(string1, string2, "C"); String[] array1 = list1.toArray(new String[list1.size()]); String[] array2 = list2.toArray(new String[list2.size()]); // ------------------------------------------------------------------------- // 1) The assertion after this comment line DOES NOT COMPILE // Assert.assertThat(array2, IsIterableContainingInOrder.contains(array1)); // ------------------------------------------------------------------------- // 2) The assertion after this comment line DOES NOT COMPILE // Assert.assertThat(list2, IsIterableContainingInOrder.contains(list1)); // ------------------------------------------------------------------------- // 3) The assertion after this comment line SUCCEEDS Assert.assertThat(list2, IsIterableContainingInOrder.contains(array1)); // ------------------------------------------------------------------------- // 4) The assertion after this comment line DOES NOT COMPILE + HAS WARNING // Assert.assertThat(array2, IsIterableContainingInOrder.contains(list1)); // ------------------------------------------------------------------------- // 5) The assertion after this comment line DOES NOT COMPILE // Assert.assertThat(array2, IsIterableContainingInOrder.contains(string1)); // ------------------------------------------------------------------------- // 6) The assertion after this comment line COMPILES but fails Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1)); // ------------------------------------------------------------------------- // 7) The assertion after this comment line COMPILES and succeeds Assert.assertThat(list2, IsIterableContainingInOrder.contains(string1, string2, string3)); // ------------------------------------------------------------------------- } </code></pre> <hr> <h3>Copious amounts of astonishment. :(</h3> <hr> <p>PS I realise all of the astonishment comes from my own ignorance and nothing else. I should brush up on Generics, type inference, varargs, etc. I could really use a thorough explanation on this, and I would probably refer back to it a few times going forward.</p> <p>PPS I <em>did</em> try to read the code first but look at it for a sec... ;) Not for the feint of heart:</p> <pre><code>@SuppressWarnings("unchecked") @Factory public static &lt;E&gt; Matcher&lt;Iterable&lt;? extends E&gt;&gt; contains(final Matcher&lt;? super E&gt; itemMatcher) { return contains(new ArrayList&lt;Matcher&lt;? super E&gt;&gt;(asList(itemMatcher))); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload