Skip to content

Commit

Permalink
Adding shouldHaveSameInstantAs matcher for OffsetDateTime. Fixes #3488 (
Browse files Browse the repository at this point in the history
  • Loading branch information
Kantis committed Apr 25, 2023
1 parent fe43809 commit fbc6b4d
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions documentation/docs/assertions/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ Matchers provided by the `kotest-assertions-core` module.
| `zonedDateTime.shouldBeToday()` | Asserts that the ZonedDateTime has the same day as the today. |
| `zonedDateTime.shouldHaveSameInstantAs(other: ZonedDateTime)` | Asserts that the ZonedDateTime is equal to other ZonedDateTime using ```ChronoZonedDateTime.isEqual```. |

| OffsetDateTime | |
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------|
| `offsetDateTime.shouldBeToday()` | Asserts that the OffsetDateTime has the same day as today. |
| `offsetDateTime.shouldHaveSameInstantAs(other: OffsetDateTime)` | Asserts that the OffsetDateTime is equal to other OffsetDateTime using ```OffsetDateTime.isEqual```. |

| Times ||
|-----------------------------------------------| ---- |
| `time.shouldHaveSameHoursAs(otherTime)` | Asserts that the time has the same hours as the given time. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot
import java.time.OffsetDateTime
import java.time.ZoneOffset

fun beInTodayODT() = object : Matcher<OffsetDateTime> {
override fun test(value: OffsetDateTime): MatcherResult {
Expand Down Expand Up @@ -37,3 +38,77 @@ fun OffsetDateTime.shouldBeToday() = this should beInTodayODT()
*/
fun OffsetDateTime.shouldNotBeToday() = this shouldNot beInTodayODT()

/**
* Asserts that this is equal to [other] using the [OffsetDateTime.isEqual]
*
* Opposite of [OffsetDateTime.shouldNotHaveSameInstantAs]
*
* ```
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.shouldHaveSameInstantAs(other) // Assertion passes
*
*
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.shouldHaveSameInstantAs(other) // Assertion fails, date is NOT equal to the other date
* ```
*
* @see OffsetDateTime.shouldNotHaveSameInstantAs
*/
infix fun OffsetDateTime.shouldHaveSameInstantAs(other: OffsetDateTime) = this should haveSameInstantAs(other)

/**
* Asserts that this is NOT equal to [other] using the [OffsetDateTime.isEqual]
*
* Opposite of [OffsetDateTime.shouldHaveSameInstantAs]
*
* ```
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.shouldNotHaveSameInstantAs(other) // Assertion passes
*
*
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.shouldNotHaveSameInstantAs(other) // Assertion fails, date is equal to the other date
* ```
*
* @see OffsetDateTime.shouldHaveSameInstantAs
*/
infix fun OffsetDateTime.shouldNotHaveSameInstantAs(other: OffsetDateTime) = this shouldNot haveSameInstantAs(other)

/**
* Matcher that checks if OffsetDateTime is equal to another OffsetDateTime using the
* [OffsetDateTime.isEqual]
*
*
* ```
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.haveSameInstantAs(other) // Assertion passes
*
*
* val date = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1))
* val other = OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-3))
*
* date.haveSameInstantAs(other) // Assertion fails, date is NOT equal to the other date
* ```
*
* @see OffsetDateTime.shouldHaveSameInstantAs
* @see OffsetDateTime.shouldNotHaveSameInstantAs
*/
fun haveSameInstantAs(other: OffsetDateTime) = object : Matcher<OffsetDateTime> {
override fun test(value: OffsetDateTime): MatcherResult =
MatcherResult(
passed = value.isEqual(other),
failureMessageFn = { "$value should be equal to $other" },
negatedFailureMessageFn = {
"$value should not be equal to $other"
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -419,5 +419,12 @@ class DateMatchersTest : StringSpec() {
ZonedDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) shouldNot haveSameInstantAs(ZonedDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-2)))
ZonedDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) shouldNotHaveSameInstantAs ZonedDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-2))
}

"OffsetDateTime should be have same instant of the given OffsetDateTime irrespective of their timezone" {
OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) should haveSameInstantAs(OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-3)))
OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) shouldHaveSameInstantAs OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-3))
OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) shouldNot haveSameInstantAs(OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-2)))
OffsetDateTime.of(2019, 2, 16, 11, 0, 0, 0, ZoneOffset.ofHours(-1)) shouldNotHaveSameInstantAs OffsetDateTime.of(2019, 2, 16, 9, 0, 0, 0, ZoneOffset.ofHours(-2))
}
}
}

0 comments on commit fbc6b4d

Please sign in to comment.