All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.dokka.testApi.context.MockContext.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package org.jetbrains.dokka.testApi.context

import org.jetbrains.dokka.DokkaConfiguration
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.DokkaPlugin
import org.jetbrains.dokka.plugability.ExtensionPoint
import org.jetbrains.dokka.utilities.DokkaConsoleLogger
import org.jetbrains.dokka.utilities.DokkaLogger
import org.jetbrains.dokka.utilities.LoggingLevel
import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.memberProperties

@Suppress("UNCHECKED_CAST") // It is only usable from tests so we do not care about safety
public class MockContext(
    vararg extensions: Pair, (DokkaContext) -> Any>,
    private val testConfiguration: DokkaConfiguration? = null,
    private val unusedExtensionPoints: List>? = null
) : DokkaContext {
    private val extensionMap by lazy {
        extensions.groupBy(Pair, (DokkaContext) -> Any>::first) {
            it.second(this)
        }
    }

    private val plugins = mutableMapOf, DokkaPlugin>()

    override fun  plugin(kclass: KClass): T = plugins.getOrPut(kclass) {
        kclass.constructors.single { it.parameters.isEmpty() }.call().also { it.injectContext(this) }
    } as T

    override fun > get(point: E): List = extensionMap[point].orEmpty() as List

    override fun > single(point: E): T = get(point).single()

    override val logger: DokkaLogger = DokkaConsoleLogger(LoggingLevel.DEBUG)

    override val configuration: DokkaConfiguration
        get() = testConfiguration ?: throw IllegalStateException("This mock context doesn't provide configuration")

    override val unusedPoints: Collection>
        get() = unusedExtensionPoints
            ?: throw IllegalStateException("This mock context doesn't provide unused extension points")
}

private fun DokkaPlugin.injectContext(context: DokkaContext) {
    (DokkaPlugin::class.memberProperties.single { it.name == "context" } as KMutableProperty<*>)
        .setter.call(this, context)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy