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

Add Assertions for BigDecimal #116

Merged
merged 11 commits into from
Aug 24, 2018
Merged

Add Assertions for BigDecimal #116

merged 11 commits into from
Aug 24, 2018

Conversation

MarkusAmshove
Copy link
Owner

@MarkusAmshove MarkusAmshove commented Aug 6, 2018

Related issue: #114

This PR brings all numerical assertions from the common module to the Java type BigInteger.

  • 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)

import java.math.BigDecimal

infix fun BigDecimal.shouldEqualTo(expected: BigDecimal) =
this.apply { assertTrue("Expected $this to be equal to $expected", this == expected) }

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.

Copy link
Owner Author

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)
}

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)
    }

Copy link
Owner Author

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 :-)

@MarkusAmshove
Copy link
Owner Author

MarkusAmshove commented Aug 14, 2018

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 BigDecimal, any thoughts on that @kostya-misura ?

@MarkusAmshove MarkusAmshove changed the title [WIP] Add Assertions for BigDecimal Add Assertions for BigDecimal Aug 24, 2018
@MarkusAmshove MarkusAmshove merged commit 85a95ca into master Aug 24, 2018
@MarkusAmshove MarkusAmshove deleted the bigdecimal-assertions branch August 24, 2018 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants