From 02f30b142aa467d3a24cc52a1fe3f2fed7ea1e33 Mon Sep 17 00:00:00 2001 From: Ignat Beresnev Date: Thu, 31 Aug 2023 20:16:01 +0200 Subject: Enable explicit API mode (#3139) --- .../kotlin/matchers/content/ContentMatchersDsl.kt | 116 ++++++++++++++------- .../kotlin/matchers/content/contentMatchers.kt | 22 ++-- 2 files changed, 90 insertions(+), 48 deletions(-) (limited to 'core/content-matcher-test-utils/src/main/kotlin/matchers') 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.() -> Unit) { +public fun ContentNode.assertNode(block: ContentMatcherBuilder.() -> Unit) { val matcher = ContentMatcherBuilder(ContentComposite::class).apply(block).build() try { matcher.tryMatch(this) @@ -24,123 +24,161 @@ fun ContentNode.assertNode(block: ContentMatcherBuilder.() -> // DSL: @DslMarker -annotation class ContentMatchersDsl +public annotation class ContentMatchersDsl @ContentMatchersDsl -class ContentMatcherBuilder @PublishedApi internal constructor(private val kclass: KClass) { +public class ContentMatcherBuilder @PublishedApi internal constructor(private val kclass: KClass) { @PublishedApi - internal val children = mutableListOf() + internal val children: MutableList = mutableListOf() internal val assertions = mutableListOf Unit>() - fun build() = CompositeMatcher(kclass, childrenOrSkip()) { assertions.forEach { it() } } + public fun build(): CompositeMatcher = 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 ContentMatcherBuilder.check(assertion: T.() -> Unit) { +public fun ContentMatcherBuilder.check(assertion: T.() -> Unit) { assertions += assertion } private val ContentComposite.extractedText get() = withDescendants().filterIsInstance().joinToString(separator = "") { it.text } -fun ContentMatcherBuilder.hasExactText(expected: String) { +public fun ContentMatcherBuilder.hasExactText(expected: String) { assertions += { assertEquals(expected, this.extractedText) } } -inline fun ContentMatcherBuilder<*>.composite( +public inline fun ContentMatcherBuilder<*>.composite( block: ContentMatcherBuilder.() -> Unit ) { children += ContentMatcherBuilder(S::class).apply(block).build() } -inline fun ContentMatcherBuilder<*>.node(noinline assertions: S.() -> Unit = {}) { +public inline fun 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.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.group(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.tabbedGroup( +public fun ContentMatcherBuilder<*>.tabbedGroup( block: ContentMatcherBuilder.() -> Unit -) = composite { - block() - check { assertContains(this.style, ContentStyle.TabbedContent) } +) { + composite { + block() + check { assertContains(this.style, ContentStyle.TabbedContent) } + } } -fun ContentMatcherBuilder<*>.tab( +public fun ContentMatcherBuilder<*>.tab( tabbedContentType: TabbedContentType, block: ContentMatcherBuilder.() -> Unit -) = composite { - block() - check { - assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) +) { + composite { + block() + check { + assertEquals(tabbedContentType, this.extra[TabbedContentTypeExtra]?.value) + } } } -fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder<*>.header(expectedLevel: Int? = null, block: ContentMatcherBuilder.() -> Unit) { composite { block() check { if (expectedLevel != null) assertEquals(expectedLevel, this.level) } } +} -fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder<*>.p(block: ContentMatcherBuilder.() -> Unit) { composite { block() check { assertContains(this.style, TextStyle.Paragraph) } } +} -fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.link(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.table(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder<*>.platformHinted(block: ContentMatcherBuilder.() -> Unit) { composite { group(block) } +} -fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.list(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.codeBlock(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder.() -> Unit) = composite(block) +public fun ContentMatcherBuilder<*>.codeInline(block: ContentMatcherBuilder.() -> Unit) { + composite(block) +} -fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder.() -> Unit) = composite { - block() - check { assertContains(this.style, ContentStyle.Caption) } +public fun ContentMatcherBuilder<*>.caption(block: ContentMatcherBuilder.() -> Unit) { + composite { + block() + check { assertContains(this.style, ContentStyle.Caption) } + } } -fun ContentMatcherBuilder<*>.br() = node() +public fun ContentMatcherBuilder<*>.br() { + node() +} -fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { +public fun ContentMatcherBuilder<*>.somewhere(block: ContentMatcherBuilder<*>.() -> Unit) { skipAllNotMatching() block() skipAllNotMatching() } -fun ContentMatcherBuilder<*>.divergentGroup(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder<*>.divergentGroup( + block: ContentMatcherBuilder.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder.divergentInstance(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder.divergentInstance( + block: ContentMatcherBuilder.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder.before(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder.before( + block: ContentMatcherBuilder.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder.divergent(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder.divergent( + block: ContentMatcherBuilder.() -> Unit +) { composite(block) +} -fun ContentMatcherBuilder.after(block: ContentMatcherBuilder.() -> Unit) = +public fun ContentMatcherBuilder.after( + block: ContentMatcherBuilder.() -> 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( - val kclass: KClass, - val assertions: T.() -> Unit = {} +public open class NodeMatcher( + public val kclass: KClass, + 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( } } -class CompositeMatcher( +public class CompositeMatcher( kclass: KClass, private val children: List, assertions: T.() -> Unit = {} ) : NodeMatcher(kclass, assertions) { + internal val normalizedChildren: List by lazy { children.fold(listOf()) { acc, e -> when { @@ -61,7 +65,7 @@ class CompositeMatcher( } } -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, -- cgit