getExisting

fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K): Expect<V>(source)

Expects that the subject of this expectation (a Map) contains the given key, creates an Expect for the corresponding value and returns the newly created Expect, so that further fluent calls are expectations about it.

Return

The newly created Expect for the extracted feature.

Samples

expect(mapOf(1 to "a"))
    .getExisting(1) // subject is of type String (actually "a")
    .toEqual("a")

fails {
    expect(mapOf(1 to "a"))
        .getExisting(1)  // subject is of type String (actually "a")
        .toContain("b")    // fails
        .toStartWith("z")  // not
}

fails {
    expect(mapOf(1 to "a"))
        .getExisting(2) // expectation fails because key 2 does not exist
        .toEqual("a")   // not evaluated/reported even though `getExisting` already fails
    //                     use `.getExisting(key) { ... }` if you want that all expectations are evaluated
}

fun <K, V, T : Map<out K, V>> Expect<T>.getExisting(key: K, assertionCreator: Expect<V>.() -> Unit): Expect<T>(source)

Expects that the subject of this expectation (a Map) contains the given key and that the corresponding value holds all assertions the given assertionCreator creates for it.

Return

an Expect for the subject of this expectation.

Samples

expect(mapOf(1 to "a"))
    .getExisting(1) {   // subject inside this expectation-group is of type String (actually "a")
        toEqual("a")
    } // subject here is back to type Map<Int, String>

fails {
    expect(mapOf(1 to "a"))
        .getExisting(1) {    // subject inside this expectation-group is of type String (actually "a")
            toEqual("b")     // fails because "a" is not equal to "b"
            toStartWith("z") // still evaluated because we use an expectation-group block
            //                  use `.getExisting(key).` if you want a fail fast behaviour
        }
}

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(2) {   // fails because key 2 does not exist
            toEqual("a")    // still evaluated because we use an expectation-group block
            //                 use `.getExisting(key).` if you want a fail fast behaviour
        }
}