Extract 'test-generator' module out of 'generators'

Also avoid using intellij API where kotlin-stdlib can be used instead
This commit is contained in:
Alexander Udalov
2017-10-19 18:11:51 +02:00
parent d79b571781
commit d0274c3c53
14 changed files with 173 additions and 98 deletions
+1
View File
@@ -32,6 +32,7 @@ dependencies {
compile(projectTests(":kotlin-annotation-processing"))
compile(projectTests(":plugins:uast-kotlin"))
compile(projectTests(":js:js.tests"))
compile(projectTests(":generators:test-generator"))
compile(ideaSdkDeps("jps-build-test", subdir = "jps/test"))
testCompile(project(":idea:idea-test-framework")) { isTransitive = false }
testCompile(project(":compiler:incremental-compilation-impl"))
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.generators.tests
import junit.framework.TestCase
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.AbstractDataFlowValueRenderingTest
import org.jetbrains.kotlin.addImport.AbstractAddImportTest
@@ -53,7 +52,8 @@ import org.jetbrains.kotlin.findUsages.AbstractFindUsagesTest
import org.jetbrains.kotlin.findUsages.AbstractKotlinFindUsagesWithLibraryTest
import org.jetbrains.kotlin.formatter.AbstractFormatterTest
import org.jetbrains.kotlin.formatter.AbstractTypingIndentationTestBase
import org.jetbrains.kotlin.generators.tests.generator.*
import org.jetbrains.kotlin.generators.tests.generator.TestGroup
import org.jetbrains.kotlin.generators.tests.generator.testGroup
import org.jetbrains.kotlin.idea.AbstractExpressionSelectionTest
import org.jetbrains.kotlin.idea.AbstractKotlinTypeAliasByExpansionShortNameIndexTest
import org.jetbrains.kotlin.idea.AbstractSmartSelectionTest
@@ -191,15 +191,11 @@ import org.jetbrains.kotlin.serialization.AbstractLocalClassProtoTest
import org.jetbrains.kotlin.shortenRefs.AbstractShortenRefsTest
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.types.AbstractTypeBindingTest
import java.io.File
import java.lang.IllegalArgumentException
import java.util.*
import java.util.regex.Pattern
@Language("RegExp") private val KT_OR_KTS = """^(.+)\.(kt|kts)$"""
@Language("RegExp") private val KT_OR_KTS_WITHOUT_DOTS_IN_NAME = """^([^.]+)\.(kt|kts)$"""
@Language("RegExp") private const val KT_OR_KTS = """^(.+)\.(kt|kts)$"""
@Language("RegExp") private const val KT_OR_KTS_WITHOUT_DOTS_IN_NAME = """^([^.]+)\.(kt|kts)$"""
@Language("RegExp") private val KT_WITHOUT_DOTS_IN_NAME = """^([^.]+)\.kt$"""
@Language("RegExp") private const val KT_WITHOUT_DOTS_IN_NAME = """^([^.]+)\.kt$"""
fun main(args: Array<String>) {
System.setProperty("java.awt.headless", "true")
@@ -1528,71 +1524,3 @@ fun main(args: Array<String>) {
}
}
}
class TestGroup(private val testsRoot: String, val testDataRoot: String) {
inline fun <reified T: TestCase> testClass(
suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName),
noinline init: TestClass.() -> Unit
) {
testClass(T::class.java.name, suiteTestClassName, init)
}
fun testClass(
baseTestClassName: String,
suiteTestClassName: String = getDefaultSuiteTestClassName(baseTestClassName.substringAfterLast('.')),
init: TestClass.() -> Unit
) {
TestGenerator(
testsRoot,
suiteTestClassName,
baseTestClassName,
TestClass().apply(init).testModels
).generateAndSave()
}
inner class TestClass {
val testModels = ArrayList<TestClassModel>()
fun model(
relativeRootPath: String,
recursive: Boolean = true,
excludeParentDirs: Boolean = false,
extension: String? = "kt", // null string means dir (name without dot)
pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$",
testMethod: String = "doTest",
singleClass: Boolean = false,
testClassName: String? = null,
targetBackend: TargetBackend = TargetBackend.ANY,
excludeDirs: List<String> = listOf(),
filenameStartsLowerCase: Boolean? = null,
skipIgnored: Boolean = false
) {
val rootFile = File(testDataRoot + "/" + relativeRootPath)
val compiledPattern = Pattern.compile(pattern)
val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile)
testModels.add(
if (singleClass) {
if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet")
SingleClassTestModel(rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend,
skipIgnored)
}
else {
SimpleTestClassModel(rootFile, recursive, excludeParentDirs,
compiledPattern, filenameStartsLowerCase, testMethod, className,
targetBackend, excludeDirs, skipIgnored)
}
)
}
}
}
fun testGroup(testsRoot: String, testDataRoot: String, init: TestGroup.() -> Unit) {
TestGroup(testsRoot, testDataRoot).init()
}
fun getDefaultSuiteTestClassName(baseTestClassName: String): String {
if (!baseTestClassName.startsWith("Abstract")) {
throw IllegalArgumentException("Doesn't start with \"Abstract\": $baseTestClassName")
}
return baseTestClassName.substringAfter("Abstract") + "Generated"
}
@@ -0,0 +1,17 @@
apply { plugin("kotlin") }
dependencies {
testCompile(project(":core:util.runtime"))
testCompile(projectTests(":compiler:tests-common"))
testCompile(projectDist(":kotlin-stdlib"))
testCompile(commonDep("junit:junit"))
testCompile(ideaSdkDeps("util"))
}
sourceSets {
"main" { }
"test" { projectDefault() }
}
testsJar {}
@@ -16,7 +16,6 @@
package org.jetbrains.kotlin.generators.tests.generator;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
@@ -87,7 +86,7 @@ public class SimpleTestClassModel implements TestClassModel {
}
if (innerTestClasses == null) {
List<TestClassModel> children = Lists.newArrayList();
List<TestClassModel> children = new ArrayList<>();
File[] files = rootFile.listFiles();
if (files != null) {
for (File file : files) {
@@ -149,7 +148,7 @@ public class SimpleTestClassModel implements TestClassModel {
));
}
else {
List<MethodModel> result = Lists.newArrayList();
List<MethodModel> result = new ArrayList<>();
result.add(new TestAllFilesPresentMethodModel());
@@ -17,7 +17,7 @@
package org.jetbrains.kotlin.generators.tests.generator;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
@@ -126,12 +126,12 @@ public class SimpleTestMethodModel implements TestMethodModel {
}
else {
String relativePath = FileUtil.getRelativePath(rootDir, file.getParentFile());
unescapedName = relativePath + "-" + StringUtil.capitalize(extractedName);
unescapedName = relativePath + "-" + StringsKt.capitalize(extractedName);
}
boolean ignored = isIgnoredTargetWithoutCheck(targetBackend, file) ||
skipIgnored && isIgnoredTarget(targetBackend, file);
return (ignored ? "ignore" : "test") + StringUtil.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
return (ignored ? "ignore" : "test") + StringsKt.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
}
@Override
@@ -16,10 +16,9 @@
package org.jetbrains.kotlin.generators.tests.generator;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.containers.ContainerUtil;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.test.KotlinTestUtils;
@@ -27,6 +26,7 @@ import org.jetbrains.kotlin.test.TargetBackend;
import org.jetbrains.kotlin.utils.Printer;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
@@ -78,7 +78,7 @@ public class SingleClassTestModel implements TestClassModel {
@Override
public Collection<MethodModel> getMethods() {
if (methods == null) {
List<TestMethodModel> result = Lists.newArrayList();
List<TestMethodModel> result = new ArrayList<>();
result.add(new TestAllFilesPresentMethodModel());
@@ -90,9 +90,7 @@ public class SingleClassTestModel implements TestClassModel {
return true;
});
ContainerUtil.sort(result, (o1, o2) -> StringUtil.compare(o1.getName(), o2.getName(), true));
methods = Lists.newArrayList(result);
methods = CollectionsKt.sortedWith(result, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
}
return methods;
@@ -0,0 +1,92 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* 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
import junit.framework.TestCase
import org.jetbrains.kotlin.test.TargetBackend
import java.io.File
import java.lang.IllegalArgumentException
import java.util.ArrayList
import java.util.regex.Pattern
class TestGroup(private val testsRoot: String, val testDataRoot: String) {
inline fun <reified T: TestCase> testClass(
suiteTestClassName: String = getDefaultSuiteTestClassName(T::class.java.simpleName),
noinline init: TestClass.() -> Unit
) {
testClass(T::class.java.name, suiteTestClassName, init)
}
fun testClass(
baseTestClassName: String,
suiteTestClassName: String = getDefaultSuiteTestClassName(baseTestClassName.substringAfterLast('.')),
init: TestClass.() -> Unit
) {
TestGenerator(
testsRoot,
suiteTestClassName,
baseTestClassName,
TestClass().apply(init).testModels
).generateAndSave()
}
inner class TestClass {
val testModels = ArrayList<TestClassModel>()
fun model(
relativeRootPath: String,
recursive: Boolean = true,
excludeParentDirs: Boolean = false,
extension: String? = "kt", // null string means dir (name without dot)
pattern: String = if (extension == null) """^([^\.]+)$""" else "^(.+)\\.$extension\$",
testMethod: String = "doTest",
singleClass: Boolean = false,
testClassName: String? = null,
targetBackend: TargetBackend = TargetBackend.ANY,
excludeDirs: List<String> = listOf(),
filenameStartsLowerCase: Boolean? = null,
skipIgnored: Boolean = false
) {
val rootFile = File(testDataRoot + "/" + relativeRootPath)
val compiledPattern = Pattern.compile(pattern)
val className = testClassName ?: TestGeneratorUtil.fileNameToJavaIdentifier(rootFile)
testModels.add(
if (singleClass) {
if (excludeDirs.isNotEmpty()) error("excludeDirs is unsupported for SingleClassTestModel yet")
SingleClassTestModel(rootFile, compiledPattern, filenameStartsLowerCase, testMethod, className, targetBackend,
skipIgnored)
}
else {
SimpleTestClassModel(rootFile, recursive, excludeParentDirs,
compiledPattern, filenameStartsLowerCase, testMethod, className,
targetBackend, excludeDirs, skipIgnored)
}
)
}
}
}
fun testGroup(testsRoot: String, testDataRoot: String, init: TestGroup.() -> Unit) {
TestGroup(testsRoot, testDataRoot).init()
}
fun getDefaultSuiteTestClassName(baseTestClassName: String): String {
if (!baseTestClassName.startsWith("Abstract")) {
throw IllegalArgumentException("Doesn't start with \"Abstract\": $baseTestClassName")
}
return baseTestClassName.substringAfter("Abstract") + "Generated"
}
@@ -16,7 +16,8 @@
package org.jetbrains.kotlin.generators.tests.generator;
import com.intellij.openapi.util.io.FileUtil;
import kotlin.io.FilesKt;
import kotlin.text.Charsets;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -68,7 +69,7 @@ public class TestGenerator {
StringBuilder out = new StringBuilder();
Printer p = new Printer(out);
p.println(FileUtil.loadFile(new File("license/LICENSE.txt")));
p.println(FilesKt.readText(new File("license/LICENSE.txt"), Charsets.UTF_8));
p.println("package ", suiteClassPackage, ";");
p.println();
p.println("import com.intellij.testFramework.TestDataPath;");
@@ -16,7 +16,7 @@
package org.jetbrains.kotlin.generators.tests.generator;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.text.StringsKt;
import org.jetbrains.annotations.NotNull;
import java.io.File;
@@ -41,6 +41,6 @@ public class TestGeneratorUtil {
@NotNull
public static String fileNameToJavaIdentifier(@NotNull File file) {
return StringUtil.capitalize(escapeForJavaIdentifier(file.getName()));
return StringsKt.capitalize(escapeForJavaIdentifier(file.getName()));
}
}
@@ -17,8 +17,9 @@
package org.jetbrains.kotlin.generators.util;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import kotlin.io.FilesKt;
import kotlin.text.Charsets;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import java.io.File;
@@ -56,7 +57,7 @@ public class GeneratorsFileUtil {
File tempFile = useTempFile ? new File(KotlinTestUtils.tmpDir(file.getName()), file.getName() + ".tmp") : file;
FileUtil.writeToFile(tempFile, newText);
FilesKt.writeText(tempFile, newText, Charsets.UTF_8);
System.out.println("File written: " + tempFile.getAbsolutePath());
if (useTempFile) {
Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING);
@@ -68,7 +69,7 @@ public class GeneratorsFileUtil {
private static boolean checkFileIgnoringLineSeparators(File file, String content) {
String currentContent;
try {
currentContent = FileUtil.loadFile(file, true);
currentContent = FilesKt.readText(file, Charsets.UTF_8);
}
catch (Throwable ignored) {
return false;
+39 -2
View File
@@ -2769,7 +2769,8 @@
"testImplementation",
"testRuntime",
"testRuntimeClasspath",
"testRuntimeOnly"
"testRuntimeOnly",
"tests-jar"
],
"extensions": {
"ext": "org.gradle.api.plugins.ExtraPropertiesExtension",
@@ -2804,7 +2805,8 @@
"testImplementation",
"testRuntime",
"testRuntimeClasspath",
"testRuntimeOnly"
"testRuntimeOnly",
"tests-jar"
],
"extensions": {
"ext": "org.gradle.api.plugins.ExtraPropertiesExtension",
@@ -3052,6 +3054,41 @@
"reporting": "org.gradle.api.reporting.ReportingExtension"
}
},
":generators:test-generator": {
"conventions": {
"base": "org.gradle.api.plugins.BasePluginConvention",
"java": "org.gradle.api.plugins.JavaPluginConvention"
},
"configurations": [
"apiElements",
"archives",
"compile",
"compileClasspath",
"compileOnly",
"default",
"implementation",
"kapt",
"kaptTest",
"runtime",
"runtimeClasspath",
"runtimeElements",
"runtimeOnly",
"testCompile",
"testCompileClasspath",
"testCompileOnly",
"testImplementation",
"testRuntime",
"testRuntimeClasspath",
"testRuntimeOnly"
],
"extensions": {
"ext": "org.gradle.api.plugins.ExtraPropertiesExtension",
"kotlin": "org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension",
"kapt": "org.jetbrains.kotlin.gradle.plugin.KaptExtension",
"defaultArtifacts": "org.gradle.api.internal.plugins.DefaultArtifactPublicationSet",
"reporting": "org.gradle.api.reporting.ReportingExtension"
}
},
":idea:formatter": {
"conventions": {
"base": "org.gradle.api.plugins.BasePluginConvention",
+1
View File
@@ -117,6 +117,7 @@ include ":kotlin-build-common",
":kotlin-ant",
":compiler:tests-java8",
":generators",
":generators:test-generator",
":tools:binary-compatibility-validator",
":tools:kotlin-stdlib-js-merger",
":tools:kotlin-stdlib-gen",