fest-assert-2.x icon indicating copy to clipboard operation
fest-assert-2.x copied to clipboard

Support usingComparator(comp).isGreaterThan and isLessThan

Open ianbrandt opened this issue 13 years ago • 2 comments

With 2.0M7 it seems I can write:

assertThat(a).usingComparator(myComparator).isEqualTo(b);

But not:

assertThat(a).usingComparator(myComparator).isGreaterThan(b);

Nor:

assertThat(a).usingComparator(myComparator).isLessThan(b);

Forgive me for not knowing anything about the FEST internals, but since all comparators return, "a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second" I would think these latter two methods would be possible.

For convenience isGreaterThanOrEqualTo and isLessThanOrEqualTo would be an added bonus.

ianbrandt avatar Aug 06 '12 23:08 ianbrandt

Hi Ian,

usingComparator was made in the first place to compare objects with a strategy not based on equals method (see this example).

Since usingComparator is available for all Assert classes, adding isGreaterThan and similar assertions will also be made available for all Assert classes. This is when it gets tricky because we can use isGreaterThan for objects that are non Comparable which does not make sense :

// no garantee that a can be compared to b, so why would we allow this assertion ?
assertThat(a).isGreaterThan(b);

At this point, we can implement this feature with the following contract :

  • if object under assertions is Comparable, we consider isGreaterThan assertion to be legal
  • if object under assertions is not Comparable but usingComparator has been called, we consider the previous assertion to be legal
  • if object under assertions is not Comparable and usingComparator has not been called, we consider the previous assertion to be illegal and we would return an IllegalArgumentException

The javadoc should clearly document this contract.

I think Comparable related assertions can be a nice addition, the only drawback I see is the case when it can't work (non Comparable object and no Comparator provided) which can be misleading, but as we would fail with a clear error message, I think we can do it anyway. I'm gonna ask other fest developers what their opinion is on this subject before doing anything.

Regards,

Joel

joel-costigliola avatar Aug 07 '12 08:08 joel-costigliola

isGreaterThan and isLessThan exist only for Comparables, pretty much in assertions for numbers . I fail to understand why would you like to override the Comparable behavior with a Comparator. Please explain.

alexruiz avatar Oct 05 '12 00:10 alexruiz