Skip to content

Commit

Permalink
Resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
IgnatBeresnev committed Aug 30, 2023
1 parent 2f86c2a commit c2ffa20
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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)
Expand All @@ -20,123 +20,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+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal val PsiElement.parentsWithSelf: Sequence<PsiElement>
get() = generateSequence(this) { if (it is PsiFile) null else it.parent }

@InternalDokkaApi
fun DRI.Companion.from(psi: PsiElement) = psi.parentsWithSelf.run {
public fun DRI.Companion.from(psi: PsiElement): DRI = psi.parentsWithSelf.run {
val psiMethod = firstIsInstanceOrNull<PsiMethod>()
val psiField = firstIsInstanceOrNull<PsiField>()
val classes = filterIsInstance<PsiClass>().filterNot { it is PsiTypeParameter }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import org.jetbrains.kotlin.psi.KtBlockExpression
import org.jetbrains.kotlin.psi.KtDeclarationWithBody
import org.jetbrains.kotlin.psi.KtFile

class KotlinSampleProviderFactory(val context: DokkaContext): SampleProviderFactory {
public class KotlinSampleProviderFactory(
public val context: DokkaContext
): SampleProviderFactory {
override fun build(): SampleProvider {
return KotlinSampleProvider(context)
}
Expand All @@ -28,7 +30,9 @@ class KotlinSampleProviderFactory(val context: DokkaContext): SampleProviderFact
* with [processBody] and [processImports]
*/
@InternalDokkaApi
open class KotlinSampleProvider(val context: DokkaContext): SampleProvider {
public open class KotlinSampleProvider(
public val context: DokkaContext
): SampleProvider {
private val kotlinAnalysis = SamplesKotlinAnalysis(
sourceSets = context.configuration.sourceSets,
context = context,
Expand Down Expand Up @@ -82,4 +86,4 @@ open class KotlinSampleProvider(val context: DokkaContext): SampleProvider {
override fun close() {
kotlinAnalysis.close()
}
}
}

0 comments on commit c2ffa20

Please sign in to comment.