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 | |
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
4 files changed, 193 insertions, 13 deletions
diff --git a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt index 176aa87f..8d04e986 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt @@ -85,7 +85,7 @@ open class PageContentBuilder( } fun signature(f: Function, block: DocumentableContentBuilder.() -> Unit) { - contents += group(f.dri, f.platformData.toSet(), ContentKind.Symbol, mainStyles, mainExtras, block) + contents += buildGroup(f.dri, f.platformData.toSet(), ContentKind.Symbol, mainStyles, mainExtras, block) } fun signature(f: Function) = signature(f) { @@ -145,7 +145,7 @@ open class PageContentBuilder( contents += ContentTable( emptyList(), elements.map { - group(it.dri, it.platformData.toSet(), kind, styles, extras) { + buildGroup(it.dri, it.platformData.toSet(), kind, styles, extras) { // TODO this will fail operation(it) } @@ -228,6 +228,17 @@ open class PageContentBuilder( styles: Set<Style> = mainStyles, extras: Set<Extra> = mainExtras, block: DocumentableContentBuilder.() -> Unit + ) { + contents += buildGroup(dri, platformData, kind, styles, extras, block) + } + + fun buildGroup( + dri: DRI = mainDRI, + platformData: Set<PlatformData> = mainPlatformData, + kind: Kind = ContentKind.Main, + styles: Set<Style> = mainStyles, + extras: Set<Extra> = mainExtras, + block: DocumentableContentBuilder.() -> Unit ): ContentGroup = contentFor(dri, platformData, kind, styles, extras, block) protected fun createText( diff --git a/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt b/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt new file mode 100644 index 00000000..7fac6450 --- /dev/null +++ b/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt @@ -0,0 +1,157 @@ +package renderers.html + +import org.jetbrains.dokka.base.DokkaBase +import org.jetbrains.dokka.base.renderers.html.HtmlRenderer +import org.jetbrains.dokka.base.resolvers.DefaultLocationProviderFactory +import org.jetbrains.dokka.base.resolvers.LocationProvider +import org.jetbrains.dokka.base.resolvers.LocationProviderFactory +import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter +import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder +import org.jetbrains.dokka.links.DRI +import org.jetbrains.dokka.model.Documentable +import org.jetbrains.dokka.model.doc.DocTag +import org.jetbrains.dokka.pages.* +import org.jetbrains.dokka.testApi.context.MockContext +import org.jetbrains.dokka.utilities.DokkaConsoleLogger +import org.junit.Test +import utils.TestOutputWriter + +class GroupWrappingTest { + + val files = TestOutputWriter() + val context = MockContext( + DokkaBase().outputWriter to { _ -> files }, + DokkaBase().locationProviderFactory to ::DefaultLocationProviderFactory + ) + + @Test + fun notWrapped() { + + val page = createPage { + group { + text("a") + text("b") + } + text("c") + } + + HtmlRenderer(context).render(page) + + assert(linesAfterContentTag().contains("abc")) + } + + @Test + fun paragraphWrapped() { + + val page = createPage { + group(styles = setOf(TextStyle.Paragraph)) { + text("a") + text("b") + } + text("c") + } + + HtmlRenderer(context).render(page) + + assert(linesAfterContentTag().contains("<p>ab</p>c")) + } + + @Test + fun blockWrapped() { + + val page = createPage { + group(styles = setOf(TextStyle.Block)) { + text("a") + text("b") + } + text("c") + } + + HtmlRenderer(context).render(page) + + assert(linesAfterContentTag().contains("<div>ab</div>c")) + } + + @Test + fun nested() { + + val page = createPage { + group(styles = setOf(TextStyle.Block)) { + text("a") + group(styles = setOf(TextStyle.Block)) { + group(styles = setOf(TextStyle.Block)) { + text("b") + text("c") + } + } + text("d") + } + } + + HtmlRenderer(context).render(page) + + assert(linesAfterContentTag().contains("<div>a<div><div>bc</div></div>d</div>")) + } + + private fun linesAfterContentTag() = + files.contents.getValue("test-page.html").lines() + .dropWhile { !it.contains("""<div id="content">""") } + .joinToString(separator = "") { it.trim() } +} + +// TODO: may be useful for other tests, consider extracting +private fun createPage( + callback: PageContentBuilder.DocumentableContentBuilder.() -> Unit +) = object : RootPageNode(), ContentPage { + override val dri: Set<DRI> = setOf(DRI.topLevel) + override val documentable: Documentable? = null + override val embeddedResources: List<String> = emptyList() + override val name: String + get() = "testPage" + override val children: List<PageNode> + get() = emptyList() + + override val content: ContentNode = PageContentBuilder(EmptyCommentConverter, DokkaConsoleLogger).contentFor( + DRI.topLevel, + emptySet(), + block = callback + ) + + override fun modified( + name: String, + content: ContentNode, + dri: Set<DRI>, + embeddedResources: List<String>, + children: List<PageNode> + ) = this + + override fun modified(name: String, children: List<PageNode>) = this +} + +private object EmptyCommentConverter : CommentsToContentConverter { + override fun buildContent( + docTag: DocTag, + dci: DCI, + platforms: Set<PlatformData>, + styles: Set<Style>, + extras: Set<Extra> + ): List<ContentNode> = emptyList() +} + +private object EmptyLocationProviderFactory: LocationProviderFactory { + override fun getLocationProvider(pageNode: RootPageNode) = object : LocationProvider { + override fun resolve(dri: DRI, platforms: List<PlatformData>, context: PageNode?): String = "" + + override fun resolve(node: PageNode, context: PageNode?, skipExtension: Boolean): String = node.name + + override fun resolveRoot(node: PageNode): String { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + override fun ancestors(node: PageNode): List<PageNode> { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } + + } + +}
\ No newline at end of file diff --git a/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt b/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt index 43161929..e2250a26 100644 --- a/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt +++ b/plugins/base/src/test/kotlin/utils/TestOutputWriter.kt @@ -5,23 +5,23 @@ import org.jetbrains.dokka.base.renderers.OutputWriter import org.jetbrains.dokka.plugability.DokkaPlugin import java.io.File -class TestOutputWriterPlugin(failOnOverwrite: Boolean): DokkaPlugin() { +class TestOutputWriterPlugin(failOnOverwrite: Boolean = true): DokkaPlugin() { private val writer = TestOutputWriter(failOnOverwrite) val testWriter by extending { plugin<DokkaBase>().outputWriter with writer } } -class TestOutputWriter(private val failOnOverwrite: Boolean): OutputWriter { +class TestOutputWriter(private val failOnOverwrite: Boolean = true): OutputWriter { val contents: Map<String, String> get() = _contents private val _contents = mutableMapOf<String, String>() override fun write(path: String, text: String, ext: String) { - val fullPath = listOf(path, ext).joinToString(separator = ".") + val fullPath = "$path$ext" _contents.putIfAbsent(fullPath, text)?.also { if (failOnOverwrite) throw AssertionError("File $fullPath is being overwritten.") } } - override fun writeResources(pathFrom: String, pathTo: String) = write(pathFrom, File(pathTo).readText(), "") + override fun writeResources(pathFrom: String, pathTo: String) = write(pathTo, "*** content of $pathFrom ***", "") } 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 |