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

toContainRegex

fun <T : CharSequence> Expect<T>.toContainRegex(pattern: String, vararg otherPatterns: String): Expect<T> (source)

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

It is a shortcut for toContain.atLeast(1).regex(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 pattern is defined as "a(b)?" and one of the otherPatterns is defined as "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.exactly(2).regex("a(b)?") instead of: toContain.atLeast(1).regex("a(b)?", "a(b)?")

expect("ABC").toContainRegex("A(B)?")

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

expect("ABC")
    .toContainRegex("A(B)?", "(B)?C") // all regex patterns match

// holds because `toContainRegex` 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")
    .toContainRegex("A(B)?", "A(B)?")

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

Parameters

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

otherPatterns - Additional patterns which are expected to have a match against the input of the search.

Return
an Expect for the subject of this expectation.

Since
0.17.0

fun <T : CharSequence> Expect<T>.toContainRegex(pattern: Regex, vararg otherPatterns: Regex): Expect<T> (source)

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

It is a shortcut for toContain.atLeast(1).regex(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 pattern is defined as "a(b)?" and one of the otherPatterns is defined as "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.exactly(2).regex(Regex("a(b)?")) instead of: toContain.atLeast(1).regex(Regex("a(b)?"), Regex("a(b)?"))

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

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

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

// holds because `toContainRegex` 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").toContainRegex(regex, regex)

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

Parameters

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

otherPatterns - Additional patterns which are expected to have a match against the input of the search.

Return
an Expect for the subject of this expectation.

Since
0.17.0