Skip to content

Commit

Permalink
Add shouldNotBePositive and shouldNotBeNegative matchers for BigD…
Browse files Browse the repository at this point in the history
…ecimal (#3818)

> Anyone relying on the old behaviour (zero being considered positive)
could switch assertion to `bd.shouldNotBeNegative()`

_Originally posted by @Kantis in
#3814 (review)

--

Okay, I got the hint 😂.

Co-authored-by: Emil Kantis <emil.kantis@protonmail.com>
  • Loading branch information
pubiqq and Kantis committed Jan 14, 2024
1 parent 998ca8a commit ce6b4fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions documentation/docs/assertions/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Matchers provided by the `kotest-assertions-core` module.
| `bigDecimal.shouldHaveScale(n)` | Asserts that the bigDecimal scale is equals than the given value n |
| `bigDecimal.shouldBePositive()` | Asserts that the bigDecimal is positive |
| `bigDecimal.shouldBeNegative()` | Asserts that the bigDecimal is negative |
| `bigDecimal.shouldNotBePositive()` | Asserts that the bigDecimal is not positive |
| `bigDecimal.shouldNotBeNegative()` | Asserts that the bigDecimal is not negative |
| `bigDecimal.shouldBeZero()` | Asserts that the bigDecimal is zero |
| `bigDecimal.shouldBeLessThan(n)` | Asserts that the bigDecimal is less than the given value n |
| `bigDecimal.shouldBeLessThanOrEquals(n)` | Asserts that the bigDecimal is less than or equ|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ public final class io/kotest/matchers/bigdecimal/MatchersKt {
public static final fun shouldNotBeInRange (Ljava/math/BigDecimal;Lkotlin/ranges/ClosedRange;)V
public static final fun shouldNotBeLessThan (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotBeLessThanOrEquals (Ljava/math/BigDecimal;Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotBeNegative (Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotBePositive (Ljava/math/BigDecimal;)Ljava/math/BigDecimal;
public static final fun shouldNotHaveScale (Ljava/math/BigDecimal;I)I
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import java.math.BigDecimal
fun BigDecimal.shouldBeZero() = this shouldBe BigDecimal.ZERO
fun BigDecimal.shouldBePositive() = this shouldBe gt(BigDecimal.ZERO)
fun BigDecimal.shouldBeNegative() = this shouldBe lt(BigDecimal.ZERO)
fun BigDecimal.shouldNotBePositive() = this shouldNotBe gt(BigDecimal.ZERO)
fun BigDecimal.shouldNotBeNegative() = this shouldNotBe lt(BigDecimal.ZERO)

infix fun BigDecimal.shouldHavePrecision(precision: Int) = this.precision() shouldBe precision

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.sksamuel.kotest.matchers.bigdecimal

import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.bigdecimal.shouldBeGreaterThan
import io.kotest.matchers.bigdecimal.shouldBeGreaterThanOrEquals
import io.kotest.matchers.bigdecimal.shouldBeInRange
Expand All @@ -16,8 +17,9 @@ import io.kotest.matchers.bigdecimal.shouldNotBeGreaterThanOrEquals
import io.kotest.matchers.bigdecimal.shouldNotBeInRange
import io.kotest.matchers.bigdecimal.shouldNotBeLessThan
import io.kotest.matchers.bigdecimal.shouldNotBeLessThanOrEquals
import io.kotest.matchers.bigdecimal.shouldNotBeNegative
import io.kotest.matchers.bigdecimal.shouldNotBePositive
import io.kotest.matchers.bigdecimal.shouldNotHaveScale
import io.kotest.core.spec.style.StringSpec
import java.math.BigDecimal

class BigDecimalMatchersTest : StringSpec() {
Expand Down Expand Up @@ -64,6 +66,18 @@ class BigDecimalMatchersTest : StringSpec() {
shouldThrowAny { BigDecimal(1).shouldBeNegative() }
shouldThrowAny { BigDecimal.ZERO.shouldBeNegative() }
}
"shouldNotBePositive" {
BigDecimal(-1).shouldNotBePositive()
BigDecimal.ZERO.shouldNotBePositive()

shouldThrowAny { BigDecimal(1).shouldNotBePositive() }
}
"shouldNotBeNegative" {
BigDecimal(1).shouldNotBeNegative()
BigDecimal.ZERO.shouldNotBeNegative()

shouldThrowAny { BigDecimal(-1).shouldNotBeNegative() }
}
"shouldBeGreaterThan" {
BigDecimal.ONE shouldBeGreaterThan BigDecimal.ZERO
BigDecimal.TEN shouldBeGreaterThan BigDecimal.ONE
Expand Down

0 comments on commit ce6b4fe

Please sign in to comment.