From 7514f448446ade8d9ecf495ef1ad84b42aa522d3 Mon Sep 17 00:00:00 2001 From: Robert Stoll Date: Sat, 29 Feb 2020 23:08:06 +0100 Subject: [PATCH] transform List.get and Map.getExisting use fun instead of param obj moreover: - move param objects to own package so that they are not automatically available when one adds an `import ch.tutteli.atrium.api.infix.*` statement - simplify MapAssertionsSpec by introducing mfun2 which covers most types (also in fluent-en_GB) --- .../api/fluent/en_GB/Fun0AssertionsSpec.kt | 17 +- .../fluent/en_GB/IterableAllAssertionsSpec.kt | 3 +- .../api/fluent/en_GB/MapAssertionsSpec.kt | 61 +++---- .../infix/en_GB/creating/feature/Feature.kt | 18 ++ .../creating/feature/FeatureWithCreator.kt | 25 +++ .../feature/MetaFeatureOptionWithCreator.kt | 22 +++ .../en_GB/creating/list/IndexWithCreator.kt | 14 ++ .../creating/list/get/builders/ListGetStep.kt | 40 ----- .../list/get/builders/impl/ListGetStepImpl.kt | 14 -- .../en_GB/creating/map/KeyWithCreator.kt | 14 ++ .../creating/map/get/builders/MapGetOption.kt | 47 ----- .../map/get/builders/impl/MapGetOptionImpl.kt | 14 -- .../api/infix/en_GB/featureAssertions.kt | 80 +++------ .../atrium/api/infix/en_GB/listAssertions.kt | 23 ++- .../atrium/api/infix/en_GB/mapAssertions.kt | 25 ++- .../api/infix/en_GB/parameterObjects.kt | 11 +- .../api/infix/en_GB/AnyAssertionsSpec.kt | 2 +- .../infix/en_GB/CollectionAssertionsSpec.kt | 6 +- .../infix/en_GB/IterableAllAssertionsSpec.kt | 21 +-- .../infix/en_GB/ListFeatureAssertionsSpec.kt | 33 ++-- .../api/infix/en_GB/MapAssertionsSpec.kt | 168 ++++++++++-------- .../infix/en_GB/MapFeatureAssertionsSpec.kt | 41 +++-- .../domain/builders/utils/VarArgHelper.kt | 2 +- 23 files changed, 328 insertions(+), 373 deletions(-) create mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/Feature.kt create mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/FeatureWithCreator.kt create mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/MetaFeatureOptionWithCreator.kt create mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/IndexWithCreator.kt delete mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/ListGetStep.kt delete mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/impl/ListGetStepImpl.kt create mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/KeyWithCreator.kt delete mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/MapGetOption.kt delete mode 100644 apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/impl/MapGetOptionImpl.kt diff --git a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/Fun0AssertionsSpec.kt b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/Fun0AssertionsSpec.kt index 504be460c..6ec6d3c08 100644 --- a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/Fun0AssertionsSpec.kt +++ b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/Fun0AssertionsSpec.kt @@ -4,21 +4,16 @@ import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.specs.feature0 import ch.tutteli.atrium.specs.feature1 import ch.tutteli.atrium.specs.notImplemented +import ch.tutteli.atrium.specs.withFeatureSuffix class Fun0AssertionsSpec : ch.tutteli.atrium.specs.integration.Fun0AssertionsSpec( - "toThrow" to Companion::toThrowFeature, - "toThrow" to Companion::toThrow, + ("toThrow" to ::toThrowFeature).withFeatureSuffix(), + "toThrow" to ::toThrow, feature0<() -> Int, Int>(Expect<() -> Int>::notToThrow), feature1<() -> Int, Expect.() -> Unit, Int>(Expect<() -> Int>::notToThrow), "⚬ ", "» " ) { - companion object { - fun toThrowFeature(expect: Expect Any?>) = expect.toThrow() - fun toThrow(expect: Expect Any?>, assertionCreator: Expect.() -> Unit) = - expect.toThrow { assertionCreator() } - } - @Suppress("unused", "UNUSED_VALUE", "UNUSED_VARIABLE") private fun ambiguityTest() { val a1: Expect<() -> Any?> = notImplemented() @@ -37,3 +32,9 @@ class Fun0AssertionsSpec : ch.tutteli.atrium.specs.integration.Fun0AssertionsSpe val r8: Expect = a2.notToThrow {} } } + +private fun toThrowFeature(expect: Expect Any?>) = + expect.toThrow() + +private fun toThrow(expect: Expect Any?>, assertionCreator: Expect.() -> Unit) = + expect.toThrow { assertionCreator() } diff --git a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/IterableAllAssertionsSpec.kt b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/IterableAllAssertionsSpec.kt index 760bb7016..46c1c7620 100644 --- a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/IterableAllAssertionsSpec.kt +++ b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/IterableAllAssertionsSpec.kt @@ -3,10 +3,11 @@ package ch.tutteli.atrium.api.fluent.en_GB import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.specs.fun1 import ch.tutteli.atrium.specs.notImplemented +import ch.tutteli.atrium.specs.withNullableSuffix object IterableAllAssertionsSpec : ch.tutteli.atrium.specs.integration.IterableAllAssertionsSpec( fun1(Expect>::all), - fun1(Expect>::all), + fun1(Expect>::all).withNullableSuffix(), "◆ ", "❗❗ ", "⚬ ", "» ", "▶ ", "◾ " ) { @Suppress("unused", "UNUSED_VALUE") diff --git a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/MapAssertionsSpec.kt b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/MapAssertionsSpec.kt index 867eda698..702158296 100644 --- a/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/MapAssertionsSpec.kt +++ b/apis/fluent-en_GB/atrium-api-fluent-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/fluent/en_GB/MapAssertionsSpec.kt @@ -2,46 +2,26 @@ package ch.tutteli.atrium.api.fluent.en_GB import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.domain.builders.utils.mapArguments -import ch.tutteli.atrium.specs.fun0 -import ch.tutteli.atrium.specs.fun1 -import ch.tutteli.atrium.specs.fun2 -import ch.tutteli.atrium.specs.notImplemented +import ch.tutteli.atrium.specs.* +import kotlin.jvm.JvmName import kotlin.reflect.KFunction3 +private fun mfun2( + f: KFunction3>, Pair, Array>, Expect>> +) = fun2(f) + class MapAssertionsSpec : ch.tutteli.atrium.specs.integration.MapAssertionsSpec( - fun2, Pair, Array>>(Expect>::contains), - fun2, Pair, Array>>(Expect>::contains), - "${containsKeyWithValueAssertionsFun.name} ${KeyValue::class.simpleName}" to Companion::containsKeyValue, - "${containsKeyWithNullableValueAssertionsFun.name} ${KeyValue::class.simpleName}" to Companion::containsNullable, + mfun2(Expect>::contains), + mfun2(Expect>::contains).withNullableSuffix(), + mfun2.() -> Unit>(::contains).adjustName { "$it ${KeyValue::class.simpleName}" }, + mfun2.() -> Unit)?>(::contains).adjustName { "$it ${KeyValue::class.simpleName}" }.withNullableSuffix(), fun1(Expect>::containsKey), - fun1(Expect>::containsKey), + fun1(Expect>::containsKey).withNullableSuffix(), fun1(Expect>::containsNotKey), - fun1(Expect>::containsNotKey), + fun1(Expect>::containsNotKey).withNullableSuffix(), fun0(Expect>::isEmpty), fun0(Expect>::isNotEmpty) ) { - companion object { - //@formatter:off - private val containsKeyWithValueAssertionsFun : KFunction3>, KeyValue, Array>, Expect>> = Expect>::contains - private val containsKeyWithNullableValueAssertionsFun : KFunction3>, KeyValue, Array>, Expect>> = Expect>::contains - //@formatter:on - - fun containsKeyValue( - expect: Expect>, - keyValue: Pair.() -> Unit>, - otherKeyValues: Array.() -> Unit>> - ) = mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> - expect.contains(first, *others) - } - - fun containsNullable( - expect: Expect>, - keyValue: Pair.() -> Unit)?>, - otherKeyValues: Array.() -> Unit)?>> - ) = mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> - expect.contains(first, *others) - } - } @Suppress("unused", "UNUSED_VALUE") private fun ambiguityTest() { @@ -208,3 +188,20 @@ class MapAssertionsSpec : ch.tutteli.atrium.specs.integration.MapAssertionsSpec( } } + +private fun contains( + expect: Expect>, + keyValue: Pair.() -> Unit>, + otherKeyValues: Array.() -> Unit>> +) = mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> + expect.contains(first, *others) +} + +@JvmName("containsNullable") +private fun contains( + expect: Expect>, + keyValue: Pair.() -> Unit)?>, + otherKeyValues: Array.() -> Unit)?>> +) = mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> + expect.contains(first, *others) +} diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/Feature.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/Feature.kt new file mode 100644 index 000000000..caa8a9d4f --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/Feature.kt @@ -0,0 +1,18 @@ +package ch.tutteli.atrium.api.infix.en_GB.creating.feature + +import kotlin.reflect.KProperty1 + +/** + * Parameter object which contains a [description] of a feature along with an [extractor] + * which actually extracts the feature out of a subject of an assertion. + * + * Use `of(K..., ...) { ... }` to create this representation where the first argument is the extractor in form of a + * [KProperty1] or a `KFunctionX` and the remaining arguments are the required arguments in case of a `KFunctionX` + * where `X` > 1. + * + * @property description The description of the feature. + * @property extractor The extractor which extracts the feature out of the subject of the assertion. + + * @since 0.10.0 + */ +data class Feature(val description: String, val extractor: (T) -> R) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/FeatureWithCreator.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/FeatureWithCreator.kt new file mode 100644 index 000000000..3aeddb1ce --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/FeatureWithCreator.kt @@ -0,0 +1,25 @@ +package ch.tutteli.atrium.api.infix.en_GB.creating.feature + +import ch.tutteli.atrium.creating.Expect +import kotlin.reflect.KProperty1 + +/** + * Parameter object which contains a [description] of a feature along with an [extractor] + * which actually extracts the feature out of a subject of an assertion + an [assertionCreator] + * which defines assertions for the feature. + * + * Use `of(K..., ...) { ... }` to create this representation where the first argument is the extractor in form of a + * [KProperty1] or a `KFunctionX`, the last an [assertionCreator]-lambda and the remaining arguments in-between the + * required arguments in case of a `KFunctionX` where `X` > 1. + * + * @property description The description of the feature. + * @property extractor The extractor which extracts the feature out of the subject of the assertion. + * @property assertionCreator The `assertionCreator`-lambda which defines assertions for the feature. + * + * @since 0.10.0 + */ +data class FeatureWithCreator( + val description: String, + val extractor: (T) -> R, + val assertionCreator: Expect.() -> Unit +) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/MetaFeatureOptionWithCreator.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/MetaFeatureOptionWithCreator.kt new file mode 100644 index 000000000..4b8097530 --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/feature/MetaFeatureOptionWithCreator.kt @@ -0,0 +1,22 @@ +package ch.tutteli.atrium.api.infix.en_GB.creating.feature + +import ch.tutteli.atrium.creating.Expect +import ch.tutteli.atrium.domain.builders.creating.MetaFeatureOption +import ch.tutteli.atrium.domain.creating.MetaFeature + +/** + * Parameter object which combines a lambda with a [MetaFeatureOption] receiver (called [provider]) + * and an [assertionCreator]. + * + * Use the function `of({ ... }) { ... }` to create this representation where the first + * argument is a lambda with a [MetaFeatureOption] as receiver which has to create a [MetaFeature] + * where the subject of the assertion is available via implicit parameter `it`. + * Usually you use [f][MetaFeatureOption.f] to create a [MetaFeature], + * e.g. `feature of({ f(it::size) }) { o toBe 3 }` + * + * @since 0.10.0 + */ +data class MetaFeatureOptionWithCreator( + val provider: MetaFeatureOption.(T) -> MetaFeature, + val assertionCreator: Expect.() -> Unit +) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/IndexWithCreator.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/IndexWithCreator.kt new file mode 100644 index 000000000..efb18670a --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/IndexWithCreator.kt @@ -0,0 +1,14 @@ +package ch.tutteli.atrium.api.infix.en_GB.creating.list + +import ch.tutteli.atrium.creating.Expect + + +/** + * Parameter object which combines an [index] of type [Int] with an [assertionCreator] which defines assertions for + * a resulting feature of type [E]. + * + * Use the function `index(Int) { ... }` to create this representation. + * + * @since 0.10.0 + */ +data class IndexWithCreator(val index: Int, val assertionCreator: Expect.() -> Unit) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/ListGetStep.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/ListGetStep.kt deleted file mode 100644 index ffc070a8b..000000000 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/ListGetStep.kt +++ /dev/null @@ -1,40 +0,0 @@ -package ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders - -import ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders.impl.ListGetStepImpl -import ch.tutteli.atrium.creating.Expect - -/** - * Represents the extension point for another step after a `get index`-step within a - * sophisticated `get` assertion building process for [List]. - * - * @param E The element type of the [List]. - * @param T A subtype of [List]. - */ -interface ListGetStep> { - /** - * The [Expect] for which this assertion is created - */ - val expect: Expect - - /** - * The given index which will be used to perform the [List.get]. - */ - val index: Int - - /** - * Makes the assertion that the given [index] is within the bounds of [Expect.subject] and that - * the corresponding entry holds all assertions the given [assertionCreator] creates for it. - * - * @return An [Expect] for the current subject of the assertion. - * @throws AssertionError Might throw an [AssertionError] if a created [Expect]s (by calling [assertionCreator]) - * does not hold. - * @throws IllegalArgumentException in case the given [assertionCreator] did not create a single assertion. - */ - infix fun assertIt(assertionCreator: Expect.() -> Unit): Expect - - companion object { - fun > create(expect: Expect, index: Int): ListGetStep = - ListGetStepImpl(expect, index) - } -} - diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/impl/ListGetStepImpl.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/impl/ListGetStepImpl.kt deleted file mode 100644 index a8f7a637d..000000000 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/list/get/builders/impl/ListGetStepImpl.kt +++ /dev/null @@ -1,14 +0,0 @@ -package ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders.impl - -import ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders.ListGetStep -import ch.tutteli.atrium.creating.Expect -import ch.tutteli.atrium.domain.builders.ExpectImpl - -internal class ListGetStepImpl>( - override val expect: Expect, - override val index: Int -) : ListGetStep { - - override infix fun assertIt(assertionCreator: Expect.() -> Unit): Expect = - ExpectImpl.list.get(expect, index).addToInitial(assertionCreator) -} diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/KeyWithCreator.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/KeyWithCreator.kt new file mode 100644 index 000000000..853472d2d --- /dev/null +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/KeyWithCreator.kt @@ -0,0 +1,14 @@ +package ch.tutteli.atrium.api.infix.en_GB.creating.map + +import ch.tutteli.atrium.creating.Expect + +/** + * Parameter object which combines an [key] of type [K] with an [assertionCreator] which defines assertions for + * a resulting feature of type [V]. + * + * Use the function `key(...) { ... }` to create this representation where the first parameter corresponds + * to the [key] and the second is the [assertionCreator] + * + * @since 0.10.0 + */ +data class KeyWithCreator(val key: K, val assertionCreator: Expect.() -> Unit) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/MapGetOption.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/MapGetOption.kt deleted file mode 100644 index 179fb375b..000000000 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/MapGetOption.kt +++ /dev/null @@ -1,47 +0,0 @@ -package ch.tutteli.atrium.api.infix.en_GB.creating.map.get.builders - -import ch.tutteli.atrium.api.infix.en_GB.creating.map.get.builders.impl.MapGetOptionImpl -import ch.tutteli.atrium.assertions.Assertion -import ch.tutteli.atrium.creating.AssertionPlant -import ch.tutteli.atrium.creating.Expect -import ch.tutteli.atrium.creating.SubjectProvider - -/** - * Represents the extension point for another option after a `get key`-step within a - * sophisticated `get` assertion building process for [Map]. - * - * @param K The key type of the [Map]. - * @param V the value type of the [Map]. - * @param T A subtype of [Map]. - */ -interface MapGetOption> { - /** - * The [AssertionPlant] for which this assertion is created - */ - val expect: Expect - - /** - * The given key which will be used to perform the [Map.get]. - */ - val key: K - - /** - * Makes the assertion that the [Assert.subject][SubjectProvider.subject] contains the previously specified [key] and that the - * corresponding value holds all assertions the given [assertionCreator] might create for it. - * - * @return This expect to support a fluent API. - * @throws AssertionError Might throw an [AssertionError] if a created [Assertion]s (by calling [assertionCreator]) - * does not hold. - * @throws IllegalArgumentException in case the given [assertionCreator] did not create a single assertion. - */ - infix fun assertIt(assertionCreator: Expect.() -> Unit): Expect - - companion object { - /** - * Creates a [MapGetOption] based on the given [expect] and [key]. - */ - fun > create(expect: Expect, key: K): MapGetOption = - MapGetOptionImpl(expect, key) - } -} - diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/impl/MapGetOptionImpl.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/impl/MapGetOptionImpl.kt deleted file mode 100644 index ae34f177f..000000000 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/creating/map/get/builders/impl/MapGetOptionImpl.kt +++ /dev/null @@ -1,14 +0,0 @@ -package ch.tutteli.atrium.api.infix.en_GB.creating.map.get.builders.impl - -import ch.tutteli.atrium.api.infix.en_GB.creating.map.get.builders.MapGetOption -import ch.tutteli.atrium.creating.Expect -import ch.tutteli.atrium.domain.builders.ExpectImpl - -internal class MapGetOptionImpl>( - override val expect: Expect, - override val key: K -) : MapGetOption { - - override infix fun assertIt(assertionCreator: Expect.() -> Unit): Expect = - expect.addAssertion(ExpectImpl.map.getExisting(expect, key).collect(assertionCreator)) -} diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/featureAssertions.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/featureAssertions.kt index 5c0c7abb3..dfe813e4d 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/featureAssertions.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/featureAssertions.kt @@ -1,5 +1,8 @@ package ch.tutteli.atrium.api.infix.en_GB +import ch.tutteli.atrium.api.infix.en_GB.creating.feature.Feature +import ch.tutteli.atrium.api.infix.en_GB.creating.feature.FeatureWithCreator +import ch.tutteli.atrium.api.infix.en_GB.creating.feature.MetaFeatureOptionWithCreator import ch.tutteli.atrium.assertions.AssertionGroup import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.creating.FeatureExpect @@ -41,8 +44,11 @@ infix fun Expect.feature(f: KFunction1): FeatureExpect = * creates a new [Expect] for it and * returns it so that subsequent calls are based on the feature. * - * Use `of(K..., ...)` to create this representation where the first argument is the extractor in form of a - * [KProperty1] or a `KFunctionX` and potentially the required arguments for a `KFunctionX` where `X` > 1. + * Use `of(K..., ...)` to create a [Feature] where the first argument is the extractor in form of a + * [KProperty1] or a `KFunctionX` and potentially the required arguments for a `KFunctionX` where `X` > 1. + * + * @param of Use `of(K..., ...)` to create a [Feature] where the first argument is the extractor in form of a + * [KProperty1] or a `KFunctionX` and potentially the required arguments for a `KFunctionX` where `X` > 1. * * @return The newly created [Expect] for the extracted feature. * @@ -57,9 +63,13 @@ infix fun Expect.feature(of: Feature): FeatureExpect = * applies an assertion group based on the given [FeatureWithCreator.assertionCreator] for the feature and * returns the initial [Expect] with the current subject. * - * Use `of(K..., ...) { ... }` to create this representation where the first argument is the extractor in form of a - * [KProperty1] or a `KFunctionX`, the last an `assertionCreator`-lambda and the remaining arguments in-between the - * required arguments in case of a `KFunctionX` where `X` > 1. + * Use `of(K..., ...) { ... }` to create a [FeatureWithCreator] where the first argument is the extractor in + * form of a [KProperty1] or a `KFunctionX`, the last an `assertionCreator`-lambda and the remaining arguments + * in-between the required arguments in case of a `KFunctionX` where `X` > 1. + * + * @param of Use `of(K..., ...) { ... }` to create a [FeatureWithCreator] where the first argument is the extractor in + * form of a [KProperty1] or a `KFunctionX`, the last an `assertionCreator`-lambda and the remaining arguments + * in-between the required arguments in case of a `KFunctionX` where `X` > 1. * * @return An [Expect] for the current subject of the assertion. * @throws AssertionError Might throw an [AssertionError] in case the created [AssertionGroup] does not hold. @@ -138,43 +148,6 @@ infix fun Expect.feature(of: MetaFeatureOptionWithCreator): Expe fun MetaFeatureOption.f(description: String, provider: R): MetaFeature = MetaFeature(description, provider) -/** - * Parameter object which contains a [description] of a feature along with an [extractor] - * which actually extracts the feature out of a subject of an assertion. - * - * Use `of(K..., ...) { ... }` to create this representation where the first argument is the extractor in form of a - * [KProperty1] or a `KFunctionX` and the remaining arguments are the required arguments in case of a `KFunctionX` - * where `X` > 1. - * - * @property description The description of the feature. - * @property extractor The extractor which extracts the feature out of the subject of the assertion. - - * @since 0.10.0 - */ -data class Feature(val description: String, val extractor: (T) -> R) - -/** - * Parameter object which contains a [description] of a feature along with an [extractor] - * which actually extracts the feature out of a subject of an assertion + an [assertionCreator] - * which defines assertions for the feature. - * - * Use `of(K..., ...) { ... }` to create this representation where the first argument is the extractor in form of a - * [KProperty1] or a `KFunctionX`, the last an [assertionCreator]-lambda and the remaining arguments in-between the - * required arguments in case of a `KFunctionX` where `X` > 1. - * - * @property description The description of the feature. - * @property extractor The extractor which extracts the feature out of the subject of the assertion. - * @property assertionCreator The `assertionCreator`-lambda which defines assertions for the feature. - * - * @since 0.10.0 - */ -data class FeatureWithCreator( - val description: String, - val extractor: (T) -> R, - val assertionCreator: Expect.() -> Unit -) - - //@formatter:off /** * Helper function to create a [Feature] based on a [KFunction2] + arguments. @@ -244,23 +217,6 @@ fun of(f: KFunction5, a1: A1, a2: A //@formatter:on -/** - * Parameter object which combines a lambda with a [MetaFeatureOption] receiver (called [provider]) - * and an [assertionCreator]. - * - * Use the function `of({ ... }) { ... }` to create this representation where the first - * argument is a lambda with a [MetaFeatureOption] as receiver which has to create a [MetaFeature] - * where the subject of the assertion is available via implicit parameter `it`. - * Usually you use [f][MetaFeatureOption.f] to create a [MetaFeature], - * e.g. `feature of({ f(it::size) }) { o toBe 3 }` - * - * @since 0.10.0 - */ -data class MetaFeatureOptionWithCreator( - val provider: MetaFeatureOption.(T) -> MetaFeature, - val assertionCreator: Expect.() -> Unit -) - /** * Helper function to create a [MetaFeatureOptionWithCreator] based on a lambda with * [MetaFeatureOption] receiver (has to return a [MetaFeature]) and an [assertionCreator]. @@ -268,4 +224,8 @@ data class MetaFeatureOptionWithCreator( fun of( provider: MetaFeatureOption.(T) -> MetaFeature, assertionCreator: Expect.() -> Unit -): MetaFeatureOptionWithCreator = MetaFeatureOptionWithCreator(provider, assertionCreator) +): MetaFeatureOptionWithCreator = + MetaFeatureOptionWithCreator( + provider, + assertionCreator + ) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/listAssertions.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/listAssertions.kt index 01344b2d9..946d6e502 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/listAssertions.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/listAssertions.kt @@ -1,6 +1,6 @@ package ch.tutteli.atrium.api.infix.en_GB -import ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders.ListGetStep +import ch.tutteli.atrium.api.infix.en_GB.creating.list.IndexWithCreator import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.domain.builders.ExpectImpl @@ -9,15 +9,26 @@ import ch.tutteli.atrium.domain.builders.ExpectImpl * returns an [Expect] for the element at that position. * * @return The newly created [Expect] for the element at position [index]. - * @throws AssertionError if the given [index] is out of bound. + * @throws AssertionError Might throw an [AssertionError] if the given [index] is out of bound. */ infix fun > Expect.get(index: Int): Expect = ExpectImpl.list.get(this, index).getExpectOfFeature() /** - * Prepares the assertion about the return value of calling [get][List.get] with the given [index]. + * Expects that the given [index][IndexWithCreator.index] is within the bounds of the subject of the assertion + * (a [List]) and that the element at that position holds all assertions the given + * [IndexWithCreator.assertionCreator] creates for it. * - * @return A fluent builder to finish the assertion. + * Use the function `index(Int) { ... }` to create an [IndexWithCreator]. + * + * @return This assertion container to support a fluent API. + * @throws AssertionError Might throw an [AssertionError] if the given [index] is out of bound or + * if the assertion made is not correct. */ -infix fun > Expect.get(index: Index): ListGetStep = - ListGetStep.create(this, index.index) +infix fun > Expect.get(index: IndexWithCreator): Expect = + ExpectImpl.list.get(this, index.index).addToInitial(index.assertionCreator) + +/** + * Helper function to create an [IndexWithCreator] based on the given [index] and [assertionCreator]. + */ +fun index(index: Int, assertionCreator: Expect.() -> Unit) = IndexWithCreator(index, assertionCreator) diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/mapAssertions.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/mapAssertions.kt index 48f30c173..072a7c081 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/mapAssertions.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/mapAssertions.kt @@ -1,6 +1,7 @@ package ch.tutteli.atrium.api.infix.en_GB -import ch.tutteli.atrium.api.infix.en_GB.creating.map.get.builders.MapGetOption +import ch.tutteli.atrium.api.infix.en_GB.creating.map.KeyWithCreator +import ch.tutteli.atrium.assertions.Assertion import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.domain.builders.ExpectImpl @@ -87,12 +88,23 @@ infix fun > Expect.getExisting(key: K): Expect = ExpectImpl.map.getExisting(this, key).getExpectOfFeature() /** - * Prepares the assertion about the return value of calling [get][Map.get] with the given [key]. + * Expects that the subject of the assertion (a [Map]) contains the given [key] and that + * the corresponding value holds all assertions the given [KeyWithCreator.assertionCreator] creates for it. * - * @return A fluent builder to finish the assertion. - * */ -infix fun > Expect.getExisting(key: Key): MapGetOption = - MapGetOption.create(this, key.key) + * @param key Use the function `key(...) { ... }` to create a [KeyWithCreator] where the first parameter corresponds + * to the key and the second is the `assertionCreator`-lambda + * + * @return An [Expect] for the current subject of the assertion. + * @throws AssertionError Might throw an [AssertionError] the given [key] does not exist or + * if the assertion made is not correct. + */ +infix fun > Expect.getExisting(key: KeyWithCreator): Expect = + ExpectImpl.map.getExisting(this, key.key).addToInitial(key.assertionCreator) + +/** + * Helper function to create an [KeyWithCreator] based on the given [key] and [assertionCreator]. + */ +fun key(key: K, assertionCreator: Expect.() -> Unit) = KeyWithCreator(key, assertionCreator) /** @@ -178,3 +190,4 @@ fun > Expect.asEntries(): Expect> infix fun > Expect.asEntries( assertionCreator: Expect>>.() -> Unit ): Expect = apply { asEntries().addAssertionsCreatedBy(assertionCreator) } + diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/parameterObjects.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/parameterObjects.kt index bcd7cef2b..aeb3f4cde 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/parameterObjects.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/main/kotlin/ch/tutteli/atrium/api/infix/en_GB/parameterObjects.kt @@ -9,14 +9,7 @@ import ch.tutteli.atrium.domain.builders.utils.VarArgHelper class All(override val expected: T, override vararg val otherExpected: T) : VarArgHelper /** - * Wrapper for a single index -- can be used as distinguishable type for an overload where Int is already in use. - */ -data class Index(val index: Int) - -data class Key(val key: K) - -/** - * Parameter object to express a key/value [Pair] whose value type is a lambda with an + * Parameter object to express a key/value [Pair] whose value type is a nullable lambda with an * [Expect] receiver, which means one can either pass a lambda or `null`. */ data class KeyValue(val key: K, val valueAssertionCreatorOrNull: (Expect.() -> Unit)?) { @@ -25,8 +18,6 @@ data class KeyValue(val key: K, val valueAssertionCreatorOrNull: "KeyValue(key=$key, value=${if (valueAssertionCreatorOrNull == null) "null" else "lambda"})" } - - /** * Parameter object to express `Pair, vararg Pair`. */ diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/AnyAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/AnyAssertionsSpec.kt index b96d9c2d6..54338cc49 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/AnyAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/AnyAssertionsSpec.kt @@ -81,7 +81,7 @@ class AnyAssertionsSpec : ch.tutteli.atrium.specs.integration.AnyAssertionsSpec( //regression for #298, should compile without the need for E : Any or List @Suppress("unused") - fun Expect>.firstIs(value: E) = o get Index(0) assertIt { o toBe value } + fun Expect>.firstIs(value: E) = o get index(0) { o toBe value } } private fun toBeNull(expect: Expect) = expect toBe null diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/CollectionAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/CollectionAssertionsSpec.kt index a630924a9..ea4802747 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/CollectionAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/CollectionAssertionsSpec.kt @@ -6,7 +6,7 @@ import ch.tutteli.atrium.specs.testutils.WithAsciiReporter class CollectionAssertionsSpec : ch.tutteli.atrium.specs.integration.CollectionAssertionsSpec( "toBe ${Empty::class.simpleName}" to ::isEmpty, - "toBe ${Empty::class.simpleName}" to ::isNotEmpty + "notToBe ${Empty::class.simpleName}" to ::isNotEmpty ) { companion object : WithAsciiReporter() @@ -28,5 +28,5 @@ class CollectionAssertionsSpec : ch.tutteli.atrium.specs.integration.CollectionA } } -fun isEmpty(expect: Expect>) = expect toBe Empty -fun isNotEmpty(expect: Expect>) = expect notToBe Empty +private fun isEmpty(expect: Expect>) = expect toBe Empty +private fun isNotEmpty(expect: Expect>) = expect notToBe Empty diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/IterableAllAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/IterableAllAssertionsSpec.kt index 606753783..4fcbc4b91 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/IterableAllAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/IterableAllAssertionsSpec.kt @@ -1,15 +1,14 @@ package ch.tutteli.atrium.api.infix.en_GB -import ch.tutteli.atrium.specs.testutils.WithAsciiReporter import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.specs.fun1 -import ch.tutteli.atrium.specs.name import ch.tutteli.atrium.specs.notImplemented +import ch.tutteli.atrium.specs.testutils.WithAsciiReporter import ch.tutteli.atrium.specs.withNullableSuffix class IterableAllAssertionsSpec : ch.tutteli.atrium.specs.integration.IterableAllAssertionsSpec( - fun1(Expect>::all).name to ::all, - fun1(Expect>::all).withNullableSuffix().name to ::allNullable, + fun1(Expect>::all), + fun1(Expect>::all).withNullableSuffix(), "* ", "(!) ", "- ", "» ", ">> ", "=> " ) { companion object : WithAsciiReporter() @@ -21,17 +20,11 @@ class IterableAllAssertionsSpec : ch.tutteli.atrium.specs.integration.IterableAl var star: Expect> = notImplemented() - a1 = a1.all {} + a1 = a1 all {} - a1b = a1b.all {} - a1b = a1b.all(null) + a1b = a1b all {} + a1b = a1b all null - star = star.all {} + star = star all {} } } - -private fun all(expect: Expect>, assertionCreator: Expect.() -> Unit) = - expect all assertionCreator - -private fun allNullable(expect: Expect>, assertionCreator: (Expect.() -> Unit)?) = - expect all assertionCreator diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/ListFeatureAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/ListFeatureAssertionsSpec.kt index f4fd936c2..4441ce7bc 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/ListFeatureAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/ListFeatureAssertionsSpec.kt @@ -1,18 +1,18 @@ package ch.tutteli.atrium.api.infix.en_GB -import ch.tutteli.atrium.api.infix.en_GB.creating.list.get.builders.ListGetStep -import ch.tutteli.atrium.specs.testutils.WithAsciiReporter import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.specs.feature1 +import ch.tutteli.atrium.specs.fun2 import ch.tutteli.atrium.specs.notImplemented +import ch.tutteli.atrium.specs.testutils.WithAsciiReporter import ch.tutteli.atrium.specs.withNullableSuffix -import kotlin.reflect.KFunction2 +import kotlin.jvm.JvmName class ListFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.ListFeatureAssertionsSpec( feature1, Int, Int>(Expect>::get), - getIndexPair(), + fun2, Int, Expect.() -> Unit>(::get), feature1, Int, Int?>(Expect>::get).withNullableSuffix(), - getIndexNullablePair() + fun2, Int, Expect.() -> Unit>(::get).withNullableSuffix() ) { companion object : WithAsciiReporter() @@ -24,29 +24,22 @@ class ListFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.ListFeatur var star: Expect> = notImplemented() a1 get 1 - a1 = a1 get Index(1) assertIt { } + a1 = a1 get index(1) { } a1b get 1 - a1b = a1b get Index(1) assertIt { } + a1b = a1b get index(1) { } star get 1 - star = star get Index(1) assertIt { } + star = star get index(1) { } } } -private val getIndexFun: KFunction2>, Index, ListGetStep>> = Expect>::get -private fun getIndexPair() = getIndexFun.name to ::getIndex +private fun get(expect: Expect>, index: Int, assertionCreator: Expect.() -> Unit) = + expect get index(index) { assertionCreator() } -private fun getIndex(expect: Expect>, index: Int, assertionCreator: Expect.() -> Unit) = - expect get Index(index) assertIt { assertionCreator() } - -private val getIndexNullableFun: KFunction2>, Index, ListGetStep>> = - Expect>::get - -private fun getIndexNullablePair() = getIndexNullableFun.name to ::getIndexNullable - -private fun getIndexNullable( +@JvmName("getNullable") +private fun get( expect: Expect>, index: Int, assertionCreator: Expect.() -> Unit -) = expect get Index(index) assertIt { assertionCreator() } +) = expect get index(index) { assertionCreator() } diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapAssertionsSpec.kt index 14ec68fc6..ff1091d90 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapAssertionsSpec.kt @@ -3,86 +3,27 @@ package ch.tutteli.atrium.api.infix.en_GB import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.domain.builders.utils.mapArguments import ch.tutteli.atrium.specs.* +import ch.tutteli.atrium.specs.testutils.WithAsciiReporter +import kotlin.jvm.JvmName +import kotlin.reflect.KFunction3 + +private fun mfun2( + f: KFunction3>, Pair, Array>, Expect>> +) = fun2(f) class MapAssertionsSpec : ch.tutteli.atrium.specs.integration.MapAssertionsSpec( - fun2(Companion::contains), - fun2(Companion::contains).name to Companion::containsNullable, - "${fun2(Companion::contains).name} ${KeyValue::class.simpleName}" to Companion::containsKeyWithValueAssertions, - "${fun2(Companion::contains).name} ${KeyValue::class.simpleName}" to Companion::containsKeyWithNullableValueAssertions, - fun1(Companion::containsKey), - fun1(Companion::containsNullableKey), - fun1(Companion::containsNotKey), - fun1(Companion::containsNotNullableKey), - /* string toBe, notToBe to avoid ambiguity error */ - "toBe ${Empty::class.simpleName}" to Companion::isEmpty, - "notToBe ${Empty::class.simpleName}" to Companion::isNotEmpty + mfun2(::contains), + mfun2(::contains).withNullableSuffix(), + mfun2.() -> Unit>(::contains).adjustName { "$it ${KeyValue::class.simpleName}" }, + mfun2.() -> Unit)?>(::contains).adjustName { "$it ${KeyValue::class.simpleName}" }.withNullableSuffix(), + fun1, String>(::containsKey), + fun1, String?>(::containsKey).withNullableSuffix(), + fun1, String>(::containsNotKey), + fun1, String?>(::containsNotKey).withNullableSuffix(), + "toBe ${Empty::class.simpleName}" to ::isEmpty, + "notToBe ${Empty::class.simpleName}" to ::isNotEmpty ) { - companion object { - private fun contains( - expect: Expect>, - pair: Pair, - otherPairs: Array> - ): Expect> { - return if (otherPairs.isEmpty()) { - expect contains (pair.first to pair.second) - } else { - expect contains Pairs(pair, *otherPairs) - } - } - - private fun containsNullable( - expect: Expect>, - pair: Pair, - otherPairs: Array> - ): Expect> { - return if (otherPairs.isEmpty()) { - expect contains (pair.first to pair.second) - } else { - expect contains Pairs(pair, *otherPairs) - } - } - - private fun containsKeyWithValueAssertions( - expect: Expect>, - keyValue: Pair.() -> Unit>, - otherKeyValues: Array.() -> Unit>> - ): Expect> { - return if (otherKeyValues.isEmpty()) { - expect contains KeyValue(keyValue.first, keyValue.second) - } else { - mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> - expect contains All(first, *others) - } - } - } - - private fun containsKeyWithNullableValueAssertions( - expect: Expect>, - keyValue: Pair.() -> Unit)?>, - otherKeyValues: Array.() -> Unit)?>> - ): Expect> { - return if (otherKeyValues.isEmpty()) { - expect contains KeyValue(keyValue.first, keyValue.second) - } else { - mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> - expect contains All(first, *others) - } - } - } - - private fun containsKey(expect: Expect>, key: String) = expect containsKey key - - private fun containsNullableKey(expect: Expect>, key: String?) = expect containsKey key - - private fun containsNotKey(expect: Expect>, key: String) = expect containsNotKey key - - private fun containsNotNullableKey(expect: Expect>, key: String?) = - expect containsNotKey key - - private fun isEmpty(expect: Expect>) = expect toBe Empty - - private fun isNotEmpty(expect: Expect>) = expect notToBe Empty - } + companion object : WithAsciiReporter() @Suppress("unused", "UNUSED_VALUE") private fun ambiguityTest() { @@ -249,3 +190,76 @@ class MapAssertionsSpec : ch.tutteli.atrium.specs.integration.MapAssertionsSpec( } } + +private fun contains( + expect: Expect>, + pair: Pair, + otherPairs: Array> +): Expect> { + return if (otherPairs.isEmpty()) { + expect contains (pair.first to pair.second) + } else { + expect contains Pairs(pair, *otherPairs) + } +} + +@JvmName("containsNullable") +private fun contains( + expect: Expect>, + pair: Pair, + otherPairs: Array> +): Expect> { + return if (otherPairs.isEmpty()) { + expect contains (pair.first to pair.second) + } else { + expect contains Pairs(pair, *otherPairs) + } +} + +@JvmName("containsKeyWithValueAssertions") +private fun contains( + expect: Expect>, + keyValue: Pair.() -> Unit>, + otherKeyValues: Array.() -> Unit>> +): Expect> { + return if (otherKeyValues.isEmpty()) { + expect contains KeyValue(keyValue.first, keyValue.second) + } else { + mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> + expect contains All(first, *others) + } + } +} + +@JvmName("containsKeyWithNullableValueAssertions") +private fun contains( + expect: Expect>, + keyValue: Pair.() -> Unit)?>, + otherKeyValues: Array.() -> Unit)?>> +): Expect> { + return if (otherKeyValues.isEmpty()) { + expect contains KeyValue(keyValue.first, keyValue.second) + } else { + mapArguments(keyValue, otherKeyValues).to { KeyValue(it.first, it.second) }.let { (first, others) -> + expect contains All(first, *others) + } + } +} + +private fun containsKey(expect: Expect>, key: String) = + expect containsKey key + +@JvmName("containsKeyNullable") +private fun containsKey(expect: Expect>, key: String?) = + expect containsKey key + +private fun containsNotKey(expect: Expect>, key: String) = + expect containsNotKey key + +@JvmName("containsNotKeyNullable") +private fun containsNotKey(expect: Expect>, key: String?) = + expect containsNotKey key + +private fun isEmpty(expect: Expect>) = expect toBe Empty + +private fun isNotEmpty(expect: Expect>) = expect notToBe Empty diff --git a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapFeatureAssertionsSpec.kt b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapFeatureAssertionsSpec.kt index 20ef9dbf2..4e3532f5b 100644 --- a/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapFeatureAssertionsSpec.kt +++ b/apis/infix-en_GB/atrium-api-infix-en_GB-common/src/test/kotlin/ch/tutteli/atrium/api/infix/en_GB/MapFeatureAssertionsSpec.kt @@ -2,6 +2,8 @@ package ch.tutteli.atrium.api.infix.en_GB import ch.tutteli.atrium.creating.Expect import ch.tutteli.atrium.specs.* +import ch.tutteli.atrium.specs.testutils.WithAsciiReporter +import kotlin.jvm.JvmName class MapFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.MapFeatureAssertionsSpec( property, Set>(Expect>::keys), @@ -9,23 +11,11 @@ class MapFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.MapFeatureA property, Collection>(Expect>::values), fun1, Expect>.() -> Unit>(Expect>::values), feature1, String, Int>(Expect>::getExisting), - fun2, String, Expect.() -> Unit>(Companion::getExisting), + fun2, String, Expect.() -> Unit>(::getExisting), feature1, String?, Int?>(Expect>::getExisting).withNullableSuffix(), - fun2(Companion::getExisting).name to Companion::getExistingNullable + fun2, String?, Expect.() -> Unit>(::getExisting).withNullableSuffix() ) { - companion object { - private fun getExisting( - expect: Expect>, - key: String, - assertionCreator: Expect.() -> Unit - ): Expect> = expect getExisting Key(key) assertIt { assertionCreator() } - - private fun getExistingNullable( - expect: Expect>, - key: String?, - assertionCreator: Expect.() -> Unit - ): Expect> = expect getExisting Key(key) assertIt { assertionCreator() } - } + companion object : WithAsciiReporter() @Suppress("unused", "UNUSED_VALUE") private fun ambiguityTest() { @@ -40,9 +30,22 @@ class MapFeatureAssertionsSpec : ch.tutteli.atrium.specs.integration.MapFeatureA a3 getExisting null as String? star getExisting "a" - a1 = a1 getExisting Key("a") assertIt { } - a2 = a2 getExisting Key(1) assertIt { } - a3 = a3 getExisting Key(null) assertIt { } - star = star getExisting Key("a") assertIt { } + a1 = a1 getExisting key("a") { } + a2 = a2 getExisting key(1) { } + a3 = a3 getExisting key(null) { } + star = star getExisting key("a") { } } } + +private fun getExisting( + expect: Expect>, + key: String, + assertionCreator: Expect.() -> Unit +): Expect> = expect getExisting key(key) { assertionCreator() } + +@JvmName("getExistingNullable") +private fun getExisting( + expect: Expect>, + key: String?, + assertionCreator: Expect.() -> Unit +): Expect> = expect getExisting key(key) { assertionCreator() } diff --git a/domain/builders/atrium-domain-builders-common/src/main/kotlin/ch/tutteli/atrium/domain/builders/utils/VarArgHelper.kt b/domain/builders/atrium-domain-builders-common/src/main/kotlin/ch/tutteli/atrium/domain/builders/utils/VarArgHelper.kt index a73e9c06c..34f69587c 100644 --- a/domain/builders/atrium-domain-builders-common/src/main/kotlin/ch/tutteli/atrium/domain/builders/utils/VarArgHelper.kt +++ b/domain/builders/atrium-domain-builders-common/src/main/kotlin/ch/tutteli/atrium/domain/builders/utils/VarArgHelper.kt @@ -19,7 +19,7 @@ interface VarArgHelper { /** * Creates an [ArgumentMapperBuilder] which allows to map [expected] and [otherExpected]. */ - val mapArguments get() = ArgumentMapperBuilder(expected, otherExpected) + val mapArguments: ArgumentMapperBuilder get() = ArgumentMapperBuilder(expected, otherExpected) /** * Returns the arguments as [List].