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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package org.jetbrains.dokka.test.tools.matchers.content
import org.jetbrains.dokka.pages.ContentComposite
import org.jetbrains.dokka.pages.ContentNode
import org.jetbrains.dokka.pages.ContentText
import kotlin.reflect.KClass
import kotlin.reflect.full.cast
import kotlin.reflect.full.safeCast
sealed class MatcherElement
class TextMatcher(val text: String) : MatcherElement()
open class NodeMatcher<T : ContentNode>(
val kclass: KClass<T>,
val assertions: T.() -> Unit = {}
) : MatcherElement() {
open fun tryMatch(node: ContentNode) {
kclass.safeCast(node)?.apply {
try {
assertions()
} catch (e: AssertionError) {
throw MatcherError(e.message.orEmpty(), this@NodeMatcher, cause = e)
}
} ?: throw MatcherError("Expected ${kclass.simpleName} but got: $node", this)
}
}
class CompositeMatcher<T : ContentComposite>(
kclass: KClass<T>,
private val children: List<MatcherElement>,
assertions: T.() -> Unit = {}
) : NodeMatcher<T>(kclass, assertions) {
internal val normalizedChildren: List<MatcherElement> by lazy {
children.fold(listOf<MatcherElement>()) { acc, e ->
when {
acc.lastOrNull() is Anything && e is Anything -> acc
acc.lastOrNull() is TextMatcher && e is TextMatcher ->
acc.dropLast(1) + TextMatcher((acc.lastOrNull() as TextMatcher).text + e.text)
else -> acc + e
}
}
}
override fun tryMatch(node: ContentNode) {
super.tryMatch(node)
kclass.cast(node).children.asSequence()
.filter { it !is ContentText || it.text.isNotBlank() }
.fold(FurtherSiblings(normalizedChildren, this).pop()) { acc, n -> acc.next(n) }.finish()
}
}
object Anything : MatcherElement()
private sealed class MatchWalkerState {
abstract fun next(node: ContentNode): MatchWalkerState
abstract fun finish()
}
private class TextMatcherState(
val text: String,
val rest: FurtherSiblings,
val anchor: TextMatcher
) : MatchWalkerState() {
override fun next(node: ContentNode): MatchWalkerState {
node as? ContentText ?: throw MatcherError("Expected text: \"$text\" but got $node", anchor)
val trimmed = node.text.trim()
return when {
text == trimmed -> rest.pop()
text.startsWith(trimmed) -> TextMatcherState(text.removePrefix(node.text).trim(), rest, anchor)
else -> throw MatcherError("Expected text: \"$text\", but got: \"${node.text}\"", anchor)
}
}
override fun finish() = throw MatcherError("\"$text\" was not found" + rest.messageEnd, anchor)
}
private class EmptyMatcherState(val parent: CompositeMatcher<*>) : MatchWalkerState() {
override fun next(node: ContentNode): MatchWalkerState {
throw MatcherError("Unexpected $node", parent, anchorAfter = true)
}
override fun finish() = Unit
}
private class NodeMatcherState(
val matcher: NodeMatcher<*>,
val rest: FurtherSiblings
) : MatchWalkerState() {
override fun next(node: ContentNode): MatchWalkerState {
matcher.tryMatch(node)
return rest.pop()
}
override fun finish() =
throw MatcherError("Content of type ${matcher.kclass} was not found" + rest.messageEnd, matcher)
}
private class SkippingMatcherState(
val innerState: MatchWalkerState
) : MatchWalkerState() {
override fun next(node: ContentNode): MatchWalkerState = runCatching { innerState.next(node) }.getOrElse { this }
override fun finish() = innerState.finish()
}
private class FurtherSiblings(val list: List<MatcherElement>, val parent: CompositeMatcher<*>) {
fun pop(): MatchWalkerState = when (val head = list.firstOrNull()) {
is TextMatcher -> TextMatcherState(head.text.trim(), drop(), head)
is NodeMatcher<*> -> NodeMatcherState(head, drop())
is Anything -> SkippingMatcherState(drop().pop())
null -> EmptyMatcherState(parent)
}
fun drop() = FurtherSiblings(list.drop(1), parent)
val messageEnd: String
get() = list.filter { it !is Anything }
.count().takeIf { it > 0 }
?.let { " and $it further matchers were not satisfied" } ?: ""
}
internal fun MatcherElement.toDebugString(anchor: MatcherElement?, anchorAfter: Boolean): String {
fun Appendable.append(element: MatcherElement, ownPrefix: String, childPrefix: String) {
if (anchor != null) {
if (element != anchor || anchorAfter) append(" ".repeat(4))
else append("--> ")
}
append(ownPrefix)
when (element) {
is Anything -> append("skipAllNotMatching\n")
is TextMatcher -> append("\"${element.text}\"\n")
is CompositeMatcher<*> -> {
append("${element.kclass.simpleName.toString()}\n")
if (element.normalizedChildren.isNotEmpty()) {
val newOwnPrefix = childPrefix + '\u251c' + '\u2500' + ' '
val lastOwnPrefix = childPrefix + '\u2514' + '\u2500' + ' '
val newChildPrefix = childPrefix + '\u2502' + ' ' + ' '
val lastChildPrefix = childPrefix + ' ' + ' ' + ' '
element.normalizedChildren.forEachIndexed { n, e ->
if (n != element.normalizedChildren.lastIndex) append(e, newOwnPrefix, newChildPrefix)
else append(e, lastOwnPrefix, lastChildPrefix)
}
}
if (element == anchor && anchorAfter) {
append("--> $childPrefix\n")
}
}
is NodeMatcher<*> -> append("${element.kclass.simpleName}\n")
}
}
return buildString { append(this@toDebugString, "", "") }
}
data class MatcherError(
override val message: String,
val anchor: MatcherElement,
val anchorAfter: Boolean = false,
override val cause: Throwable? = null
) : AssertionError(message, cause)
// Creating this whole mechanism was most scala-like experience I had since I stopped using scala.
// I don't know how I should feel about it.
|