From c92427162c3232cb54c73f63867258a31be526e9 Mon Sep 17 00:00:00 2001 From: Paweł Marks Date: Tue, 17 Mar 2020 13:24:37 +0100 Subject: Add matchers for html tests --- .../test/kotlin/renderers/RenderingOnlyTestBase.kt | 30 ++++++++++++++++++++++ .../kotlin/renderers/html/GroupWrappingTest.kt | 12 ++++----- 2 files changed, 36 insertions(+), 6 deletions(-) (limited to 'plugins/base/src/test/kotlin/renderers') diff --git a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt index a6977258..686baf04 100644 --- a/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt +++ b/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt @@ -14,6 +14,10 @@ import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.* import org.jetbrains.dokka.testApi.context.MockContext import org.jetbrains.dokka.utilities.DokkaConsoleLogger +import org.jsoup.Jsoup +import org.jsoup.nodes.Element +import org.jsoup.nodes.Node +import org.jsoup.nodes.TextNode import utils.TestOutputWriter abstract class RenderingOnlyTestBase { @@ -23,6 +27,10 @@ abstract class RenderingOnlyTestBase { DokkaBase().locationProviderFactory to ::DefaultLocationProviderFactory ) + protected val renderedContent: Element by lazy { + files.contents.getValue("test-page.html").let { Jsoup.parse(it) }.select("#content").single() + } + protected fun linesAfterContentTag() = files.contents.getValue("test-page.html").lines() .dropWhile { !it.contains("""
""") } @@ -60,6 +68,28 @@ class TestPage(callback: PageContentBuilder.DocumentableContentBuilder.() -> Uni override fun modified(name: String, children: List) = this } +fun Element.match(vararg matchers: Any): Unit = + childNodes() + .filter { it !is TextNode || it.text().isNotBlank() } + .let { it.drop(it.size - matchers.size) } + .zip(matchers) + .forEach { (n, m) -> m.accepts(n) } + +open class Tag(val name: String, vararg val matchers: Any) +class Div(vararg matchers: Any): Tag("div", *matchers) +class P(vararg matchers: Any): Tag("p", *matchers) + +private fun Any.accepts(n: Node) { + when (this) { + is String -> assert(n is TextNode && n.text().trim() == this.trim()) { "\"$this\" expected but found: $n" } + is Tag -> { + assert(n is Element && n.tagName() == name) { "Tag $name expected but found: $n" } + if (n is Element && matchers.isNotEmpty()) n.match(*matchers) + } + else -> throw IllegalArgumentException("$this is not proper matcher") + } +} + internal object EmptyCommentConverter : CommentsToContentConverter { override fun buildContent( diff --git a/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt b/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt index f39d845a..eead07a7 100644 --- a/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt +++ b/plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt @@ -2,9 +2,9 @@ package renderers.html import org.jetbrains.dokka.base.renderers.html.HtmlRenderer import org.jetbrains.dokka.pages.TextStyle +import org.jsoup.Jsoup import org.junit.jupiter.api.Test -import renderers.RenderingOnlyTestBase -import renderers.TestPage +import renderers.* class GroupWrappingTest: RenderingOnlyTestBase() { @@ -20,7 +20,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() { HtmlRenderer(context).render(page) - assert(linesAfterContentTag().contains("abc")) + renderedContent.match("abc") } @Test @@ -35,7 +35,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() { HtmlRenderer(context).render(page) - assert(linesAfterContentTag().contains("

ab

c")) + renderedContent.match(P("ab"), "c") } @Test @@ -50,7 +50,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() { HtmlRenderer(context).render(page) - assert(linesAfterContentTag().contains("
ab
c")) + renderedContent.match(Div("ab"), "c") } @Test @@ -70,7 +70,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() { HtmlRenderer(context).render(page) - assert(linesAfterContentTag().contains("
a
bc
d
")) + renderedContent.match(Div("a", Div(Div("bc")), "d")) } } -- cgit