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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package testRunner
import org.jetbrains.dokka.*
import org.jetbrains.dokka.model.Module
import org.jetbrains.dokka.pages.ModulePageNode
import org.jetbrains.dokka.pages.PlatformData
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.utilities.DokkaConsoleLogger
import org.junit.rules.TemporaryFolder
import java.io.File
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.InvalidPathException
import java.nio.file.Path
import java.nio.file.Paths
// TODO: take dokka configuration from file
abstract class AbstractCoreTest {
protected val logger = DokkaConsoleLogger
protected fun getTestDataDir(name: String) =
File("src/test/resources/$name").takeIf { it.exists() }?.toPath()
?: throw InvalidPathException(name, "Cannot be found")
protected fun testFromData(
configuration: DokkaConfigurationImpl,
cleanupOutput: Boolean = true,
block: TestBuilder.() -> Unit
) {
val testMethods = TestBuilder().apply(block).build()
val tempDir = getTempDir(cleanupOutput)
if (!cleanupOutput)
logger.info("Output generated under: ${tempDir.root.absolutePath}")
val newConfiguration =
configuration.copy(
outputDir = tempDir.root.toPath().toAbsolutePath().toString()
)
DokkaTestGenerator(newConfiguration, logger, testMethods).generate()
}
protected fun testInline(
query: String,
configuration: DokkaConfigurationImpl,
cleanupOutput: Boolean = true,
block: TestBuilder.() -> Unit
) {
val testMethods = TestBuilder().apply(block).build()
val testDirPath = getTempDir(cleanupOutput).root.toPath()
val fileMap = query.toFileMap()
fileMap.materializeFiles(testDirPath.toAbsolutePath())
if (!cleanupOutput)
logger.info("Output generated under: ${testDirPath.toAbsolutePath()}")
val newConfiguration =
configuration.copy(
outputDir = testDirPath.toAbsolutePath().toString(),
passesConfigurations = configuration.passesConfigurations
.map { it.copy(sourceRoots = it.sourceRoots.map { it.copy(path = "${testDirPath.toAbsolutePath()}/${it.path}") }) }
)
DokkaTestGenerator(newConfiguration, logger, testMethods).generate()
}
private fun String.toFileMap(): Map<String, String> = this.replace("\r\n", "\n")
.split("\n/")
.map { fileString ->
fileString.split("\n", limit = 2)
.let {
it.first().trim().removePrefix("/") to it.last().trim()
}
}.toMap()
private fun Map<String, String>.materializeFiles(
root: Path = Paths.get("."),
charset: Charset = Charset.forName("utf-8")
) = this.map { (path, content) ->
val file = root.resolve(path)
Files.createDirectories(file.parent)
Files.write(file, content.toByteArray(charset))
}
private fun getTempDir(cleanupOutput: Boolean) = if (cleanupOutput) {
TemporaryFolder().apply { create() }
} else {
object : TemporaryFolder() {
override fun after() {}
}.apply { create() }
}
protected class TestBuilder {
var analysisSetupStage: (Map<PlatformData, EnvironmentAndFacade>) -> Unit = {}
var pluginsSetupStage: (DokkaContext) -> Unit = {}
var documentablesCreationStage: (List<Module>) -> Unit = {}
var documentablesMergingStage: (Module) -> Unit = {}
var documentablesTransformationStage: (Module) -> Unit = {}
var pagesGenerationStage: (ModulePageNode) -> Unit = {}
var pagesTransformationStage: (ModulePageNode) -> Unit = {}
fun build() = TestMethods(
analysisSetupStage,
pluginsSetupStage,
documentablesCreationStage,
documentablesMergingStage,
documentablesTransformationStage,
pagesGenerationStage,
pagesTransformationStage
)
}
protected fun dokkaConfiguration(block: DokkaConfigurationBuilder.() -> Unit): DokkaConfigurationImpl =
DokkaConfigurationBuilder().apply(block).build()
@DslMarker
protected annotation class DokkaConfigurationDsl
@DokkaConfigurationDsl
protected class DokkaConfigurationBuilder {
var outputDir: String = "out"
var format: String = "html"
var generateIndexPages: Boolean = true
var cacheRoot: String? = null
var pluginsClasspath: List<File> = emptyList()
private val passesConfigurations = mutableListOf<PassConfigurationImpl>()
fun build() = DokkaConfigurationImpl(
outputDir = outputDir,
format = format,
generateIndexPages = generateIndexPages,
cacheRoot = cacheRoot,
impliedPlatforms = emptyList(),
passesConfigurations = passesConfigurations,
pluginsClasspath = pluginsClasspath
)
fun passes(block: Passes.() -> Unit) {
passesConfigurations.addAll(Passes().apply(block))
}
}
@DokkaConfigurationDsl
protected class Passes : ArrayList<PassConfigurationImpl>() {
fun pass(block: DokkaPassConfigurationBuilder.() -> Unit) =
add(DokkaPassConfigurationBuilder().apply(block).build())
}
@DokkaConfigurationDsl
protected class DokkaPassConfigurationBuilder(
var moduleName: String = "root",
var classpath: List<String> = emptyList(),
var sourceRoots: List<String> = emptyList(),
var samples: List<String> = emptyList(),
var includes: List<String> = emptyList(),
var includeNonPublic: Boolean = true,
var includeRootPackage: Boolean = true,
var reportUndocumented: Boolean = false,
var skipEmptyPackages: Boolean = false,
var skipDeprecated: Boolean = false,
var jdkVersion: Int = 6,
var languageVersion: String? = null,
var apiVersion: String? = null,
var noStdlibLink: Boolean = false,
var noJdkLink: Boolean = false,
var suppressedFiles: List<String> = emptyList(),
var collectInheritedExtensionsFromLibraries: Boolean = true,
var analysisPlatform: String = "jvm",
var targets: List<String> = listOf("jvm"),
var sinceKotlin: String? = null,
var perPackageOptions: List<PackageOptionsImpl> = emptyList(),
var externalDocumentationLinks: List<ExternalDocumentationLinkImpl> = emptyList(),
var sourceLinks: List<SourceLinkDefinitionImpl> = emptyList()
) {
fun build() = PassConfigurationImpl(
moduleName = moduleName,
classpath = classpath,
sourceRoots = sourceRoots.map { SourceRootImpl(it) },
samples = samples,
includes = includes,
includeNonPublic = includeNonPublic,
includeRootPackage = includeRootPackage,
reportUndocumented = reportUndocumented,
skipEmptyPackages = skipEmptyPackages,
skipDeprecated = skipDeprecated,
jdkVersion = jdkVersion,
languageVersion = languageVersion,
apiVersion = apiVersion,
noStdlibLink = noStdlibLink,
noJdkLink = noJdkLink,
suppressedFiles = suppressedFiles,
collectInheritedExtensionsFromLibraries = collectInheritedExtensionsFromLibraries,
analysisPlatform = Platform.fromString(analysisPlatform),
targets = targets,
sinceKotlin = sinceKotlin,
perPackageOptions = perPackageOptions,
externalDocumentationLinks = externalDocumentationLinks,
sourceLinks = sourceLinks
)
}
}
data class TestMethods(
val analysisSetupStage: (Map<PlatformData, EnvironmentAndFacade>) -> Unit,
val pluginsSetupStage: (DokkaContext) -> Unit,
val documentablesCreationStage: (List<Module>) -> Unit,
val documentablesMergingStage: (Module) -> Unit,
val documentablesTransformationStage: (Module) -> Unit,
val pagesGenerationStage: (ModulePageNode) -> Unit,
val pagesTransformationStage: (ModulePageNode) -> Unit
)
|