aboutsummaryrefslogtreecommitdiff
path: root/test/src
diff options
context:
space:
mode:
Diffstat (limited to 'test/src')
-rw-r--r--test/src/markdown/MarkdownTestRunner.kt130
-rw-r--r--test/src/markdown/ParserTest.kt52
-rw-r--r--test/src/markdown/Specification.kt10
-rw-r--r--test/src/model/CommentTest.kt30
4 files changed, 207 insertions, 15 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
diff --git a/test/src/model/CommentTest.kt b/test/src/model/CommentTest.kt
index 7f56f688..28f717db 100644
--- a/test/src/model/CommentTest.kt
+++ b/test/src/model/CommentTest.kt
@@ -77,8 +77,8 @@ public class CommentTest {
with(model.members.single().members.single()) {
assertEquals(NormalStyle, NormalStyle)
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals(RichString.empty, text)
}
@@ -90,8 +90,8 @@ public class CommentTest {
verifyModel("test/data/comments/section1.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals("section one".toRichString(), text)
}
@@ -103,12 +103,12 @@ public class CommentTest {
verifyModel("test/data/comments/section2.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(2, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(3, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals("section one".toRichString(), text)
}
- with (doc.sections.elementAt(1)) {
+ with (doc.sections["two"]!!) {
assertEquals("two", label)
assertEquals("section two".toRichString(), text)
}
@@ -120,8 +120,8 @@ public class CommentTest {
verifyModel("test/data/comments/sectionOnOneLine.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals("same line".toRichString(), text)
}
@@ -133,8 +133,8 @@ public class CommentTest {
verifyModel("test/data/comments/emptySectionOnOneLine.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals(RichString.empty, text)
}
@@ -146,8 +146,8 @@ public class CommentTest {
verifyModel("test/data/comments/multilineSection.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["one"]!!) {
assertEquals("one", label)
assertEquals("""line one
line two""".toRichString(), text)
@@ -160,8 +160,8 @@ line two""".toRichString(), text)
verifyModel("test/data/comments/sectionWithBracedLabel.kt") { model ->
with(model.members.single().members.single()) {
assertEquals("Summary".toRichString(), doc.summary)
- assertEquals(1, doc.sections.count())
- with (doc.sections.elementAt(0)) {
+ assertEquals(2, doc.sections.count())
+ with (doc.sections["this.label.is.really.long"]!!) {
assertEquals("this.label.is.really.long", label)
assertEquals("section one".toRichString(), text)
}