aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/utils
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/base/src/test/kotlin/utils')
-rw-r--r--plugins/base/src/test/kotlin/utils/ModelUtils.kt33
-rw-r--r--plugins/base/src/test/kotlin/utils/TestUtils.kt68
2 files changed, 101 insertions, 0 deletions
diff --git a/plugins/base/src/test/kotlin/utils/ModelUtils.kt b/plugins/base/src/test/kotlin/utils/ModelUtils.kt
new file mode 100644
index 00000000..6893c65f
--- /dev/null
+++ b/plugins/base/src/test/kotlin/utils/ModelUtils.kt
@@ -0,0 +1,33 @@
+package utils
+
+import org.jetbrains.dokka.model.Module
+import org.jetbrains.dokka.model.doc.DocumentationNode
+import testApi.testRunner.AbstractCoreTest
+
+abstract class AbstractModelTest(val path: String? = null, val pkg: String) : ModelDSL(), AssertDSL {
+
+ fun inlineModelTest(
+ query: String,
+ platform: String = "jvm",
+ targetList: List<String> = listOf("jvm"),
+ prependPackage: Boolean = true,
+ block: Module.() -> Unit
+ ) {
+ val configuration = dokkaConfiguration {
+ passes {
+ pass {
+ sourceRoots = listOf("src/")
+ analysisPlatform = platform
+ targets = targetList
+ }
+ }
+ }
+ val prepend = path.let { p -> p?.let { "|$it\n" } ?: "" } + if(prependPackage) "|package $pkg" else ""
+
+ testInline(("$prepend\n$query").trim().trimIndent(), configuration) {
+ documentablesTransformationStage = block
+ }
+ }
+
+
+}
diff --git a/plugins/base/src/test/kotlin/utils/TestUtils.kt b/plugins/base/src/test/kotlin/utils/TestUtils.kt
new file mode 100644
index 00000000..641c68a2
--- /dev/null
+++ b/plugins/base/src/test/kotlin/utils/TestUtils.kt
@@ -0,0 +1,68 @@
+package utils
+
+import org.jetbrains.dokka.model.Class
+import org.jetbrains.dokka.model.Documentable
+import org.jetbrains.dokka.model.Function
+import org.jetbrains.dokka.model.Property
+import org.jetbrains.dokka.model.doc.*
+import testApi.testRunner.AbstractCoreTest
+import kotlin.reflect.KClass
+import kotlin.reflect.full.safeCast
+
+@DslMarker
+annotation class TestDSL
+
+@TestDSL
+abstract class ModelDSL : AbstractCoreTest() {
+ operator fun Documentable?.div(name: String): Documentable? =
+ this?.children?.find { it.name == name }
+
+ inline fun <reified T : Documentable> Documentable?.cast(): T =
+ (this as? T).assertNotNull()
+}
+
+@TestDSL
+interface AssertDSL {
+ infix fun Any?.equals(other: Any?) = this.assertEqual(other)
+ infix fun Collection<Any>?.allEquals(other: Any?) =
+ this?.also { c -> c.forEach { it equals other } } ?: run { assert(false) { "Collection is empty" } }
+
+ infix fun <T> Collection<T>?.counts(n: Int) = this.orEmpty().assertCount(n)
+
+ infix fun <T> T?.notNull(name: String): T = this.assertNotNull(name)
+
+ fun <T> Collection<T>.assertCount(n: Int, prefix: String = "") =
+ assert(count() == n) { "${prefix}Expected $n, got ${count()}" }
+
+ fun <T> T?.assertEqual(expected: T, prefix: String = "") = assert(this == expected) {
+ "${prefix}Expected $expected, got $this"
+ }
+}
+
+inline fun <reified T : Any> Any?.assertIsInstance(name: String): T =
+ this.let { it as? T } ?: throw AssertionError("$name should not be null")
+
+fun List<DocumentationNode>.commentsToString(): String =
+ this.flatMap { it.children }.joinToString(separator = "\n") { it.root.docTagSummary() }
+
+fun TagWrapper.text(): String = when (val t = this) {
+ is NamedTagWrapper -> "${t.name}: [${t.root.text()}]"
+ else -> t.root.text()
+}
+
+fun DocTag.text(): String = when (val t = this) {
+ is Text -> t.body
+ is Code -> t.children.joinToString("\n") { it.text() }
+ is P -> t.children.joinToString(separator = "\n") { it.text() }
+ else -> t.toString()
+}
+
+fun <T : Documentable> T?.comments(): String = docs().map { it.text() }
+ .joinToString(separator = "\n") { it }
+
+fun <T> T?.assertNotNull(name: String = ""): T = this ?: throw AssertionError("$name should not be null")
+
+fun <T : Documentable> T?.docs() = this?.documentation.orEmpty().values.flatMap { it.children }
+
+val Class.supers
+ get() = supertypes.flatMap{it.component2()} \ No newline at end of file