kotest-assertions-core / io.kotest.matchers.date / java.time.LocalDate / shouldBeWithin

shouldBeWithin

fun LocalDate.shouldBeWithin(period: Period, date: LocalDate): Unit

Asserts that this is within period of date

Verifies that this is within period of date. For example, 09/02/1998 is within 3 days of 10/02/1998, and this assertion should pass for this comparison.

Opposite of LocalDate.shouldNotBeWithin

    val firstDate = LocalDate.of(1998, 2, 9)
    val secondDate = LocalDate.of(1998, 2, 10)

    firstDate.shouldBeWithin(Period.ofDays(3), secondDate) // Assertion passes


    val firstDate = LocalDate.of(1998, 2, 9)
    val secondDate = LocalDate.of(1998, 2, 25)

    firstDate.shouldBeWithin(Period.ofDays(3), secondDate)   // Assertion fails, firstDate is not within 3 days of secondDate