Allow non-Comparable subjects to be compared using an explicit Comparator
This allows a subject with an arbitrary type to be treated as a "faux" Comparable, with propositions similar to those provided to a ComparableSubject.
assertThat(objectA).whenComparedUsing(comparator).isAtMost(objectB);
FauxComparable deliberately does not extend Subject, to prevent meaningless (and potentially confusing) constructs like assertThat(a).whenComparedUsing(comparator).isNotNull();. (This is broadly similar to Ordered.)
Implementing .whenComparedUsing() also required parameterizing DefaultSubject, so that type checking can be done on the supplied Comparator.
Hey Ben,
Thanks for sending this over...we've been trying to gauge interest in a similar API internally for a few months now. Here are some questions we had from our discussions:
- What do we name foo? Keep in mind it has to go on the base Subject type, so it will be fairly visible to anyone <TAB> completing from assertThat(foo). (this is what you called
whenComparedUsing)using(Comparator)usingComparator(Comparator)accordingTo(Comparator)comparedWith(Comparator)comparedUsing(Comparator) - What do we name the new Subject? (not hugely important…)
OrderedSubjectComparatorSubjectComparisonSubjectComparedSubjectComparableSubjectComparingSubject - What methods do we put on the new Subject? Do we just match what's on ComparableSubject?
The "obvious" ones:
isLessThan(), isGreaterThan(), isAtMost(), isAtLeast(). But what do we name the check forcompare(a, b) == 0? - Do we force types with natural ordering to go through another API?
assertThat(3).isLessThan(4);vs.assertThat(3).usingNaturalOrdering().isLessThan(4);
We've spent a good deal of time discussing this...now we just need to do some research over the Google codebase to figure out if this will be generally useful for people.
Thanks, -kak
/cc @PeteGillin