toContain
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 toContain.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 toContain
to create a more sophisticated toContain
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: toContain.exactly(2).value("a")
instead of: toContain("a", "a")
Return
an Expect for the subject of this
expectation.
Since
0.17.0
Throws
in case expected or one of the otherExpected is not a CharSequence, Number or Char.
Samples
expect("ABC").toContain("B")
expect("ABC123").toContain("AB", 'C', 12)
// holds because `toContain` does not search for unique matches
// use `toContain.exactly(2).value("A")` to check if the subject contains two "A"s
expect("ABC").toContain("A", "A")
fails {
expect("ABC").toContain("X")
}
fails { // because subject does not contain all values
expect("ABC").toContain("A", 99)
}
Expects that the subject of this
expectation (an Iterable) contains the expected value and the otherExpected values (if given).
It is a shortcut for toContain.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: toContain.inAnyOrder.exactly(2).value('a')
instead of: toContain('a', 'a')
Return
an Expect for the subject of this
expectation.
Since
0.17.0
Samples
expect(listOf("A", "B", "C")).toContain("B", "C")
fails {
expect(setOf(4, 2, 1)).toContain(3)
}
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.inAnyOrder.atLeast(1).entry(assertionCreatorOrNull)
Return
an Expect for the subject of this
expectation.
Since
0.17.0
Parameters
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 { toBeGreaterThan(3) }
fails {
expect(list).toContain { toBeGreaterThan(4) }
}
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 toContain.inAnyOrder.atLeast(1).entries(assertionCreatorOrNull, *otherAssertionCreatorsOrNulls)
Return
an Expect for the subject of this
expectation.
Since
0.17.0
Parameters
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.
Additional identification lambdas which each identify (separately) an entry which we are looking for (see assertionCreatorOrNull for more information).
Samples
expect(listOf(1, 2, 2, 4))
.toContain( // multiple expectation-group entries are evaluated independently
{ toBeLessThan(2) },
{ toBeGreaterThan(3) }
)
fails {
expect(listOf(1, 2, 2, 4))
.toContain(
{ toEqual(3) }, // fails because no element in the list equals 3
{ toEqual(5) } // still evaluated and also fails
)
}
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 toContain.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.
Return
an Expect for the subject of this
expectation.
Since
0.17.0
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")
}
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 toContain.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.
Return
an Expect for the subject of this
expectation.
Since
0.17.0
Samples
expect(mapOf(1 to "a"))
.toContain(KeyValue(1) { // subject inside this expectation-group is of type String (actually "a")
toEqual("a")
})
fails {
expect(mapOf(1 to "a"))
.toContain(KeyValue(1) { // subject inside this expectation-group is of type String (actually "a")
toEqual("b") // fails because "a" is not equal to "b"
})
}
Starts a sophisticated toContain
assertion building process based on this Expect.
Return
The newly created builder.
Since
0.17.0
Samples
expect("ABC")
.toContain.exactly(1).value("A")
expect("ABBC")
.toContain.atLeast(2).value("B")
fails {
expect("AAAAAA")
.toContain.atMost(3).value("A")
}
Starts a sophisticated toContain
assertion building process based on this Expect.
Return
The newly created builder.
Since
0.17.0
Samples
expect(listOf("A", "B"))
.toContain
.inOrder // order specifier
.only
.values("A", "B")
fails {
expect(listOf(5, 3, 2, 2, 4))
.toContain
.inAnyOrder // order specifier
.atMost(2)
.entry { // expectation-group about the entry
toBeGreaterThan(2)
}
}
Starts a sophisticated toContain
expectation building process based on this Expect.
Return
The newly created builder.
Since
0.17.0
Samples
expect(mapOf(1 to "a")).toContain.inAnyOrder.entries(1 to "a")
fails { // because the map does not contain key 1 with value "b"
expect(mapOf(1 to "a")).toContain.inAnyOrder.entries(1 to "b")
}