arashja / hamcrest

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

Confusing failure description when using negation #107

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The following code:

assertThat(strings, allOf(          hasItem(containsString("foo")), 
hasItem(not(endsWith("bar")))

produces this failure description when run on strings = ["foo bar", "blah
bar"]:

 Expected: (a collection containing a string containing "foo"
 and a collection containing not a string ending with "bar")
 got: <[foo bar, blah bar]>

when it should be 'a collection containing a string containing "foo" and a
collection containing a string not ending with "bar"' (note the position of
the "not").

Tested with hamcrest-1.1 as available via Maven.

Original issue reported on code.google.com by schm4...@gmail.com on 27 May 2010 at 6:54

GoogleCodeExporter commented 8 years ago
The problem is that the description

    a string ending with "bar"

is produced by endsWith(), and not() doesn't parse English so it merely 
prefixes it with

    not

to produce the awkward phrase you see. Each Matcher would need to be aware of 
negation to produce better messages. One solution would be to create a 
Negatable interface (yes, find a better name) that has one method:

    describeNegativeMismatch()

The name comes from the PHP version of Hamcrest, so maybe Java's is different. 
It would produce a similar message but with "not" embedded within it. 
Not.describeMismatch() would check if the matcher it is negating implements 
that interface and call the above method if it does. If not, it would prefix 
"not" as it does now.

This would allow the majority of matchers that work fine as-is to continue on 
while providing a simple method to generate nice-reading failure messages.

Original comment by dharkn...@gmail.com on 5 Jul 2010 at 6:02

GoogleCodeExporter commented 8 years ago

Original comment by dharkn...@gmail.com on 26 Jul 2010 at 9:49

GoogleCodeExporter commented 8 years ago
tagged

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