truth icon indicating copy to clipboard operation
truth copied to clipboard

Allow non-Comparable subjects to be compared using an explicit Comparator

Open benblank opened this issue 10 years ago • 2 comments

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.

benblank avatar Jul 22 '15 00:07 benblank

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:

  1. 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)
  2. What do we name the new Subject? (not hugely important…) OrderedSubject ComparatorSubject ComparisonSubject ComparedSubject ComparableSubject ComparingSubject
  3. 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 for compare(a, b) == 0?
  4. 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

kluever avatar Jul 29 '15 20:07 kluever

/cc @PeteGillin

kluever avatar Oct 28 '19 13:10 kluever