From 447051631a22d286ff897504cb0f0102ea86f8c8 Mon Sep 17 00:00:00 2001 From: Vsevolod Tolstopyatov Date: Mon, 25 Mar 2024 11:24:48 +0100 Subject: [PATCH] Add context to the DayOfWeek constructor precondition (#371) --- core/common/src/DayOfWeek.kt | 2 +- core/common/test/DayOfWeekTest.kt | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 core/common/test/DayOfWeekTest.kt diff --git a/core/common/src/DayOfWeek.kt b/core/common/src/DayOfWeek.kt index 54013d40..496d61be 100644 --- a/core/common/src/DayOfWeek.kt +++ b/core/common/src/DayOfWeek.kt @@ -27,6 +27,6 @@ public val DayOfWeek.isoDayNumber: Int get() = ordinal + 1 * Returns the [DayOfWeek] instance for the given ISO-8601 week day number. Monday is 1, Sunday is 7. */ public fun DayOfWeek(isoDayNumber: Int): DayOfWeek { - require(isoDayNumber in 1..7) + require(isoDayNumber in 1..7) { "Expected ISO day-of-week number in 1..7, got $isoDayNumber" } return DayOfWeek.entries[isoDayNumber - 1] } diff --git a/core/common/test/DayOfWeekTest.kt b/core/common/test/DayOfWeekTest.kt new file mode 100644 index 00000000..cbdb252a --- /dev/null +++ b/core/common/test/DayOfWeekTest.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2019-2024 JetBrains s.r.o. and contributors. + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. + */ + +package kotlinx.datetime.test + +import kotlinx.datetime.* +import kotlin.test.* + +class DayOfWeekTest { + + @Test + fun testDayOfWeek() { + for (i in 1..7) { + assertEquals(i, DayOfWeek(i).isoDayNumber) + } + assertFailsWith { DayOfWeek(-1) } + assertFailsWith { DayOfWeek(8) } + assertFailsWith { DayOfWeek(Int.MIN_VALUE) } + } +} \ No newline at end of file