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

toContainOnly

infix fun <K, V, T : Map<out K, V>> Expect<T>.toContainOnly(keyValuePair: Pair<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains only one entry with a key as defined by keyValuePair's Pair.first and a corresponding value as defined by keyValuePair's Pair.second

Delegates to 'it toContain o inAny order but only entry keyValuePair'.

expect(mapOf(1 to "a")) toContainOnly (1 to "a")

fails { // because the map contains key 2 with value "b" in addition
    expect(mapOf(1 to "a", 2 to "b")) toContainOnly (1 to "a")
}

Return
an Expect for the subject of this expectation.

infix fun <K, V, T : Map<out K, V>> Expect<T>.toContainOnly(pairs: Pairs<K, V>): Expect<T> (source)

Expects the subject of this expectation (a Map) contains only (in any order) for each key-value pair in pairs, a key as defined by that entry's Pair.first with a corresponding value as defined by entry's Pair.second.

Delegates to it toContain o inAny order but only the pairs

expect(mapOf(1 to "a", 2 to "b")) toContainOnly pairs(1 to "a", 2 to "b")

fails { // because the map contains key 2 with value "b" in addition
    expect(mapOf(1 to "a", 2 to "b")) toContainOnly pairs(1 to "a")
}

Parameters

pairs - The key-value Pairs expected to be contained within this Map -- use the function pairs(x to y, ...) to create a Pairs.

Return
an Expect for the subject of this expectation.

inline infix fun <K, reified V : Any, T : Map<out K, V?>> Expect<T>.toContainOnly(keyValue: KeyWithValueCreator<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains only one entry with a key as defined by keyValue's KeyWithValueCreator.key with a corresponding value which either holds all assertions keyValue's KeyWithValueCreator.valueAssertionCreatorOrNull creates or needs to be null in case KeyWithValueCreator.valueAssertionCreatorOrNull is defined as null

Delegates to it toContain o inAny order but only entry keyValue

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

fails { // fails because the map contains key 2 with value "b" in addition
    expect(
        mapOf(1 to "a", 2 to "b")
    ) toContainOnly keyValue(1) {  // subject inside this block is of type String (actually "a")
        this toEqual "a"
    }
}

Parameters

keyValue - The KeyWithValueCreator whose key is expected to be contained within this Map and where the corresponding value holds all assertions the KeyWithValueCreator.valueAssertionCreatorOrNull creates or needs to be null in case KeyWithValueCreator.valueAssertionCreatorOrNull is defined as null -- use the function keyValue(x) { ... } to create a KeyWithValueCreator.

Return
an Expect for the subject of this expectation.

inline infix fun <K, reified V : Any, T : Map<out K, V?>> Expect<T>.toContainOnly(keyValues: KeyValues<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains only (in any order) for each KeyWithValueCreator in keyValues, a key as defined by KeyWithValueCreator.key with a corresponding value which either holds all assertions KeyWithValueCreator's KeyWithValueCreator.valueAssertionCreatorOrNull creates or needs to be null in case KeyWithValueCreator.valueAssertionCreatorOrNull is defined as null

Delegates to it toContain o inAny order but only the keyValues

expect(mapOf(1 to "a", 2 to "b")) toContainOnly keyValues(
    keyValue(1) { // subject inside this block is of type String (actually "a")
        this toEqual "a"
    },
    keyValue(2) { // subject inside this block is of type String (actually "b")
        this toEqual "b"
    }
)

fails {
    expect(mapOf(1 to "a", 2 to "b")) toContainOnly keyValues(
        keyValue(1) { // subject inside this block is of type String (actually "a")
            this toEqual "a"
        }
    )
}

Parameters

keyValues - The KeyWithValueCreators -- use the function keyValues(keyValue(key1) { ... }, keyValue(key2) { ... }, ...) to create a KeyValues.

Return
an Expect for the subject of this expectation.