Skip to content

Commit

Permalink
Allow equals comparison of BigDecimals with different scales (#3924)
Browse files Browse the repository at this point in the history
Closes #3806
  • Loading branch information
sksamuel committed Mar 11, 2024
1 parent 13f4735 commit 5a8d132
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
Expand Up @@ -247,7 +247,9 @@ public final class io/kotest/matchers/atomic/AtomicBooleanMatchersKt {
}

public final class io/kotest/matchers/bigdecimal/MatchersKt {
public static final fun beEqualIgnoringScale (Ljava/math/BigDecimal;)Lio/kotest/matchers/Matcher;
public static final fun beInClosedRange (Lkotlin/ranges/ClosedRange;)Lio/kotest/matchers/Matcher;
public static final fun shouldBeEqualIgnoringScale (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)V
public static final fun shouldBeGreaterThan (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/lang/Object;
public static final fun shouldBeGreaterThanOrEquals (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/lang/Object;
public static final fun shouldBeInRange (Ljava/math/BigDecimal;Lkotlin/ranges/ClosedRange;)V
Expand All @@ -258,6 +260,7 @@ public final class io/kotest/matchers/bigdecimal/MatchersKt {
public static final fun shouldBeZero (Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldHavePrecision (Ljava/math/BigDecimal;I)I
public static final fun shouldHaveScale (Ljava/math/BigDecimal;I)I
public static final fun shouldNotBeEqualIgnoringScale (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)V
public static final fun shouldNotBeGreaterThan (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotBeGreaterThanOrEquals (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotBeInRange (Ljava/math/BigDecimal;Lkotlin/ranges/ClosedRange;)V
Expand Down
@@ -1,11 +1,11 @@
package io.kotest.matchers.bigdecimal

import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.comparables.gt
import io.kotest.matchers.comparables.gte
import io.kotest.matchers.comparables.lt
import io.kotest.matchers.comparables.lte
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNot
Expand Down Expand Up @@ -39,7 +39,18 @@ fun beInClosedRange(range: ClosedRange<BigDecimal>) = object : Matcher<BigDecima
override fun test(value: BigDecimal) = MatcherResult(
range.contains(value),
{ "Value $value should be in range from ${range.start} to ${range.endInclusive} (Inclusive)" },
{
"Value $value should not be in range from ${range.start} to ${range.endInclusive} (Inclusive)"
})
{ "Value $value should not be in range from ${range.start} to ${range.endInclusive} (Inclusive)" }
)
}

infix fun BigDecimal.shouldBeEqualIgnoringScale(other: BigDecimal) = this should beEqualIgnoringScale(other)
infix fun BigDecimal.shouldNotBeEqualIgnoringScale(other: BigDecimal) = this shouldNot beEqualIgnoringScale(other)

fun beEqualIgnoringScale(other: BigDecimal) = object : Matcher<BigDecimal> {
override fun test(value: BigDecimal) = MatcherResult(
value.compareTo(other) == 0,
{ "BigDecimal $value should be equal ignoring scale to $other" },
{ "BigDecimal $value should not be equal ignoring scale to $other" },
)
}

Expand Up @@ -35,19 +35,6 @@ class BigDecimalMatchersTest : StringSpec() {
10.1.toBigDecimal() shouldHavePrecision 3
BigDecimal.ZERO shouldHavePrecision 1
}
"shouldHaveScale" {
BigDecimal(10).setScale(3) shouldHaveScale 3
BigDecimal(10.1) shouldHaveScale 49
10.444.toBigDecimal() shouldHaveScale 3
0.toBigDecimal() shouldHaveScale 0
BigDecimal.ZERO shouldHaveScale 0

BigDecimal(10).setScale(3) shouldNotHaveScale 1
BigDecimal(10.1) shouldNotHaveScale 5
10.444.toBigDecimal() shouldNotHaveScale 2
0.toBigDecimal() shouldNotHaveScale 1
BigDecimal.ZERO shouldNotHaveScale 2
}
"shouldBePositive" {
BigDecimal(10).shouldBePositive()
BigDecimal.ONE.shouldBePositive()
Expand Down
@@ -0,0 +1,44 @@
package com.sksamuel.kotest.matchers.bigdecimal

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.bigdecimal.shouldBeEqualIgnoringScale
import io.kotest.matchers.bigdecimal.shouldHaveScale
import io.kotest.matchers.bigdecimal.shouldNotBeEqualIgnoringScale
import io.kotest.matchers.bigdecimal.shouldNotHaveScale
import java.math.BigDecimal

class BigDecimalScaleTest : FunSpec() {
init {
test("shouldHaveScale") {
BigDecimal(10).setScale(3) shouldHaveScale 3
BigDecimal(10.1) shouldHaveScale 49
10.444.toBigDecimal() shouldHaveScale 3
0.toBigDecimal() shouldHaveScale 0
BigDecimal.ZERO shouldHaveScale 0

BigDecimal(10).setScale(3) shouldNotHaveScale 1
BigDecimal(10.1) shouldNotHaveScale 5
10.444.toBigDecimal() shouldNotHaveScale 2
0.toBigDecimal() shouldNotHaveScale 1
BigDecimal.ZERO shouldNotHaveScale 2
}

test("shouldBeEqualIgnoringScale") {
BigDecimal(10) shouldBeEqualIgnoringScale BigDecimal(10)
BigDecimal(10) shouldBeEqualIgnoringScale BigDecimal(10.0)
BigDecimal(10.00) shouldBeEqualIgnoringScale BigDecimal(10.0)

BigDecimal(10) shouldNotBeEqualIgnoringScale BigDecimal(11)
BigDecimal(10) shouldNotBeEqualIgnoringScale BigDecimal(11.0)

shouldThrow<AssertionError> {
BigDecimal(10) shouldNotBeEqualIgnoringScale BigDecimal(10)
}

shouldThrow<AssertionError> {
BigDecimal(10) shouldNotBeEqualIgnoringScale BigDecimal(10.0)
}
}
}
}

0 comments on commit 5a8d132

Please sign in to comment.