aboutsummaryrefslogtreecommitdiff
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
parenteabba0aa02355d75acce1d7a5195103f26c68429 (diff)
downloaddokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.tar.gz
dokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.tar.bz2
dokka-cae0ab0b5c9358f4b7d42209d59fad337c01d9be.zip
Test Api, "testInline": Add support for idiomatic trimIndent and trimMargin
-rw-r--r--plugins/base/src/test/kotlin/transformers/ReportUndocumentedTransformerTest.kt52
-rw-r--r--testApi/src/main/kotlin/testApi/testRunner/TestRunner.kt48
2 files changed, 62 insertions, 38 deletions
diff --git a/plugins/base/src/test/kotlin/transformers/ReportUndocumentedTransformerTest.kt b/plugins/base/src/test/kotlin/transformers/ReportUndocumentedTransformerTest.kt
index fac1e060..523813fc 100644
--- a/plugins/base/src/test/kotlin/transformers/ReportUndocumentedTransformerTest.kt
+++ b/plugins/base/src/test/kotlin/transformers/ReportUndocumentedTransformerTest.kt
@@ -26,7 +26,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|package sample
|
|class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -53,7 +53,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|package sample
|
|internal class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -82,7 +82,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|class X {
| fun x()
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -112,7 +112,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|class X {
| val x: Int = 0
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -141,7 +141,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/** Documented */
|class X(private val x: Int) {
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -169,7 +169,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/** Documented */
|data class X(val x: Int) {
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -200,7 +200,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|class X {
| constructor(unit: Unit) : this()
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -233,7 +233,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|
|/** Documented */
|class B : A()
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -266,7 +266,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|
|/** Documented */
|class B : A()
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -303,7 +303,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|class B : A() {
| override fun a() = throw Exception()
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -339,7 +339,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|class B : A() {
| override val a = 1
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -365,7 +365,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|package sample
|
|class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -395,7 +395,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|package sample
|
|class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -432,7 +432,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/main/kotlin/sample/enabled/Enabled.kt
|package sample.enabled
|class Enabled
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -470,7 +470,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/main/kotlin/sample/enabled/Enabled.kt
|package sample.enabled
|class Enabled
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -512,7 +512,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/jvmMain/kotlin/sample/JvmMain.kt
|package sample
|actual class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -556,7 +556,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/jvmMain/kotlin/sample/JvmMain.kt
|package sample
|actual class X
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -611,7 +611,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/jvmMain/kotlin/sample/JvmMain.kt
|package sample
|actual fun x() = Unit
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -638,7 +638,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/main/java/sample/Test.java
|package sample
|public class Test { }
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -665,7 +665,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|/src/main/java/sample/Test.java
|package sample
|class Test { }
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -694,7 +694,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
| public Test() {
| }
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -722,7 +722,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|public class X {
| public void x { }
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -752,7 +752,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|public class X {
| public int x = 0;
|}
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -790,7 +790,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
| public void x() {}
|}
|
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -830,7 +830,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
|
|}
|
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
@@ -868,7 +868,7 @@ class ReportUndocumentedTransformerTest : AbstractCoreTest() {
| public void x() {}
|}
|
- """.trimIndent(),
+ """.trimMargin(),
configuration
) {
pagesTransformationStage = {
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
+)