diff --git a/shared/src/commonMain/kotlin/com/tkhs0604/kmmsandbox/NewYear.kt b/shared/src/commonMain/kotlin/com/tkhs0604/kmmsandbox/NewYear.kt index 37955e0..70ade20 100644 --- a/shared/src/commonMain/kotlin/com/tkhs0604/kmmsandbox/NewYear.kt +++ b/shared/src/commonMain/kotlin/com/tkhs0604/kmmsandbox/NewYear.kt @@ -6,8 +6,7 @@ import kotlinx.datetime.TimeZone import kotlinx.datetime.daysUntil import kotlinx.datetime.todayIn -fun daysUntilNewYear(): Int { - val today = Clock.System.todayIn(TimeZone.currentSystemDefault()) - val closestNewYear = LocalDate(today.year + 1, 1, 1) - return today.daysUntil(closestNewYear) +fun daysUntilNewYear(startingDate: LocalDate = Clock.System.todayIn(TimeZone.currentSystemDefault())): Int { + val closestNewYear = LocalDate(startingDate.year + 1, 1, 1) + return startingDate.daysUntil(closestNewYear) } diff --git a/shared/src/commonTest/kotlin/com/tkhs0604/kmmsandbox/CommonNewYearTest.kt b/shared/src/commonTest/kotlin/com/tkhs0604/kmmsandbox/CommonNewYearTest.kt new file mode 100644 index 0000000..1aa2d84 --- /dev/null +++ b/shared/src/commonTest/kotlin/com/tkhs0604/kmmsandbox/CommonNewYearTest.kt @@ -0,0 +1,35 @@ +package com.tkhs0604.kmmsandbox + +import kotlinx.datetime.LocalDate +import kotlin.test.Test +import kotlin.test.assertEquals + +class CommonNewYearTest { + + @Test + fun `Days remaining until the next year are 1 if the starting date is the end of the year`() { + val startingDate = LocalDate(2022, 12, 31) + + val remainingDays = daysUntilNewYear(startingDate) + + assertEquals(1, remainingDays) + } + + @Test + fun `Days remaining until the next year are 365 if the starting date is not a leap year and the beginning of the year`() { + val startingDate = LocalDate(2023, 1, 1) + + val remainingDays = daysUntilNewYear(startingDate) + + assertEquals(365, remainingDays) + } + + @Test + fun `Days remaining until the next year are 366 if the starting date is a leap year and the beginning of the year`() { + val startingDate = LocalDate(2024, 1, 1) + + val remainingDays = daysUntilNewYear(startingDate) + + assertEquals(366, remainingDays) + } +} \ No newline at end of file