doc / ch.tutteli.atrium.api.fluent.en_GB / contains

contains

val <T : CharSequence> Expect<T>.contains: CharSequenceContains.EntryPointStep<T, NoOpSearchBehaviour> (source)

Starts a sophisticated contains assertion building process based on this Expect.

expect("ABC")
    .contains.exactly(1).value("A")

expect("ABBC")
    .contains.atLeast(2).value("B")

fails {
    expect("AAAAAA")
        .contains.atMost(3).value("A")
}

Return
The newly created builder.

fun <T : CharSequence> Expect<T>.contains(expected: CharSequenceOrNumberOrChar, vararg otherExpected: CharSequenceOrNumberOrChar): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains expected's toString representation and the toString representation of the otherExpected (if given), using a non disjoint search.

It is a shortcut for contains.atLeast(1).values(expected, *otherExpected).

Notice that a runtime check applies which assures that only CharSequence, Number and Char are passed. This function expects CharSequenceOrNumberOrChar (which is a typealias for Any) for your convenience, so that you can mix String and Int for instance.

By non disjoint is meant that "aa" in "aaaa" is found three times and not only two times. Also notice, that it does not search for unique matches. Meaning, if the input of the search is "a" and expected is defined as "a" and one otherExpected is defined as "a" as well, then both match, even though they match the same sequence in the input of the search. Use the property contains to create a more sophisticated contains assertion where you can use options such as atLeast, atMost and exactly to control the number of occurrences you expect.

Meaning you might want to use: contains.exactly(2).value("a") instead of: contains("a", "a")

expect("ABC").contains("B")

expect("ABC123").contains("AB", 'C', 12)

// holds because `contains` does not search for unique matches
// use `contains.exactly(2).value("A")` to check if subject contains two "A"s
expect("ABC").contains("A", "A")

fails {
    expect("ABC").contains("X")
}

fails { // because subject does not contain all values
    expect("ABC").contains("A", 99)
}

Exceptions

IllegalArgumentException - in case expected or one of the otherExpected is not a CharSequence, Number or Char.

Return
an Expect for the subject of this expectation.

fun <E, T : Iterable<E>> Expect<T>.contains(expected: E, vararg otherExpected: E): Expect<T> (source)

Expects that the subject of this expectation (an Iterable) contains the expected value and the otherExpected values (if given).

It is a shortcut for contains.inAnyOrder.atLeast(1).values(expected, *otherExpected)

Notice, that it does not search for unique matches. Meaning, if the iterable is setOf('a', 'b') and expected is defined as 'a' and one otherExpected is defined as 'a' as well, then both match, even though they match the same entry. Use an option such as atLeast, atMost and exactly to control the number of occurrences you expect.

Meaning you might want to use: contains.inAnyOrder.exactly(2).value('a') instead of: contains('a', 'a')

Return
an Expect for the subject of this expectation.

fun <E : Any, T : Iterable<E?>> Expect<T>.contains(assertionCreatorOrNull: (Expect<E>.() -> Unit)?): Expect<T> (source)

Expects that the subject of this expectation (an Iterable) contains an entry holding the assertions created by assertionCreatorOrNull or an entry which is null in case assertionCreatorOrNull is defined as null.

It is a shortcut for contains.inAnyOrder.atLeast(1).entry(assertionCreatorOrNull)

Parameters

assertionCreatorOrNull - The identification lambda which creates the assertions which the entry we are looking for has to hold; or in other words, the function which defines whether an entry is the one we are looking for or not. In case it is defined as null, then an entry is identified if it is null as well.

Return
an Expect for the subject of this expectation.

fun <E : Any, T : Iterable<E?>> Expect<T>.contains(assertionCreatorOrNull: (Expect<E>.() -> Unit)?, vararg otherAssertionCreatorsOrNulls: (Expect<E>.() -> Unit)?): Expect<T> (source)

Expects that the subject of this expectation (an Iterable) contains an entry holding the assertions created by assertionCreatorOrNull or an entry which is null in case assertionCreatorOrNull is defined as null -- likewise an entry (can be the same) is searched for each of the otherAssertionCreatorsOrNulls.

It is a shortcut for contains.inAnyOrder.atLeast(1).entries(assertionCreatorOrNull, *otherAssertionCreatorsOrNulls)

Parameters

assertionCreatorOrNull - The identification lambda which creates the assertions which the entry we are looking for has to hold; or in other words, the function which defines whether an entry is the one we are looking for or not. In case it is defined as null, then an entry is identified if it is null as well.

otherAssertionCreatorsOrNulls - Additional identification lambdas which each identify (separately) an entry which we are looking for (see assertionCreatorOrNull for more information).

Return
an Expect for the subject of this expectation.

fun <K, V, T : Map<out K, V>> Expect<T>.contains(keyValuePair: Pair<K, V>, vararg otherPairs: Pair<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains a key as defined by keyValuePair's Pair.first with a corresponding value as defined by keyValuePair's Pair.second -- optionally the same assertions are created for the otherPairs.

Delegates to contains.inAnyOrder.entries(keyValuePair, *otherPairs)

Notice, that it does not search for unique matches. Meaning, if the map is mapOf('a' to 1) and keyValuePair is defined as 'a' to 1 and one of the otherPairs is defined as 'a' to 1 as well, then both match, even though they match the same entry.

expect(mapOf(1 to "a")).contains(1 to "a")

fails {
    expect(mapOf(1 to "a"))
        .contains(1 to "b") // fails because the map does not contain Pair<1,"b">
}

Return
an Expect for the subject of this expectation.

inline fun <K, reified V : Any, T : Map<out K, V?>> Expect<T>.contains(keyValue: KeyValue<K, V>, vararg otherKeyValues: KeyValue<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains a key as defined by keyValue's KeyValue.key with a corresponding value which either holds all assertions keyValue's KeyValue.valueAssertionCreatorOrNull creates or needs to be null in case KeyValue.valueAssertionCreatorOrNull is defined as null -- optionally the same assertions are created for the otherKeyValues.

Delegates to contains.inAnyOrder.entries(keyValue, *otherKeyValues)

Notice, that it does not search for unique matches. Meaning, if the map is mapOf('a' to 1) and keyValue is defined as Key('a') { isGreaterThan(0) } and one of the otherKeyValues is defined as Key('a') { isLessThan(2) } , then both match, even though they match the same entry.

expect(mapOf(1 to "a"))
    .contains(
        KeyValue(1, { // subject inside this block is of type String (actually "a")
            toBe("a")
        })
    )

fails {
    expect(mapOf(1 to "a"))
        .contains(
            KeyValue(1, {   // subject inside this block is of type String (actually "a")
                toBe("b")   // fails because "a" is not equal to "b"
            })
        )
}

Return
an Expect for the subject of this expectation.