-
Notifications
You must be signed in to change notification settings - Fork 64
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
Add Assertions for BigDecimal #116
Conversation
import java.math.BigDecimal | ||
|
||
infix fun BigDecimal.shouldEqualTo(expected: BigDecimal) = | ||
this.apply { assertTrue("Expected $this to be equal to $expected", this == expected) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is correct implementation. The whole point is to avoid using ==
operator on BigDecimals and use compareTo
instead. Same applies for other comparison operators (<, >, >=, !=, <=). This answer on stack overflow explains this much better then I do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, at the first glance it looked like kotlin had overloads for the oprerators
val a = BigDecimal(1925112616126126) | ||
val b = BigDecimal(1925112616126126) | ||
a.shouldEqualTo(b) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add following test and it will fail in your current implementation:
@Test
fun passWhenComparingTwoEqualValuesWithDifferentScale() {
val a = BigDecimal(BigInteger.valueOf(1), -1) // 10
val b = BigDecimal(BigInteger.valueOf(10)) // 10
a.shouldEqualTo(b)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, that was what I was curious about, thanks :-)
If you want you can review this PR now, as I've implemented the major assertions. I'm still unsure if ranges would make sense in case of |
Related issue: #114
This PR brings all numerical assertions from the
common
module to the Java typeBigInteger
.shouldEqualTo
shouldNotEqualTo
shouldBeGreaterThan
shouldNotBeGreaterThan
shouldBeGreaterOrEqualTo
shouldNotBeGreaterOrEqualTo
shouldBeLessThan
shouldNotBeLessThan
shouldBeLessOrEqualTo
shouldNotBeLessOrEqualTo
shouldBePositive
shouldBeNegative
shouldBeInRange
(Unclear what overloads this should have)shouldNotBeInRange
(Unclear what overloads this should have)