Skip to content

Commit

Permalink
add unit tests for daysUntilNewYear
Browse files Browse the repository at this point in the history
  • Loading branch information
tkhs0604 committed Jun 11, 2023
1 parent 4ce7bf4 commit 7635e06
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
Expand Up @@ -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)
}
@@ -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)
}
}

0 comments on commit 7635e06

Please sign in to comment.