From def1fc9a99e48101b034623a1b44262549469f0e Mon Sep 17 00:00:00 2001 From: rkiselev <67838927+rkiselev@users.noreply.github.com> Date: Mon, 13 Jul 2020 12:01:31 +0300 Subject: [PATCH] #481 accept date and time as string in ISO 8601 format for ChronoLocalDateTime [WIP] (#528) --- .../en_GB/ChronoLocalDateTimeAssertionSpec.kt | 12 ++++++ .../atrium/logic/chronoLocalDateTime.kt | 11 +++++ .../logic/ChronoLocalDateTimeAssertions.kt | 26 ++++++++++++ .../DefaultChronoLocalDateTimeAssertions.kt | 38 ++++++++++++++++- .../creating/chronoLocalDateTimeAssertions.kt | 41 +++++++++++++++++++ .../ChronoLocalDateTimeAssertionsImpl.kt | 2 + 6 files changed, 128 insertions(+), 2 deletions(-) diff --git a/apis/fluent-en_GB/atrium-api-fluent-en_GB-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/ChronoLocalDateTimeAssertionSpec.kt b/apis/fluent-en_GB/atrium-api-fluent-en_GB-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/ChronoLocalDateTimeAssertionSpec.kt index 7c03d78b2d..15598d539b 100644 --- a/apis/fluent-en_GB/atrium-api-fluent-en_GB-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/ChronoLocalDateTimeAssertionSpec.kt +++ b/apis/fluent-en_GB/atrium-api-fluent-en_GB-jvm/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/ChronoLocalDateTimeAssertionSpec.kt @@ -8,9 +8,11 @@ import java.time.LocalDate import java.time.LocalDateTime import java.time.chrono.ChronoLocalDate import java.time.chrono.ChronoLocalDateTime +import java.time.format.DateTimeFormatter class ChronoLocalDateTimeAssertionSpec : Spek({ include(ChronoLocalDateTimeSpec) + include(StringSpec) }) { object ChronoLocalDateTimeSpec : ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAssertionSpec( fun1(Expect>::isBefore), @@ -20,12 +22,22 @@ class ChronoLocalDateTimeAssertionSpec : Spek({ fun1(Expect>::isEqual) ) + object StringSpec : ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAssertionSpec( + fun1(Companion::isBefore), + //TODO #481 introduce more companions + fun1(Expect>::isBeforeOrEqual), + fun1(Expect>::isAfter), + fun1(Expect>::isAfterOrEqual), + fun1(Expect>::isEqual) + ) + companion object { fun isBefore( expect: Expect>, expected: ChronoLocalDateTime<*> ): Expect> = //TODO #481 turn into string in ISO format + //expect.isBefore(expected.format(DateTimeFormatter.ISO_DATE_TIME)) expect.isBefore(expected) } diff --git a/logic/atrium-logic-jvm/src/generated/kotlin/ch/tutteli/atrium/logic/chronoLocalDateTime.kt b/logic/atrium-logic-jvm/src/generated/kotlin/ch/tutteli/atrium/logic/chronoLocalDateTime.kt index d9a21e83b2..9661d71bc0 100644 --- a/logic/atrium-logic-jvm/src/generated/kotlin/ch/tutteli/atrium/logic/chronoLocalDateTime.kt +++ b/logic/atrium-logic-jvm/src/generated/kotlin/ch/tutteli/atrium/logic/chronoLocalDateTime.kt @@ -25,3 +25,14 @@ fun > AssertionContainer.isAfter fun > AssertionContainer.isAfterOrEqual(expected: ChronoLocalDateTime<*>): Assertion = _chronoLocalDateTimeImpl.isAfterOrEqual(this, expected) fun > AssertionContainer.isEqual(expected: ChronoLocalDateTime<*>): Assertion = _chronoLocalDateTimeImpl.isEqual(this, expected) + +fun > AssertionContainer.isBefore(expected: String): Assertion = _chronoLocalDateTimeImpl.isBefore(this, expected) + +fun > AssertionContainer.isBeforeOrEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isBeforeOrEqual(this, expected) + +fun > AssertionContainer.isAfter(expected: String): Assertion = _chronoLocalDateTimeImpl.isAfter(this, expected) + +fun > AssertionContainer.isAfterOrEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isAfterOrEqual(this, expected) + +fun > AssertionContainer.isEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isEqual(this, expected) + diff --git a/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/ChronoLocalDateTimeAssertions.kt b/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/ChronoLocalDateTimeAssertions.kt index bfb72b7ca9..9b6c86fd0c 100644 --- a/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/ChronoLocalDateTimeAssertions.kt +++ b/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/ChronoLocalDateTimeAssertions.kt @@ -38,4 +38,30 @@ interface ChronoLocalDateTimeAssertions { container: AssertionContainer, expected: ChronoLocalDateTime<*> ): Assertion + + fun > isBefore( + container: AssertionContainer, + expected: String + ): Assertion + + fun > isBeforeOrEqual( + container: AssertionContainer, + expected: String + ): Assertion + + fun > isAfter( + container: AssertionContainer, + expected: String + ): Assertion + + fun > isAfterOrEqual( + container: AssertionContainer, + expected: String + ): Assertion + + fun > isEqual( + container: AssertionContainer, + expected: String + ): Assertion + } diff --git a/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/impl/DefaultChronoLocalDateTimeAssertions.kt b/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/impl/DefaultChronoLocalDateTimeAssertions.kt index a0139ae675..66350bd63c 100644 --- a/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/impl/DefaultChronoLocalDateTimeAssertions.kt +++ b/logic/atrium-logic-jvm/src/main/kotlin/ch/tutteli/atrium/logic/impl/DefaultChronoLocalDateTimeAssertions.kt @@ -7,9 +7,10 @@ package ch.tutteli.atrium.logic.impl import ch.tutteli.atrium.assertions.Assertion import ch.tutteli.atrium.creating.AssertionContainer -import ch.tutteli.atrium.logic.ChronoLocalDateTimeAssertions -import ch.tutteli.atrium.logic.createDescriptiveAssertion +import ch.tutteli.atrium.logic.* import ch.tutteli.atrium.translations.DescriptionDateTimeLikeAssertion.* +import java.time.LocalDate +import java.time.LocalDateTime import java.time.chrono.ChronoLocalDate import java.time.chrono.ChronoLocalDateTime @@ -19,6 +20,11 @@ class DefaultChronoLocalDateTimeAssertions : ChronoLocalDateTimeAssertions { expected: ChronoLocalDateTime<*> ): Assertion = container.createDescriptiveAssertion(IS_BEFORE, expected) { it.isBefore(expected) } + override fun > isBefore( + container: AssertionContainer, + expected: String + ): Assertion = container.isBefore(stringToLocalDateTime(expected)) + override fun > isBeforeOrEqual( container: AssertionContainer, expected: ChronoLocalDateTime<*> @@ -26,11 +32,21 @@ class DefaultChronoLocalDateTimeAssertions : ChronoLocalDateTimeAssertions { it.isBefore(expected) || it.isEqual(expected) } + override fun > isBeforeOrEqual( + container: AssertionContainer, + expected: String + ): Assertion = container.isBeforeOrEqual(stringToLocalDateTime(expected)) + override fun > isAfter( container: AssertionContainer, expected: ChronoLocalDateTime<*> ): Assertion = container.createDescriptiveAssertion(IS_AFTER, expected) { it.isAfter(expected) } + override fun > isAfter( + container: AssertionContainer, + expected: String + ): Assertion = container.isAfter(stringToLocalDateTime(expected)) + override fun > isAfterOrEqual( container: AssertionContainer, expected: ChronoLocalDateTime<*> @@ -38,10 +54,28 @@ class DefaultChronoLocalDateTimeAssertions : ChronoLocalDateTimeAssertions { it.isAfter(expected) || it.isEqual(expected) } + override fun > isAfterOrEqual( + container: AssertionContainer, + expected: String + ): Assertion = container.isAfterOrEqual(stringToLocalDateTime(expected)) + override fun > isEqual( container: AssertionContainer, expected: ChronoLocalDateTime<*> ): Assertion = container.createDescriptiveAssertion(IS_EQUAL_TO, expected) { it.isEqual(expected) } + + override fun > isEqual( + container: AssertionContainer, + expected: String + ): Assertion = container.isEqual(stringToLocalDateTime(expected)) + + private fun stringToLocalDateTime(data: String): LocalDateTime { + return if (data.contains("T")) { + LocalDateTime.parse(data) + } else { + LocalDate.parse(data).atStartOfDay() + } + } } diff --git a/misc/deprecated/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/chronoLocalDateTimeAssertions.kt b/misc/deprecated/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/chronoLocalDateTimeAssertions.kt index ae533e377f..c22ae18038 100644 --- a/misc/deprecated/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/chronoLocalDateTimeAssertions.kt +++ b/misc/deprecated/domain/robstoll-lib/atrium-domain-robstoll-lib-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/lib/creating/chronoLocalDateTimeAssertions.kt @@ -11,6 +11,8 @@ import ch.tutteli.atrium.assertions.Assertion import ch.tutteli.atrium.assertions.builders.assertionBuilder import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.translations.DescriptionDateTimeLikeAssertion.* +import java.time.LocalDate +import java.time.LocalDateTime import java.time.chrono.ChronoLocalDate import java.time.chrono.ChronoLocalDateTime @@ -19,6 +21,9 @@ import java.time.chrono.ChronoLocalDateTime fun > _isBefore(expect: Expect, expected: ChronoLocalDateTime<*>): Assertion = assertionBuilder.createDescriptive(expect, IS_BEFORE, expected) { it.isBefore(expected) } +fun > _isBefore(expect: Expect, expected: String): Assertion = + assertionBuilder.createDescriptive(expect, IS_BEFORE, expected) { it.isBefore(stringToLocalDateTime(expected)) } + @Suppress("DeprecatedCallableAddReplaceWith") @Deprecated("use the function from atrium-logic instead, will be removed with 1.0.0") fun > _isBeforeOrEquals( @@ -29,6 +34,13 @@ fun > _isBeforeOrEquals( it.isBefore(expected) || it.isEqual(expected) } +fun > _isBeforeOrEquals( + expect: Expect, + expected: String +): Assertion = assertionBuilder.createDescriptive(expect, IS_BEFORE_OR_EQUAL, expected) { + it.isBefore(stringToLocalDateTime(expected)) || it.isEqual(stringToLocalDateTime(expected)) + } + @Suppress("DeprecatedCallableAddReplaceWith") @Deprecated("use the function from atrium-logic instead, will be removed with 1.0.0") fun > _isAfter( @@ -38,6 +50,13 @@ fun > _isAfter( @Suppress("DeprecatedCallableAddReplaceWith") @Deprecated("use the function from atrium-logic instead, will be removed with 1.0.0") +fun > _isAfter( + expect: Expect, + expected: String +): Assertion = assertionBuilder.createDescriptive(expect, IS_AFTER, expected) { + it.isAfter(stringToLocalDateTime(expected)) +} + fun > _isAfterOrEquals( expect: Expect, expected: ChronoLocalDateTime<*> @@ -47,9 +66,31 @@ fun > _isAfterOrEquals( @Suppress("DeprecatedCallableAddReplaceWith") @Deprecated("use the function from atrium-logic instead, will be removed with 1.0.0") +fun > _isAfterOrEquals( + expect: Expect, + expected: String +): Assertion = assertionBuilder.createDescriptive(expect, IS_AFTER_OR_EQUAL, expected) { + it.isAfter(stringToLocalDateTime(expected)) || it.isEqual(stringToLocalDateTime(expected)) +} + fun > _isEqual( expect: Expect, expected: ChronoLocalDateTime<*> ): Assertion = assertionBuilder.createDescriptive(expect, IS_EQUAL_TO, expected) { it.isEqual(expected) } + +fun > _isEqual( + expect: Expect, + expected: String +): Assertion = assertionBuilder.createDescriptive(expect, IS_EQUAL_TO, expected) { + it.isEqual(stringToLocalDateTime(expected)) +} + +private fun stringToLocalDateTime(data: String): LocalDateTime { + return if (data.contains("T")) { + LocalDateTime.parse(data) + } else { + LocalDate.parse(data).atStartOfDay() + } +} diff --git a/misc/deprecated/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/ChronoLocalDateTimeAssertionsImpl.kt b/misc/deprecated/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/ChronoLocalDateTimeAssertionsImpl.kt index 32a58acc85..cf6dbdf207 100644 --- a/misc/deprecated/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/ChronoLocalDateTimeAssertionsImpl.kt +++ b/misc/deprecated/domain/robstoll/atrium-domain-robstoll-jvm/src/main/kotlin/ch/tutteli/atrium/domain/robstoll/creating/ChronoLocalDateTimeAssertionsImpl.kt @@ -11,6 +11,8 @@ import ch.tutteli.atrium.assertions.Assertion import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.domain.creating.ChronoLocalDateTimeAssertions import ch.tutteli.atrium.domain.robstoll.lib.creating.* +import java.time.LocalDate +import java.time.LocalDateTime import java.time.chrono.ChronoLocalDate import java.time.chrono.ChronoLocalDateTime