From f322aba768781031a74c9e57f57587654598040d Mon Sep 17 00:00:00 2001 From: "victor.petukhov" Date: Tue, 28 Aug 2018 19:31:50 +0300 Subject: [PATCH] Add gradle task to generate json tests map --- compiler/tests-spec/build.gradle.kts | 5 +++ .../kotlin/spec/TestsJsonMapBuilder.kt | 39 +++++++++++++++++++ .../kotlin/spec/tasks/GenerateJsonTestsMap.kt | 35 +++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 compiler/tests-spec/tests/org/jetbrains/kotlin/spec/TestsJsonMapBuilder.kt create mode 100644 compiler/tests-spec/tests/org/jetbrains/kotlin/spec/tasks/GenerateJsonTestsMap.kt diff --git a/compiler/tests-spec/build.gradle.kts b/compiler/tests-spec/build.gradle.kts index 41cd25aefaa..6baa25b7f00 100644 --- a/compiler/tests-spec/build.gradle.kts +++ b/compiler/tests-spec/build.gradle.kts @@ -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" +} diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/TestsJsonMapBuilder.kt b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/TestsJsonMapBuilder.kt new file mode 100644 index 00000000000..24bf8ff73ca --- /dev/null +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/TestsJsonMapBuilder.kt @@ -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>() {}.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)) + } +} \ No newline at end of file diff --git a/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/tasks/GenerateJsonTestsMap.kt b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/tasks/GenerateJsonTestsMap.kt new file mode 100644 index 00000000000..479974e2ae1 --- /dev/null +++ b/compiler/tests-spec/tests/org/jetbrains/kotlin/spec/tasks/GenerateJsonTestsMap.kt @@ -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) { + 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()) +} \ No newline at end of file