toContain

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

Return

The newly created builder.

Since

0.17.0

Parameters

o

The filler object o.

Samples

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

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.

Return

an Expect for the subject of this expectation.

Since

0.17.0

Throws

Samples

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

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")

Return

an Expect for the subject of this expectation.

Since

0.17.0

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.

Throws

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

Samples

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

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.

Return

an Expect for the subject of this expectation.

Since

0.17.0

Parameters

pattern

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

Samples

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

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

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)?")

Return

an Expect for the subject of this expectation.

Since

0.17.0

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.

Samples

// 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")
}

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)?"))

Return

an Expect for the subject of this expectation.

Since

0.17.0

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.

Samples

// 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())
}

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

Return

The newly created builder.

Since

0.17.0

Parameters

o

The filler object o.

Samples

expect(listOf("A", "B")) toContain o inGiven order and only the values("A", "B")

fails {
    expect(listOf(5, 3, 2, 2, 4)) toContain o inAny order atMost 2 entry {
        it toBeGreaterThan 2
    }

}

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

Return

an Expect for the subject of this expectation.

Since

0.17.0

Samples

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

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

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')`

Return

an Expect for the subject of this expectation.

Since

0.17.0

Parameters

values

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

Samples

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

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

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

Return

an Expect for the subject of this expectation.

Since

0.17.0

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.

Samples

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

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

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({ ... }, ...)

Return

an Expect for the subject of this expectation.

Since

0.17.0

Parameters

entries

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

Samples

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



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

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

Return

The newly created builder.

Samples

expect(mapOf(1 to "a")) toContain o inAny order entry (1 to "a")

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

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'.

Return

an Expect for the subject of this expectation.

Samples

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

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.

Return

an Expect for the subject of this expectation.

Parameters

pairs

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

Samples

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

infix inline fun <K, 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

Return

an Expect for the subject of this expectation.

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.

Samples

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

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

infix inline fun <K, 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.

Return

an Expect for the subject of this expectation.

Parameters

keyValues

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

Samples

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

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