aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorPaweł Marks <pmarks@virtuslab.com>2020-03-17 13:24:37 +0100
committerPaweł Marks <pmarks@virtuslab.com>2020-03-18 16:27:28 +0100
commitc92427162c3232cb54c73f63867258a31be526e9 (patch)
tree741115c8861c85fa8bdd49b7d83864a22cf1db05 /plugins
parentb339915bc129e532ee6916e5a27e56002fa47a2f (diff)
downloaddokka-c92427162c3232cb54c73f63867258a31be526e9.tar.gz
dokka-c92427162c3232cb54c73f63867258a31be526e9.tar.bz2
dokka-c92427162c3232cb54c73f63867258a31be526e9.zip
Add matchers for html tests
Diffstat (limited to 'plugins')
-rw-r--r--plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt30
-rw-r--r--plugins/base/src/test/kotlin/renderers/html/GroupWrappingTest.kt12
2 files changed, 36 insertions, 6 deletions
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("""<div id="content">""") }
@@ -60,6 +68,28 @@ class TestPage(callback: PageContentBuilder.DocumentableContentBuilder.() -> Uni
override fun modified(name: String, children: List<PageNode>) = 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("<p>ab</p>c"))
+ renderedContent.match(P("ab"), "c")
}
@Test
@@ -50,7 +50,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() {
HtmlRenderer(context).render(page)
- assert(linesAfterContentTag().contains("<div>ab</div>c"))
+ renderedContent.match(Div("ab"), "c")
}
@Test
@@ -70,7 +70,7 @@ class GroupWrappingTest: RenderingOnlyTestBase() {
HtmlRenderer(context).render(page)
- assert(linesAfterContentTag().contains("<div>a<div><div>bc</div></div>d</div>"))
+ renderedContent.match(Div("a", Div(Div("bc")), "d"))
}
}