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

toContain

infix fun <T : CharSequence> Expect<T>.toContain(o: o): CharSequenceContains.EntryPointStep<T, NoOpSearchBehaviour> (source)

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

expect("ABC") toContain o exactly 1 value "A"

expect("ABBC") toContain o atLeast 2 value "B"

fails {
    expect("AAAAAA") toContain o atMost 3 value "A"
}

Parameters

o - The filler object o.

Return
The newly created builder.

Since
0.17.0

infix fun <T : CharSequence> Expect<T>.toContain(expected: CharSequenceOrNumberOrChar): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains the expected's toString representation.

It is a shortcut for toContain o atLeast 1 value expected.

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.

expect("ABC") toContain "B"

expect("ABC123") toContain values("AB", 'C', 12)

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

fails {
    expect("ABC") toContain "X"
}

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

Exceptions

IllegalArgumentException - in case expected is not a CharSequence, Number or Char.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <T : CharSequence> Expect<T>.toContain(values: Values<CharSequenceOrNumberOrChar>): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains the toString representation of the given values using a non-disjoint search.

It is a shortcut for toContain o atLeast 1 the 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 Values is defined as values("a", "a"), then both match, even though they match the same sequence in the input of the search. Use an option such as atLeast, atMost and exactly to control the number of occurrences you expect.

Meaning you might want to use: toContain o exactly 2 value "a" instead of: toContain values("a", "a")

expect("ABC") toContain "B"

expect("ABC123") toContain values("AB", 'C', 12)

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

fails {
    expect("ABC") toContain "X"
}

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

Parameters

values - The values which are expected to be contained within the input of the search -- use the function values(t, ...) to create a Values.

Exceptions

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

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <T : CharSequence> Expect<T>.toContain(pattern: Regex): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains a sequence which matches the given regular expression pattern.

It is a shortcut for toContain o atLeast 1 matchFor pattern.

expect("ABC") toContain "(B)?C".toRegex()

fails {
    expect("ABC") toContain "X".toRegex()
}

Parameters

pattern - The pattern which is expected to have a match against the input of the search.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <T : CharSequence> Expect<T>.toContain(regexPatterns: RegexPatterns): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains a sequence which matches the given regular expression regexPatterns, using a non-disjoint search.

It is a shortcut for toContain o atLeast 1 the regexPatterns(pattern, *otherPatterns).

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 "ab" and RegexPatterns is defined as regexPatterns("a(b)?", "a(b)?") as well, then both match, even though they match the same sequence in the input of the search. Use an option such as atLeast, atMost and exactly to control the number of occurrences you expect.

Meaning you might want to use: toContain o exactly 2 regex "a(b)?" instead of: toContain o atLeast 1 the regexPatterns("a(b)?", "a(b)?")

// all regex patterns match
expect("ABC") toContain regexPatterns("A(B)?", "(B)?C")

// holds because `toContain regexPatterns(...)` does not search for unique matches
// use `toContain exactly 2 regex "A(B)?"` to check if the subject contains the regex two times
expect("ABC") toContain regexPatterns("A(B)?", "A(B)?")

fails { // because second regex doesn't match
    expect("ABC") toContain regexPatterns("A", "X")
}

Parameters

regexPatterns - The patterns which are expected to have a match against the input of the search -- use the function regexPatterns(t, ...) to create a RegexPatterns.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <T : CharSequence> Expect<T>.toContain(patterns: All<Regex>): Expect<T> (source)

Expects that the subject of this expectation (a CharSequence) contains a sequence which matches the given regular expression patterns, using a non-disjoint search.

It is a shortcut for toContain o atLeast 1 regex All(pattern, *otherPatterns).

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 "ab" and All is defined as all(Regex("a(b)?"), Regex("a(b)?")) as well, then both match, even though they match the same sequence in the input of the search. Use an option such as atLeast, atMost and exactly to control the number of occurrences you expect.

Meaning you might want to use: toContain o exactly 2 regex "a(b)?" instead of: toContain o atLeast 1 the all(Regex("a(b)?"), Regex("a(b)?"))

// all regex patterns match
expect("ABC") toContain all("A".toRegex(), "B".toRegex())

// holds because `toContain all(...)` does not search for unique matches
// use `toContain exactly 2 regex regex` to check if the subject contains the regex two times
val regex = "A(B)?".toRegex()
expect("ABC") toContain all(regex, regex)

fails { // because second regex doesn't match
    expect("ABC") toContain all("A".toRegex(), "X".toRegex())
}

Parameters

patterns - The patterns which are expected to have a match against the input of the search -- use the function all(Regex(...), ...) to create a All.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <E, T : Iterable<E>> Expect<T>.toContain(expected: E): Expect<T> (source)

Expects that the subject of this expectation (an Iterable) contains the expected value.

It is a shortcut for toContain o inAny order atLeast 1 value expected

expect(listOf("A", "B", "C")) toContain "B"

fails {
    expect(setOf(4, 2, 1)) toContain 3
}

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <E, T : Iterable<E>> Expect<T>.toContain(values: Values<E>): Expect<T> (source)

Expects that the subject of this expectation (an Iterable) contains the expected values.

It is a shortcut for toContain o inAny order atLeast 1 the values(...)

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

Meaning you might want to use: toContain o inAny order exactly 2 value 'a' instead of: toContain values('a', 'a')`

expect(listOf("A", "B", "C")) toContain values("B", "C")

fails {
    expect(setOf(4, 2, 1)) toContain values(3, 5)
}

Parameters

values - The values which are expected to be contained within the Iterable -- use the function values(t, ...) to create a Values.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <E : Any, T : Iterable<E?>> Expect<T>.toContain(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 toContain o inAny order atLeast 1 entry assertionCreatorOrNull

val list = listOf(1, 2, 2, 4)
expect(list) toContain {
    toBeGreaterThan(3)
}

fails {
    expect(list) toContain {
        toBeGreaterThan(4)
    }
}

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.

Since
0.17.0

infix fun <E : Any, T : Iterable<E?>> Expect<T>.toContain(entries: Entries<E>): Expect<T> (source)

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

It is a shortcut for toContain o inAny order atLeast 1 the entries({ ... }, ...)

expect(listOf(1, 2, 2, 4)) toContain entries(
    // multiple assertion group entries are evaluated independently
    { toBeLessThan(2) },
    { toBeGreaterThan(3) }
)



fails {
    expect(listOf(1, 2, 2, 4)) toContain entries(
        { toEqual(3) }, // fails because no element in the list equals 3
        { toEqual(5) }  // still evaluated and also fails
    )
}

Parameters

entries - The entries which are expected to be contained within the Iterable -- use the function entries(t, ...) to create an Entries.

Return
an Expect for the subject of this expectation.

Since
0.17.0

infix fun <K, V, T : Map<out K, V>> Expect<T>.toContain(keyValuePair: 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

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

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

fails { // because the map does not contain key 1 with value "b"
    expect(mapOf(1 to "a")) toContain (1 to "b")
}

Return
an Expect for the subject of this expectation.

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

Expects the subject of this expectation (a Map) contains 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 the pairs

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

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

fails { // because the map contains key 2 with value "b" and not key 1 with value "b"
    expect(mapOf(1 to "a", 2 to "b")) toContain pairs(1 to "a", 1 to "b")
}

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>.toContain(keyValue: KeyWithValueCreator<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains 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 keyValue keyValue

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

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

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>.toContain(keyValues: KeyValues<K, V>): Expect<T> (source)

Expects that the subject of this expectation (a Map) contains 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 the keyValues

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

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

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

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.