Skip to content

Commit d38f84d

Browse files
committed
add String.replaceOrFail
1 parent 68a3824 commit d38f84d

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/FancyShould.kt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -49,10 +49,15 @@ fun <T, U : T> T.trimmedShouldBe(expected: U?, vararg excludeFromStack: KClass<*
4949
}
5050
}
5151

52-
fun <T> T.trimmedAssert(
52+
fun <R> trimmedAssert(
5353
vararg excludeFromStack: KClass<*>,
54-
assertion: suspend T.() -> Unit
55-
) {
54+
assertion: suspend () -> R
55+
): R = Unit.trimmedAssert(*excludeFromStack) { assertion() }
56+
57+
fun <T, R> T.trimmedAssert(
58+
vararg excludeFromStack: KClass<*>,
59+
assertion: suspend T.() -> R
60+
): R {
5661

5762
/*
5863
Any AssertionError generated by this function will have this function at the top of its stacktrace.
@@ -61,7 +66,7 @@ fun <T> T.trimmedAssert(
6166
6267
So, we can catch the assertion error, remove this function from the stacktrace, and rethrow.
6368
*/
64-
try {
69+
return try {
6570
runBlocking { assertion() }
6671
} catch (assertionError: AssertionError) {
6772

modulecheck-internal-testing/src/main/kotlin/modulecheck/testing/asserts.kt

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2021-2022 Rick Busarow
2+
* Copyright (C) 2021-2023 Rick Busarow
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -15,13 +15,66 @@
1515

1616
package modulecheck.testing
1717

18+
import io.kotest.assertions.asClue
19+
import io.kotest.matchers.shouldNotBe
1820
import org.junit.jupiter.api.fail
1921

2022
fun <T : Any> T?.requireNotNullOrFail(
2123
lazyMessage: () -> String = { "The receiver cannot be null, but it was. ¯\\_(ツ)_/¯" }
2224
): T {
23-
2425
if (this != null) return this
2526

2627
fail(lazyMessage)
2728
}
29+
30+
/**
31+
* Asserts that the receiver string is changed after calling [String.replace]
32+
*
33+
* @since 0.12.4
34+
*/
35+
fun String.replaceOrFail(oldValue: String, replacement: String): String {
36+
return assertChanged(
37+
oldString = this@replaceOrFail,
38+
newString = replace(oldValue, replacement),
39+
token = oldValue,
40+
replacement = replacement
41+
)
42+
}
43+
44+
/**
45+
* Asserts that the receiver string is changed after calling [String.replace]
46+
*
47+
* @since 0.12.4
48+
*/
49+
fun String.replaceOrFail(regex: Regex, replacement: String): String {
50+
return assertChanged(
51+
oldString = this@replaceOrFail,
52+
newString = replace(regex, replacement),
53+
token = regex,
54+
replacement = replacement
55+
)
56+
}
57+
58+
private fun assertChanged(
59+
oldString: String,
60+
newString: String,
61+
token: Any,
62+
replacement: String
63+
) = newString.also { new ->
64+
trimmedAssert {
65+
66+
val tokenName = (if (token is Regex) "regex" else "oldValue").padStart(9)
67+
68+
"""
69+
|String replacement did not change the original string.
70+
|
71+
| $tokenName: $token
72+
| replacement: $replacement
73+
|
74+
|original string (starting on the new line):
75+
|$oldString
76+
|____________________________________________________
77+
|""".replaceIndentByMargin()
78+
.asClue { new shouldNotBe oldString }
79+
}
80+
}

0 commit comments

Comments
 (0)