-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTestClassWriter.kt
More file actions
133 lines (107 loc) · 3.97 KB
/
TestClassWriter.kt
File metadata and controls
133 lines (107 loc) · 3.97 KB
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.utbot.contest
import org.utbot.framework.codegen.Import
import org.utbot.framework.codegen.StaticImport
import org.utbot.framework.plugin.api.UtMethod
import mu.KotlinLogging
import org.utbot.common.FileUtil
private val logger = KotlinLogging.logger {}
class TestClassWriter(
private val cut: ClassUnderTest,
) {
private val tab = " "
private var tabsAmount = 0
private val importsBuilder = StringBuilder()
@Suppress("JoinDeclarationAndAssignment")
private val codeBuilder: StringBuilder
private val sbCapacity: Int = 500
private val existingImports = mutableSetOf<Import>()
// map from method name to its test methods' code
private val methodToTests: MutableMap<String, MutableList<String>> = mutableMapOf()
private val utilMethods: MutableList<String> = mutableListOf()
private val lineSeparator = System.lineSeparator()
private val methodsSeparator: String = "$lineSeparator$lineSeparator"
init {
codeBuilder = StringBuilder(
"""
public class ${cut.testClassSimpleName} {
}
""".trimIndent()
)
}
private fun addImports(imports: String) {
importsBuilder.appendLine(imports)
}
private fun addMethod(name: String, method: String) {
methodToTests[name]?.let {
it += method
} ?: run {
methodToTests[name] = mutableListOf(method)
}
}
fun addImports(imports: List<Import>) {
imports.filter { it !in existingImports }.takeIf { it.isNotEmpty() }?.let { newImports ->
addImports(newImports.toText())
existingImports += newImports
}
}
fun addTest(methodUnderTest: UtMethod<*>, method: String) {
addMethod(methodUnderTest.callable.name, reformatMethod(method))
}
fun addUtilMethod(method: String) {
utilMethods += method
}
private fun List<Import>.toText(): String =
joinToString(lineSeparator) {
@Suppress("REDUNDANT_ELSE_IN_WHEN")
when (it) {
is StaticImport -> "import static ${it.qualifiedName};"
else -> "import ${it.qualifiedName};"
}
}
fun writeTestClass() {
val targetDir = cut.generatedTestFile.parentFile
targetDir.mkdirs()
val tests = methodToTests.flatMap { (_, tests) -> tests }.joinToString(methodsSeparator)
val utils = utilMethods.joinToString(methodsSeparator)
codeBuilder.run {
insert(0, importsBuilder)
if (cut.packageName.isNotEmpty())
insert(0, "package ${cut.packageName};$lineSeparator$lineSeparator")
insert(lastIndexOf("}"), tests)
insert(lastIndexOf("}"), utils)
}
logger.info { "File size for ${cut.testClassSimpleName}: ${FileUtil.byteCountToDisplaySize(codeBuilder.length.toLong())}" }
cut.generatedTestFile.writeText(codeBuilder.toString(), charset)
}
private fun reformatMethod(method: String): String = buildString(sbCapacity) {
tabbed(1) {
method.lines().map { it.trim() }.apply {
take(2).forEach {
line(it)
}
tabbed(1) {
subList(2, lastIndex).forEach {
line(it)
}
}
line(last())
}
line()
}
}
private fun <R> tabbed(@Suppress("SameParameterValue") tabs: Int, block: () -> R): R {
val initialTabsAmount = tabsAmount
try {
tabsAmount += tabs
return block()
} finally {
tabsAmount = initialTabsAmount
}
}
private fun StringBuilder.line() = appendLine()
private fun StringBuilder.line(text: String) = lineTabbed(tabsAmount, text)
private fun StringBuilder.lineTabbed(tabs: Int, text: String) {
repeat(tabs) { append(tab) }
append("$text\n")
}
}