From 0d653764a66516b89cbbed619d55700a57028a63 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 22 Nov 2023 15:29:04 +0100 Subject: [PATCH] Feature/versions (#18) * Switch Builder Test on Kotest * Switch Parser Tests on Kotest * Add Versions support * Cover versions by tests * Increase Version * Update README * Fix code smells --- README.md | 23 +++- build.gradle.kts | 3 +- .../kotlin/com/ucasoft/kcron/KCron.kt | 9 +- .../kcron/exceptions/WrongCronExpression.kt | 4 +- .../com/ucasoft/kcron/parsers/Parser.kt | 21 ++- .../com/ucasoft/kcron/settings/Settings.kt | 5 + .../com/ucasoft/kcron/settings/Version.kt | 10 ++ .../com/ucasoft/kcron/BuildAndParseTests.kt | 70 ++++++++++ .../ucasoft/kcron/builders/BuilderTests.kt | 122 ++++++++++-------- .../kcron/builders/DaysBuilderTests.kt | 30 +++-- .../kcron/builders/DaysOfWeekBuilderTests.kt | 30 +++-- .../kcron/builders/HoursBuilderTests.kt | 12 +- .../kcron/builders/MinutesBuilderTests.kt | 12 +- .../kcron/builders/MonthsBuilderTests.kt | 14 +- .../kcron/builders/SecondsBuilderTests.kt | 12 +- .../kcron/builders/YearsBuilderTests.kt | 18 +-- .../kcron/parsers/DaysOfWeekPartTests.kt | 14 +- .../ucasoft/kcron/parsers/DaysPartTests.kt | 15 ++- .../ucasoft/kcron/parsers/HoursPartTests.kt | 15 ++- .../ucasoft/kcron/parsers/MonthPartTests.kt | 15 ++- .../com/ucasoft/kcron/parsers/ParserTests.kt | 60 +++++++-- .../ucasoft/kcron/parsers/SecondsPartTests.kt | 15 ++- .../ucasoft/kcron/parsers/YearPartTests.kt | 15 ++- 23 files changed, 377 insertions(+), 167 deletions(-) create mode 100644 src/commonMain/kotlin/com/ucasoft/kcron/settings/Settings.kt create mode 100644 src/commonMain/kotlin/com/ucasoft/kcron/settings/Version.kt create mode 100644 src/commonTest/kotlin/com/ucasoft/kcron/BuildAndParseTests.kt diff --git a/README.md b/README.md index d909db6..1053fc1 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # KCron Common Cron realization for Kotlin Multiplatform -[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Scogun_kcron-common&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Scogun_kcron-common) ![GitHub](https://img.shields.io/github/license/Scogun/kcron-common?color=blue) ![Publish workflow](https://github.com/Scogun/kcron-common/actions/workflows/publish.yml/badge.svg) [![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.kcron/kcron-common/0.7.3?color=blue)](https://search.maven.org/artifact/com.ucasoft.kcron/kcron-common/0.7.3/jar) +[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Scogun_kcron-common&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Scogun_kcron-common) ![GitHub](https://img.shields.io/github/license/Scogun/kcron-common?color=blue) ![Publish workflow](https://github.com/Scogun/kcron-common/actions/workflows/publish.yml/badge.svg) [![Maven Central with version prefix filter](https://img.shields.io/maven-central/v/com.ucasoft.kcron/kcron-common/0.8.0?color=blue)](https://search.maven.org/artifact/com.ucasoft.kcron/kcron-common/0.8.0/jar) ### Features * Kotlin Multiplatform library @@ -38,7 +38,7 @@ kotlin { sourceSets { commonMain { dependencies { - implementation 'com.ucasoft.kcron:kcron-common:0.7.3' + implementation 'com.ucasoft.kcron:kcron-common:0.8.0' } } } @@ -57,9 +57,12 @@ builder .years(2050) println(builder.expression) // 0/10 5-25 5,12 ? * 7#5 2050 ``` -***Parse Cron expression*** +***Parse as Classic as well as Modern Cron expressions*** ```kotlin -val builder = KCron.parseAndBuild("0/10 5-25 5,12 ? * 7#5 2050", WeekDays.Sunday) +// Auto detect +val builder = KCron.parseAndBuild("0/10 5-25 5,12 ? * 7#5 2050") { + it.firstDayOfWeek = WeekDays.Sunday +} println(builder.nextRunList()) // 10 is a default list size /* Result: [ @@ -74,7 +77,15 @@ println(builder.nextRunList()) // 10 is a default list size 2050-01-29T05:06:20, 2050-01-29T05:06:30 ] -*/ +*/ +// OR Force to parse only Classic expressions +try { + val builder = KCron.parseAndBuild("0/10 5-25 5,12 ? * 7#5 2050") { + it.version = Version.Classic + } +} catch(e: WrongCronExpression) { + println(e.message) // Expression 0/10 5-25 5,12 ? * 7#5 2050 is not Classic Cron one! +} ``` ***Days of week and months can be defined in a parsed expression as numbers as well as short names*** ```kotlin @@ -91,6 +102,6 @@ builder.years(2021..2025) println(builder.expression) // 0/10 5-25 5,12 ? * SUN#5 2021-2025 ``` ### Current status -This library is on beta version `0.7.3`. +This library is on beta version `0.8.0`. However, it will be a part of another cool library. Check the news! diff --git a/build.gradle.kts b/build.gradle.kts index 706cec6..e9cd4b0 100755 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "com.ucasoft.kcron" -version = "0.7.3" +version = "0.8.0" repositories { mavenCentral() @@ -36,6 +36,7 @@ kotlin { dependencies { implementation(kotlin("test-common")) implementation(kotlin("test-annotations-common")) + implementation("io.kotest:kotest-assertions-core:5.8.0") } } val jsTest by getting { diff --git a/src/commonMain/kotlin/com/ucasoft/kcron/KCron.kt b/src/commonMain/kotlin/com/ucasoft/kcron/KCron.kt index 9b37093..dbfd079 100644 --- a/src/commonMain/kotlin/com/ucasoft/kcron/KCron.kt +++ b/src/commonMain/kotlin/com/ucasoft/kcron/KCron.kt @@ -3,14 +3,17 @@ package com.ucasoft.kcron import com.ucasoft.kcron.builders.Builder import com.ucasoft.kcron.common.WeekDays import com.ucasoft.kcron.parsers.Parser +import com.ucasoft.kcron.settings.Settings class KCron { companion object { - fun parseAndBuild(expression: String, firstDayOfWeek: WeekDays = WeekDays.Monday) : Builder { - val parseResult = Parser().parse(expression) - return Builder(firstDayOfWeek).build(parseResult.parts) + fun parseAndBuild(expression: String, block: (Settings) -> Unit = {}) : Builder { + val settings = Settings() + block.invoke(settings) + val parseResult = Parser().parse(expression, settings.version) + return Builder(settings.firstDayOfWeek).build(parseResult.parts) } fun builder(firstDayOfWeek: WeekDays = WeekDays.Monday): Builder { diff --git a/src/commonMain/kotlin/com/ucasoft/kcron/exceptions/WrongCronExpression.kt b/src/commonMain/kotlin/com/ucasoft/kcron/exceptions/WrongCronExpression.kt index e23c054..9deb78d 100644 --- a/src/commonMain/kotlin/com/ucasoft/kcron/exceptions/WrongCronExpression.kt +++ b/src/commonMain/kotlin/com/ucasoft/kcron/exceptions/WrongCronExpression.kt @@ -1,3 +1,5 @@ package com.ucasoft.kcron.exceptions -class WrongCronExpression(expression: String) : Throwable("Expression $expression is not Cron one!") +import com.ucasoft.kcron.settings.Version + +class WrongCronExpression(expression: String, version: Version) : Throwable("Expression $expression is not ${if (version != Version.Auto) "$version " else ""}Cron one!") diff --git a/src/commonMain/kotlin/com/ucasoft/kcron/parsers/Parser.kt b/src/commonMain/kotlin/com/ucasoft/kcron/parsers/Parser.kt index 6296028..2cb1b65 100644 --- a/src/commonMain/kotlin/com/ucasoft/kcron/parsers/Parser.kt +++ b/src/commonMain/kotlin/com/ucasoft/kcron/parsers/Parser.kt @@ -2,6 +2,7 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.* import com.ucasoft.kcron.exceptions.* +import com.ucasoft.kcron.settings.Version class Parser { @@ -28,19 +29,27 @@ class Parser { CombinationRule(CronPart.DaysOfWeek, DayOfWeekGroups.OfMonth, listOf(CombinationRule(CronPart.Days, DayGroups.Any))) ) - fun parse(expression: String): ParseResult { - val expressionParts = splitExpression(expression) + fun parse(expression: String, version: Version = Version.Auto): ParseResult { + val expressionParts = splitExpression(expression, version) val parseResult = parsePartsAndEnsureValid(expressionParts) ensureCombinationRules(parseResult.parts) return parseResult } - private fun splitExpression(expression: String): List { + private fun splitExpression(expression: String, version: Version): List { val expressionParts = expression.split(' ') - if (expressionParts.size != 7) { - throw WrongCronExpression(expression) + if (expressionParts.size == 5 && (version == Version.Auto || version == Version.Classic)) { + return expressionParts.toMutableList().also { + it.add(0, "0") + it.add("*") + } + } + + if (expressionParts.size == 7 && (version == Version.Auto || version == Version.Modern)) { + return expressionParts } - return expressionParts + + throw WrongCronExpression(expression, version) } private fun parsePartsAndEnsureValid(expressionParts: List): ParseResult { diff --git a/src/commonMain/kotlin/com/ucasoft/kcron/settings/Settings.kt b/src/commonMain/kotlin/com/ucasoft/kcron/settings/Settings.kt new file mode 100644 index 0000000..da0de94 --- /dev/null +++ b/src/commonMain/kotlin/com/ucasoft/kcron/settings/Settings.kt @@ -0,0 +1,5 @@ +package com.ucasoft.kcron.settings + +import com.ucasoft.kcron.common.WeekDays + +data class Settings(var firstDayOfWeek: WeekDays = WeekDays.Monday, var version: Version = Version.Auto) \ No newline at end of file diff --git a/src/commonMain/kotlin/com/ucasoft/kcron/settings/Version.kt b/src/commonMain/kotlin/com/ucasoft/kcron/settings/Version.kt new file mode 100644 index 0000000..0d305a1 --- /dev/null +++ b/src/commonMain/kotlin/com/ucasoft/kcron/settings/Version.kt @@ -0,0 +1,10 @@ +package com.ucasoft.kcron.settings + +enum class Version { + + Auto, + + Classic, + + Modern +} \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/BuildAndParseTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/BuildAndParseTests.kt new file mode 100644 index 0000000..8a284f8 --- /dev/null +++ b/src/commonTest/kotlin/com/ucasoft/kcron/BuildAndParseTests.kt @@ -0,0 +1,70 @@ +package com.ucasoft.kcron + +import com.ucasoft.kcron.exceptions.WrongCronExpression +import com.ucasoft.kcron.settings.Version +import io.kotest.assertions.throwables.shouldThrowWithMessage +import io.kotest.matchers.nulls.shouldNotBeNull +import io.kotest.matchers.shouldBe +import kotlinx.datetime.Clock +import kotlinx.datetime.TimeZone +import kotlinx.datetime.toLocalDateTime +import kotlin.test.BeforeTest +import kotlin.test.Test + +class BuildAndParseTests { + + private var currentYear : Int = 2023 + + private val modernCronExpression = "30 * * ? * * 2050" + + @BeforeTest + fun setupOnce() { + currentYear = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).year + } + + @Test + fun parseAndBuildAuto() { + var builder = KCron.parseAndBuild("* 12 ? * *") + builder.expression.shouldBe("0 * 12 ? * * *") + builder.nextRun.shouldNotBeNull() + .year.shouldBe(currentYear) + builder = KCron.parseAndBuild(modernCronExpression) + builder.expression.shouldBe(modernCronExpression) + builder.nextRun.shouldNotBeNull() + .year.shouldBe(2050) + shouldThrowWithMessage("Expression * * * ? * * is not Cron one!") { + KCron.parseAndBuild("* * * ? * *") + } + } + + @Test + fun parseAndBuildClassic() { + val builder = KCron.parseAndBuild("* 12 ? * *") { + it.version = Version.Classic + } + builder.expression.shouldBe("0 * 12 ? * * *") + builder.nextRun.shouldNotBeNull() + .year.shouldBe(currentYear) + shouldThrowWithMessage("Expression * * * ? * * * is not Classic Cron one!") { + KCron.parseAndBuild("* * * ? * * *") { + it.version = Version.Classic + } + } + } + + @Test + fun parseAndBuildModern() { + val builder = KCron.parseAndBuild(modernCronExpression) { + it.version = Version.Modern + } + builder.expression.shouldBe(modernCronExpression) + builder.nextRun.shouldNotBeNull() + .year.shouldBe(2050) + shouldThrowWithMessage("Expression * * ? * * is not Modern Cron one!") { + KCron.parseAndBuild("* * ? * *") { + it.version = Version.Modern + } + } + } + +} \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/BuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/BuilderTests.kt index fa667da..a5e1f5b 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/BuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/BuilderTests.kt @@ -3,122 +3,130 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.DayOfWeekGroups import com.ucasoft.kcron.common.WeekDays import com.ucasoft.kcron.extensions.* +import io.kotest.matchers.collections.shouldBeSingleton +import io.kotest.matchers.collections.shouldHaveElementAt +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.shouldBe import kotlinx.datetime.* import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNull class BuilderTests { @Test fun buildMany() { val builder = Builder() - assertEquals("* * * ? * * *", builder.expression) + builder.expression.shouldBe("* * * ? * * *") builder.days(1) - assertEquals("* * * 1 * ? *", builder.expression) + builder.expression.shouldBe("* * * 1 * ? *") builder.anyDays().years(2099) - assertEquals("* * * ? * * 2099", builder.expression) + builder.expression.shouldBe("* * * ? * * 2099") val expected = LocalDateTime(2099, 1, 1, 0, 0) - assertEquals(expected, builder.nextRun) + builder.nextRun.shouldBe(expected) builder.hours(12).minutes(0).seconds(0) var result = builder.nextRunList(2) - assertEquals(expected.plusHours(12), result[0]) - assertEquals(expected.plusHours(36), result[1]) + result.shouldHaveElementAt(0, expected.plusHours(12)) + result.shouldHaveElementAt(1, expected.plusHours(36)) builder.anyHours().anyMonths().anySeconds().lastDay() - assertEquals(expected.plusDays(30), builder.nextRun) + builder.nextRun.shouldBe(expected.plusDays(30)) builder.months(2).lastWorkDay() - assertEquals(expected.plusDays(57), builder.nextRun) + builder.nextRun.shouldBe(expected.plusDays(57)) builder.daysOfWeek(DayOfWeekGroups.EveryStartingAt, "6/1") result = builder.nextRunList(1) - assertEquals(DayOfWeek.SUNDAY, result[0].dayOfWeek) - assertEquals(expected.plusDays(31), result[0]) + result.shouldBeSingleton { + it.dayOfWeek.shouldBe(DayOfWeek.SUNDAY) + it.shouldBe(expected.plusDays(31)) + } builder.hours(0).minutes(0).seconds(0).daysOfWeek(2, 4, 6) result = builder.nextRunList(3) - assertEquals(expected.plusDays(33), result[0]) - assertEquals(expected.plusDays(35), result[1]) - assertEquals(expected.plusDays(37), result[2]) + result.shouldHaveElementAt(0, expected.plusDays(33)) + result.shouldHaveElementAt(1, expected.plusDays(35)) + result.shouldHaveElementAt(2, expected.plusDays(37)) builder.nearestWorkDay(30) - assertNull(builder.nextRun) + builder.nextRun.shouldBeNull() builder.months(5).nearestWorkDay(31) - assertEquals(expected.plusDays(148), builder.nextRun) + builder.nextRun.shouldBe(expected.plusDays(148)) builder.months(10) - assertEquals(expected.plusDays(302), builder.nextRun) - assertEquals("0 0 0 31W 10 ? 2099", builder.expression) + builder.nextRun.shouldBe(expected.plusDays(302)) + builder.expression.shouldBe("0 0 0 31W 10 ? 2099") builder.seconds(0..50) - assertEquals("0-50 0 0 31W 10 ? 2099", builder.expression) + builder.expression.shouldBe("0-50 0 0 31W 10 ? 2099") builder.seconds(0).minutes(0..25) - assertEquals("0 0-25 0 31W 10 ? 2099", builder.expression) + builder.expression.shouldBe("0 0-25 0 31W 10 ? 2099") builder.minutes(0).hours(0..18) - assertEquals("0 0 0-18 31W 10 ? 2099", builder.expression) + builder.expression.shouldBe("0 0 0-18 31W 10 ? 2099") builder.hours(0).months(1..6) - assertEquals("0 0 0 31W 1-6 ? 2099", builder.expression) - assertEquals(expected.plusDays(29), builder.nextRun) + builder.expression.shouldBe("0 0 0 31W 1-6 ? 2099") + builder.nextRun.shouldBe(expected.plusDays(29)) builder.months(10).seconds(10 at 0) - assertEquals("0/10 0 0 31W 10 ? 2099", builder.expression) + builder.expression.shouldBe("0/10 0 0 31W 10 ? 2099") builder.seconds(0).months(1).daysOfWeek(1 at 6) - assertEquals("0 0 0 ? 1 6/1 2099", builder.expression) + builder.expression.shouldBe("0 0 0 ? 1 6/1 2099") result = builder.nextRunList(2) - assertEquals(expected.plusDays(2), result[0]) - assertEquals(expected.plusDays(3), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(2)) + result.shouldHaveElementAt(1, expected.plusDays(3)) builder.daysOfWeek(3 at WeekDays.Saturday) - assertEquals("0 0 0 ? 1 SAT/3 2099", builder.expression) + builder.expression.shouldBe("0 0 0 ? 1 SAT/3 2099") result = builder.nextRunList(2) - assertEquals(expected.plusDays(2), result[0]) - assertEquals(expected.plusDays(9), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(2)) + result.shouldHaveElementAt(1, expected.plusDays(9)) builder.daysOfWeek(6 on 5) - assertEquals("0 0 0 ? 1 6#5 2099", builder.expression) - assertEquals(expected.plusDays(30), builder.nextRun) + builder.expression.shouldBe("0 0 0 ? 1 6#5 2099") + builder.nextRun.shouldBe(expected.plusDays(30)) } @Test fun buildManyFirstWeekDayIsSunday() { + val expected = LocalDateTime(2099, 1, 1, 0, 0) val builder = Builder(WeekDays.Sunday) builder.years(2099).months(2).daysOfWeek(DayOfWeekGroups.EveryStartingAt, "6/1") var result = builder.nextRunList(1) - assertEquals(DayOfWeek.FRIDAY, result[0].dayOfWeek) - val expected = LocalDateTime(2099, 1, 1, 0, 0) - assertEquals(expected.plusDays(36), result[0]) + result.shouldBeSingleton { + it.dayOfWeek.shouldBe(DayOfWeek.FRIDAY) + it.shouldBe(expected.plusDays(36)) + } builder.hours(0).minutes(0).seconds(0).daysOfWeek(2, 4, 6) result = builder.nextRunList(3) - assertEquals(expected.plusDays(32), result[0]) - assertEquals(expected.plusDays(34), result[1]) - assertEquals(expected.plusDays(36), result[2]) + result.shouldHaveElementAt(0, expected.plusDays(32)) + result.shouldHaveElementAt(1, expected.plusDays(34)) + result.shouldHaveElementAt(2, expected.plusDays(36)) builder.seconds(0).months(1).daysOfWeek(1 at 6) result = builder.nextRunList(2) - assertEquals(expected.plusDays(1), result[0]) - assertEquals(expected.plusDays(2), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(1)) + result.shouldHaveElementAt(1, expected.plusDays(2)) builder.daysOfWeek(3 at WeekDays.Friday) result = builder.nextRunList(2) - assertEquals(expected.plusDays(1), result[0]) - assertEquals(expected.plusDays(8), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(1)) + result.shouldHaveElementAt(1, expected.plusDays(8)) builder.daysOfWeek(WeekDays.Saturday on 5) - assertEquals("0 0 0 ? 1 SAT#5 2099", builder.expression) - assertEquals(expected.plusDays(30), builder.nextRun) + builder.expression.shouldBe("0 0 0 ? 1 SAT#5 2099") + builder.nextRun.shouldBe(expected.plusDays(30)) } @Test fun buildManyFirstWeekDayIsWednesday() { + val expected = LocalDateTime(2099, 1, 1, 0, 0) val builder = Builder(WeekDays.Wednesday) builder.years(2099).months(2).daysOfWeek(DayOfWeekGroups.EveryStartingAt, "6/1") var result = builder.nextRunList(1) - assertEquals(DayOfWeek.MONDAY, result[0].dayOfWeek) - val expected = LocalDateTime(2099, 1, 1, 0, 0) - assertEquals(expected.plusDays(32), result[0]) + result.shouldBeSingleton { + it.dayOfWeek.shouldBe(DayOfWeek.MONDAY) + it.shouldBe(expected.plusDays(32)) + } builder.hours(0).minutes(0).seconds(0).daysOfWeek(2, 4, 6) result = builder.nextRunList(3) - assertEquals(expected.plusDays(32), result[0]) - assertEquals(expected.plusDays(35), result[1]) - assertEquals(expected.plusDays(37), result[2]) + result.shouldHaveElementAt(0, expected.plusDays(32)) + result.shouldHaveElementAt(1, expected.plusDays(35)) + result.shouldHaveElementAt(2, expected.plusDays(37)) builder.seconds(0).months(1).daysOfWeek(1 at 6) result = builder.nextRunList(2) - assertEquals(expected.plusDays(4), result[0]) - assertEquals(expected.plusDays(5), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(4)) + result.shouldHaveElementAt(1, expected.plusDays(5)) builder.daysOfWeek(3 at WeekDays.Monday) result = builder.nextRunList(2) - assertEquals(expected.plusDays(4), result[0]) - assertEquals(expected.plusDays(11), result[1]) + result.shouldHaveElementAt(0, expected.plusDays(4)) + result.shouldHaveElementAt(1, expected.plusDays(11)) builder.daysOfWeek(WeekDays.Thursday on 5) - assertEquals("0 0 0 ? 1 THU#5 2099", builder.expression) - assertEquals(expected.plusDays(28), builder.nextRun) + builder.expression.shouldBe("0 0 0 ? 1 THU#5 2099") + builder.nextRun.shouldBe(expected.plusDays(28)) } } diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysBuilderTests.kt index 225b1ae..35e8312 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysBuilderTests.kt @@ -2,10 +2,12 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.DayGroups import com.ucasoft.kcron.exceptions.UnknownCronPart +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.collections.shouldBeSingleton +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.shouldBe import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith class DaysBuilderTests { @@ -19,20 +21,28 @@ class DaysBuilderTests { @Test fun buildDays() { builder.build(DayGroups.Any, "?") - assertEquals(listOf(1..31).flatten(), builder.days) + builder.days.shouldContainExactly(listOf(1..31).flatten()) builder.build(DayGroups.Specific, "1,5,10,15,20,23,31") - assertEquals(listOf(1, 5, 10, 15, 20, 23, 31), builder.days) + builder.days.shouldContainExactly(1, 5, 10, 15, 20, 23, 31) builder.build(DayGroups.EveryStartingAt, "13/8") - assertEquals(listOf(13, 21, 29), builder.days) + builder.days.shouldContainExactly(13, 21, 29) builder.build(DayGroups.LastDay, "L") - assertEquals(listOf(-32), builder.days) + builder.days.shouldBeSingleton { + it.shouldBe(-32) + } builder.build(DayGroups.LastWeekday, "LW") - assertEquals(listOf(32), builder.days) + builder.days.shouldBeSingleton { + it.shouldBe(32) + } builder.build(DayGroups.BeforeTheEnd, "L-3") - assertEquals(listOf(-3), builder.days) + builder.days.shouldBeSingleton { + it.shouldBe(-3) + } builder.build(DayGroups.NearestWeekday, "25W") - assertEquals(listOf(2500), builder.days) - assertFailsWith(UnknownCronPart::class) { + builder.days.shouldBeSingleton { + it.shouldBe(2500) + } + shouldThrow { builder.build(DayGroups.Unknown, "") } } diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysOfWeekBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysOfWeekBuilderTests.kt index 83a76a3..45b996b 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysOfWeekBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/DaysOfWeekBuilderTests.kt @@ -3,9 +3,11 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.DayOfWeekGroups import com.ucasoft.kcron.common.WeekDays import com.ucasoft.kcron.exceptions.UnknownCronPart +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.collections.shouldBeSingleton +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.shouldBe import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFailsWith class DaysOfWeekBuilderTests { @@ -15,22 +17,24 @@ class DaysOfWeekBuilderTests { fun buildDaysOfWeek() { builder = DaysOfWeekBuilder() builder.build(DayOfWeekGroups.Any, "*") - assertEquals(listOf(1..7).flatten(), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(listOf(1..7).flatten()) builder.build(DayOfWeekGroups.Specific, "MON,TUE,THU,FRI,SUN") - assertEquals(listOf(1, 2, 4, 5, 7), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(1, 2, 4, 5 ,7) builder.build(DayOfWeekGroups.Specific, "1,3,5,7") - assertEquals(listOf(1, 3, 5, 7), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(1, 3, 5 ,7) builder.build(DayOfWeekGroups.EveryStartingAt, "2/2") - assertEquals(listOf(2, 4, 6), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(2, 4, 6) builder.build(DayOfWeekGroups.EveryStartingAt, "MON/3") - assertEquals(listOf(1, 4, 7), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(1, 4, 7) builder.build(DayOfWeekGroups.Last, "2L") - assertEquals(listOf(-2), builder.daysOfWeek) + builder.daysOfWeek.shouldBeSingleton { + it.shouldBe(-2) + } builder.build(DayOfWeekGroups.OfMonth, "1#5") - assertEquals(listOf(10, 50), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(10, 50) builder.build(DayOfWeekGroups.OfMonth, "SAT#5") - assertEquals(listOf(60, 50), builder.daysOfWeek) - assertFailsWith(UnknownCronPart::class) { + builder.daysOfWeek.shouldContainExactly(60, 50) + shouldThrow { builder.build(DayOfWeekGroups.Unknown, "") } } @@ -39,13 +43,13 @@ class DaysOfWeekBuilderTests { fun firstDaySunday() { builder = DaysOfWeekBuilder(WeekDays.Sunday) builder.build(DayOfWeekGroups.Specific, "TUE,THU,FRI,SUN") - assertEquals(listOf(1, 3, 5, 6), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(1, 3, 5, 6) } @Test fun firstDayWednesday() { builder = DaysOfWeekBuilder(WeekDays.Wednesday) builder.build(DayOfWeekGroups.Specific, "MON,TUE,THU,FRI") - assertEquals(listOf(2, 3, 6, 7), builder.daysOfWeek) + builder.daysOfWeek.shouldContainExactly(2, 3, 6, 7) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/HoursBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/HoursBuilderTests.kt index 3b52eb1..6f8a02e 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/HoursBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/HoursBuilderTests.kt @@ -1,9 +1,9 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.TimeGroups +import io.kotest.matchers.collections.shouldContainExactly import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals class HoursBuilderTests { @@ -17,14 +17,14 @@ class HoursBuilderTests { @Test fun buildHours() { builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..23).flatten(), builder.hours) + builder.hours.shouldContainExactly(listOf(0..23).flatten()) builder.build(TimeGroups.Specific, "0,5,10,15,20,23") - assertEquals(listOf(0, 5, 10, 15, 20, 23), builder.hours) + builder.hours.shouldContainExactly(0, 5, 10, 15, 20, 23) builder.build(TimeGroups.EveryStartingAt, "13/8") - assertEquals(listOf(13, 21), builder.hours) + builder.hours.shouldContainExactly(13, 21) builder.build(TimeGroups.EveryBetween, "8-17") - assertEquals(listOf(8..17).flatten(), builder.hours) + builder.hours.shouldContainExactly(listOf(8..17).flatten()) builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..23).flatten(), builder.hours) + builder.hours.shouldContainExactly(listOf(0..23).flatten()) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/MinutesBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/MinutesBuilderTests.kt index 98e16ac..92ff6e1 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/MinutesBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/MinutesBuilderTests.kt @@ -1,9 +1,9 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.TimeGroups +import io.kotest.matchers.collections.shouldContainExactly import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals class MinutesBuilderTests { @@ -17,14 +17,14 @@ class MinutesBuilderTests { @Test fun buildMinutes() { builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..59).flatten(), builder.minutes) + builder.minutes.shouldContainExactly(listOf(0..59).flatten()) builder.build(TimeGroups.Specific, "0,5,10,15,20,30,45,59") - assertEquals(listOf(0, 5, 10, 15, 20, 30, 45, 59), builder.minutes) + builder.minutes.shouldContainExactly(0, 5, 10, 15, 20, 30, 45, 59) builder.build(TimeGroups.EveryStartingAt, "13/17") - assertEquals(listOf(13, 30, 47), builder.minutes) + builder.minutes.shouldContainExactly(13, 30, 47) builder.build(TimeGroups.EveryBetween, "29-37") - assertEquals(listOf(29..37).flatten(), builder.minutes) + builder.minutes.shouldContainExactly(listOf(29..37).flatten()) builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..59).flatten(), builder.minutes) + builder.minutes.shouldContainExactly(listOf(0..59).flatten()) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/MonthsBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/MonthsBuilderTests.kt index b55c400..1c09c2d 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/MonthsBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/MonthsBuilderTests.kt @@ -1,9 +1,9 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.MonthGroups +import io.kotest.matchers.collections.shouldContainExactly import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals class MonthsBuilderTests { @@ -17,16 +17,16 @@ class MonthsBuilderTests { @Test fun buildMonths() { builder.build(MonthGroups.Any, "*") - assertEquals(listOf(1..12).flatten(), builder.months) + builder.months.shouldContainExactly(listOf(1..12).flatten()) builder.build(MonthGroups.Specific, "1,2,6,10,12") - assertEquals(listOf(1, 2, 6, 10, 12), builder.months) + builder.months.shouldContainExactly(1, 2, 6, 10, 12) builder.build(MonthGroups.Specific, "JAN,FEB,JUN,OCT,DEC") - assertEquals(listOf(1, 2, 6, 10, 12), builder.months) + builder.months.shouldContainExactly(1, 2, 6, 10, 12) builder.build(MonthGroups.EveryStartingAt, "3/2") - assertEquals(listOf(3, 5, 7, 9, 11), builder.months) + builder.months.shouldContainExactly(3, 5, 7, 9, 11) builder.build(MonthGroups.EveryBetween, "6-8") - assertEquals(listOf(6, 7, 8), builder.months) + builder.months.shouldContainExactly(6, 7, 8) builder.build(MonthGroups.Any, "*") - assertEquals(listOf(1..12).flatten(), builder.months) + builder.months.shouldContainExactly(listOf(1..12).flatten()) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/SecondsBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/SecondsBuilderTests.kt index 7b25d42..0e1f010 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/SecondsBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/SecondsBuilderTests.kt @@ -1,9 +1,9 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.TimeGroups +import io.kotest.matchers.collections.shouldContainExactly import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals class SecondsBuilderTests { @@ -17,14 +17,14 @@ class SecondsBuilderTests { @Test fun buildSeconds() { builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..59).flatten(), builder.seconds) + builder.seconds.shouldContainExactly(listOf(0..59).flatten()) builder.build(TimeGroups.Specific, "0,5,10,15,20,30,45,59") - assertEquals(listOf(0, 5, 10, 15, 20, 30, 45, 59), builder.seconds) + builder.seconds.shouldContainExactly(0, 5, 10, 15, 20, 30, 45, 59) builder.build(TimeGroups.EveryStartingAt, "13/17") - assertEquals(listOf(13, 30, 47), builder.seconds) + builder.seconds.shouldContainExactly(13, 30, 47) builder.build(TimeGroups.EveryBetween, "29-37") - assertEquals(listOf(29..37).flatten(), builder.seconds) + builder.seconds.shouldContainExactly(listOf(29..37).flatten()) builder.build(TimeGroups.Any, "*") - assertEquals(listOf(0..59).flatten(), builder.seconds) + builder.seconds.shouldContainExactly(listOf(0..59).flatten()) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/builders/YearsBuilderTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/builders/YearsBuilderTests.kt index 45242ec..b3037b7 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/builders/YearsBuilderTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/builders/YearsBuilderTests.kt @@ -1,12 +1,14 @@ package com.ucasoft.kcron.builders import com.ucasoft.kcron.common.YearGroups +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.collections.shouldEndWith +import io.kotest.matchers.collections.shouldHaveElementAt import kotlinx.datetime.Clock import kotlinx.datetime.TimeZone import kotlinx.datetime.toLocalDateTime import kotlin.test.BeforeTest import kotlin.test.Test -import kotlin.test.assertEquals class YearsBuilderTests { @@ -23,16 +25,16 @@ class YearsBuilderTests { @Test fun buildYears() { builder.build(YearGroups.Any, "*") - assertEquals(currentYear, builder.years[0]) - assertEquals(2099, builder.years.last()) + builder.years.shouldHaveElementAt(0, currentYear) + builder.years.shouldEndWith(2099) builder.build(YearGroups.Specific, "2020,2045,2060,2079,2099") - assertEquals(listOf(2020, 2045, 2060, 2079, 2099), builder.years) + builder.years.shouldContainExactly(2020, 2045, 2060, 2079, 2099) builder.build(YearGroups.EveryStartingAt, "2021/35") - assertEquals(listOf(2021, 2056, 2091), builder.years) + builder.years.shouldContainExactly(2021, 2056, 2091) builder.build(YearGroups.EveryBetween, "2021-2025") - assertEquals(listOf(2021, 2022, 2023, 2024, 2025), builder.years) + builder.years.shouldContainExactly(2021, 2022, 2023, 2024, 2025) builder.build(YearGroups.Any, "*") - assertEquals(currentYear, builder.years[0]) - assertEquals(2099, builder.years.last()) + builder.years.shouldHaveElementAt(0, currentYear) + builder.years.shouldEndWith(2099) } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysOfWeekPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysOfWeekPartTests.kt index a3033fb..debe06b 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysOfWeekPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysOfWeekPartTests.kt @@ -1,6 +1,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.DayOfWeekGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class DaysOfWeekPartTests { @@ -64,8 +68,10 @@ class DaysOfWeekPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid, value.value) - assertEquals(value.group, parser.group) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -73,8 +79,8 @@ class DaysOfWeekPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(DayOfWeekGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(DayOfWeekGroups.Unknown) } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysPartTests.kt index c1cd6b6..0767bed 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/DaysPartTests.kt @@ -1,6 +1,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.DayGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class DaysPartTests { @@ -58,8 +62,11 @@ class DaysPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid, value.value) - assertEquals(value.group, parser.group) + parser.parse(value.value) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -67,8 +74,8 @@ class DaysPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(DayGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(DayGroups.Unknown) } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/HoursPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/HoursPartTests.kt index 1e2fbc0..d533107 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/HoursPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/HoursPartTests.kt @@ -1,6 +1,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.TimeGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class HoursPartTests { @@ -46,8 +50,11 @@ class HoursPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid) - assertEquals(value.group, parser.group) + parser.parse(value.value) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -55,8 +62,8 @@ class HoursPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(TimeGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(TimeGroups.Unknown) } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/MonthPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/MonthPartTests.kt index cbb9e92..752ef21 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/MonthPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/MonthPartTests.kt @@ -1,6 +1,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.MonthGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class MonthPartTests { @@ -52,8 +56,11 @@ class MonthPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid, value.value) - assertEquals(value.group, parser.group) + parser.parse(value.value) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -61,8 +68,8 @@ class MonthPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(MonthGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(MonthGroups.Unknown) } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/ParserTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/ParserTests.kt index 6a078c5..b107ce5 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/ParserTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/ParserTests.kt @@ -4,37 +4,71 @@ import com.ucasoft.kcron.exceptions.WrongCronExpression import com.ucasoft.kcron.exceptions.WrongPartCombinations import com.ucasoft.kcron.exceptions.WrongPartExpression import com.ucasoft.kcron.exceptions.WrongPartsExpression +import com.ucasoft.kcron.settings.Version +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.assertions.throwables.shouldThrowWithMessage import kotlin.test.Test -import kotlin.test.assertFailsWith class ParserTests { private val parser = Parser() + private val allAnyClassicCronExpression = "* * ? * *" + + private val allAnyModernCronExpression = "* * * ? * * *" + @Test - fun goodExpressions() { - parser.parse("* * * ? * * *") + fun goodAutoExpressions() { + parser.parse(allAnyModernCronExpression) + parser.parse(allAnyClassicCronExpression) parser.parse("* * * 29 * ? *") - parser.parse("* * * ? * 1L *") - parser.parse("* * * ? * MON/1 *") + parser.parse("* * ? * ?") + } + + @Test + fun goodClassicExpressions() { + parser.parse(allAnyClassicCronExpression, Version.Classic) + parser.parse("* * 29 * ?", Version.Classic) + parser.parse("* * ? * 1L", Version.Classic) + parser.parse("* * ? * MON/1", Version.Classic) + } + + @Test + fun goodModernExpressions() { + parser.parse(allAnyModernCronExpression, Version.Modern) + parser.parse("* * * 29 * ? *", Version.Modern) + parser.parse("* * * ? * 1L *", Version.Modern) + parser.parse("* * * ? * MON/1 *", Version.Modern) + } + + @Test + fun badAutoExpressions() { + shouldThrowWithMessage("Expression * * * is not Cron one!") { parser.parse("* * *") } + shouldThrow { parser.parse("* * * ? ? * * *") } + } + + @Test + fun badClassicExpressions() { + shouldThrow { parser.parse("* * ? * * *", Version.Classic) } + shouldThrowWithMessage("Expression $allAnyModernCronExpression is not Classic Cron one!") { parser.parse(allAnyModernCronExpression, Version.Classic) } } @Test - fun badExpressions() { - assertFailsWith { parser.parse("* * *") } - assertFailsWith { parser.parse("* * * ? ? * * *") } + fun badModernExpressions() { + shouldThrow { parser.parse(allAnyClassicCronExpression, Version.Modern) } + shouldThrowWithMessage("Expression * * * ? ? * * * is not Modern Cron one!") { parser.parse("* * * ? ? * * *", Version.Modern) } } @Test fun badExpressionParts() { - assertFailsWith{ parser.parse("62 * * ? * * *") } - assertFailsWith { parser.parse("* * * ? * MON#6 *") } - assertFailsWith { parser.parse("62 * 25 ? * * *") } + shouldThrow{ parser.parse("62 * * ? * * *") } + shouldThrow { parser.parse("* * * ? * MON#6 *") } + shouldThrow { parser.parse("62 * 25 ? * * *") } } @Test fun badCombinationParts() { - assertFailsWith { parser.parse("* * * 1/31 * 1/1 *") } - assertFailsWith { parser.parse("* * * 31/31 * SUN *") } + shouldThrow { parser.parse("* * * 1/31 * 1/1 *") } + shouldThrow { parser.parse("* * * 31/31 * SUN *") } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/SecondsPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/SecondsPartTests.kt index ceb31d9..8af9729 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/SecondsPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/SecondsPartTests.kt @@ -2,6 +2,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.CronPart import com.ucasoft.kcron.common.TimeGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class SecondsPartTests { @@ -47,8 +51,11 @@ class SecondsPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid, value.value) - assertEquals(value.group, parser.group) + parser.parse(value.value) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -56,8 +63,8 @@ class SecondsPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(TimeGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(TimeGroups.Unknown) } } } \ No newline at end of file diff --git a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/YearPartTests.kt b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/YearPartTests.kt index 5729640..a773057 100644 --- a/src/commonTest/kotlin/com/ucasoft/kcron/parsers/YearPartTests.kt +++ b/src/commonTest/kotlin/com/ucasoft/kcron/parsers/YearPartTests.kt @@ -1,6 +1,10 @@ package com.ucasoft.kcron.parsers import com.ucasoft.kcron.common.YearGroups +import io.kotest.assertions.withClue +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +import io.kotest.matchers.shouldBe import kotlin.test.* class YearPartTests { @@ -46,8 +50,11 @@ class YearPartTests { fun allowedValues() { for (value in allowedValues) { parser.parse(value.value) - assertTrue(parser.isValid, value.value) - assertEquals(value.group, parser.group) + parser.parse(value.value) + withClue(value.value) { + parser.isValid.shouldBeTrue() + } + parser.group.shouldBe(value.group) } } @@ -55,8 +62,8 @@ class YearPartTests { fun deniedValues() { for (value in deniedValues) { parser.parse(value) - assertFalse(parser.isValid) - assertEquals(YearGroups.Unknown, parser.group) + parser.isValid.shouldBeFalse() + parser.group.shouldBe(YearGroups.Unknown) } } } \ No newline at end of file