diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2023-08-31 20:16:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-31 20:16:01 +0200 |
commit | 02f30b142aa467d3a24cc52a1fe3f2fed7ea1e33 (patch) | |
tree | 66f6d6f089a93b863bf1144666491eca6729ad05 /core/content-matcher-test-utils/src/main/kotlin/matchers | |
parent | 6a181a7a2b03ec263788d137610e86937a57d434 (diff) | |
download | dokka-02f30b142aa467d3a24cc52a1fe3f2fed7ea1e33.tar.gz dokka-02f30b142aa467d3a24cc52a1fe3f2fed7ea1e33.tar.bz2 dokka-02f30b142aa467d3a24cc52a1fe3f2fed7ea1e33.zip |
Enable explicit API mode (#3139)
Diffstat (limited to 'core/content-matcher-test-utils/src/main/kotlin/matchers')
-rw-r--r-- | core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt | 116 | ||||
-rw-r--r-- | core/content-matcher-test-utils/src/main/kotlin/matchers/content/contentMatchers.kt | 22 |
2 files changed, 90 insertions, 48 deletions
diff --git a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt index 5a8de9ce..026f7b6b 100644 --- a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt +++ b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/ContentMatchersDsl.kt @@ -12,7 +12,7 @@ import kotlin.test.assertEquals import kotlin.test.asserter // entry point: -fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) { +public fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) { val matcher = ContentMatcherBuilder(ContentComposite::class).apply(block).build() try { matcher.tryMatch(this) @@ -24,123 +24,161 @@ fun ContentNode.assertNode(block: ContentMatcherBuilder<ContentComposite>.() -> // DSL: @DslMarker -annotation class ContentMatchersDsl +public annotation class ContentMatchersDsl @ContentMatchersDsl -class ContentMatcherBuilder<T : ContentComposite> @PublishedApi internal constructor(private val kclass: KClass<T>) { +public class ContentMatcherBuilder<T : ContentComposite> @PublishedApi internal constructor(private val kclass: KClass<T>) { @PublishedApi - internal val children = mutableListOf<MatcherElement>() + internal val children: MutableList<MatcherElement> = mutableListOf() internal val assertions = mutableListOf<T.() -> Unit>() - fun build() = CompositeMatcher(kclass, childrenOrSkip()) { assertions.forEach { it() } } + public fun build(): CompositeMatcher<T> = CompositeMatcher(kclass, childrenOrSkip()) { assertions.forEach { it() } } // part of DSL that cannot be defined as an extension - operator fun String.unaryPlus() { + public 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) { +public 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) { +public fun <T : ContentComposite> ContentMatcherBuilder<T>.hasExactText(expected: String) { assertions += { assertEquals(expected, this.extractedText) } } -inline fun <reified S : ContentComposite> ContentMatcherBuilder<*>.composite( +public 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 = {}) { +public inline fun <reified S : ContentNode> ContentMatcherBuilder<*>.node(noinline assertions: S.() -> Unit = {}) { children += NodeMatcher(S::class, assertions) } -fun ContentMatcherBuilder<*>.skipAllNotMatching() { +public fun ContentMatcherBuilder<*>.skipAllNotMatching() { children += Anything } // Convenience functions: -fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.tabbedGroup( +public fun ContentMatcherBuilder<*>.tabbedGroup( block: ContentMatcherBuilder<ContentGroup>.() -> Unit -) = composite<ContentGroup> { - block() - check { assertContains(this.style, ContentStyle.TabbedContent) } +) { + composite<ContentGroup> { + block() + check { assertContains(this.style, ContentStyle.TabbedContent) } + } } -fun ContentMatcherBuilder<*>.tab( +public fun ContentMatcherBuilder<*>.tab( tabbedContentType: TabbedContentType, block: ContentMatcherBuilder<ContentGroup>.() -> Unit -) = composite<ContentGroup> { - block() - check { - assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) +) { + composite<ContentGroup> { + block() + check { + assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) + } } } -fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) = +public fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder<ContentHeader>.() -> Unit) { composite<ContentHeader> { block() check { if (expectedLevel != null) assertEquals(expectedLevel, this.level) } } +} -fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = +public fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { composite<ContentGroup> { block() check { assertContains(this.style, TextStyle.Paragraph) } } +} -fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder<ContentLink>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder<ContentTable>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder<ContentTable>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = +public fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { composite<PlatformHintedContent> { group(block) } +} -fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder<ContentList>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder<ContentList>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder<ContentCodeBlock>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder<ContentCodeBlock>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder<ContentCodeInline>.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder<ContentCodeInline>.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) = composite<ContentGroup> { - block() - check { assertContains(this.style, ContentStyle.Caption) } +public fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder<ContentGroup>.() -> Unit) { + composite<ContentGroup> { + block() + check { assertContains(this.style, ContentStyle.Caption) } + } } -fun ContentMatcherBuilder<*>.br() = node<ContentBreakLine>() +public fun ContentMatcherBuilder<*>.br() { + node<ContentBreakLine>() +} -fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { +public fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { skipAllNotMatching() block() skipAllNotMatching() } -fun ContentMatcherBuilder<*>.divergentGroup(block: ContentMatcherBuilder<ContentDivergentGroup>.() -> Unit) = +public fun ContentMatcherBuilder<*>.divergentGroup( + block: ContentMatcherBuilder<ContentDivergentGroup>.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder<ContentDivergentGroup>.divergentInstance(block: ContentMatcherBuilder<ContentDivergentInstance>.() -> Unit) = +public fun ContentMatcherBuilder<ContentDivergentGroup>.divergentInstance( + block: ContentMatcherBuilder<ContentDivergentInstance>.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder<ContentDivergentInstance>.before(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = +public fun ContentMatcherBuilder<ContentDivergentInstance>.before( + block: ContentMatcherBuilder<ContentComposite>.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder<ContentDivergentInstance>.divergent(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = +public fun ContentMatcherBuilder<ContentDivergentInstance>.divergent( + block: ContentMatcherBuilder<ContentComposite>.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder<ContentDivergentInstance>.after(block: ContentMatcherBuilder<ContentComposite>.() -> Unit) = +public fun ContentMatcherBuilder<ContentDivergentInstance>.after( + block: ContentMatcherBuilder<ContentComposite>.() -> Unit +) { composite(block) +} /* * TODO replace with kotlin.test.assertContains after migrating to Kotlin language version 1.5+ diff --git a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/contentMatchers.kt b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/contentMatchers.kt index a6549b31..412f728b 100644 --- a/core/content-matcher-test-utils/src/main/kotlin/matchers/content/contentMatchers.kt +++ b/core/content-matcher-test-utils/src/main/kotlin/matchers/content/contentMatchers.kt @@ -14,15 +14,18 @@ import kotlin.reflect.KClass import kotlin.reflect.full.cast import kotlin.reflect.full.safeCast -sealed class MatcherElement +public sealed class MatcherElement -class TextMatcher(val text: String) : MatcherElement() +public class TextMatcher( + public val text: String +) : MatcherElement() -open class NodeMatcher<T : ContentNode>( - val kclass: KClass<T>, - val assertions: T.() -> Unit = {} +public open class NodeMatcher<T : ContentNode>( + public val kclass: KClass<T>, + public val assertions: T.() -> Unit = {} ) : MatcherElement() { - open fun tryMatch(node: ContentNode) { + + public open fun tryMatch(node: ContentNode) { kclass.safeCast(node)?.apply { try { assertions() @@ -37,11 +40,12 @@ open class NodeMatcher<T : ContentNode>( } } -class CompositeMatcher<T : ContentComposite>( +public class CompositeMatcher<T : ContentComposite>( kclass: KClass<T>, private val children: List<MatcherElement>, assertions: T.() -> Unit = {} ) : NodeMatcher<T>(kclass, assertions) { + internal val normalizedChildren: List<MatcherElement> by lazy { children.fold(listOf()) { acc, e -> when { @@ -61,7 +65,7 @@ class CompositeMatcher<T : ContentComposite>( } } -object Anything : MatcherElement() +public object Anything : MatcherElement() private sealed class MatchWalkerState { abstract fun next(node: ContentNode): MatchWalkerState @@ -176,7 +180,7 @@ private fun ContentNode.debugRepresentation() = asPrintableTree { element -> ) } -data class MatcherError( +public data class MatcherError( override val message: String, val anchor: MatcherElement, val anchorAfter: Boolean = false, |