Skip to content

Commit

Permalink
Merge pull request #556 from robstoll/#481-ChronoLocalDateTime-as-string
Browse files Browse the repository at this point in the history
#481 ChronoLocalDateTime as string
  • Loading branch information
robstoll committed Aug 19, 2020
2 parents cf330c8 + 9bb53ee commit 12a7a24
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ch.tutteli.atrium.api.fluent.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.*
import java.time.LocalDateTime
import java.time.chrono.ChronoLocalDate
import java.time.chrono.ChronoLocalDateTime

Expand Down Expand Up @@ -74,3 +75,103 @@ fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfterOrEqual(
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isEqual(
expected: ChronoLocalDateTime<*>
): Expect<T> = _logicAppend { isEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a
* date without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isBefore(
expected: String
): Expect<T> = _logicAppend { isBefore(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a
* date without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isBeforeOrEqual(
expected: String
): Expect<T> = _logicAppend { isBeforeOrEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfter(
expected: String
): Expect<T> = _logicAppend { isAfter(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfterOrEqual(
expected: String
): Expect<T> = _logicAppend { isAfterOrEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isEqual(
expected: String
): Expect<T> = _logicAppend { isEqual(expected) }
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import java.time.chrono.ChronoLocalDateTime

class ChronoLocalDateTimeAssertionSpec : Spek({
include(ChronoLocalDateTimeSpec)
include(ChronoLocalDateTimeAsStringSpec)
}) {
object ChronoLocalDateTimeSpec : ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAssertionSpec(
fun1(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1(Expect<ChronoLocalDateTime<*>>::isEqual)
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isEqual)
)

companion object {
fun isBefore(
expect: Expect<ChronoLocalDateTime<*>>,
expected: ChronoLocalDateTime<*>
): Expect<ChronoLocalDateTime<*>> =
//TODO #481 turn into string in ISO format
expect.isBefore(expected)
}

object ChronoLocalDateTimeAsStringSpec :
ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAsStringAssertionSpec(
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isEqual)
)

@Suppress("unused", "UNUSED_VALUE")
private fun ambiguityTest() {
Expand Down Expand Up @@ -85,5 +85,29 @@ class ChronoLocalDateTimeAssertionSpec : Spek({
a4 = a4.isAfter(chronoLocalDateTime)
a4 = a4.isAfterOrEqual(chronoLocalDateTime)
a4 = a4.isEqual(chronoLocalDateTime)

a1 = a1.isBefore("also not ambiguous if string is passed")
a1 = a1.isBeforeOrEqual("also not ambiguous if string is passed")
a1 = a1.isAfter("also not ambiguous if string is passed")
a1 = a1.isAfterOrEqual("also not ambiguous if string is passed")
a1 = a1.isEqual("also not ambiguous if string is passed")

a2 = a2.isBefore("also not ambiguous if string is passed")
a2 = a2.isBeforeOrEqual("also not ambiguous if string is passed")
a2 = a2.isAfter("also not ambiguous if string is passed")
a2 = a2.isAfterOrEqual("also not ambiguous if string is passed")
a2 = a2.isEqual("also not ambiguous if string is passed")

a3 = a3.isBefore("also not ambiguous if string is passed")
a3 = a3.isBeforeOrEqual("also not ambiguous if string is passed")
a3 = a3.isAfter("also not ambiguous if string is passed")
a3 = a3.isAfterOrEqual("also not ambiguous if string is passed")
a3 = a3.isEqual("also not ambiguous if string is passed")

a4 = a4.isBefore("also not ambiguous if string is passed")
a4 = a4.isBeforeOrEqual("also not ambiguous if string is passed")
a4 = a4.isAfter("also not ambiguous if string is passed")
a4 = a4.isAfterOrEqual("also not ambiguous if string is passed")
a4 = a4.isEqual("also not ambiguous if string is passed")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package ch.tutteli.atrium.api.infix.en_GB

import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.*
import java.time.LocalDateTime
import java.time.chrono.ChronoLocalDate
import java.time.chrono.ChronoLocalDateTime

Expand Down Expand Up @@ -74,3 +75,103 @@ infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfterOrEqua
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isEqual(
expected: ChronoLocalDateTime<*>
): Expect<T> = _logicAppend { isEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a
* date without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isBefore(
expected: String
): Expect<T> = _logicAppend { isBefore(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a
* date without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isBeforeOrEqual(
expected: String
): Expect<T> = _logicAppend { isBeforeOrEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfter(
expected: String
): Expect<T> = _logicAppend { isAfter(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isAfterOrEqual(
expected: String
): Expect<T> = _logicAppend { isAfterOrEqual(expected) }

/**
* Expects that the subject of the assertion (a [ChronoLocalDateTime])
* is before the [expected] [LocalDateTime] given as [String] in (modified) ISO 8601 format.
* The string will be converted to a LocalDateTime according to ISO 8601 but with a slight deviation.
* The alternative notation (e.g. 20200401120001 instead of 2020-04-01T12:00:01) is not supported and we accept a date
* without time in which case 00:00:00 is used. Following the supported formats from the longest to the shortest:
* yyyy-mm-ddThh:mm:ss.sss (up to 9 digits for nanoseconds)
* yyyy-mm-ddThh:mm:ss
* yyyy-mm-ddThh:mm
* yyyy-mm-dd
*
* @return An [Expect] for the current subject of the assertion.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*
* @since 0.13.0
*/
infix fun <T : ChronoLocalDateTime<out ChronoLocalDate>> Expect<T>.isEqual(
expected: String
): Expect<T> = _logicAppend { isEqual(expected) }
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,33 @@ import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.specs.fun1
import ch.tutteli.atrium.specs.notImplemented
import ch.tutteli.atrium.specs.testutils.WithAsciiReporter
import org.spekframework.spek2.Spek
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.chrono.ChronoLocalDate
import java.time.chrono.ChronoLocalDateTime

class ChronoLocalDateTimeAssertionSpec : ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAssertionSpec(
fun1(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1(Expect<ChronoLocalDateTime<*>>::isEqual)
) {
class ChronoLocalDateTimeAssertionSpec : Spek({
include(ChronoLocalDateTimeSpec)
include(ChronoLocalDateTimeAsStringSpec)
}) {
object ChronoLocalDateTimeSpec : ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAssertionSpec(
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1<ChronoLocalDateTime<*>, ChronoLocalDateTime<*>>(Expect<ChronoLocalDateTime<*>>::isEqual)
)

object ChronoLocalDateTimeAsStringSpec :
ch.tutteli.atrium.specs.integration.ChronoLocalDateTimeAsStringAssertionSpec(
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isBefore),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isBeforeOrEqual),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isAfter),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isAfterOrEqual),
fun1<ChronoLocalDateTime<*>, String>(Expect<ChronoLocalDateTime<*>>::isEqual)
)

companion object : WithAsciiReporter()

@Suppress("unused", "UNUSED_VALUE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isAfter
fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isAfterOrEqual(expected: ChronoLocalDateTime<*>): Assertion = _chronoLocalDateTimeImpl.isAfterOrEqual(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isEqual(expected: ChronoLocalDateTime<*>): Assertion = _chronoLocalDateTimeImpl.isEqual(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isBefore(expected: String): Assertion = _chronoLocalDateTimeImpl.isBefore(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isBeforeOrEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isBeforeOrEqual(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isAfter(expected: String): Assertion = _chronoLocalDateTimeImpl.isAfter(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isAfterOrEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isAfterOrEqual(this, expected)

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> AssertionContainer<T>.isEqual(expected: String): Assertion = _chronoLocalDateTimeImpl.isEqual(this, expected)

Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,30 @@ interface ChronoLocalDateTimeAssertions {
container: AssertionContainer<T>,
expected: ChronoLocalDateTime<*>
): Assertion

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> isBefore(
container: AssertionContainer<T>,
expected: String
): Assertion

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> isBeforeOrEqual(
container: AssertionContainer<T>,
expected: String
): Assertion

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> isAfter(
container: AssertionContainer<T>,
expected: String
): Assertion

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> isAfterOrEqual(
container: AssertionContainer<T>,
expected: String
): Assertion

fun <T : ChronoLocalDateTime<out ChronoLocalDate>> isEqual(
container: AssertionContainer<T>,
expected: String
): Assertion

}
Loading

0 comments on commit 12a7a24

Please sign in to comment.