aboutsummaryrefslogtreecommitdiff
path: root/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-03-25 16:11:21 +0100
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-03-31 16:02:09 +0200
commit136a835671ef0cdf09c3475db1b61e15aee1be48 (patch)
tree5884860a75c1f835cf6139539a7625ade7d95e49 /test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
parenta8e5b94f8e95138dbfa913b74a4d2d65fd5993c1 (diff)
downloaddokka-136a835671ef0cdf09c3475db1b61e15aee1be48.tar.gz
dokka-136a835671ef0cdf09c3475db1b61e15aee1be48.tar.bz2
dokka-136a835671ef0cdf09c3475db1b61e15aee1be48.zip
Add test-tools module and matchers for content inside of it
Diffstat (limited to 'test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt')
-rw-r--r--test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt61
1 files changed, 61 insertions, 0 deletions
diff --git a/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt b/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
new file mode 100644
index 00000000..ae3ecab7
--- /dev/null
+++ b/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
@@ -0,0 +1,61 @@
+package matchers.content
+
+import org.jetbrains.dokka.pages.ContentComposite
+import org.jetbrains.dokka.pages.ContentGroup
+import org.jetbrains.dokka.pages.ContentNode
+import org.jetbrains.dokka.test.tools.matchers.content.*
+import kotlin.reflect.KClass
+
+// entry point:
+fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) {
+ ContentMatcherBuilder(ContentComposite::class).apply(block).build().tryMatch(this)
+}
+
+
+// DSL:
+@DslMarker
+annotation class ContentMatchersDsl
+
+@ContentMatchersDsl
+class ContentMatcherBuilder<T : ContentComposite> @PublishedApi internal constructor(private val kclass: KClass<T>) {
+ @PublishedApi
+ internal val children = mutableListOf<MatcherElement>()
+ internal val assertions = mutableListOf<T.() -> Unit>()
+
+ fun build() = CompositeMatcher(kclass, children) { assertions.forEach { it() } }
+
+ // part of DSL that cannot be defined as an extension
+ operator fun String.unaryPlus() {
+ children += TextMatcher(this)
+ }
+}
+
+fun <T : ContentComposite> ContentMatcherBuilder<T>.check(assertion: T.() -> Unit) {
+ assertions += assertion
+}
+
+inline fun <reified S : ContentComposite> ContentMatcherBuilder<*>.composite(
+ block: ContentMatcherBuilder<S>.() -> Unit
+) {
+ children += ContentMatcherBuilder(S::class).apply(block).build()
+}
+
+inline fun <reified S : ContentNode> ContentMatcherBuilder<*>.node(noinline assertions: S.() -> Unit) {
+ children += NodeMatcher(S::class, assertions)
+}
+
+fun ContentMatcherBuilder<*>.skipAllNotMatching() {
+ children += Anything
+}
+
+
+// Convenience functions:
+fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) {
+ children += ContentMatcherBuilder(ContentGroup::class).apply(block).build()
+}
+
+fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) {
+ skipAllNotMatching()
+ block()
+ skipAllNotMatching()
+} \ No newline at end of file