fest-assert-2.x
fest-assert-2.x copied to clipboard
Support usingComparator(comp).isGreaterThan and isLessThan
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.
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 considerisGreaterThanassertion to be legal - if object under assertions is not
ComparablebutusingComparatorhas been called, we consider the previous assertion to be legal - if object under assertions is not
ComparableandusingComparatorhas 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
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.