Add gradle task to generate json tests map

This commit is contained in:
victor.petukhov
2018-08-28 19:31:50 +03:00
parent 88d8bb84ac
commit f322aba768
3 changed files with 79 additions and 0 deletions
+5
View File
@@ -22,3 +22,8 @@ val printSpecTestsStatistic by smartJavaExec {
classpath = javaPluginConvention().sourceSets.getByName("test").runtimeClasspath
main = "org.jetbrains.kotlin.spec.tasks.PrintSpecTestsStatisticKt"
}
val generateJsonTestsMap by smartJavaExec {
classpath = javaPluginConvention().sourceSets.getByName("test").runtimeClasspath
main = "org.jetbrains.kotlin.spec.tasks.GenerateJsonTestsMapKt"
}
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.spec
import com.google.gson.*
import com.google.gson.reflect.TypeToken
import org.jetbrains.kotlin.spec.validators.*
import java.io.File
object TestsJsonMapBuilder {
private val stringListType = object : TypeToken<List<String>>() {}.type
private fun addJsonIfNotExist(element: JsonObject, key: Any): JsonObject {
val stringKey = key.toString()
if (!element.has(stringKey)) element.add(stringKey, JsonObject())
return element.get(stringKey).asJsonObject
}
fun buildJsonElement(testInfo: LinkedSpecTest, testsMap: JsonObject) {
val sectionElement = addJsonIfNotExist(testsMap, testInfo.section)
val paragraphElement = addJsonIfNotExist(sectionElement, testInfo.paragraphNumber)
val sentenceElement = addJsonIfNotExist(paragraphElement, testInfo.sentenceNumber)
val testAreaElement = addJsonIfNotExist(sentenceElement, testInfo.testArea.name.toLowerCase())
val testTypeElement = addJsonIfNotExist(testAreaElement, testInfo.testType.type)
val testNumberElement = addJsonIfNotExist(testTypeElement, testInfo.testNumber)
testNumberElement.addProperty("description", testInfo.description)
testNumberElement.addProperty("caseNumber", testInfo.cases!!.size)
if (testInfo.unexpectedBehavior!!)
testNumberElement.addProperty("unexpectedBehavior", testInfo.unexpectedBehavior)
if (testInfo.issues!!.isNotEmpty())
testNumberElement.add("issues", Gson().toJsonTree(testInfo.issues, stringListType))
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.spec.tasks
import com.google.gson.JsonObject
import org.jetbrains.kotlin.spec.TestsJsonMapBuilder
import org.jetbrains.kotlin.spec.validators.LinkedSpecTestValidator
import org.jetbrains.kotlin.spec.validators.SpecTestValidationException
import java.io.File
private const val TEST_DATA_DIR = "./testData"
private const val OUT_DIR = "./out"
private const val OUT_FILENAME = "testsMap.json"
fun main(args: Array<String>) {
val testsMap = JsonObject()
File(TEST_DATA_DIR).walkTopDown().forEach {
val specTestValidator = LinkedSpecTestValidator(it)
try {
specTestValidator.parseTestInfo()
} catch (e: SpecTestValidationException) {
return@forEach
}
TestsJsonMapBuilder.buildJsonElement(specTestValidator.testInfo, testsMap)
}
File(OUT_DIR).mkdir()
File("$OUT_DIR/$OUT_FILENAME").writeText(testsMap.toString())
}