Merge pull request #929 from robstoll/MapEntyAssertionSamples

Map enty assertion samples
This commit is contained in:
Robert Stoll
2021-05-31 12:54:53 +02:00
committed by GitHub
4 changed files with 105 additions and 0 deletions

View File

@@ -12,6 +12,8 @@ import ch.tutteli.atrium.logic.*
* reporting etc.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.deprecated.MapEntryAssertionSamples.isKeyValue
*/
@Deprecated("Use toEqualKeyValue; will be removed with 1.0.0 at the latest", ReplaceWith("this.toEqualKeyValue<K, V, T>(keyValuePair)"))
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.isKeyValue(keyValuePair: Pair<K, V>): Expect<T> =
@@ -23,6 +25,8 @@ infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.isKeyValue(keyValuePair: Pair<K,
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.keyFeature
*/
val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
get() = _logic.key().transform()
@@ -34,6 +38,8 @@ val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
* returns an [Expect] for the current subject of `this` expectation.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.key
*/
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.() -> Unit): Expect<T> =
_logic.key().collectAndAppend(assertionCreator)
@@ -44,6 +50,8 @@ infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.
* so that further fluent calls are assertions about it.
*
* @return The newly created [Expect] for the extracted feature.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.valueFeature
*/
val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
get() = _logic.value().transform()
@@ -55,6 +63,8 @@ val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
* returns an [Expect] for the current subject of `this` expectation.
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.value
*/
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.value(assertionCreator: Expect<V>.() -> Unit): Expect<T> =
_logic.value().collectAndAppend(assertionCreator)

View File

@@ -9,6 +9,8 @@ import ch.tutteli.atrium.logic.*
*
* @return an [Expect] for the subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.toEqualKeyValue
*
* @since 0.17.0
*/
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.toEqualKeyValue(keyValuePair: Pair<K, V>): Expect<T> =

View File

@@ -0,0 +1,72 @@
package ch.tutteli.atrium.api.infix.en_GB.samples
import ch.tutteli.atrium.api.infix.en_GB.key
import ch.tutteli.atrium.api.infix.en_GB.toEqual
import ch.tutteli.atrium.api.infix.en_GB.toEqualKeyValue
import ch.tutteli.atrium.api.infix.en_GB.value
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 to "a")
fails { // because (1 to "a") is not equal to (1 to "b")
expect(mapOf(1 to "a").entries.first()) toEqualKeyValue (1 to "b")
}
}
@Test
fun keyFeature() {
expect(mapOf(1 to "a").entries.first()).key toEqual 1
// | | subject here is type of Int (actually 1)
// | subject here is of type Map.Entry<Int, String>
fails { // because 1 is not equal to 2
expect(mapOf(1 to "a").entries.first()).key toEqual 2
// | | subject here is type of Int (actually 1)
// | subject here is of type Map.Entry<Int, String>
}
}
@Test
fun key() {
expect(mapOf(1 to "a").entries.first()).key { // subject inside this block is of type Int (actually 1)
this toEqual 1
}
fails { // because 1 is not equal to 2
expect(mapOf(1 to "a").entries.first()).key { // subject inside this block is of type Int (actually 1)
this toEqual 2
}
}
}
@Test
fun valueFeature() {
expect(mapOf(1 to "a").entries.first()).value toEqual "a"
// | | subject here is of type String (actually "a")
// | subject here is of type Map.Entry<Int, String>
fails { // because "a" is not equal to "b"
expect(mapOf(1 to "a").entries.first()).value toEqual "b"
// | | subject here is of type String (actually "a")
// | subject here is of type Map.Entry<Int, String>
}
}
@Test
fun value() {
expect(mapOf(1 to "a").entries.first()) value { // subject inside this block is of type String (actually "a")
this toEqual "a"
}
fails { // because "a" is not equal to "b"
expect(mapOf(1 to "a").entries.first()) value { // subject inside this block is of type String (actually "a")
this toEqual "b"
}
}
}
}

View File

@@ -0,0 +1,21 @@
//TODO remove file with 1.0.0
@file:Suppress("DEPRECATION")
package ch.tutteli.atrium.api.infix.en_GB.samples.deprecated
import ch.tutteli.atrium.api.infix.en_GB.*
import ch.tutteli.atrium.api.infix.en_GB.samples.fails
import ch.tutteli.atrium.api.verbs.internal.expect
import kotlin.test.Test
class MapEntryAssertionSamples {
@Test
fun isKeyValue() {
expect(mapOf(1 to "a").entries.first()) isKeyValue (1 to "a")
fails {
expect(mapOf(1 to "a").entries.first()) isKeyValue (1 to "b")
}
}
}