Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

benblank
Copy link

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.

@kluever
Copy link
Member

kluever commented Jul 29, 2015

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 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 kluever added P3 not scheduled type=addition A new feature labels Oct 28, 2019
@kluever
Copy link
Member

kluever commented Oct 28, 2019

/cc @PeteGillin

@google-cla google-cla bot added cla: no and removed cla: yes labels Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: no P3 not scheduled type=addition A new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants