|
1 | 1 | package org.neo4j.graphql.utils |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.ObjectMapper |
| 4 | +import com.fasterxml.jackson.module.kotlin.registerKotlinModule |
4 | 5 | import com.intellij.rt.execution.junit.FileComparisonFailure |
5 | 6 | import org.junit.jupiter.api.DynamicContainer |
6 | 7 | import org.junit.jupiter.api.DynamicNode |
7 | 8 | import org.junit.jupiter.api.DynamicTest |
8 | 9 | import java.io.File |
9 | 10 | import java.io.FileWriter |
10 | | -import java.math.BigInteger |
11 | 11 | import java.net.URI |
12 | 12 | import java.util.* |
13 | 13 | import java.util.regex.Pattern |
@@ -57,6 +57,7 @@ open class AsciiDocTestSuite( |
57 | 57 | var start: Int? = null |
58 | 58 | var end: Int? = null |
59 | 59 | var adjustedCode: String? = null |
| 60 | + var reformattedCode: String? = null |
60 | 61 | val code: StringBuilder = StringBuilder() |
61 | 62 |
|
62 | 63 | fun code() = code.trim().toString() |
@@ -153,6 +154,10 @@ open class AsciiDocTestSuite( |
153 | 154 | this@AsciiDocTestSuite::writeAdjustedTestFile |
154 | 155 | ) |
155 | 156 | ) |
| 157 | + } else if (REFORMAT_TEST_FILE) { |
| 158 | + root?.afterTests?.add( |
| 159 | + DynamicTest.dynamicTest("Reformat Testfile", srcLocation, this@AsciiDocTestSuite::reformatTestFile) |
| 160 | + ) |
156 | 161 | } else if (GENERATE_TEST_FILE_DIFF) { |
157 | 162 | // this test prints out the adjusted test file |
158 | 163 | root?.afterTests?.add( |
@@ -207,29 +212,40 @@ open class AsciiDocTestSuite( |
207 | 212 | } |
208 | 213 | } |
209 | 214 |
|
| 215 | + private fun reformatTestFile() { |
| 216 | + val content = generateAdjustedFileContent { it.reformattedCode } |
| 217 | + FileWriter(File("src/test/resources/", fileName)).use { |
| 218 | + it.write(content) |
| 219 | + } |
| 220 | + } |
| 221 | + |
210 | 222 | private fun printAdjustedTestFile() { |
211 | 223 | val rebuildTest = generateAdjustedFileContent() |
212 | 224 | if (!Objects.equals(rebuildTest, fileContent.toString())) { |
213 | 225 | // This special exception will be handled by intellij so that you can diff directly with the file |
214 | 226 | throw FileComparisonFailure( |
215 | | - null, rebuildTest, fileContent.toString(), |
216 | | - null, File("src/test/resources/", fileName).absolutePath |
| 227 | + null, fileContent.toString(), rebuildTest, |
| 228 | + File("src/test/resources/", fileName).absolutePath, null |
217 | 229 | ) |
218 | 230 | } |
219 | 231 | } |
220 | 232 |
|
221 | | - private fun generateAdjustedFileContent(): String { |
| 233 | + private fun generateAdjustedFileContent(extractor: (ParsedBlock) -> String? = { it.adjustedCode }): String { |
222 | 234 | knownBlocks.sortWith(compareByDescending<ParsedBlock> { it.start } |
223 | 235 | .thenByDescending { testCaseMarkers.indexOf(it.marker) }) |
224 | 236 | val rebuildTest = StringBuffer(fileContent) |
225 | | - knownBlocks.filter { it.adjustedCode != null }.forEach { block -> |
226 | | - val start = block.start ?: throw IllegalArgumentException("unknown start position") |
227 | | - if (block.end == null) { |
228 | | - rebuildTest.insert(start, ".${block.headline}\n${block.marker}\n----\n${block.adjustedCode}\n----\n\n") |
229 | | - } else { |
230 | | - rebuildTest.replace(start, block.end!!, block.adjustedCode + "\n") |
| 237 | + knownBlocks.filter { extractor(it) != null } |
| 238 | + .forEach { block -> |
| 239 | + val start = block.start ?: throw IllegalArgumentException("unknown start position") |
| 240 | + if (block.end == null) { |
| 241 | + rebuildTest.insert( |
| 242 | + start, |
| 243 | + ".${block.headline}\n${block.marker}\n----\n${extractor(block)}\n----\n\n" |
| 244 | + ) |
| 245 | + } else { |
| 246 | + rebuildTest.replace(start, block.end!!, extractor(block) + "\n") |
| 247 | + } |
231 | 248 | } |
232 | | - } |
233 | 249 | return rebuildTest.toString() |
234 | 250 | } |
235 | 251 |
|
@@ -278,8 +294,9 @@ open class AsciiDocTestSuite( |
278 | 294 | */ |
279 | 295 | val FLATTEN_TESTS = System.getProperty("neo4j-graphql-java.flatten-tests", "false") == "true" |
280 | 296 | val GENERATE_TEST_FILE_DIFF = System.getProperty("neo4j-graphql-java.generate-test-file-diff", "true") == "true" |
| 297 | + val REFORMAT_TEST_FILE = System.getProperty("neo4j-graphql-java.reformat", "false") == "true" |
281 | 298 | val UPDATE_TEST_FILE = System.getProperty("neo4j-graphql-java.update-test-file", "false") == "true" |
282 | | - val MAPPER = ObjectMapper() |
| 299 | + val MAPPER = ObjectMapper().registerKotlinModule() |
283 | 300 | val HEADLINE_PATTERN: Pattern = Pattern.compile("^(=+) (.*)$") |
284 | 301 |
|
285 | 302 | const val SCHEMA_MARKER = "[source,graphql,schema=true]" |
@@ -330,18 +347,6 @@ open class AsciiDocTestSuite( |
330 | 347 | } |
331 | 348 | } |
332 | 349 |
|
333 | | - fun fixNumber(v: Any?): Any? = when (v) { |
334 | | - is Float -> v.toDouble() |
335 | | - is Int -> v.toLong() |
336 | | - is BigInteger -> v.toLong() |
337 | | - is Iterable<*> -> v.map { fixNumber(it) } |
338 | | - is Sequence<*> -> v.map { fixNumber(it) } |
339 | | - is Map<*, *> -> v.mapValues { fixNumber(it.value) } |
340 | | - else -> v |
341 | | - } |
342 | | - |
343 | | - fun fixNumbers(params: Map<String, Any?>) = params.mapValues { (_, v) -> fixNumber(v) } |
344 | | - |
345 | 350 | fun String.parseJsonMap(): Map<String, Any?> = this.let { |
346 | 351 | @Suppress("UNCHECKED_CAST") |
347 | 352 | MAPPER.readValue(this, Map::class.java) as Map<String, Any?> |
|
0 commit comments