rename mapEntryAssertion functions to new schema in api-fluent

This commit is contained in:
Robert Stoll
2021-05-19 12:40:54 +02:00
parent 6b6a19c121
commit 2e0e25d2ab
7 changed files with 125 additions and 29 deletions

View File

@@ -182,7 +182,7 @@ fun <K, T : Map<out K, *>> Expect<T>.containsNotKey(key: K): Expect<T> =
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.getExistingFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.getExistingFeature
*/
fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K): Expect<V> =
_logic.getExisting(::identity, key).transform()
@@ -194,7 +194,7 @@ fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K): Expect<V> =
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.getExisting
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.getExisting
*/
fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K, assertionCreator: Expect<V>.() -> Unit): Expect<T> =
_logic.getExisting(::identity, key).collectAndAppend(assertionCreator)
@@ -206,7 +206,7 @@ fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K, assertionCreator: Ex
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.keysFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.keysFeature
*/
val <K, T : Map<out K, *>> Expect<T>.keys: Expect<Set<K>>
get() = _logic.property(Map<out K, *>::keys).transform()
@@ -219,7 +219,7 @@ val <K, T : Map<out K, *>> Expect<T>.keys: Expect<Set<K>>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.keys
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.keys
*/
fun <K, V, T : Map<out K, V>> Expect<T>.keys(assertionCreator: Expect<Set<K>>.() -> Unit): Expect<T> =
_logic.property(Map<out K, *>::keys).collectAndAppend(assertionCreator)
@@ -231,7 +231,7 @@ fun <K, V, T : Map<out K, V>> Expect<T>.keys(assertionCreator: Expect<Set<K>>.()
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.valuesFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.valuesFeature
*/
val <V, T : Map<*, V>> Expect<T>.values: Expect<Collection<V>>
get() = _logic.property(Map<out Any?, V>::values).transform()
@@ -244,7 +244,7 @@ val <V, T : Map<*, V>> Expect<T>.values: Expect<Collection<V>>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.values
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.values
*/
fun <K, V, T : Map<out K, V>> Expect<T>.values(assertionCreator: Expect<Collection<V>>.() -> Unit): Expect<T> =
_logic.property(Map<out K, V>::values).collectAndAppend(assertionCreator)
@@ -258,7 +258,7 @@ fun <K, V, T : Map<out K, V>> Expect<T>.values(assertionCreator: Expect<Collecti
*
* @return The newly created [Expect] for the transformed subject.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.asEntriesFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.asEntriesFeature
*/
fun <K, V, T : Map<out K, V>> Expect<T>.asEntries(): Expect<Set<Map.Entry<K, V>>> =
_logic.changeSubject.unreported { it.entries }
@@ -273,7 +273,7 @@ fun <K, V, T : Map<out K, V>> Expect<T>.asEntries(): Expect<Set<Map.Entry<K, V>>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapAssertionSamples.asEntries
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapExpectationSamples.asEntries
*/
fun <K, V, T : Map<out K, V>> Expect<T>.asEntries(
assertionCreator: Expect<Set<Map.Entry<K, V>>>.() -> Unit

View File

@@ -1,3 +1,4 @@
//TODO rename file with 0.18.0 to ...Expectations
package ch.tutteli.atrium.api.fluent.en_GB
import ch.tutteli.atrium.creating.Expect

View File

@@ -18,17 +18,20 @@ import ch.tutteli.atrium.logic.*
fun <K, V, T : Map.Entry<K, V>> Expect<T>.isKeyValue(key: K, value: V): Expect<T> =
_logicAppend { isKeyValue(key, value) }
//TODO move to mapEntryExpectations with 0.18.0
/**
* Creates an [Expect] for the property [Map.Entry.key] of the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapEntryAssertionSamples.keyFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapEntryExpectationSamples.keyFeature
*/
val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
get() = _logic.key().transform()
//TODO move to mapEntryExpectations with 0.18.0
/**
* Expects that the property [Map.Entry.key] of the subject of `this` expectation
* holds all assertions the given [assertionCreator] creates for it and
@@ -36,22 +39,24 @@ val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapEntryAssertionSamples.key
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapEntryExpectationSamples.key
*/
fun <K, V, T : Map.Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.() -> Unit): Expect<T> =
_logic.key().collectAndAppend(assertionCreator)
//TODO move to mapEntryExpectations with 0.18.0
/**
* Creates an [Expect] for the property [Map.Entry.value] of the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapEntryAssertionSamples.valueFeature
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapEntryExpectationSamples.valueFeature
*/
val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
get() = _logic.value().transform()
//TODO move to mapEntryExpectations with 0.18.0
/**
* Expects that the property [Map.Entry.value] of the subject of `this` expectation
* holds all assertions the given [assertionCreator] creates for it and
@@ -59,7 +64,7 @@ val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.deprecated.MapEntryAssertionSamples.value
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapEntryExpectationSamples.value
*/
fun <K, V, T : Map.Entry<K, V>> Expect<T>.value(assertionCreator: Expect<V>.() -> Unit): Expect<T> =
_logic.value().collectAndAppend(assertionCreator)

View File

@@ -0,0 +1,17 @@
package ch.tutteli.atrium.api.fluent.en_GB
import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.logic.*
/**
* Expects that the property [Map.Entry.key] of the subject of `this` expectation
* is equal to the given [key] and the property [Map.Entry.value] is equal to the given [value].
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.fluent.en_GB.samples.MapEntryExpectationSamples.toEqualKeyValue
*
* @since 0.17.0
*/
fun <K, V, T : Map.Entry<K, V>> Expect<T>.toEqualKeyValue(key: K, value: V): Expect<T> =
_logicAppend { isKeyValue(key, value) }

View File

@@ -4,8 +4,8 @@ import ch.tutteli.atrium.creating.Expect
import ch.tutteli.atrium.specs.*
object MapEntryExpectationsSpec : ch.tutteli.atrium.specs.integration.MapEntryExpectationsSpec(
fun2(Expect<Map.Entry<String, Int>>::isKeyValue),
fun2(Expect<Map.Entry<String?, Int?>>::isKeyValue).withNullableSuffix(),
fun2(Expect<Map.Entry<String, Int>>::toEqualKeyValue),
fun2(Expect<Map.Entry<String?, Int?>>::toEqualKeyValue).withNullableSuffix(),
property<Map.Entry<String, Int>, String>(Expect<Map.Entry<String, Int>>::key),
fun1<Map.Entry<String, Int>, Expect<String>.() -> Unit>(Expect<Map.Entry<String, Int>>::key),
property<Map.Entry<String, Int>, Int>(Expect<Map.Entry<String, Int>>::value),

View File

@@ -0,0 +1,73 @@
package ch.tutteli.atrium.api.fluent.en_GB.samples
import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.api.verbs.internal.expect
import kotlin.test.Test
class MapEntryExpectationSamples {
@Test
fun toEqualKeyValue() {
expect(mapOf(1 to "a").entries.first()).toEqualKeyValue(1, "a")
fails {
expect(mapOf(1 to "a").entries.first()).toEqualKeyValue(1, "b")
}
}
@Test
fun keyFeature() {
expect(mapOf(1 to "a").entries.first())
.key // subject here is of type Int (actually 1)
.toEqual(1)
fails {
expect(mapOf(1 to "a").entries.first())
.key // subject here is of type Int (actually 1)
.toEqual(2) // fails because 1 is not equal to 2
}
}
@Test
fun key() {
expect(mapOf(1 to "a").entries.first())
.key { // subject inside this block is of type Int (actually 1)
toEqual(1)
}
fails {
expect(mapOf(1 to "a").entries.first())
.key { // subject inside this block is of type Int (actually 1)
toEqual(2) // fails because 1 is not equal to 2
}
}
}
@Test
fun valueFeature() {
expect(mapOf(1 to "a").entries.first())
.value // subject here is of type String (actually "a")
.toEqual("a")
fails {
expect(mapOf(1 to "a").entries.first())
.value // subject here is of type String (actually "a")
.toEqual("b") // fails because "a" is not equal to "b"
}
}
@Test
fun value() {
expect(mapOf(1 to "a").entries.first())
.value { // subject inside this block is of type String (actually "a")
toEqual("a")
}
fails {
expect(mapOf(1 to "a").entries.first())
.value { // subject inside this block is of type String (actually "a")
toEqual("b") // fails because "a" is not equal to "b"
}
}
}
}

View File

@@ -8,8 +8,8 @@ import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.Suite
abstract class MapEntryExpectationsSpec(
isKeyValue: Fun2<Map.Entry<String, Int>, String, Int>,
isKeyValueNullable: Fun2<Map.Entry<String?, Int?>, String?, Int?>,
toEqualKeyValue: Fun2<Map.Entry<String, Int>, String, Int>,
toEqualKeyValueNullable: Fun2<Map.Entry<String?, Int?>, String?, Int?>,
keyFeature: Feature0<Map.Entry<String, Int>, String>,
key: Fun1<Map.Entry<String, Int>, Expect<String>.() -> Unit>,
valueFeature: Feature0<Map.Entry<String, Int>, Int>,
@@ -23,12 +23,12 @@ abstract class MapEntryExpectationsSpec(
include(object : SubjectLessSpec<Map.Entry<String, Int>>(
describePrefix,
isKeyValue.forSubjectLess("key", 1)
toEqualKeyValue.forSubjectLess("key", 1)
) {})
include(object : SubjectLessSpec<Map.Entry<String?, Int?>>(
"$describePrefix[nullable] ",
isKeyValueNullable.forSubjectLess("key", 1)
toEqualKeyValueNullable.forSubjectLess("key", 1)
) {})
include(object : KeyValueLikeExpectationsSpec<Map.Entry<String, Int>, Map.Entry<String?, Int?>>(
@@ -53,18 +53,18 @@ abstract class MapEntryExpectationsSpec(
val mapEntry = mapEntry("hello", 1)
val fluent = expect(mapEntry)
describeFun(isKeyValue, isKeyValueNullable) {
val isKeyValueFunctions = uncheckedToNonNullable(isKeyValue, isKeyValueNullable)
describeFun(toEqualKeyValue, toEqualKeyValueNullable) {
val toEqualKeyValueFunctions = uncheckedToNonNullable(toEqualKeyValue, toEqualKeyValueNullable)
context("map $mapEntry") {
isKeyValueFunctions.forEach { (name, isKeyValueFun) ->
toEqualKeyValueFunctions.forEach { (name, toEqualKeyValueFun) ->
it("$name - hello to 1 does not throw") {
fluent.isKeyValueFun("hello", 1)
fluent.toEqualKeyValueFun("hello", 1)
}
it("$name - hello to 2 throws AssertionError") {
expect {
fluent.isKeyValueFun("hello", 2)
fluent.toEqualKeyValueFun("hello", 2)
}.toThrow<AssertionError> {
message {
toContain("value: 1", "$toBeDescr: 2")
@@ -74,7 +74,7 @@ abstract class MapEntryExpectationsSpec(
}
it("$name - b to 1 throws AssertionError") {
expect {
fluent.isKeyValueFun("b", 1)
fluent.toEqualKeyValueFun("b", 1)
}.toThrow<AssertionError> {
message {
toContain("key: \"hello\"", "$toBeDescr: \"b\"")
@@ -86,19 +86,19 @@ abstract class MapEntryExpectationsSpec(
}
}
describeFun(isKeyValueNullable) {
val isKeyValueFun = isKeyValueNullable.lambda
describeFun(toEqualKeyValueNullable) {
val toEqualKeyValueFun = toEqualKeyValueNullable.lambda
val mapEntryNullable2 = mapEntry(null as String?, null as Int?)
val fluentNullable = expect(mapEntryNullable2)
context("map $mapEntryNullable2") {
it("null to null does not throw") {
fluentNullable.isKeyValueFun(null, null)
fluentNullable.toEqualKeyValueFun(null, null)
}
it("null to 2 throws AssertionError") {
expect {
fluentNullable.isKeyValueFun(null, 2)
fluentNullable.toEqualKeyValueFun(null, 2)
}.toThrow<AssertionError> {
message {
toContain("value: null", "$toBeDescr: 2")
@@ -108,7 +108,7 @@ abstract class MapEntryExpectationsSpec(
}
it("b to null throws AssertionError") {
expect {
fluentNullable.isKeyValueFun("b", null)
fluentNullable.toEqualKeyValueFun("b", null)
}.toThrow<AssertionError> {
message {
toContain("key: null", "$toBeDescr: \"b\"")