hardayal / hamcrest

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

Add ToStringMatcher #117

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

Sometimes there are classes which don't have equals() defined, but their 
toString() is enough to talk about equality, so that if both toString() produce 
the same string, then objects are same. 

Could you please add ToStringMatcher<T> to the library?

Original issue reported on code.google.com by illarion.kovalchuk on 26 Jul 2010 at 1:05

Attachments:

GoogleCodeExporter commented 8 years ago
Oddly enough I just added HasToString to the PHP library yesterday. Instead of 
taking an object of the same type as the value to be asserted, I recommend 
accepting a Matcher and having two factory methods: one taking a Matcher and 
another taking a String.

The test is then free to pass in a literal string, a Matcher, or any object to 
have its toString() method called.* This would make it more generic while still 
satisfying the original request.

    HasToString(Matcher matcher)
    matches(Object actual) { return matcher.matches(actual.toString()); }

    hasToString(Matcher matcher) { return new HasToString(matcher); }
    hasToString(String expected) { return hasToString(eqaulTo(expected)); }

* IIRC Java automatically calls toString() whenever an object is passed to a 
method taking String. If not, add a third factory taking Object.

Original comment by dharkn...@gmail.com on 27 Jul 2010 at 6:41

GoogleCodeExporter commented 8 years ago

Original comment by smgfree...@gmail.com on 28 Apr 2012 at 11:40