aboutsummaryrefslogtreecommitdiff
path: root/testApi/src
diff options
context:
space:
mode:
authorsebastian.sellmair <sebastian.sellmair@jetbrains.com>2020-06-29 11:12:35 +0200
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-06-29 13:19:29 +0200
commitcae0ab0b5c9358f4b7d42209d59fad337c01d9be (patch)
tree8f424313dcc5e52e6a9c7d659339cb7a7afc7860 /testApi/src
parenteabba0aa02355d75acce1d7a5195103f26c68429 (diff)
downloaddokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.tar.gz
dokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.tar.bz2
dokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.zip
Test Api, "testInline": Add support for idiomatic trimIndent and trimMargin
Diffstat (limited to 'testApi/src')
-rw-r--r--testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt48
1 files changed, 36 insertions, 12 deletions
diff --git a/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt b/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt
index 624ea914..057045a8 100644
--- a/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt
+++ b/testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt
@@ -56,8 +56,7 @@ abstract class AbstractCoreTest {
) {
val testMethods = TestBuilder().apply(block).build()
val testDirPath = getTempDir(cleanupOutput).root.toPath()
- val fileMap = query//.replace("""\n\s*\n?""".toRegex(), "\n")
- .replace("""\|/[^\w]""".toRegex()) { it.value.replace("|/", "| /") }.toFileMap()
+ val fileMap = query.toFileMap()
fileMap.materializeFiles(testDirPath.toAbsolutePath())
if (!cleanupOutput)
logger.info("Output generated under: ${testDirPath.toAbsolutePath()}")
@@ -76,15 +75,36 @@ abstract class AbstractCoreTest {
).generate()
}
- private fun String.toFileMap(): Map<String, String> = this.trimMargin().removePrefix("|")
- .replace("\r\n", "\n")
- .split("\n/")
- .map { fileString ->
- fileString.split("\n", limit = 2)
- .let {
- it.first().trim().removePrefix("/") to it.last().trim()
- }
- }.toMap()
+
+ private fun String.toFileMap(): Map<String, String> {
+ return this.trimIndent().trimMargin()
+ .replace("\r\n", "\n")
+ .sliceAt(filePathRegex)
+ .filter { it.isNotEmpty() && it.isNotBlank() && "\n" in it }
+ .map { fileDeclaration -> fileDeclaration.trim() }
+ .map { fileDeclaration ->
+ val filePathAndContent = fileDeclaration.split("\n", limit = 2)
+ val filePath = filePathAndContent.first().removePrefix("/").trim()
+ val content = filePathAndContent.last().trim()
+ filePath to content
+ }
+ .toMap()
+ }
+
+ private fun String.sliceAt(regex: Regex): List<String> {
+ val matchesStartIndices = regex.findAll(this).toList().map { match -> match.range.first }
+ return sequence {
+ yield(0)
+ yieldAll(matchesStartIndices)
+ yield(this@sliceAt.length)
+ }
+ .zipWithNext { startIndex: Int, endIndex: Int -> substring(startIndex, endIndex) }
+ .toList()
+ .also { slices ->
+ /* Post-condition verifying that no character is lost */
+ check(slices.sumBy { it.length } == length)
+ }
+ }
private fun Map<String, String>.materializeFiles(
root: Path = Paths.get("."),
@@ -233,6 +253,10 @@ abstract class AbstractCoreTest {
?.replace("file:", "")
?.replaceAfter(".jar", "")
}
+
+ companion object {
+ private val filePathRegex = Regex("""[\n^](/\w+)+(\.\w+)?\s*\n""")
+ }
}
data class TestMethods(
@@ -244,4 +268,4 @@ data class TestMethods(
val pagesGenerationStage: (RootPageNode) -> Unit,
val pagesTransformationStage: (RootPageNode) -> Unit,
val renderingStage: (RootPageNode, DokkaContext) -> Unit
-) \ No newline at end of file
+)