cactoos-matchers icon indicating copy to clipboard operation
cactoos-matchers copied to clipboard

TextIs should test asString(), toString(), equals() and hashCode()

Open fabriciofx opened this issue 5 years ago • 7 comments

Actually, TextIs matcher just checks if two Text#asString() are equals. IMHO, to two Text are truly equals, asString(), toString(), equals() and hashCode() must be equals, because:

  • asString() and toString()` are the same thing, should be equals;
  • If both are equals, by logic, equals() must be equals too;
  • As hashCode() is closely linked to equals(), both hashCode() should be equals too.

So, @victornoel and @llorllale WDYT?

fabriciofx avatar Sep 25 '20 16:09 fabriciofx

@fabriciofx yes, I agree.

And also, if someone just want to test that two Text are equal without the extra checks, they can use the IsEqual matcher.

For the record, this ticket comes from a discussion there: yegor256/cactoos#1472.

victornoel avatar Sep 25 '20 17:09 victornoel

@fabriciofx I agree with all but toString(): there are no identity semantics attached to its definition.

llorllale avatar Sep 26 '20 15:09 llorllale

@llorllale I was wondering also about toString(): in cactoos, it is kind of expected that the contract of Text is that toString() returns the same thing as asString(), no?

victornoel avatar Sep 27 '20 09:09 victornoel

@victornoel no one should rely on Text.toString() being equal to Text.asString(). Seems to me like if anyone is then they're actually relying on implementation of TextOf which, again, has no commitment to having those two equal.

Are we thinking about formatting strings using Text here? That's the only practical place I see where it would make sense for Text.toString() be equal to Text.asString().

llorllale avatar Sep 27 '20 13:09 llorllale

@llorllale I see your point. I'm a bit confused about the meaning of toString for Text then... what is it?

victornoel avatar Sep 27 '20 13:09 victornoel

This issue was also brought up here https://github.com/yegor256/cactoos/pull/1485#discussion_r515449483

@fabriciofx Testing for equals & hashCode breaks TextIs for implementations that are not based upon TextEnvelope + TextOf, in cactoos it's text.Sticky. The contract for equals/hashCode [3] is not obvious for a user who implements Text . But since TextOf uses original string for hashCode /equals, when TextIs checks for equals it's no different than new IsEqual<>(new TextOf(...))

Btw, TextIs and TextHasValue are almost identical (the later calls contains instead of equals). And there is no way to write something like new TextHasValue(Matchers.regexp("...."))

Also, should TextIs be named IsText?

1: https://github.com/llorllale/cactoos-matchers/blob/master/src/main/java/org/llorllale/cactoos/matchers/TextHasString.java 2: https://github.com/llorllale/cactoos-matchers/blob/master/src/main/java/org/llorllale/cactoos/matchers/TextIs.java 3: https://github.com/yegor256/cactoos/blob/master/src/main/java/org/cactoos/text/TextOf.java#L641

andreoss avatar Nov 06 '20 03:11 andreoss

@andreoss just reacting about the point about equals: every implementation of text could/should implement it. You don't have to rely on TextOf for that. It's not ideal and it's not 100% clear if the text interface contract should include equals and hashcode but for now, it seems to be the case...

victornoel avatar Nov 07 '20 15:11 victornoel