Skip to content

Commit

Permalink
Merge pull request #64 from uaArsen/map-not-contains-key
Browse files Browse the repository at this point in the history
add containsNotKey for Map (#59)
  • Loading branch information
robstoll authored Jan 9, 2019
2 parents 8772f75 + c2fc05a commit 543d9e8
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ inline fun <K, reified V : Any, T: Map<K, V?>> Assert<T>.enthaeltNullable(keyVal
fun <K> Assert<Map<K, *>>.enthaeltKey(key: K)
= addAssertion(AssertImpl.map.containsKey(this, key))

/**
* Makes the assertion that [AssertionPlant.subject] does not contain the given [key].
*
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
fun <K> Assert<Map<K, *>>.enthaeltNichtKey(key: K)
= addAssertion(AssertImpl.map.containsNotKey(this, key))

/**
* Makes the assertion that [AssertionPlant.subject] contains the given [key] and that the corresponding value
* holds all assertions the given [assertionCreator] might create for it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MapAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAssertionsSpec(
"${containsKeyWithNullableValueAssertionsFun.name} ${KeyNullableValue::class.simpleName}" to Assert<Map<String?, Int?>>::enthaeltNullable,
Assert<Map<String, *>>::enthaeltKey.name to Assert<Map<String, *>>::enthaeltKey,
"${Assert<Map<String?, *>>::enthaeltKey.name} for nullable" to Assert<Map<String?, *>>::enthaeltKey,
Assert<Map<String, *>>::enthaeltNichtKey.name to Assert<Map<String, *>>::enthaeltNichtKey,
"${Assert<Map<String?, *>>::enthaeltNichtKey.name} for nullable" to Assert<Map<String?, *>>::enthaeltNichtKey,
Assert<Map<*, *>>::hatDieGroesse.name to Assert<Map<*, *>>::hatDieGroesse,
Assert<Map<*, *>>::istLeer.name to Assert<Map<*, *>>::istLeer,
Assert<Map<*, *>>::istNichtLeer.name to Assert<Map<*, *>>::istNichtLeer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ inline fun <K, reified V : Any, T: Map<K, V?>> Assert<T>.containsNullable(keyVal
fun <K> Assert<Map<K, *>>.containsKey(key: K)
= addAssertion(AssertImpl.map.containsKey(this, key))

/**
* Makes the assertion that [AssertionPlant.subject] does not contain the given [key].
*
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
fun <K> Assert<Map<K, *>>.containsNotKey(key: K)
= addAssertion(AssertImpl.map.containsNotKey(this, key))

/**
* Makes the assertion that [AssertionPlant.subject] contains the given [key] and that the corresponding value
* holds all assertions the given [assertionCreator] might create for it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class MapAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAssertionsSpec(
"${containsKeyWithNullableValueAssertionsFun.name} ${KeyNullableValue::class.simpleName}" to Assert<Map<String?, Int?>>::containsNullable,
Assert<Map<String, *>>::containsKey.name to Assert<Map<String, *>>::containsKey,
"${Assert<Map<String?, *>>::containsKey.name} for nullable" to Assert<Map<String?, *>>::containsKey,
Assert<Map<String, *>>::containsKey.name to Assert<Map<String, *>>::containsNotKey,
"${Assert<Map<String?, *>>::containsKey.name} for nullable" to Assert<Map<String?, *>>::containsNotKey,
Assert<Map<*, *>>::hasSize.name to Assert<Map<*, *>>::hasSize,
Assert<Map<*, *>>::isEmpty.name to Assert<Map<*, *>>::isEmpty,
Assert<Map<*, *>>::isNotEmpty.name to Assert<Map<*, *>>::isNotEmpty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ inline infix fun <K, reified V : Any, T: Map<K, V?>> Assert<T>.containsNullable(
infix fun <K> Assert<Map<K, *>>.containsKey(key: K)
= addAssertion(AssertImpl.map.containsKey(this, key))

/**
* Makes the assertion that [AssertionPlant.subject] does not contain the given [key].
*
* @return This plant to support a fluent API.
* @throws AssertionError Might throw an [AssertionError] if the assertion made is not correct.
*/
infix fun <K> Assert<Map<K, *>>.containsNotKey(key: K)
= addAssertion(AssertImpl.map.containsNotKey(this, key))

/**
* Prepares the assertion about the return value of calling [get][Map.get] with the given [key].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class MapAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAssertionsSpec(
"${containsKeyWithNullableValueAssertionsFun.name} ${KeyNullableValue::class.simpleName}" to Companion::containsKeyWithNullableValueAssertions,
Assert<Map<String, Int>>::containsKey.name to Companion::containsKey,
"${Assert<Map<String?, *>>::containsKey.name} for nullable" to Companion::containsNullableKey,
Assert<Map<String, Int>>::containsNotKey.name to Companion::containsNotKey,
"${Assert<Map<String?, *>>::containsNotKey.name} for nullable" to Companion::containsNotNullableKey,
Assert<Map<*, *>>::hasSize.name to Companion::hasSize,
"${Assert<Map<*, *>>::toBe.name} ${Empty::class.simpleName}" to Companion::isEmpty,
"${Assert<Map<*, *>>::notToBe.name} ${Empty::class.simpleName}" to Companion::isNotEmpty
Expand Down Expand Up @@ -62,6 +64,12 @@ class MapAssertionsSpec : ch.tutteli.atrium.spec.integration.MapAssertionsSpec(
private fun containsNullableKey(plant: Assert<Map<String?, *>>, key: String?)
= plant containsKey key

private fun containsNotKey(plant: Assert<Map<String, *>>, key: String)
= plant containsNotKey key

private fun containsNotNullableKey(plant: Assert<Map<String?, *>>, key: String?)
= plant containsNotKey key

private fun hasSize(plant: Assert<Map<*, *>>, size: Int)
= plant hasSize size

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface MapAssertions {
fun <K, V: Any> containsKeyWithValueAssertions(plant: AssertionPlant<Map<K, V>>, keyValues: List<KeyValue<K, V>>): Assertion
fun <K, V: Any> containsKeyWithNullableValueAssertions(plant: AssertionPlant<Map<K, V?>>, type: KClass<V>, keyValues: List<KeyNullableValue<K, V>>): Assertion
fun <K> containsKey(plant: AssertionPlant<Map<K, *>>, key: K): Assertion
fun <K> containsNotKey(plant: AssertionPlant<Map<K, *>>, key: K): Assertion
fun <K, V: Any> getExisting(plant: AssertionPlant<Map<K, V>>, key: K, assertionCreator: AssertionPlant<V>.() -> Unit): Assertion
fun <K, V> getExistingNullable(plant: AssertionPlant<Map<K, V>>, key: K, assertionCreator: AssertionPlantNullable<V>.() -> Unit): Assertion
fun hasSize(plant: AssertionPlant<Map<*, *>>, size: Int): Assertion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ object MapAssertionsBuilder : MapAssertions {
override inline fun <K> containsKey(plant: AssertionPlant<Map<K, *>>, key: K)
= mapAssertions.containsKey(plant, key)

override inline fun <K> containsNotKey(plant: AssertionPlant<Map<K, *>>, key: K)
= mapAssertions.containsNotKey(plant, key)

override inline fun <K, V : Any> getExisting(
plant: AssertionPlant<Map<K, V>>,
key: K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ch.tutteli.atrium.domain.robstoll.lib.creating
import ch.tutteli.atrium.api.cc.en_GB.property
import ch.tutteli.atrium.api.cc.en_GB.toBe
import ch.tutteli.atrium.assertions.Assertion
import ch.tutteli.atrium.assertions.builders.assertionBuilder
import ch.tutteli.atrium.creating.*
import ch.tutteli.atrium.domain.builders.AssertImpl
import ch.tutteli.atrium.domain.creating.feature.extract.FeatureExtractor
Expand Down Expand Up @@ -90,6 +89,9 @@ private fun <K, V, M, A : BaseAssertionPlant<V, A>, C : BaseCollectingAssertion
fun <K> _containsKey(plant: AssertionPlant<Map<K, *>>, key: K): Assertion
= AssertImpl.builder.createDescriptive(DescriptionMapAssertion.CONTAINS_KEY, key) { plant.subject.containsKey(key) }

fun <K> _containsNotKey(plant: AssertionPlant<Map<K, *>>, key: K): Assertion
= AssertImpl.builder.createDescriptive(DescriptionMapAssertion.CONTAINS_NOT_KEY, key) { plant.subject.containsKey(key).not() }

fun <K, V : Any> _getExisting(
plant: AssertionPlant<Map<K, V>>,
key: K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class MapAssertionsImpl : MapAssertions {
override fun <K> containsKey(plant: AssertionPlant<Map<K, *>>, key: K)
= _containsKey(plant, key)

override fun <K> containsNotKey(plant: AssertionPlant<Map<K, *>>, key: K)
= _containsNotKey(plant, key)

override fun <K, V : Any> getExisting(
plant: AssertionPlant<Map<K, V>>,
key: K,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ abstract class MapAssertionsSpec(
containsKeyWithNullableValueAssertionsPair: Pair<String, Assert<Map<String?, Int?>>.(KeyNullableValue<String?, Int>, Array<out KeyNullableValue<String?, Int>>) -> Assert<Map<String?, Int?>>>,
containsKeyPair: Pair<String, Assert<Map<String, *>>.(String) -> Assert<Map<*, *>>>,
containsNullableKeyPair: Pair<String, Assert<Map<String?, *>>.(String?) -> Assert<Map<String?, *>>>,
containsNotKeyPair: Pair<String, Assert<Map<String, *>>.(String) -> Assert<Map<*, *>>>,
containsNotNullableKeyPair: Pair<String, Assert<Map<String?, *>>.(String?) -> Assert<Map<String?, *>>>,
hasSizePair: Pair<String, Assert<Map<*, *>>.(Int) -> Assert<Map<*, *>>>,
isEmptyPair: Pair<String, Assert<Map<*, *>>.() -> Assert<Map<*, *>>>,
isNotEmptyPair: Pair<String, Assert<Map<*, *>>.() -> Assert<Map<*, *>>>,
Expand All @@ -31,14 +33,16 @@ abstract class MapAssertionsSpec(
containsPair.first to mapToCreateAssertion { containsPair.second(this, "key" to 1, arrayOf()) },
containsKeyWithValueAssertionsPair.first to mapToCreateAssertion { containsKeyWithValueAssertionsPair.second(this, KeyValue("a") { toBe(1) }, arrayOf(KeyValue("a") { isLessOrEquals(2) })) },
containsKeyPair.first to mapToCreateAssertion{ containsKeyPair.second(this, "a") },
containsNotKeyPair.first to mapToCreateAssertion{ containsKeyPair.second(this, "a") },
hasSizePair.first to mapToCreateAssertion { hasSizePair.second(this, 2) },
isEmptyPair.first to mapToCreateAssertion { isEmptyPair.second(this) },
isNotEmptyPair.first to mapToCreateAssertion { isNotEmptyPair.second(this) }
) {})
include(object : SubjectLessAssertionSpec<Map<String?, Int?>>("$describePrefix[nullable Key] ",
containsNullablePair.first to mapToCreateAssertion{ containsNullablePair.second(this, null to 1, arrayOf("a" to null)) },
containsKeyWithNullableValueAssertionsPair.first to mapToCreateAssertion { containsKeyWithNullableValueAssertionsPair.second(this, KeyNullableValue(null) { toBe(1) }, arrayOf(KeyNullableValue("a", null))) },
containsNullableKeyPair.first to mapToCreateAssertion{ containsNullableKeyPair.second(this, null) }
containsNullableKeyPair.first to mapToCreateAssertion{ containsNullableKeyPair.second(this, null) },
containsNotNullableKeyPair.first to mapToCreateAssertion{ containsNotNullableKeyPair.second(this, null) }
) {})

include(object : CheckingAssertionSpec<Map<String, Int>>(verbs, describePrefix,
Expand All @@ -48,6 +52,7 @@ abstract class MapAssertionsSpec(
mapOf("a" to 1, "b" to 2), mapOf("a" to 2, "b" to 3)
),
checkingTriple(containsKeyPair.first, {containsKeyPair.second(this, "a")}, mapOf("a" to 1), mapOf("b" to 1)),
checkingTriple(containsNotKeyPair.first, {containsNotKeyPair.second(this, "b")}, mapOf("a" to 1), mapOf("b" to 1)),
checkingTriple(hasSizePair.first, { hasSizePair.second(this, 1) }, mapOf("a" to 1), mapOf("a" to 1, "b" to 2)),
checkingTriple(isEmptyPair.first, { isEmptyPair.second(this) }, mapOf(), mapOf("a" to 1, "b" to 2)),
checkingTriple(isNotEmptyPair.first, { isNotEmptyPair.second(this) }, mapOf("b" to 2), mapOf())
Expand All @@ -64,6 +69,10 @@ abstract class MapAssertionsSpec(
checkingTriple(containsNullableKeyPair.first,
{ containsNullableKeyPair.second(this, null)},
mapOf("a" to 1, null to 1), mapOf<String?, Int?>("b" to 1)
),
checkingTriple(containsNotNullableKeyPair.first,
{ containsNotNullableKeyPair.second(this, null)},
mapOf<String?, Int?>("a" to 1, "b" to 1), mapOf<String?, Int?>(null to 1)
)
) {})

Expand All @@ -83,6 +92,8 @@ abstract class MapAssertionsSpec(
val (containsKeyWithNullableValueAssertions, containsKeyWithNullableValueAssertionsFun) = containsKeyWithNullableValueAssertionsPair
val (containsKey, containsKeyFun) = containsKeyPair
val (containsNullableKey, containsNullableKeyFun) = containsNullableKeyPair
val (containsNotKey, containsNotKeyFun) = containsNotKeyPair
val (containsNotNullableKey, containsNotNullableKeyFun) = containsNotNullableKeyPair
val (hasSize, hasSizeFun) = hasSizePair
val (isEmpty, isEmptyFun) = isEmptyPair
val (isNotEmpty, isNotEmptyFun) = isNotEmptyPair
Expand All @@ -91,6 +102,7 @@ abstract class MapAssertionsSpec(
val isNotDescr = DescriptionBasic.IS_NOT.getDefault()
val empty = DescriptionCollectionAssertion.EMPTY.getDefault()
val containsKeyDescr = DescriptionMapAssertion.CONTAINS_KEY.getDefault()
val containsNotKeyDescr = DescriptionMapAssertion.CONTAINS_NOT_KEY.getDefault()
val toBeDescr = DescriptionAnyAssertion.TO_BE.getDefault()
val keyDoesNotExist = DescriptionMapAssertion.KEY_DOES_NOT_EXIST.getDefault()
val lessThanDescr = DescriptionComparableAssertion.IS_LESS_THAN.getDefault()
Expand Down Expand Up @@ -295,6 +307,30 @@ abstract class MapAssertionsSpec(
fluent.containsKeyFun("a")
}

describeFun(containsNotKey) {
it("does not throw if the map does not contain the key") {
fluent.containsNotKeyFun("c")
}

it("throws an AssertionError if the map contains the key") {
expect {
fluent.containsNotKeyFun("a")
}.toThrow<AssertionError> { messageContains("$containsNotKeyDescr: \"a\"")}
}
}

describeFun(containsNotNullableKey) {
it("does not throw if the map does not contain the key") {
verbs.checkImmediately(mapOf<String?, Int>("a" to 1, "b" to 2)).containsNotNullableKeyFun(null)
}

it("throws an AssertionError if the map contains the key") {
expect {
verbs.checkImmediately(mapOf("a" to 1, null to 2)).containsNotNullableKeyFun(null)
}.toThrow<AssertionError> { messageContains("$containsNotKeyDescr: null")}
}
}

describeFun(hasSize) {
context("map with two entries") {
test("expect 2 does not throw") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class DescriptionMapAssertion(override val value: String) : StringBasedTran
CANNOT_EVALUATE_KEY_DOES_NOT_EXIST("$COULD_NOT_EVALUATE_DEFINED_ASSERTIONS -- der gegebene Key existiert nicht.\n$VISIT_COULD_NOT_EVALUATE_ASSERTIONS"),
CONTAINS_IN_ANY_ORDER("enthält, in beliebiger Reihenfolge"),
CONTAINS_KEY("enthält Key"),
CONTAINS_NOT_KEY("enthält nicht den Key"),
ENTRY_WITH_KEY("Eintrag %s"),
KEY_DOES_NOT_EXIST("❗❗ Key existiert nicht"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum class DescriptionMapAssertion(override val value: String) : StringBasedTran
CANNOT_EVALUATE_KEY_DOES_NOT_EXIST("$COULD_NOT_EVALUATE_DEFINED_ASSERTIONS -- given key does not exist.\n$VISIT_COULD_NOT_EVALUATE_ASSERTIONS"),
CONTAINS_IN_ANY_ORDER("contains, in any order"),
CONTAINS_KEY("contains key"),
CONTAINS_NOT_KEY("does not contain key"),
ENTRY_WITH_KEY("entry %s"),
KEY_DOES_NOT_EXIST("❗❗ key does not exist"),
}

0 comments on commit 543d9e8

Please sign in to comment.