mirror of
https://github.com/InsanusMokrassar/krontab.git
synced 2024-11-22 16:23:55 +00:00
tests and fixes in week work
This commit is contained in:
parent
bfa546ad1f
commit
86cdda51a1
@ -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)
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
38
src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt
Normal file
38
src/commonTest/kotlin/dev/inmo/krontab/utils/WeekDaysTest.kt
Normal file
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user