[TEST-GEN] Convert Test Generation DSL classes from java to kotlin
This commit is contained in:
+16
-47
@@ -13,58 +13,27 @@
|
||||
* 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;
|
||||
open class DelegatingTestClassModel(private val delegate: TestClassModel) : TestClassModel() {
|
||||
override val name: String
|
||||
get() = delegate.name
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
override val innerTestClasses: Collection<TestClassModel>
|
||||
get() = delegate.innerTestClasses
|
||||
|
||||
import java.util.Collection;
|
||||
override val methods: Collection<MethodModel>
|
||||
get() = delegate.methods
|
||||
|
||||
public class DelegatingTestClassModel extends TestClassModel {
|
||||
private final TestClassModel delegate;
|
||||
override val isEmpty: Boolean
|
||||
get() = delegate.isEmpty
|
||||
|
||||
public DelegatingTestClassModel(TestClassModel delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
override val dataPathRoot: String?
|
||||
get() = delegate.dataPathRoot
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return delegate.getName();
|
||||
}
|
||||
override val dataString: String?
|
||||
get() = delegate.dataString
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<TestClassModel> getInnerTestClasses() {
|
||||
return delegate.getInnerTestClasses();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
return delegate.getMethods();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return delegate.isEmpty();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDataPathRoot() {
|
||||
return delegate.getDataPathRoot();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return delegate.getDataString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<AnnotationModel> getAnnotations() {
|
||||
return delegate.getAnnotations();
|
||||
}
|
||||
override val annotations: Collection<AnnotationModel>
|
||||
get() = delegate.annotations
|
||||
}
|
||||
|
||||
+159
-238
@@ -2,268 +2,189 @@
|
||||
* Copyright 2010-2018 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.
|
||||
*/
|
||||
package org.jetbrains.kotlin.generators.tests.generator
|
||||
|
||||
package org.jetbrains.kotlin.generators.tests.generator;
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.generators.tests.generator.TestGeneratorUtil.fileNameToJavaIdentifier
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TargetBackend;
|
||||
import org.jetbrains.kotlin.utils.Printer;
|
||||
class SimpleTestClassModel(
|
||||
private val rootFile: File,
|
||||
private val recursive: Boolean,
|
||||
private val excludeParentDirs: Boolean,
|
||||
private val filenamePattern: Pattern,
|
||||
private val excludePattern: Pattern?,
|
||||
private val checkFilenameStartsLowerCase: Boolean?,
|
||||
private val doTestMethodName: String,
|
||||
private val testClassName: String,
|
||||
private val targetBackend: TargetBackend,
|
||||
excludeDirs: Collection<String>,
|
||||
private val skipIgnored: Boolean,
|
||||
private val testRunnerMethodName: String,
|
||||
private val additionalRunnerArguments: List<String>,
|
||||
private val deep: Int?,
|
||||
override val annotations: Collection<AnnotationModel>,
|
||||
) : TestClassModel() {
|
||||
override val name: String
|
||||
get() = testClassName
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
private val excludeDirs: Set<String> = excludeDirs.toSet()
|
||||
|
||||
public class SimpleTestClassModel extends TestClassModel {
|
||||
private static final Comparator<TestEntityModel> BY_NAME = Comparator.comparing(TestEntityModel::getName);
|
||||
|
||||
@NotNull
|
||||
private final File rootFile;
|
||||
private final boolean recursive;
|
||||
private final boolean excludeParentDirs;
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@Nullable
|
||||
private final Pattern excludePattern;
|
||||
@Nullable
|
||||
private final Boolean checkFilenameStartsLowerCase;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@NotNull
|
||||
private final String testClassName;
|
||||
private final Integer deep;
|
||||
@NotNull
|
||||
private final TargetBackend targetBackend;
|
||||
@NotNull
|
||||
private final Set<String> excludeDirs;
|
||||
@Nullable
|
||||
private Collection<TestClassModel> innerTestClasses;
|
||||
@Nullable
|
||||
private Collection<MethodModel> testMethods;
|
||||
|
||||
@NotNull
|
||||
private final Collection<AnnotationModel> annotations;
|
||||
|
||||
private final boolean skipIgnored;
|
||||
private final String testRunnerMethodName;
|
||||
private final List<String> additionalRunnerArguments;
|
||||
|
||||
public SimpleTestClassModel(
|
||||
@NotNull File rootFile,
|
||||
boolean recursive,
|
||||
boolean excludeParentDirs,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludedPattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
@NotNull Collection<String> excludeDirs,
|
||||
boolean skipIgnored,
|
||||
String testRunnerMethodName,
|
||||
List<String> additionalRunnerArguments,
|
||||
Integer deep,
|
||||
@NotNull Collection<AnnotationModel> annotations
|
||||
) {
|
||||
this.rootFile = rootFile;
|
||||
this.recursive = recursive;
|
||||
this.excludeParentDirs = excludeParentDirs;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.excludePattern = excludedPattern;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
this.targetBackend = targetBackend;
|
||||
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
|
||||
this.excludeDirs = excludeDirs.isEmpty() ? Collections.emptySet() : new LinkedHashSet<>(excludeDirs);
|
||||
this.skipIgnored = skipIgnored;
|
||||
this.testRunnerMethodName = testRunnerMethodName;
|
||||
this.additionalRunnerArguments = additionalRunnerArguments;
|
||||
this.deep = deep;
|
||||
this.annotations = annotations;
|
||||
override val innerTestClasses: Collection<TestClassModel> by lazy {
|
||||
if (!rootFile.isDirectory || !recursive || deep != null && deep < 1) {
|
||||
return@lazy emptyList()
|
||||
}
|
||||
val children = mutableListOf<TestClassModel>()
|
||||
val files = rootFile.listFiles() ?: return@lazy emptyList()
|
||||
for (file in files) {
|
||||
if (file.isDirectory && dirHasFilesInside(file) && !excludeDirs.contains(file.name)) {
|
||||
val innerTestClassName = fileNameToJavaIdentifier(file)
|
||||
children.add(
|
||||
SimpleTestClassModel(
|
||||
file,
|
||||
true,
|
||||
excludeParentDirs,
|
||||
filenamePattern,
|
||||
excludePattern,
|
||||
checkFilenameStartsLowerCase,
|
||||
doTestMethodName,
|
||||
innerTestClassName,
|
||||
targetBackend,
|
||||
excludesStripOneDirectory(file.name),
|
||||
skipIgnored,
|
||||
testRunnerMethodName,
|
||||
additionalRunnerArguments,
|
||||
if (deep != null) deep - 1 else null,
|
||||
annotations,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
children.sortWith(BY_NAME)
|
||||
children
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<TestClassModel> getInnerTestClasses() {
|
||||
if (!rootFile.isDirectory() || !recursive || deep != null && deep < 1) {
|
||||
return Collections.emptyList();
|
||||
|
||||
private fun excludesStripOneDirectory(directoryName: String): Set<String> {
|
||||
if (excludeDirs.isEmpty()) return excludeDirs
|
||||
val result: MutableSet<String> = LinkedHashSet()
|
||||
for (excludeDir in excludeDirs) {
|
||||
val firstSlash = excludeDir.indexOf('/')
|
||||
if (firstSlash >= 0 && excludeDir.substring(0, firstSlash) == directoryName) {
|
||||
result.add(excludeDir.substring(firstSlash + 1))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
if (innerTestClasses == null) {
|
||||
List<TestClassModel> children = new ArrayList<>();
|
||||
File[] files = rootFile.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isDirectory() && dirHasFilesInside(file) && !excludeDirs.contains(file.getName())) {
|
||||
String innerTestClassName = TestGeneratorUtil.fileNameToJavaIdentifier(file);
|
||||
children.add(new SimpleTestClassModel(
|
||||
file, true, excludeParentDirs, filenamePattern, excludePattern, checkFilenameStartsLowerCase,
|
||||
doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()),
|
||||
skipIgnored, testRunnerMethodName, additionalRunnerArguments, deep != null ? deep - 1 : null, annotations
|
||||
)
|
||||
);
|
||||
|
||||
override val methods: Collection<MethodModel> by lazy {
|
||||
if (!rootFile.isDirectory) {
|
||||
return@lazy listOf(
|
||||
SimpleTestMethodModel(
|
||||
rootFile, rootFile, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
)
|
||||
)
|
||||
}
|
||||
val result = mutableListOf<MethodModel>()
|
||||
result.add(RunTestMethodModel(targetBackend, doTestMethodName, testRunnerMethodName, additionalRunnerArguments))
|
||||
result.add(TestAllFilesPresentMethodModel())
|
||||
val listFiles = rootFile.listFiles()
|
||||
if (listFiles != null && (deep == null || deep == 0)) {
|
||||
for (file in listFiles) {
|
||||
val excluded = excludePattern != null && excludePattern.matcher(file.name).matches()
|
||||
if (filenamePattern.matcher(file.name).matches() && !excluded) {
|
||||
if (file.isDirectory && excludeParentDirs && dirHasSubDirs(file)) {
|
||||
continue
|
||||
}
|
||||
result.add(
|
||||
SimpleTestMethodModel(
|
||||
rootFile, file, filenamePattern,
|
||||
checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
children.sort(BY_NAME);
|
||||
innerTestClasses = children;
|
||||
}
|
||||
return innerTestClasses;
|
||||
result.sortWith(BY_NAME)
|
||||
result
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Set<String> excludesStripOneDirectory(@NotNull String directoryName) {
|
||||
if (excludeDirs.isEmpty()) return excludeDirs;
|
||||
override val isEmpty: Boolean
|
||||
get() {
|
||||
val noTestMethods = methods.size == 1
|
||||
return noTestMethods && innerTestClasses.isEmpty()
|
||||
}
|
||||
|
||||
Set<String> result = new LinkedHashSet<>();
|
||||
for (String excludeDir : excludeDirs) {
|
||||
int firstSlash = excludeDir.indexOf('/');
|
||||
if (firstSlash >= 0 && excludeDir.substring(0, firstSlash).equals(directoryName)) {
|
||||
result.add(excludeDir.substring(firstSlash + 1));
|
||||
override val dataString: String
|
||||
get() = KotlinTestUtils.getFilePath(rootFile)
|
||||
|
||||
override val dataPathRoot: String
|
||||
get() = "\$PROJECT_ROOT"
|
||||
|
||||
private inner class TestAllFilesPresentMethodModel : TestMethodModel() {
|
||||
override val name: String
|
||||
get() = "testAllFilesPresentIn$testClassName"
|
||||
|
||||
override val dataString: String?
|
||||
get() = null
|
||||
|
||||
override fun generateBody(p: Printer) {
|
||||
val exclude = StringBuilder()
|
||||
for (dir in excludeDirs) {
|
||||
exclude.append(", \"")
|
||||
exclude.append(StringUtil.escapeStringCharacters(dir))
|
||||
exclude.append("\"")
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static boolean dirHasFilesInside(@NotNull File dir) {
|
||||
return !FileUtil.processFilesRecursively(dir, File::isDirectory);
|
||||
}
|
||||
|
||||
private static boolean dirHasSubDirs(@NotNull File dir) {
|
||||
File[] listFiles = dir.listFiles();
|
||||
if (listFiles == null) {
|
||||
return false;
|
||||
}
|
||||
for (File file : listFiles) {
|
||||
if (file.isDirectory()) {
|
||||
return true;
|
||||
val excludedArgument = if (excludePattern != null) {
|
||||
String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()))
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val assertTestsPresentStr = if (targetBackend === TargetBackend.ANY) {
|
||||
String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile),
|
||||
StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
excludedArgument,
|
||||
recursive,
|
||||
exclude
|
||||
)
|
||||
} else {
|
||||
String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
excludedArgument, TargetBackend::class.java.simpleName, targetBackend.toString(), recursive, exclude
|
||||
)
|
||||
}
|
||||
p.println(assertTestsPresentStr)
|
||||
}
|
||||
|
||||
override fun shouldBeGenerated(): Boolean {
|
||||
return true
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
if (testMethods == null) {
|
||||
if (!rootFile.isDirectory()) {
|
||||
testMethods = Collections.singletonList(new SimpleTestMethodModel(
|
||||
rootFile, rootFile, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
));
|
||||
}
|
||||
else {
|
||||
List<MethodModel> result = new ArrayList<>();
|
||||
companion object {
|
||||
private val BY_NAME = Comparator.comparing(TestEntityModel::name)
|
||||
|
||||
result.add(new RunTestMethodModel(targetBackend, doTestMethodName, testRunnerMethodName, additionalRunnerArguments));
|
||||
private fun dirHasFilesInside(dir: File): Boolean {
|
||||
return !FileUtil.processFilesRecursively(dir) { obj: File -> obj.isDirectory }
|
||||
}
|
||||
|
||||
result.add(new TestAllFilesPresentMethodModel());
|
||||
|
||||
File[] listFiles = rootFile.listFiles();
|
||||
|
||||
if (listFiles != null && (deep == null || deep == 0)) {
|
||||
for (File file : listFiles) {
|
||||
boolean excluded = excludePattern != null && excludePattern.matcher(file.getName()).matches();
|
||||
if (filenamePattern.matcher(file.getName()).matches() && !excluded) {
|
||||
|
||||
if (file.isDirectory() && excludeParentDirs && dirHasSubDirs(file)) {
|
||||
continue;
|
||||
}
|
||||
result.add(new SimpleTestMethodModel(rootFile, file, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored));
|
||||
}
|
||||
}
|
||||
private fun dirHasSubDirs(dir: File): Boolean {
|
||||
val listFiles = dir.listFiles() ?: return false
|
||||
for (file in listFiles) {
|
||||
if (file.isDirectory) {
|
||||
return true
|
||||
}
|
||||
|
||||
result.sort(BY_NAME);
|
||||
|
||||
testMethods = result;
|
||||
}
|
||||
}
|
||||
return testMethods;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
boolean noTestMethods = getMethods().size() == 1;
|
||||
return noTestMethods && getInnerTestClasses().isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return KotlinTestUtils.getFilePath(rootFile);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDataPathRoot() {
|
||||
return "$PROJECT_ROOT";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return testClassName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<AnnotationModel> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private class TestAllFilesPresentMethodModel extends TestMethodModel {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "testAllFilesPresentIn" + testClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
StringBuilder exclude = new StringBuilder();
|
||||
for (String dir : excludeDirs) {
|
||||
exclude.append(", \"");
|
||||
exclude.append(StringUtil.escapeStringCharacters(dir));
|
||||
exclude.append("\"");
|
||||
}
|
||||
|
||||
String excludedArgument;
|
||||
if (excludePattern != null) {
|
||||
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
|
||||
} else {
|
||||
excludedArgument = null;
|
||||
}
|
||||
|
||||
String assertTestsPresentStr;
|
||||
|
||||
if (targetBackend == TargetBackend.ANY) {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument, recursive, exclude
|
||||
);
|
||||
} else {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
excludedArgument, TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
|
||||
);
|
||||
}
|
||||
|
||||
p.println(assertTestsPresentStr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeGenerated() {
|
||||
return true;
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+50
-85
@@ -2,100 +2,65 @@
|
||||
* Copyright 2010-2018 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.
|
||||
*/
|
||||
package org.jetbrains.kotlin.generators.tests.generator
|
||||
|
||||
package org.jetbrains.kotlin.generators.tests.generator;
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import org.jetbrains.kotlin.generators.tests.generator.TestGeneratorUtil.escapeForJavaIdentifier
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import java.io.File
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import kotlin.text.StringsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.test.InTextDirectivesUtils;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TargetBackend;
|
||||
import org.jetbrains.kotlin.utils.Printer;
|
||||
open class SimpleTestMethodModel(
|
||||
private val rootDir: File,
|
||||
protected val file: File,
|
||||
private val filenamePattern: Pattern,
|
||||
checkFilenameStartsLowerCase: Boolean?,
|
||||
protected val targetBackend: TargetBackend,
|
||||
private val skipIgnored: Boolean
|
||||
) : TestMethodModel() {
|
||||
override fun generateBody(p: Printer) {
|
||||
val filePath = KotlinTestUtils.getFilePath(file) + if (file.isDirectory) "/" else ""
|
||||
p.println(RunTestMethodModel.METHOD_NAME, "(\"", filePath, "\");")
|
||||
}
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
override val dataString: String
|
||||
get() {
|
||||
val path = FileUtil.getRelativePath(rootDir, file)!!
|
||||
return KotlinTestUtils.getFilePath(File(path))
|
||||
}
|
||||
|
||||
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTarget;
|
||||
override fun shouldBeGenerated(): Boolean {
|
||||
return InTextDirectivesUtils.isCompatibleTarget(targetBackend, file)
|
||||
}
|
||||
|
||||
public class SimpleTestMethodModel extends TestMethodModel {
|
||||
|
||||
@NotNull
|
||||
private final File rootDir;
|
||||
@NotNull
|
||||
protected final File file;
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@NotNull
|
||||
protected final TargetBackend targetBackend;
|
||||
|
||||
private final boolean skipIgnored;
|
||||
|
||||
public SimpleTestMethodModel(
|
||||
@NotNull File rootDir,
|
||||
@NotNull File file,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean skipIgnored
|
||||
) {
|
||||
this.rootDir = rootDir;
|
||||
this.file = file;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.targetBackend = targetBackend;
|
||||
this.skipIgnored = skipIgnored;
|
||||
override val name: String
|
||||
get() {
|
||||
val matcher = filenamePattern.matcher(file.name)
|
||||
val found = matcher.find()
|
||||
assert(found) { file.name + " isn't matched by regex " + filenamePattern.pattern() }
|
||||
assert(matcher.groupCount() >= 1) { filenamePattern.pattern() }
|
||||
val extractedName = matcher.group(1) ?: error("extractedName should not be null: " + filenamePattern.pattern())
|
||||
val unescapedName = if (rootDir == file.parentFile) {
|
||||
extractedName
|
||||
} else {
|
||||
val relativePath = FileUtil.getRelativePath(rootDir, file.parentFile)
|
||||
relativePath + "-" + extractedName.capitalize()
|
||||
}
|
||||
val ignored = skipIgnored && InTextDirectivesUtils.isIgnoredTarget(targetBackend, file)
|
||||
return (if (ignored) "ignore" else "test") + escapeForJavaIdentifier(unescapedName).capitalize()
|
||||
}
|
||||
|
||||
init {
|
||||
if (checkFilenameStartsLowerCase != null) {
|
||||
char c = file.getName().charAt(0);
|
||||
val c = file.name[0]
|
||||
if (checkFilenameStartsLowerCase) {
|
||||
assert Character.isLowerCase(c) : "Invalid file name '" + file + "', file name should start with lower-case letter";
|
||||
}
|
||||
else {
|
||||
assert Character.isUpperCase(c) : "Invalid file name '" + file + "', file name should start with upper-case letter";
|
||||
assert(Character.isLowerCase(c)) { "Invalid file name '$file', file name should start with lower-case letter" }
|
||||
} else {
|
||||
assert(Character.isUpperCase(c)) { "Invalid file name '$file', file name should start with upper-case letter" }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
String filePath = KotlinTestUtils.getFilePath(file) + (file.isDirectory() ? "/" : "");
|
||||
p.println(RunTestMethodModel.METHOD_NAME, "(\"", filePath, "\");");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
String path = FileUtil.getRelativePath(rootDir, file);
|
||||
assert path != null;
|
||||
return KotlinTestUtils.getFilePath(new File(path));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeGenerated() {
|
||||
return InTextDirectivesUtils.isCompatibleTarget(targetBackend, file);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
Matcher matcher = filenamePattern.matcher(file.getName());
|
||||
boolean found = matcher.find();
|
||||
assert found : file.getName() + " isn't matched by regex " + filenamePattern.pattern();
|
||||
assert matcher.groupCount() >= 1 : filenamePattern.pattern();
|
||||
String extractedName = matcher.group(1);
|
||||
assert extractedName != null : "extractedName should not be null: " + filenamePattern.pattern();
|
||||
|
||||
String unescapedName;
|
||||
if (rootDir.equals(file.getParentFile())) {
|
||||
unescapedName = extractedName;
|
||||
}
|
||||
else {
|
||||
String relativePath = FileUtil.getRelativePath(rootDir, file.getParentFile());
|
||||
unescapedName = relativePath + "-" + StringsKt.capitalize(extractedName);
|
||||
}
|
||||
|
||||
boolean ignored = skipIgnored && isIgnoredTarget(targetBackend, file);
|
||||
return (ignored ? "ignore" : "test") + StringsKt.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
|
||||
}
|
||||
}
|
||||
|
||||
+73
-161
@@ -13,184 +13,96 @@
|
||||
* 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;
|
||||
import com.intellij.openapi.util.io.FileUtil
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import org.jetbrains.kotlin.test.TargetBackend
|
||||
import org.jetbrains.kotlin.utils.Printer
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TargetBackend;
|
||||
import org.jetbrains.kotlin.utils.Printer;
|
||||
class SingleClassTestModel(
|
||||
private val rootFile: File,
|
||||
private val filenamePattern: Pattern,
|
||||
private val excludePattern: Pattern?,
|
||||
private val checkFilenameStartsLowerCase: Boolean?,
|
||||
private val doTestMethodName: String,
|
||||
private val testClassName: String,
|
||||
private val targetBackend: TargetBackend,
|
||||
private val skipIgnored: Boolean,
|
||||
private val testRunnerMethodName: String,
|
||||
private val additionalRunnerArguments: List<String>,
|
||||
override val annotations: List<AnnotationModel>
|
||||
) : TestClassModel() {
|
||||
override val name: String
|
||||
get() = testClassName
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SingleClassTestModel extends TestClassModel {
|
||||
@NotNull
|
||||
private final File rootFile;
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@Nullable
|
||||
private final Pattern excludePattern;
|
||||
@Nullable
|
||||
private final Boolean checkFilenameStartsLowerCase;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@NotNull
|
||||
private final String testClassName;
|
||||
@NotNull
|
||||
private final TargetBackend targetBackend;
|
||||
@Nullable
|
||||
private Collection<MethodModel> methods;
|
||||
|
||||
private final boolean skipIgnored;
|
||||
private final String testRunnerMethodName;
|
||||
private final List<String> additionalRunnerArguments;
|
||||
|
||||
@NotNull
|
||||
private final List<AnnotationModel> annotations;
|
||||
|
||||
public SingleClassTestModel(
|
||||
@NotNull File rootFile,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Pattern excludePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean skipIgnored,
|
||||
String testRunnerMethodName,
|
||||
List<String> additionalRunnerArguments,
|
||||
@NotNull List<AnnotationModel> annotations
|
||||
) {
|
||||
this.rootFile = rootFile;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.excludePattern = excludePattern;
|
||||
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
this.targetBackend = targetBackend;
|
||||
this.skipIgnored = skipIgnored;
|
||||
this.testRunnerMethodName = testRunnerMethodName;
|
||||
this.additionalRunnerArguments = additionalRunnerArguments;
|
||||
this.annotations = annotations;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public final Collection<TestClassModel> getInnerTestClasses() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
if (methods == null) {
|
||||
List<MethodModel> result = new ArrayList<>();
|
||||
|
||||
result.add(new RunTestMethodModel(targetBackend, doTestMethodName, testRunnerMethodName, additionalRunnerArguments));
|
||||
|
||||
result.add(new TestAllFilesPresentMethodModel());
|
||||
|
||||
FileUtil.processFilesRecursively(rootFile, file -> {
|
||||
if (!file.isDirectory() && filenamePattern.matcher(file.getName()).matches()) {
|
||||
result.addAll(getTestMethodsFromFile(file));
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
methods = CollectionsKt.sortedWith(result, (o1, o2) -> o1.getName().compareToIgnoreCase(o2.getName()));
|
||||
override val methods: Collection<MethodModel> by lazy {
|
||||
val result: MutableList<MethodModel> = ArrayList()
|
||||
result.add(RunTestMethodModel(targetBackend, doTestMethodName, testRunnerMethodName, additionalRunnerArguments))
|
||||
result.add(TestAllFilesPresentMethodModel())
|
||||
FileUtil.processFilesRecursively(rootFile) { file: File ->
|
||||
if (!file.isDirectory && filenamePattern.matcher(file.name).matches()) {
|
||||
result.addAll(getTestMethodsFromFile(file))
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
return methods;
|
||||
result.sortedWith { o1: MethodModel, o2: MethodModel -> o1.name.compareTo(o2.name, ignoreCase = true) }
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<TestMethodModel> getTestMethodsFromFile(File file) {
|
||||
return Collections.singletonList(new SimpleTestMethodModel(
|
||||
override val innerTestClasses: Collection<TestClassModel>
|
||||
get() = emptyList()
|
||||
|
||||
private fun getTestMethodsFromFile(file: File): Collection<TestMethodModel> {
|
||||
return listOf(
|
||||
SimpleTestMethodModel(
|
||||
rootFile, file, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
));
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
// There's always one test for checking if all tests are present
|
||||
return getMethods().size() <= 1;
|
||||
}
|
||||
// There's always one test for checking if all tests are present
|
||||
override val isEmpty: Boolean
|
||||
get() = methods.size <= 1
|
||||
override val dataString: String = KotlinTestUtils.getFilePath(rootFile)
|
||||
override val dataPathRoot: String = "\$PROJECT_ROOT"
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return KotlinTestUtils.getFilePath(rootFile);
|
||||
}
|
||||
private inner class TestAllFilesPresentMethodModel : TestMethodModel() {
|
||||
override val name: String = "testAllFilesPresentIn$testClassName"
|
||||
override val dataString: String?
|
||||
get() = null
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDataPathRoot() {
|
||||
return "$PROJECT_ROOT";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return testClassName;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<AnnotationModel> getAnnotations() {
|
||||
return annotations;
|
||||
}
|
||||
|
||||
private class TestAllFilesPresentMethodModel extends TestMethodModel {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "testAllFilesPresentIn" + testClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
String assertTestsPresentStr;
|
||||
|
||||
String excludedArgument;
|
||||
if (excludePattern != null) {
|
||||
excludedArgument = String.format("Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(excludePattern.pattern()));
|
||||
override fun generateBody(p: Printer) {
|
||||
val assertTestsPresentStr: String
|
||||
val excludedArgument = if (excludePattern != null) {
|
||||
String.format(
|
||||
"Pattern.compile(\"%s\")", StringUtil.escapeStringCharacters(
|
||||
excludePattern.pattern()
|
||||
)
|
||||
)
|
||||
} else {
|
||||
excludedArgument = null;
|
||||
null
|
||||
}
|
||||
|
||||
if (targetBackend != TargetBackend.ANY) {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
excludedArgument, TargetBackend.class.getSimpleName(), targetBackend.toString()
|
||||
);
|
||||
assertTestsPresentStr = if (targetBackend !== TargetBackend.ANY) {
|
||||
String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s, %s.%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
excludedArgument, TargetBackend::class.java.simpleName, targetBackend.toString()
|
||||
)
|
||||
} else {
|
||||
assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument
|
||||
);
|
||||
String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClassWithExcluded(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), excludedArgument
|
||||
)
|
||||
}
|
||||
p.println(assertTestsPresentStr);
|
||||
p.println(assertTestsPresentStr)
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeGenerated() {
|
||||
return true;
|
||||
override fun shouldBeGenerated(): Boolean {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+17
-21
@@ -13,34 +13,30 @@
|
||||
* 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;
|
||||
import kotlin.text.capitalize
|
||||
import org.jetbrains.kotlin.generators.tests.generator.TestGeneratorUtil
|
||||
import java.io.File
|
||||
import java.lang.StringBuilder
|
||||
|
||||
import kotlin.text.StringsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TestGeneratorUtil {
|
||||
@NotNull
|
||||
public static String escapeForJavaIdentifier(String fileName) {
|
||||
object TestGeneratorUtil {
|
||||
@JvmStatic
|
||||
fun escapeForJavaIdentifier(fileName: String): String {
|
||||
// A file name may contain characters (like ".") that can't be a part of method name
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (int i = 0; i < fileName.length(); i++) {
|
||||
char c = fileName.charAt(i);
|
||||
|
||||
val result = StringBuilder()
|
||||
for (c in fileName) {
|
||||
if (Character.isJavaIdentifierPart(c)) {
|
||||
result.append(c);
|
||||
}
|
||||
else {
|
||||
result.append("_");
|
||||
result.append(c)
|
||||
} else {
|
||||
result.append("_")
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
return result.toString()
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String fileNameToJavaIdentifier(@NotNull File file) {
|
||||
return StringsKt.capitalize(escapeForJavaIdentifier(file.getName()));
|
||||
@JvmStatic
|
||||
fun fileNameToJavaIdentifier(file: File): String {
|
||||
return escapeForJavaIdentifier(file.name).capitalize()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user