Skip to content

Commit

Permalink
Add context to the DayOfWeek constructor precondition (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Mar 25, 2024
1 parent 02e4e4d commit 4470516
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/common/src/DayOfWeek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
22 changes: 22 additions & 0 deletions core/common/test/DayOfWeekTest.kt
Original file line number Diff line number Diff line change
@@ -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<IllegalArgumentException> { DayOfWeek(-1) }
assertFailsWith<IllegalArgumentException> { DayOfWeek(8) }
assertFailsWith<IllegalArgumentException> { DayOfWeek(Int.MIN_VALUE) }
}
}

0 comments on commit 4470516

Please sign in to comment.