Extract 'test-generator' module out of 'generators'
Also avoid using intellij API where kotlin-stdlib can be used instead
This commit is contained in:
@@ -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 {}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public class DelegatingTestClassModel implements TestClassModel {
|
||||
private final TestClassModel delegate;
|
||||
|
||||
public DelegatingTestClassModel(TestClassModel delegate) {
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return delegate.getName();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SimpleTestClassModel implements 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 Boolean checkFilenameStartsLowerCase;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@NotNull
|
||||
private final String testClassName;
|
||||
@NotNull
|
||||
private final TargetBackend targetBackend;
|
||||
@NotNull
|
||||
private final Set<String> excludeDirs;
|
||||
@Nullable
|
||||
private Collection<TestClassModel> innerTestClasses;
|
||||
@Nullable
|
||||
private Collection<MethodModel> testMethods;
|
||||
|
||||
private final boolean skipIgnored;
|
||||
|
||||
public SimpleTestClassModel(
|
||||
@NotNull File rootFile,
|
||||
boolean recursive,
|
||||
boolean excludeParentDirs,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
@NotNull Collection<String> excludeDirs,
|
||||
boolean skipIgnored
|
||||
) {
|
||||
this.rootFile = rootFile;
|
||||
this.recursive = recursive;
|
||||
this.excludeParentDirs = excludeParentDirs;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
this.targetBackend = targetBackend;
|
||||
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
|
||||
this.excludeDirs = excludeDirs.isEmpty() ? Collections.emptySet() : new LinkedHashSet<>(excludeDirs);
|
||||
this.skipIgnored = skipIgnored;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<TestClassModel> getInnerTestClasses() {
|
||||
if (!rootFile.isDirectory() || !recursive) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
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, checkFilenameStartsLowerCase,
|
||||
doTestMethodName, innerTestClassName, targetBackend, excludesStripOneDirectory(file.getName()),
|
||||
skipIgnored)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
children.sort(BY_NAME);
|
||||
innerTestClasses = children;
|
||||
}
|
||||
return innerTestClasses;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Set<String> excludesStripOneDirectory(@NotNull String directoryName) {
|
||||
if (excludeDirs.isEmpty()) return excludeDirs;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
if (testMethods == null) {
|
||||
if (!rootFile.isDirectory()) {
|
||||
testMethods = Collections.singletonList(new SimpleTestMethodModel(
|
||||
rootFile, rootFile, doTestMethodName, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
));
|
||||
}
|
||||
else {
|
||||
List<MethodModel> result = new ArrayList<>();
|
||||
|
||||
result.add(new TestAllFilesPresentMethodModel());
|
||||
|
||||
File[] listFiles = rootFile.listFiles();
|
||||
if (listFiles != null) {
|
||||
for (File file : listFiles) {
|
||||
if (filenamePattern.matcher(file.getName()).matches()) {
|
||||
|
||||
if (file.isDirectory() && excludeParentDirs && dirHasSubDirs(file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(new SimpleTestMethodModel(rootFile, file, doTestMethodName, filenamePattern,
|
||||
checkFilenameStartsLowerCase, targetBackend, skipIgnored));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
private class TestAllFilesPresentMethodModel implements 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 assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s, %s%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), TargetBackend.class.getSimpleName(), targetBackend.toString(), recursive, exclude
|
||||
);
|
||||
p.println(assertTestsPresentStr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateSignature(@NotNull Printer p) {
|
||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeGenerated() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 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;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTarget;
|
||||
import static org.jetbrains.kotlin.test.InTextDirectivesUtils.isIgnoredTargetWithoutCheck;
|
||||
|
||||
public class SimpleTestMethodModel implements TestMethodModel {
|
||||
|
||||
@NotNull
|
||||
private final File rootDir;
|
||||
@NotNull
|
||||
private final File file;
|
||||
@NotNull
|
||||
private final String doTestMethodName;
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@NotNull
|
||||
private final TargetBackend targetBackend;
|
||||
|
||||
private final boolean skipIgnored;
|
||||
|
||||
public SimpleTestMethodModel(
|
||||
@NotNull File rootDir,
|
||||
@NotNull File file,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean skipIgnored
|
||||
) {
|
||||
this.rootDir = rootDir;
|
||||
this.file = file;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.targetBackend = targetBackend;
|
||||
this.skipIgnored = skipIgnored;
|
||||
|
||||
if (checkFilenameStartsLowerCase != null) {
|
||||
char c = file.getName().charAt(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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
String filePath = KotlinTestUtils.getFilePath(file) + (file.isDirectory() ? "/" : "");
|
||||
p.println("String fileName = KotlinTestUtils.navigationMetadata(\"", filePath, "\");");
|
||||
|
||||
if (isIgnoredTarget(targetBackend, file)) {
|
||||
p.println("try {");
|
||||
p.pushIndent();
|
||||
}
|
||||
|
||||
p.println(doTestMethodName, "(fileName);");
|
||||
|
||||
if (isIgnoredTarget(targetBackend, file)) {
|
||||
p.popIndent();
|
||||
p.println("}");
|
||||
p.println("catch (Throwable ignore) {");
|
||||
p.pushIndent();
|
||||
p.println("return;");
|
||||
p.popIndent();
|
||||
p.println("}");
|
||||
p.println("throw new AssertionError(\"Looks like this test can be unmuted. Remove IGNORE_BACKEND directive for that.\");");
|
||||
}
|
||||
}
|
||||
|
||||
@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 = isIgnoredTargetWithoutCheck(targetBackend, file) ||
|
||||
skipIgnored && isIgnoredTarget(targetBackend, file);
|
||||
return (ignored ? "ignore" : "test") + StringsKt.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateSignature(@NotNull Printer p) {
|
||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
||||
}
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 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;
|
||||
|
||||
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;
|
||||
|
||||
public class SingleClassTestModel implements TestClassModel {
|
||||
@NotNull
|
||||
private final File rootFile;
|
||||
@NotNull
|
||||
private final Pattern filenamePattern;
|
||||
@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;
|
||||
|
||||
public SingleClassTestModel(
|
||||
@NotNull File rootFile,
|
||||
@NotNull Pattern filenamePattern,
|
||||
@Nullable Boolean checkFilenameStartsLowerCase,
|
||||
@NotNull String doTestMethodName,
|
||||
@NotNull String testClassName,
|
||||
@NotNull TargetBackend targetBackend,
|
||||
boolean skipIgnored
|
||||
) {
|
||||
this.rootFile = rootFile;
|
||||
this.filenamePattern = filenamePattern;
|
||||
this.checkFilenameStartsLowerCase = checkFilenameStartsLowerCase;
|
||||
this.doTestMethodName = doTestMethodName;
|
||||
this.testClassName = testClassName;
|
||||
this.targetBackend = targetBackend;
|
||||
this.skipIgnored = skipIgnored;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public final Collection<TestClassModel> getInnerTestClasses() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
if (methods == null) {
|
||||
List<TestMethodModel> result = new ArrayList<>();
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private Collection<TestMethodModel> getTestMethodsFromFile(File file) {
|
||||
return Collections.singletonList(new SimpleTestMethodModel(
|
||||
rootFile, file, doTestMethodName, filenamePattern, checkFilenameStartsLowerCase, targetBackend, skipIgnored
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
// There's always one test for checking if all tests are present
|
||||
return getMethods().size() <= 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return KotlinTestUtils.getFilePath(rootFile);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDataPathRoot() {
|
||||
return "$PROJECT_ROOT";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return testClassName;
|
||||
}
|
||||
|
||||
private class TestAllFilesPresentMethodModel implements TestMethodModel {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return "testAllFilesPresentIn" + testClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateBody(@NotNull Printer p) {
|
||||
String assertTestsPresentStr = String.format(
|
||||
"KotlinTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s.%s);",
|
||||
KotlinTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()),
|
||||
TargetBackend.class.getSimpleName(), targetBackend.toString()
|
||||
);
|
||||
p.println(assertTestsPresentStr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateSignature(@NotNull Printer p) {
|
||||
TestMethodModel.DefaultImpls.generateSignature(this, p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldBeGenerated() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+92
@@ -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"
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 kotlin.io.FilesKt;
|
||||
import kotlin.text.Charsets;
|
||||
import kotlin.text.StringsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.generators.util.GeneratorsFileUtil;
|
||||
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.test.TargetBackend;
|
||||
import org.jetbrains.kotlin.test.TestMetadata;
|
||||
import org.jetbrains.kotlin.utils.Printer;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static kotlin.collections.CollectionsKt.single;
|
||||
|
||||
public class TestGenerator {
|
||||
private static final Set<String> GENERATED_FILES = new HashSet<>();
|
||||
private static final Class RUNNER = JUnit3RunnerWithInners.class;
|
||||
|
||||
private final String baseTestClassPackage;
|
||||
private final String suiteClassPackage;
|
||||
private final String suiteClassName;
|
||||
private final String baseTestClassName;
|
||||
private final Collection<TestClassModel> testClassModels;
|
||||
private final String testSourceFilePath;
|
||||
|
||||
public TestGenerator(
|
||||
@NotNull String baseDir,
|
||||
@NotNull String suiteTestClassFqName,
|
||||
@NotNull String baseTestClassFqName,
|
||||
@NotNull Collection<? extends TestClassModel> testClassModels
|
||||
) {
|
||||
this.baseTestClassPackage = StringsKt.substringBeforeLast(baseTestClassFqName, '.', "");
|
||||
this.baseTestClassName = StringsKt.substringAfterLast(baseTestClassFqName, '.', baseTestClassFqName);
|
||||
this.suiteClassPackage = StringsKt.substringBeforeLast(suiteTestClassFqName, '.', baseTestClassPackage);
|
||||
this.suiteClassName = StringsKt.substringAfterLast(suiteTestClassFqName, '.', suiteTestClassFqName);
|
||||
this.testClassModels = new ArrayList<>(testClassModels);
|
||||
|
||||
this.testSourceFilePath = baseDir + "/" + this.suiteClassPackage.replace(".", "/") + "/" + this.suiteClassName + ".java";
|
||||
|
||||
if (!GENERATED_FILES.add(testSourceFilePath)) {
|
||||
throw new IllegalArgumentException("Same test file already generated in current session: " + testSourceFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
public void generateAndSave() throws IOException {
|
||||
StringBuilder out = new StringBuilder();
|
||||
Printer p = new Printer(out);
|
||||
|
||||
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;");
|
||||
p.println("import ", RUNNER.getCanonicalName(), ";");
|
||||
p.println("import " + KotlinTestUtils.class.getCanonicalName() + ";");
|
||||
p.println("import " + TargetBackend.class.getCanonicalName() + ";");
|
||||
if (!suiteClassPackage.equals(baseTestClassPackage)) {
|
||||
p.println("import " + baseTestClassPackage + "." + baseTestClassName + ";");
|
||||
}
|
||||
p.println("import " + TestMetadata.class.getCanonicalName() + ";");
|
||||
p.println("import " + RunWith.class.getCanonicalName() + ";");
|
||||
p.println();
|
||||
p.println("import java.io.File;");
|
||||
p.println("import java.util.regex.Pattern;");
|
||||
p.println();
|
||||
p.println("/** This class is generated by {@link ", KotlinTestUtils.TEST_GENERATOR_NAME, "}. DO NOT MODIFY MANUALLY */");
|
||||
|
||||
generateSuppressAllWarnings(p);
|
||||
|
||||
TestClassModel model;
|
||||
if (testClassModels.size() == 1) {
|
||||
model = new DelegatingTestClassModel(single(testClassModels)) {
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return suiteClassName;
|
||||
}
|
||||
};
|
||||
}
|
||||
else {
|
||||
model = new TestClassModel() {
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<TestClassModel> getInnerTestClasses() {
|
||||
return testClassModels;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<MethodModel> getMethods() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getName() {
|
||||
return suiteClassName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDataString() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String getDataPathRoot() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
generateTestClass(p, model, false);
|
||||
|
||||
File testSourceFile = new File(testSourceFilePath);
|
||||
GeneratorsFileUtil.writeFileIfContentChanged(testSourceFile, out.toString(), false);
|
||||
}
|
||||
|
||||
private void generateTestClass(Printer p, TestClassModel testClassModel, boolean isStatic) {
|
||||
String staticModifier = isStatic ? "static " : "";
|
||||
|
||||
generateMetadata(p, testClassModel);
|
||||
generateTestDataPath(p, testClassModel);
|
||||
p.println("@RunWith(", RUNNER.getSimpleName(), ".class)");
|
||||
|
||||
p.println("public " + staticModifier + "class ", testClassModel.getName(), " extends ", baseTestClassName, " {");
|
||||
p.pushIndent();
|
||||
|
||||
Collection<MethodModel> testMethods = testClassModel.getMethods();
|
||||
Collection<TestClassModel> innerTestClasses = testClassModel.getInnerTestClasses();
|
||||
|
||||
boolean first = true;
|
||||
|
||||
for (MethodModel methodModel : testMethods) {
|
||||
if (!methodModel.shouldBeGenerated()) continue;
|
||||
|
||||
if (first) {
|
||||
first = false;
|
||||
}
|
||||
else {
|
||||
p.println();
|
||||
}
|
||||
|
||||
generateTestMethod(p, methodModel);
|
||||
}
|
||||
|
||||
for (TestClassModel innerTestClass : innerTestClasses) {
|
||||
if (!innerTestClass.isEmpty()) {
|
||||
if (first) {
|
||||
first = false;
|
||||
}
|
||||
else {
|
||||
p.println();
|
||||
}
|
||||
|
||||
generateTestClass(p, innerTestClass, true);
|
||||
}
|
||||
}
|
||||
|
||||
p.popIndent();
|
||||
p.println("}");
|
||||
}
|
||||
|
||||
private static void generateTestMethod(Printer p, MethodModel methodModel) {
|
||||
generateMetadata(p, methodModel);
|
||||
|
||||
methodModel.generateSignature(p);
|
||||
p.printWithNoIndent(" {");
|
||||
p.println();
|
||||
|
||||
p.pushIndent();
|
||||
|
||||
methodModel.generateBody(p);
|
||||
|
||||
p.popIndent();
|
||||
p.println("}");
|
||||
}
|
||||
|
||||
private static void generateMetadata(Printer p, TestEntityModel testDataSource) {
|
||||
String dataString = testDataSource.getDataString();
|
||||
if (dataString != null) {
|
||||
p.println("@TestMetadata(\"", dataString, "\")");
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateTestDataPath(Printer p, TestClassModel testClassModel) {
|
||||
String dataPathRoot = testClassModel.getDataPathRoot();
|
||||
if (dataPathRoot != null) {
|
||||
p.println("@TestDataPath(\"", dataPathRoot, "\")");
|
||||
}
|
||||
}
|
||||
|
||||
private static void generateSuppressAllWarnings(Printer p) {
|
||||
p.println("@SuppressWarnings(\"all\")");
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 kotlin.text.StringsKt;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class TestGeneratorUtil {
|
||||
@NotNull
|
||||
public static String escapeForJavaIdentifier(String fileName) {
|
||||
// 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);
|
||||
|
||||
if (Character.isJavaIdentifierPart(c)) {
|
||||
result.append(c);
|
||||
}
|
||||
else {
|
||||
result.append("_");
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String fileNameToJavaIdentifier(@NotNull File file) {
|
||||
return StringsKt.capitalize(escapeForJavaIdentifier(file.getName()));
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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 org.jetbrains.kotlin.utils.Printer
|
||||
|
||||
interface TestEntityModel {
|
||||
val name: String
|
||||
val dataString: String?
|
||||
}
|
||||
|
||||
interface TestClassModel : TestEntityModel {
|
||||
val innerTestClasses: Collection<TestClassModel>
|
||||
val methods: Collection<MethodModel>
|
||||
val isEmpty: Boolean
|
||||
val dataPathRoot: String?
|
||||
}
|
||||
|
||||
interface MethodModel : TestEntityModel {
|
||||
fun shouldBeGenerated(): Boolean = true
|
||||
fun generateSignature(p: Printer)
|
||||
fun generateBody(p: Printer)
|
||||
}
|
||||
|
||||
interface TestMethodModel : MethodModel {
|
||||
override fun generateSignature(p: Printer) {
|
||||
p.print("public void $name() throws Exception")
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2010-2015 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.util;
|
||||
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
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;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
|
||||
|
||||
public class GeneratorsFileUtil {
|
||||
@SuppressWarnings("UseOfSystemOutOrSystemErr")
|
||||
public static void writeFileIfContentChanged(File file, String newText) throws IOException {
|
||||
writeFileIfContentChanged(file, newText, true);
|
||||
}
|
||||
|
||||
@SuppressWarnings("UseOfSystemOutOrSystemErr")
|
||||
public static void writeFileIfContentChanged(File file, String newText, boolean logNotChanged) throws IOException {
|
||||
File parentFile = file.getParentFile();
|
||||
if (!parentFile.exists()) {
|
||||
if (parentFile.mkdirs()) {
|
||||
System.out.println("Directory created: " + parentFile.getAbsolutePath());
|
||||
}
|
||||
else {
|
||||
throw new IllegalStateException("Cannot create directory: " + parentFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (checkFileIgnoringLineSeparators(file, newText)) {
|
||||
if (logNotChanged) {
|
||||
System.out.println("Not changed: " + file.getAbsolutePath());
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
boolean useTempFile = !SystemInfo.isWindows;
|
||||
|
||||
File tempFile = useTempFile ? new File(KotlinTestUtils.tmpDir(file.getName()), file.getName() + ".tmp") : file;
|
||||
|
||||
FilesKt.writeText(tempFile, newText, Charsets.UTF_8);
|
||||
System.out.println("File written: " + tempFile.getAbsolutePath());
|
||||
if (useTempFile) {
|
||||
Files.move(tempFile.toPath(), file.toPath(), REPLACE_EXISTING);
|
||||
System.out.println("Renamed " + tempFile + " to " + file);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
private static boolean checkFileIgnoringLineSeparators(File file, String content) {
|
||||
String currentContent;
|
||||
try {
|
||||
currentContent = FilesKt.readText(file, Charsets.UTF_8);
|
||||
}
|
||||
catch (Throwable ignored) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return StringUtil.convertLineSeparators(content).equals(currentContent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user