allow equals comparison of BigDecimals
currently you can't easily compare bigdecimals for equality in kotest (ie : BigDecimal("1.0") shouldBe BigDecimal("1") passing)
if we add the following
infix fun BigDecimal?.shouldEqual(other: BigDecimal?) = this?.compareTo(other) shouldBe 0
to the BigDecimal comparitors file, it'll make it easier
happy to raise a PR
We could also add an Eq instance for BigDecimal to make it work? then BigDecimal("1.0") shouldBe BigDecimal("1") would work which I think is more intuitive ?
it is more intuitive but those two bigdecimals aren't equal according to the bigdecimal spec (BigDecimal("1.0")==BigDecimal("1") is false) due to the number of decimal spots. Changing "shouldBe" to return true in that circumstance might be undesirable for some folks?
BigDecimal would have the folloring assertions then, given this suggestion
-
shouldBe -
shouldEqual -
shouldBeEqual
I think it might be a bit confusing to know which to pick. Perhaps naming the new one shouldHaveSameValue would set it apart sufficiently?
We do have matchers for compareTo
-
shouldBeEqualComparingTo
Would this solve the issue for different scales? BigDecimal returns 0 for BigDecimal("1.0").compareTo(BigDecimal("1")
So one could just use
BigDecimal("1.005") shouldBeEqualComparingTo BigDecimal("1.0050000")
We do have matchers for
compareTo
shouldBeEqualComparingToWould this solve the issue for different scales? BigDecimal returns 0 for
BigDecimal("1.0").compareTo(BigDecimal("1")So one could just use
BigDecimal("1.005") shouldBeEqualComparingTo BigDecimal("1.0050000")
ah yes that does work, but I think (given a few of us weren't aware of it) - its not very discoverable. A function alias within the io.kotest.matchers.bigdecimal package would be nice to aid discovery?
I think so, yes!
Now what to call it is tough. Any idea?
shouldBeEqualWithoutScale?
as a match in the bigdecimal package ?
shouldBeSameValue?
Value is a word that could be used either way. Is 5 and 5.0 the same value or not the same value in general?
@LeoColman what's your final thoughts before I implement it?
I'm trying to think if mathematically we have a term that means 'this number represents the same value as this one, in totality'
And I can only think of shouldBeNumericallyEquivalentTo or shouldBeMathematicallyEquivalentTo
So
15.05 shouldBeNumericallyEquivalentTo 15.050000
How come you don't like the word scale? Since that's what big decimal itself uses. https://medium.com/beingabetterdeveloper/comparing-bigdecimal-s-with-different-scales-2901bf26538f
Like shouldBeIgnoringScale or the one you suggested?
Nothing against it at all 😁 go for it
shouldBeIgnoringScale or shouldBeEqualIgnoringScale
What do you think ?
shouldBeEqualIgnoringScale sounds better
@LeoColman https://github.com/kotest/kotest/pull/3924