aboutsummaryrefslogtreecommitdiff
path: root/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
diff options
context:
space:
mode:
authorsebastian.sellmair <sebastian.sellmair@jetbrains.com>2020-08-24 14:02:07 +0200
committerPaweł Marks <Kordyjan@users.noreply.github.com>2020-08-25 16:21:32 +0200
commit7196323582dce7ca3f9b07262a1f94ecd8514539 (patch)
tree0c326aee84d4727d5cd5bc5c8198b992d5b58de6 /test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
parent219e2c98f5d03fc8581fd6ce9dd870919523be44 (diff)
downloaddokka-7196323582dce7ca3f9b07262a1f94ecd8514539.tar.gz
dokka-7196323582dce7ca3f9b07262a1f94ecd8514539.tar.bz2
dokka-7196323582dce7ca3f9b07262a1f94ecd8514539.zip
- Move `test` projects into semantic parent projects
- Implement new `:test-utils` project - Resolve requirement for Android SDK installation
Diffstat (limited to 'test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt')
-rw-r--r--test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt120
1 files changed, 0 insertions, 120 deletions
diff --git a/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt b/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
deleted file mode 100644
index 67c0e692..00000000
--- a/test-tools/src/main/kotlin/matchers/content/ContentMatchersDsl.kt
+++ /dev/null
@@ -1,120 +0,0 @@
-package matchers.content
-
-import assertk.assertThat
-import assertk.assertions.contains
-import assertk.assertions.isEqualTo
-import assertk.assertions.matches
-import org.jetbrains.dokka.model.withDescendants
-import org.jetbrains.dokka.pages.*
-import org.jetbrains.dokka.test.tools.matchers.content.*
-import kotlin.reflect.KClass
-
-// entry point:
-fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) {
- val matcher = ContentMatcherBuilder(ContentComposite::class).apply(block).build()
- try {
- matcher.tryMatch(this)
- } catch (e: MatcherError) {
- throw AssertionError(e.message + "\n" + matcher.toDebugString(e.anchor, e.anchorAfter))
- }
-}
-
-
-// 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, childrenOrSkip()) { assertions.forEach { it() } }
-
- // part of DSL that cannot be defined as an extension
- operator fun String.unaryPlus() {
- children += TextMatcher(this)
- }
-
- private fun childrenOrSkip() = if (children.isEmpty() && assertions.isNotEmpty()) listOf(Anything) else children
-}
-
-fun <T : ContentComposite> ContentMatcherBuilder<T>.check(assertion: T.() -> Unit) {
- assertions += assertion
-}
-
-private val ContentComposite.extractedText
- get() = withDescendants().filterIsInstance<ContentText>().joinToString(separator = "") { it.text }
-
-fun <T : ContentComposite> ContentMatcherBuilder<T>.hasExactText(expected: String) {
- assertions += {
- assertThat(this::extractedText).isEqualTo(expected)
- }
-}
-
-fun <T : ContentComposite> ContentMatcherBuilder<T>.textMatches(pattern: Regex) {
- assertions += {
- assertThat(this::extractedText).matches(pattern)
- }
-}
-
-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) = composite(block)
-
-fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) =
- composite<ContentHeader> {
- block()
- check { if (expectedLevel != null) assertThat(this::level).isEqualTo(expectedLevel) }
- }
-
-fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) =
- composite<ContentGroup> {
- block()
- check { assertThat(this::style).contains(TextStyle.Paragraph) }
- }
-
-fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) = composite(block)
-
-fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder<ContentTable>.() -> Unit) = composite(block)
-
-fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) =
- composite<PlatformHintedContent> { group(block) }
-
-fun ContentMatcherBuilder<*>.br() = node<ContentBreakLine>()
-
-fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) {
- skipAllNotMatching()
- block()
- skipAllNotMatching()
-}
-
-fun ContentMatcherBuilder<*>.divergentGroup(block: ContentMatcherBuilder<ContentDivergentGroup>.() -> Unit) =
- composite(block)
-
-fun ContentMatcherBuilder<ContentDivergentGroup>.divergentInstance(block: ContentMatcherBuilder<ContentDivergentInstance>.() -> Unit) =
- composite(block)
-
-fun ContentMatcherBuilder<ContentDivergentInstance>.before(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) =
- composite(block)
-
-fun ContentMatcherBuilder<ContentDivergentInstance>.divergent(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) =
- composite(block)
-
-fun ContentMatcherBuilder<ContentDivergentInstance>.after(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) =
- composite(block) \ No newline at end of file