From cae0ab0b5c9358f4b7d42209d59fad337c01d9be Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Mon, 29 Jun 2020 11:12:35 +0200 Subject: Test Api, "testInline": Add support for idiomatic trimIndent and trimMargin --- .../main/kotlin/testApi/testRunner/TestRunner.kt | 48 ++++++++++++++++------ 1 file changed, 36 insertions(+), 12 deletions(-) (limited to 'testApi/src') 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 = 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 { + 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 { + 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.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 +) -- cgit