doc / ch.tutteli.atrium.api.infix.en_GB / key

key

val <K, T : Entry<K, *>> Expect<T>.key: Expect<K> (source)

Creates an Expect for the property Map.Entry.key of the subject of this expectation, so that further fluent calls are assertions about it.

val entry = mapOf(1 to "a").entries.first()

expect(entry).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(entry).key toEqual 2
    //      |      | subject here is type of Int (actually 1)
    //      | subject here is of type Map.Entry<Int, String>
}

Return
The newly created Expect for the extracted feature.

infix fun <K, V, T : Entry<K, V>> Expect<T>.key(assertionCreator: Expect<K>.() -> Unit): Expect<T> (source)

Expects that the property Map.Entry.key of the subject of this expectation holds all assertions the given assertionCreator creates for it and returns an Expect for the current subject of this expectation.

val entry = mapOf(1 to "a").entries.first()

expect(entry).key { // subject inside this block is of type Int (actually 1)
    this toEqual 1
} // subject here is back to type Map.Entry<Int, String>

fails { // because 1 is not equal to 2
    expect(entry).key { // subject inside this block is of type Int (actually 1)
        this toEqual 2
    }
}

Return
an Expect for the subject of this expectation.

fun <K, V> key(key: K, assertionCreator: Expect<V>.() -> Unit): KeyWithCreator<K, V> (source)

Helper function to create an KeyWithCreator based on the given key and assertionCreator.

expect(mapOf(1 to "a")) getExisting key(1) {   // subject inside this block is of type String (actually "a")
    this toEqual "a"
}

fails {
    expect(mapOf(1 to "a")) getExisting key(1) {  // subject inside this block is of type String (actually "a")
        this toEqual "b"   // fails because "a" is not equal to "b"
    }
}

fails {
    // all expectations are evaluated inside an expectation group block; for more details:
    // https://github.com/robstoll/atrium#define-single-assertions-or-assertion-groups

    expect(mapOf(1 to "a")) getExisting key(2) {   // fails because key 2 does not exist
        this toEqual "a"    // still evaluated because we use an expectation group block
        // use `.getExisting(key).` if you want a fail fast behaviour
    }
}