alexruiz / fest-assert-2.x

FEST Fluent Assertions 2.x
http://fest.easytesting.org
Apache License 2.0
401 stars 69 forks source link

Support usingComparator(comp).isGreaterThan and isLessThan #93

Open ianbrandt opened 12 years ago

ianbrandt commented 12 years ago

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.

joel-costigliola commented 12 years ago

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 :

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

alexruiz commented 12 years ago

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.