1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
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() != "." }
val markdown = testMark.join("\n", postfix = "\n", prefix = "\n")
val html = testHtml.join("\n", postfix = "\n")
val markdownTestCase = MarkdownTestCase(spec, markdown, html)
parent.children.add(markdownTestCase)
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
}
}
|