From 86cdda51a1f7562db6e0926e0f487c97999c18e7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Apr 2021 12:24:26 +0600 Subject: [PATCH] tests and fixes in week work --- .../dev/inmo/krontab/internal/CronDateTime.kt | 2 +- .../krontab/internal/CronDateTimeScheduler.kt | 4 +- .../dev/inmo/krontab/utils/WeekDaysTest.kt | 38 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt diff --git a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt index cc3667f..49cf462 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTime.kt @@ -54,7 +54,7 @@ internal fun CronDateTime.toNearDateTime(relativelyTo: DateTime = DateTime.now() current = (current + diff.days).startOfDay val next = toNearDateTime(current) - if (next ?.dayOfWeek ?.index0 == weekDay) { + if (next == null || next.dayOfWeek.index0 == weekDay) { return next } } while (true) diff --git a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt index 751c179..d522dd4 100644 --- a/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt +++ b/src/commonMain/kotlin/dev/inmo/krontab/internal/CronDateTimeScheduler.kt @@ -27,8 +27,8 @@ internal data class CronDateTimeScheduler internal constructor( * * @see toNearDateTime */ - override suspend fun next(relatively: DateTime): DateTime { - return cronDateTimes.mapNotNull { it.toNearDateTime(relatively) }.minOrNull() ?: getAnyNext(relatively) + override suspend fun next(relatively: DateTime): DateTime? { + return cronDateTimes.mapNotNull { it.toNearDateTime(relatively) }.minOrNull() } } diff --git a/src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt b/src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt new file mode 100644 index 0000000..9b8d8b7 --- /dev/null +++ b/src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt @@ -0,0 +1,38 @@ +package dev.inmo.krontab.utils + +import com.soywiz.klock.* +import dev.inmo.krontab.builder.buildSchedule +import kotlin.math.ceil +import kotlin.test.* + +class WeekDaysTest { + @Test + fun testThatWeekDaysSchedulingWorks() { + val startDateTime = DateTime.now().startOfDay + val weekDay = startDateTime.dayOfWeek.index0 + val testDays = 400 + val scheduler = buildSchedule { + dayOfWeek { + at(weekDay) + } + years { + at(startDateTime.yearInt) + } + } + runTest { + for (day in 0 until testDays) { + val currentDateTime = startDateTime + day.days + val next = scheduler.next(currentDateTime) + val expected = when { + day % 7 == 0 -> currentDateTime + else -> startDateTime + ceil(day.toFloat() / 7).weeks + } + if (expected.yearInt != startDateTime.yearInt) { + assertNull(next) + } else { + assertEquals(expected, next) + } + } + } + } +}