aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt
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/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt
parentb339915bc129e532ee6916e5a27e56002fa47a2f (diff)
downloaddokka-c92427162c3232cb54c73f63867258a31be526e9.tar.gz
dokka-c92427162c3232cb54c73f63867258a31be526e9.tar.bz2
dokka-c92427162c3232cb54c73f63867258a31be526e9.zip
Add matchers for html tests
Diffstat (limited to 'plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt')
-rw-r--r--plugins/base/src/test/kotlin/renderers/RenderingOnlyTestBase.kt30
1 files changed, 30 insertions, 0 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(