Skip to content

Commit d529997

Browse files
Gooolerevant
authored andcommitted
Add doesNotContainMatch assertion for CharSequence
1 parent 6b65668 commit d529997

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

assertk/src/commonMain/kotlin/assertk/assertions/charsequence.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,14 @@ fun Assert<CharSequence>.doesNotContain(expected: Iterable<CharSequence>, ignore
117117
expected("to not contain:${show(expected)} but was:${show(actual)}")
118118
}
119119

120+
/**
121+
* Asserts the char sequence does not contain the expected regular expression.
122+
*/
123+
fun Assert<CharSequence>.doesNotContainMatch(regex: Regex) = given { actual ->
124+
if (!regex.containsMatchIn(actual)) return
125+
expected("to not contain match:${show(regex)} but was:${show(actual)}")
126+
}
127+
120128
/**
121129
* Asserts the char sequence starts with the expected char sequence.
122130
* @param ignoreCase true to compare ignoring case, the default if false.

assertk/src/commonTest/kotlin/test/assertk/assertions/CharSequenceTest.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,22 @@ class CharSequenceTest {
223223
}
224224
//endregion
225225

226+
//region doesNotContainMatch
227+
@Test
228+
fun doesNotContainMatch_non_matching_value_passes() {
229+
assertThat("abcd").doesNotContainMatch(Regex("\\d\\d\\d\\d"))
230+
}
231+
232+
@Test
233+
fun doesNotContainMatch_matching_value_fails() {
234+
val regex = Regex("\\d\\d\\d\\d")
235+
val error = assertFailsWith<AssertionError> {
236+
assertThat("1234").doesNotContainMatch(regex)
237+
}
238+
assertEquals("expected to not contain match:${show(regex)} but was:<\"1234\">", error.message)
239+
}
240+
//endregion
241+
226242
//region startsWith
227243
@Test
228244
fun startsWith_value_prefix_passes() {

0 commit comments

Comments
 (0)