Skip to content

Commit 4470516

Browse files
authored
Add context to the DayOfWeek constructor precondition (#371)
1 parent 02e4e4d commit 4470516

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

core/common/src/DayOfWeek.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public val DayOfWeek.isoDayNumber: Int get() = ordinal + 1
2727
* Returns the [DayOfWeek] instance for the given ISO-8601 week day number. Monday is 1, Sunday is 7.
2828
*/
2929
public fun DayOfWeek(isoDayNumber: Int): DayOfWeek {
30-
require(isoDayNumber in 1..7)
30+
require(isoDayNumber in 1..7) { "Expected ISO day-of-week number in 1..7, got $isoDayNumber" }
3131
return DayOfWeek.entries[isoDayNumber - 1]
3232
}

core/common/test/DayOfWeekTest.kt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2019-2024 JetBrains s.r.o. and contributors.
3+
* Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
4+
*/
5+
6+
package kotlinx.datetime.test
7+
8+
import kotlinx.datetime.*
9+
import kotlin.test.*
10+
11+
class DayOfWeekTest {
12+
13+
@Test
14+
fun testDayOfWeek() {
15+
for (i in 1..7) {
16+
assertEquals(i, DayOfWeek(i).isoDayNumber)
17+
}
18+
assertFailsWith<IllegalArgumentException> { DayOfWeek(-1) }
19+
assertFailsWith<IllegalArgumentException> { DayOfWeek(8) }
20+
assertFailsWith<IllegalArgumentException> { DayOfWeek(Int.MIN_VALUE) }
21+
}
22+
}

0 commit comments

Comments
 (0)