key

infix fun <K, V, T : Map.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.

Return

an Expect for the subject of this expectation.

Samples

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

expect(entry).key { // subject inside this expectation-group is of type Int (actually 1)
    it 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 expectation-group is of type Int (actually 1)
        it toEqual 2
    }
}

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.

Samples

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

fails { // because key 2 does not exist but...

    expect(mapOf(1 to "a")) getExisting key(2) {
        this toEqual "a"    //... still reported as we are inside an expectation-group block
    }
}

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

    expect(mapOf(1 to "a")) getExisting key(1) {
        it toEqual "b"   // fails because "a" is not equal to "b"
        it toEqual "c"   // still evaluated, use `getExisting 1` if you want a fail fast behaviour
    }
}

val <K, T : Map.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.

Return

The newly created Expect for the extracted feature.

Samples

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>
}