This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 566
/
TestRunner.kt
223 lines (199 loc) · 9.02 KB
/
TestRunner.kt
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package konan.test
import kotlin.IllegalArgumentException
import kotlin.system.getTimeMillis
import kotlin.system.measureTimeMillis
import kotlin.text.StringBuilder
object TestRunner {
private val suites_ = mutableListOf<TestSuite>()
val suites: Collection<TestSuite>
get() = suites_
var logger: TestLogger = GTestLogger()
val listeners = mutableSetOf<TestListener>()
val filters = mutableListOf<(TestCase) -> Boolean>()
var exitCode = 0
private set
private val TestCase.matchFilters: Boolean
get() = filters.map { it(this) }.all { it }
private val TestSuite.testCasesFiltered
get() = testCases.values.filter { it.matchFilters }
// TODO: We can cache it.
fun filterTests(): Collection<TestCase> = suites.flatMap { it.testCasesFiltered }
fun register(suite: TestSuite) = suites_.add(suite)
fun register(suites: Iterable<TestSuite>) = suites_.addAll(suites)
fun register(vararg suites: TestSuite) = suites_.addAll(suites)
// TODO: Support short aliases.
// TODO: Support several test iterations.
/**
* Initialize the TestRunner using the command line options passed in [args].
* Returns: true if tests may be ran, false otherwise (there are unrecognized options or just help).
* The following options are available:
*
* --gtest_list_tests
* --ktest_list_tests - Show all available tests.
*
* --gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS] - Run only the tests whose name matches one of the
* --ktest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS] positive patterns but none of the negative patterns.
* '?' matches any single character; '*' matches any
* substring; ':' separates two patterns.
*
* --ktest_regex_filter=PATTERN - Run only the tests whose name matches the pattern.
* The pattern is a Kotlin regular expression.
*
* --ktest_negative_regex_filter=PATTERN - Run only the tests whose name doesn't match the pattern.
* The pattern is a Kotlin regular expression.
*
* --ktest_logger=GTEST|TEAMCITY|SIMPLE|SILENT - Use the specified output format. The default one is GTEST.
*/
fun useArgs(args: Array<String>): Boolean {
try {
return parseArgs(args)
} catch (e: IllegalArgumentException) {
logger.log("Error: ${e.message}")
return false
}
}
private fun parseArgs(args: Array<String>): Boolean {
var result = true
args.filter {
it.startsWith("--gtest_") || it.startsWith("--ktest_") || it == "--help"
}.forEach {
val arg = it.split('=')
when (arg.size) {
1 -> when (arg[0]) {
"--gtest_list_tests",
"--ktest_list_tests" -> { logger.logTestList(this); result = false }
"--help" -> { logger.log(help); result = false }
else -> throw IllegalArgumentException("Unknown option: $it\n$help")
}
2 -> {
val key = arg[0]
val value = arg[1]
when (key) {
"--ktest_logger" -> setLoggerFromArg(value)
"--gtest_filter",
"--ktest_filter" -> setGTestFilterFromArg(value)
"--ktest_regex_filter"-> setRegexFilterFromArg(value, true)
"--ktest_negative_regex_filter" -> setRegexFilterFromArg(value, false)
else -> throw IllegalArgumentException("Unknown option: $it\n$help")
}
}
else -> throw IllegalArgumentException("Unknown option: $it\n$help")
}
}
return result
}
private fun String.substringEscaped(range: IntRange) =
this.substring(range).let { if(it.isNotEmpty()) Regex.escape(it) else "" }
private fun String.toGTestPatterns() = splitToSequence(':').map { pattern ->
val result = StringBuilder()
var prevIndex = 0
pattern.forEachIndexed { index, c ->
if (c == '*' || c == '?') {
result.append(pattern.substringEscaped(prevIndex until index))
prevIndex = index+1
result.append( if (c == '*') ".*" else ".")
}
}
result.append(pattern.substringEscaped(prevIndex until pattern.length))
return@map result.toString().toRegex()
}.toList()
private fun setGTestFilterFromArg(filter: String) {
if (filter.isEmpty()) {
throw IllegalArgumentException("Empty filter")
}
val filters = filter.split('-')
if (filters.size > 2) {
throw IllegalArgumentException("Wrong pattern syntax: $filter.")
}
val positivePatterns = filters[0].toGTestPatterns()
val negativePatterns = filters.getOrNull(1)?.toGTestPatterns() ?: emptyList()
this.filters.add { testCase ->
positivePatterns.all { testCase.prettyName.matches(it) } &&
negativePatterns.none { testCase.prettyName.matches(it) }
}
}
private fun setRegexFilterFromArg(filter: String, positive: Boolean = true) {
if (filter.isEmpty()) {
throw IllegalArgumentException("Empty filter")
}
val pattern = filter.toRegex()
filters.add { testCase ->
testCase.prettyName.matches(pattern) == positive
}
}
private fun setLoggerFromArg(logger: String) {
when(logger.toUpperCase()) {
"GTEST" -> this.logger = GTestLogger()
"TEAMCITY" -> this.logger = TeamCityLogger()
"SIMPLE" -> this.logger = SimpleTestLogger()
"SILENT" -> this.logger = SilentTestLogger()
else -> throw IllegalArgumentException("Unknown logger type. Available types: GTEST, TEAMCITY, SIMPLE")
}
}
private val help: String
get() = """
|Available options:
|--gtest_list_tests
|--ktest_list_tests - Show all available tests.
|
|--gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
|--ktest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS] - Run only the tests whose name matches one of the
| positive patterns but none of the negative patterns.
| '?' matches any single character; '*' matches any
| substring; ':' separates two patterns.
|
|--ktest_regex_filter=PATTERN - Run only the tests whose name matches the pattern.
| The pattern is a Kotlin regular expression.
|
|--ktest_negative_regex_filter=PATTERN - Run only the tests whose name doesn't match the pattern.
| The pattern is a Kotlin regular expression.
|
|--ktest_logger=GTEST|TEAMCITY|SIMPLE|SILENT - Use the specified output format. The default one is GTEST.
""".trimMargin()
private inline fun sendToListeners(event: TestListener.() -> Unit) {
logger.event()
listeners.forEach(event)
}
private fun TestSuite.run() {
doBeforeClass()
testCasesFiltered.forEach { testCase ->
if (testCase.ignored) {
sendToListeners { ignore(testCase) }
} else {
val startTime = getTimeMillis()
try {
sendToListeners { start(testCase) }
testCase.run()
sendToListeners { pass(testCase, getTimeMillis() - startTime) }
} catch (e: Throwable) {
sendToListeners { fail(testCase, e, getTimeMillis() - startTime) }
exitCode = 1
}
}
}
doAfterClass()
}
fun run(args: Array<String>): Int {
return if (useArgs(args)) {
run()
} else {
0
}
}
fun run(): Int {
sendToListeners { startTesting(this@TestRunner) }
val totalTime = measureTimeMillis {
suites.forEach {
if (it.ignored) {
sendToListeners { ignoreSuite(it) }
} else {
sendToListeners { startSuite(it) }
val time = measureTimeMillis { it.run() }
sendToListeners { endSuite(it, time) }
}
}
}
sendToListeners { endTesting(this@TestRunner, totalTime) }
return exitCode
}
}