TestGenerationDSL: cleanup code

This commit is contained in:
Dmitry Gridin
2019-08-19 19:55:23 +07:00
parent bddf768d59
commit 65ce4aed1a
@@ -1,17 +1,6 @@
/* /*
* Copyright 2010-2017 JetBrains s.r.o. * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ */
package org.jetbrains.kotlin.generators.tests.generator package org.jetbrains.kotlin.generators.tests.generator
@@ -19,28 +8,27 @@ package org.jetbrains.kotlin.generators.tests.generator
import junit.framework.TestCase import junit.framework.TestCase
import org.jetbrains.kotlin.test.TargetBackend import org.jetbrains.kotlin.test.TargetBackend
import java.io.File import java.io.File
import java.lang.IllegalArgumentException
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
class TestGroup(private val testsRoot: String, val testDataRoot: String, val testRunnerMethodName: String) { class TestGroup(private val testsRoot: String, val testDataRoot: String, val testRunnerMethodName: String) {
inline fun <reified T: TestCase> testClass( inline fun <reified T : TestCase> testClass(
suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName), suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName),
noinline init: TestClass.() -> Unit noinline init: TestClass.() -> Unit
) { ) {
testClass(T::class.java.name, suiteTestClassName, init) testClass(T::class.java.name, suiteTestClassName, init)
} }
fun testClass( fun testClass(
baseTestClassName: String, baseTestClassName: String,
suiteTestClassName: String = getDefaultSuiteTestClassName(baseTestClassName.substringAfterLast('.')), suiteTestClassName: String = getDefaultSuiteTestClassName(baseTestClassName.substringAfterLast('.')),
init: TestClass.() -> Unit init: TestClass.() -> Unit
) { ) {
TestGenerator( TestGenerator(
testsRoot, testsRoot,
suiteTestClassName, suiteTestClassName,
baseTestClassName, baseTestClassName,
TestClass().apply(init).testModels TestClass().apply(init).testModels
).generateAndSave() ).generateAndSave()
} }
@@ -48,45 +36,51 @@ class TestGroup(private val testsRoot: String, val testDataRoot: String, val tes
val testModels = ArrayList<TestClassModel>() val testModels = ArrayList<TestClassModel>()
fun model( fun model(
relativeRootPath: String, relativeRootPath: String,
recursive: Boolean = true, recursive: Boolean = true,
excludeParentDirs: Boolean = false, excludeParentDirs: Boolean = false,
extension: String? = "kt", // null string means dir (name without dot) extension: String? = "kt", // null string means dir (name without dot)
pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$", pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$",
testMethod: String = "doTest", testMethod: String = "doTest",
singleClass: Boolean = false, singleClass: Boolean = false,
testClassName: String? = null, testClassName: String? = null,
targetBackend: TargetBackend = TargetBackend.ANY, targetBackend: TargetBackend = TargetBackend.ANY,
excludeDirs: List<String> = listOf(), excludeDirs: List<String> = listOf(),
filenameStartsLowerCase: Boolean? = null, filenameStartsLowerCase: Boolean? = null,
skipIgnored: Boolean = false skipIgnored: Boolean = false
) { ) {
val rootFile = File(testDataRoot + "/" + relativeRootPath) val rootFile = File("$testDataRoot/$relativeRootPath")
val compiledPattern = Pattern.compile(pattern) val compiledPattern = Pattern.compile(pattern)
val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile) val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile)
testModels.add( testModels.add(
if (singleClass) { if (singleClass) {
if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet") if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet")
SingleClassTestModel(rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend, SingleClassTestModel(
skipIgnored, testRunnerMethodName) rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend,
} skipIgnored, testRunnerMethodName
else { )
SimpleTestClassModel(rootFile, recursive, excludeParentDirs, } else {
compiledPattern, filenameStartsLowerCase, testMethod, className, SimpleTestClassModel(
targetBackend, excludeDirs, skipIgnored, testRunnerMethodName) rootFile, recursive, excludeParentDirs,
} compiledPattern, filenameStartsLowerCase, testMethod, className,
targetBackend, excludeDirs, skipIgnored, testRunnerMethodName
)
}
) )
} }
} }
} }
fun testGroup(testsRoot: String, testDataRoot: String, testRunnerMethodName: String = RunTestMethodModel.METHOD_NAME, init: TestGroup.() -> Unit) { fun testGroup(
testsRoot: String,
testDataRoot: String,
testRunnerMethodName: String = RunTestMethodModel.METHOD_NAME,
init: TestGroup.() -> Unit
) {
TestGroup(testsRoot, testDataRoot, testRunnerMethodName).init() TestGroup(testsRoot, testDataRoot, testRunnerMethodName).init()
} }
fun getDefaultSuiteTestClassName(baseTestClassName: String): String { fun getDefaultSuiteTestClassName(baseTestClassName: String): String {
if (!baseTestClassName.startsWith("Abstract")) { require(baseTestClassName.startsWith("Abstract")) { "Doesn't start with \"Abstract\": $baseTestClassName" }
throw IllegalArgumentException("Doesn't start with \"Abstract\": $baseTestClassName")
}
return baseTestClassName.substringAfter("Abstract") + "Generated" return baseTestClassName.substringAfter("Abstract") + "Generated"
} }