aboutsummaryrefslogtreecommitdiff
path: root/test/src/markdown
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-09-25 22:20:58 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-09-25 22:20:58 +0400
commitf7bab78839cea5674658a6a0298f88ef5ccca019 (patch)
tree51d17c02f8f935b26a9c9d85905fc33c18ebd6a1 /test/src/markdown
parenta070217e942a16ed7b5ee63e98a4183788c8e660 (diff)
downloaddokka-f7bab78839cea5674658a6a0298f88ef5ccca019.tar.gz
dokka-f7bab78839cea5674658a6a0298f88ef5ccca019.tar.bz2
dokka-f7bab78839cea5674658a6a0298f88ef5ccca019.zip
Markdown, sections, styles and lots more.
Diffstat (limited to 'test/src/markdown')
-rw-r--r--test/src/markdown/MarkdownTestRunner.kt130
-rw-r--r--test/src/markdown/ParserTest.kt52
-rw-r--r--test/src/markdown/Specification.kt10
3 files changed, 192 insertions, 0 deletions
diff --git a/test/src/markdown/MarkdownTestRunner.kt b/test/src/markdown/MarkdownTestRunner.kt
new file mode 100644
index 00000000..bf1d9516
--- /dev/null
+++ b/test/src/markdown/MarkdownTestRunner.kt
@@ -0,0 +1,130 @@
+package org.jetbrains.kmark.test
+
+import org.junit.runner.*
+import org.junit.runner.notification.*
+import java.io.File
+import org.junit.runners.ParentRunner
+import java.io.Serializable
+import kotlin.properties.Delegates
+import org.junit.ComparisonFailure
+
+data class MarkdownTestUniqueId(val id: Int) : Serializable {
+ class object {
+ var id = 0
+ fun next() = MarkdownTestUniqueId(id++)
+ }
+}
+
+public open class MarkdownSpecification(val path: String, val processor: (String) -> String)
+
+
+trait MarkdownTest {
+ fun description(): Description
+}
+
+public open class MarkdownTestCase(val spec: MarkdownSpecification, val input: String, val expected: String) : MarkdownTest, Runner() {
+ val _description by Delegates.lazy {
+ Description.createSuiteDescription(input, MarkdownTestUniqueId.next())!!
+ }
+
+ override fun description(): Description = _description
+
+ override fun getDescription(): Description? = description()
+ override fun run(notifier: RunNotifier?) {
+ notifier!!
+
+ notifier.fireTestStarted(_description)
+ val result = spec.processor(input)
+ when (result) {
+ expected -> notifier.fireTestFinished(_description)
+ else -> notifier.fireTestFailure(Failure(_description, ComparisonFailure("Output mismatch", expected, result)))
+ }
+ }
+}
+
+public open class MarkdownTestSection(val spec: MarkdownSpecification, val title: String) : MarkdownTest, ParentRunner<MarkdownTest>(spec.javaClass) {
+ val children = arrayListOf<MarkdownTest>();
+
+ val _description by Delegates.lazy {
+ val desc = Description.createSuiteDescription(title, MarkdownTestUniqueId.next())!!
+ for (item in getChildren()!!) {
+ desc.addChild(describeChild(item))
+ }
+ desc
+ }
+
+ override fun description(): Description = _description
+
+ override fun getChildren(): MutableList<MarkdownTest>? = children
+
+ override fun describeChild(child: MarkdownTest?): Description? = child!!.description()
+
+ override fun runChild(child: MarkdownTest?, notifier: RunNotifier?) {
+ notifier!!
+ when (child) {
+ is MarkdownTestCase -> child.run(notifier)
+ is MarkdownTestSection -> {
+ if (child.children.size == 0) {
+ notifier.fireTestStarted(child.description())
+ notifier.fireTestFinished(child.description())
+ } else {
+ child.run(notifier)
+ }
+ }
+ }
+ }
+}
+
+public class MarkdownTestRunner(specificationClass: Class<MarkdownSpecification>) : MarkdownTestSection(specificationClass.newInstance(), "Tests") {
+ {
+ val lines = File(spec.path).readLines()
+ createSections(this, lines, 1)
+ }
+
+ private fun createTests(parent: MarkdownTestSection, lines: List<String>): Int {
+ val testMark = lines.takeWhile { it.trim() != "." }
+ val testHtml = lines.drop(testMark.size).drop(1).takeWhile { it.trim() != "." }
+ parent.children.add(MarkdownTestCase(spec, testMark.join("\n", postfix = "\n"), testHtml.join("\n", postfix = "\n")))
+ return testMark.size + testHtml.size + 3
+ }
+
+ private fun createSections(parent: MarkdownTestSection, lines: List<String>, level: Int): Int {
+ var sectionNumber = 1
+ var index = 0
+ while (index < lines.size) {
+ val line = lines[index]
+
+ if (line.trim() == ".") {
+ index = createTests(parent, lines.subList(index + 1, lines.lastIndex)) + index + 1
+ continue
+ }
+
+ val head = line.takeWhile { it == '#' }.length
+ if (head == 0) {
+ index++
+ continue
+ }
+
+ if (head < level) {
+ return index
+ }
+
+ if (head == level) {
+ val title = lines[index].dropWhile { it == '#' }.dropWhile { it.isWhitespace() }
+ sectionNumber++
+ val section = MarkdownTestSection(spec, title)
+ val lastIndex = createSections(section, lines.subList(index + 1, lines.lastIndex), level + 1) + index + 1
+ if (section.children.size > 0)
+ parent.children.add(section)
+ val nextHead = lines[lastIndex].takeWhile { it == '#' }.length
+ if (nextHead < level) {
+ return lastIndex
+ }
+ index = lastIndex
+ continue
+ }
+ index++
+ }
+ return lines.size
+ }
+} \ No newline at end of file
diff --git a/test/src/markdown/ParserTest.kt b/test/src/markdown/ParserTest.kt
new file mode 100644
index 00000000..b4538b07
--- /dev/null
+++ b/test/src/markdown/ParserTest.kt
@@ -0,0 +1,52 @@
+package org.jetbrains.dokka.tests
+
+import org.junit.Test
+import org.jetbrains.dokka.*
+
+public class ParserTest {
+ Test fun text() {
+ val markdown = MarkdownProcessor().parse("text")
+ println(markdown.dump())
+ }
+
+ Test fun textWithSpaces() {
+ val markdown = MarkdownProcessor().parse("text and string")
+ println(markdown.dump())
+ }
+
+ Test fun multiline() {
+ val markdown = MarkdownProcessor().parse(
+"""
+text
+and
+string
+""")
+ println(markdown.dump())
+ }
+
+ Test fun para() {
+ val markdown = MarkdownProcessor().parse(
+"""paragraph number
+one
+
+paragraph
+number two
+""")
+ println(markdown.dump())
+ }
+
+ Test fun bulletList() {
+ val markdown = MarkdownProcessor().parse(
+"""
+* list item 1
+* list item 2
+""")
+ println(markdown.dump())
+ }
+
+ Test fun emph() {
+ val markdown = MarkdownProcessor().parse("*text*")
+ println(markdown.dump())
+ }
+}
+
diff --git a/test/src/markdown/Specification.kt b/test/src/markdown/Specification.kt
new file mode 100644
index 00000000..e0cda024
--- /dev/null
+++ b/test/src/markdown/Specification.kt
@@ -0,0 +1,10 @@
+package org.jetbrains.kmark.test
+
+import org.junit.runner.*
+import org.jetbrains.kmark.test.*
+import org.jetbrains.dokka.*
+
+[RunWith(javaClass<MarkdownTestRunner>())]
+class Specification : MarkdownSpecification("test/data/markdown/spec.txt", {
+ markdownToHtml(it.replace("→", "\t"))
+}) \ No newline at end of file