add MapEntyAssertionSamples in infix package (#911)

This commit is contained in:
Bacho Kurtanidze
2021-05-31 14:08:22 +04:00
committed by GitHub
parent e786141e57
commit 37b8749566
4 changed files with 156 additions and 0 deletions

View File

@@ -22,6 +22,8 @@ infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.isKeyValue(keyValuePair: Pair<K,
* Creates an [Expect] for the property [Map.Entry.key] of the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.keyFeature
*
* @return The newly created [Expect] for the extracted feature.
*/
val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
@@ -33,6 +35,8 @@ val <K, T : Map.Entry<K, *>> Expect<T>.key: Expect<K>
* holds all assertions the given [assertionCreator] creates for it and
* returns an [Expect] for the current subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.key
*
* @return an [Expect] for the subject of `this` expectation.
*/
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.() -> Unit): Expect<T> =
@@ -43,6 +47,8 @@ infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.
* Creates an [Expect] for the property [Map.Entry.value] of the subject of `this` expectation,
* so that further fluent calls are assertions about it.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.valueFeature
*
* @return The newly created [Expect] for the extracted feature.
*/
val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
@@ -54,6 +60,8 @@ val <V, T : Map.Entry<*, V>> Expect<T>.value: Expect<V>
* holds all assertions the given [assertionCreator] creates for it and
* returns an [Expect] for the current subject of `this` expectation.
*
* @sample ch.tutteli.atrium.api.infix.en_GB.samples.MapEntryExpectationSamples.value
*
* @return an [Expect] for the subject of `this` expectation.
*/
infix fun <K, V, T : Map.Entry<K, V>> Expect<T>.value(assertionCreator: Expect<V>.() -> Unit): Expect<T> =

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,90 @@
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")
// | | | subject here is type of Map.Entry<Int, String> (actually (1 to "a"))
// | | subject here is type of Set<Map.Entry<Int, String>>
// | subject here is type of Map<Int, String>
fails {
// fails because (1 to "a") is not equal to (1 to "b")
expect(mapOf(1 to "a").entries.first()) toEqualKeyValue (1 to "b")
// | | | subject here is type of Map.Entry<Int, String> (actually (1 to "a"))
// | | subject here is type of Set<Map.Entry<Int, String>>
// | subject here is type of Map<Int, String>
}
}
@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<Int, String>
fails {
// 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<Int, String>
}
}
@Test
fun key() {
// | subject here is of type Map<Int, String>
expect(mapOf(1 to "a").entries.first()).key { // subject inside this block is of type Int (actually 1)
this toEqual 1
}
fails {
// fails because 1 is not equal to 2
// | subject here is of type Map<Int, String>
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<Int, String>
fails {
// 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<Int, String>
}
}
@Test
fun value() {
// | subject here is of type Map<Int, String>
expect(mapOf(1 to "a").entries.first()) value { // subject inside this block is of type String (actually "a")
this toEqual "a"
}
fails {
// fails because "a" is not equal to "b"
// | subject here is of type Map<Int, String>
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,56 @@
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()) toEqualKeyValue (1 to "a")
fails {
expect(mapOf(1 to "a").entries.first()) toEqualKeyValue (1 to "b")
}
}
@Test
fun keyFeature() {
expect(mapOf(1 to "a").entries.first()).key toEqual 1
fails {
expect(mapOf(1 to "a").entries.first()).key toEqual 2
}
}
@Test
fun key() {
expect(mapOf(1 to "a").entries.first()) key { it toEqual 1 }
fails {
expect(mapOf(1 to "a").entries.first()) key { it toEqual 2 }
}
}
@Test
fun valueFeature() {
expect(mapOf(1 to "a").entries.first()).value toEqual "a"
fails {
expect(mapOf(1 to "a").entries.first()).value toEqual "b"
}
}
@Test
fun value() {
expect(mapOf(1 to "a").entries.first()) value { it toEqual "a" }
fails {
expect(mapOf(1 to "a").entries.first()) value { it toEqual "b" }
}
}
}