diff options
author | Paweł Marks <pmarks@virtuslab.com> | 2020-03-02 17:10:16 +0100 |
---|---|---|
committer | Paweł Marks <Kordyjan@users.noreply.github.com> | 2020-03-04 14:28:14 +0100 |
commit | 973cc5238e2f7ede6d9cf54437785770a3e020c9 (patch) | |
tree | a1e3e9176c37b33a4c900b3abf1f23f501b05931 /testApi/src/main/kotlin | |
parent | 1347329aa18203e9657e096447e0e30ae759b137 (diff) | |
download | dokka-973cc5238e2f7ede6d9cf54437785770a3e020c9.tar.gz dokka-973cc5238e2f7ede6d9cf54437785770a3e020c9.tar.bz2 dokka-973cc5238e2f7ede6d9cf54437785770a3e020c9.zip |
Tests for wrapping groups in renderer. Also util for groups
Diffstat (limited to 'testApi/src/main/kotlin')
-rw-r--r-- | testApi/src/main/kotlin/testApi/context/MockContext.kt | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/testApi/src/main/kotlin/testApi/context/MockContext.kt b/testApi/src/main/kotlin/testApi/context/MockContext.kt index 4f481bce..758a4311 100644 --- a/testApi/src/main/kotlin/testApi/context/MockContext.kt +++ b/testApi/src/main/kotlin/testApi/context/MockContext.kt @@ -8,23 +8,30 @@ import org.jetbrains.dokka.plugability.DokkaPlugin import org.jetbrains.dokka.plugability.ExtensionPoint import org.jetbrains.dokka.utilities.DokkaConsoleLogger 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 class MockContext( - vararg extensions: Pair<ExtensionPoint<*>, Any>, + vararg extensions: Pair<ExtensionPoint<*>, (DokkaContext) -> Any>, private val testConfiguration: DokkaConfiguration? = null, private val testPlatforms: Map<PlatformData, EnvironmentAndFacade>? = null ) : DokkaContext { - private val extensionMap: Map<ExtensionPoint<*>, List<Any>> = - extensions.groupBy(Pair<ExtensionPoint<*>, Any>::first, Pair<ExtensionPoint<*>, Any>::second) - - override fun <T : DokkaPlugin> plugin(kclass: KClass<T>): T? = null.also { - logger.warn("Cannot access plugins from mock context") + private val extensionMap by lazy { + extensions.groupBy(Pair<ExtensionPoint<*>, (DokkaContext) -> Any>::first) { + it.second(this) + } } + private val plugins = mutableMapOf<KClass<out DokkaPlugin>, DokkaPlugin>() + + override fun <T : DokkaPlugin> plugin(kclass: KClass<T>): T? = plugins.getOrPut(kclass) { + kclass.constructors.single { it.parameters.isEmpty() }.call().also { it.injectContext(this) } + } as T + override fun <T : Any, E : ExtensionPoint<T>> get(point: E): List<T> = extensionMap[point] as List<T> - override fun <T : Any, E : ExtensionPoint<T>> single(point: E): T = get(point).single() + override fun <T : Any, E : ExtensionPoint<T>> single(point: E): T = get(point).single() override val logger = DokkaConsoleLogger @@ -33,4 +40,9 @@ class MockContext( override val platforms: Map<PlatformData, EnvironmentAndFacade> get() = testPlatforms ?: throw IllegalStateException("This mock context doesn't provide platforms data") +} + +private fun DokkaPlugin.injectContext(context: DokkaContext) { + (DokkaPlugin::class.memberProperties.single { it.name == "context" } as KMutableProperty<*>) + .setter.call(this, context) }
\ No newline at end of file |