Test generator moved to the 'generators' module

This commit is contained in:
Andrey Breslav
2013-09-23 14:12:39 -07:00
parent 39b5be6e2b
commit ade544a8b7
9 changed files with 15 additions and 12 deletions
@@ -57,9 +57,9 @@ import org.jetbrains.jet.psi.AbstractJetPsiMatcherTest;
import org.jetbrains.jet.resolve.AbstractResolveBaseTest;
import org.jetbrains.jet.resolve.AbstractResolveTest;
import org.jetbrains.jet.safeDelete.AbstractJetSafeDeleteTest;
import org.jetbrains.jet.test.generator.SimpleTestClassModel;
import org.jetbrains.jet.test.generator.TestClassModel;
import org.jetbrains.jet.test.generator.TestGenerator;
import org.jetbrains.jet.generators.tests.generator.SimpleTestClassModel;
import org.jetbrains.jet.generators.tests.generator.TestClassModel;
import org.jetbrains.jet.generators.tests.generator.TestGenerator;
import java.io.File;
import java.io.IOException;
@@ -0,0 +1,56 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public class DelegatingTestClassModel implements TestClassModel {
private final TestClassModel delegate;
public DelegatingTestClassModel(TestClassModel delegate) {
this.delegate = delegate;
}
@Override
public String getName() {
return delegate.getName();
}
@NotNull
@Override
public Collection<TestClassModel> getInnerTestClasses() {
return delegate.getInnerTestClasses();
}
@NotNull
@Override
public Collection<TestMethodModel> getTestMethods() {
return delegate.getTestMethods();
}
@Override
public boolean isEmpty() {
return delegate.isEmpty();
}
@Override
public String getDataString() {
return delegate.getDataString();
}
}
@@ -0,0 +1,141 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Pattern;
public class SimpleTestClassModel implements TestClassModel {
private static final Comparator<TestEntityModel> BY_NAME = new Comparator<TestEntityModel>() {
@Override
public int compare(TestEntityModel o1, TestEntityModel o2) {
return o1.getName().compareTo(o2.getName());
}
};
private final File rootFile;
private final boolean recursive;
private final Pattern filenamePattern;
private final String doTestMethodName;
private final String testClassName;
private Collection<TestClassModel> innerTestClasses;
private Collection<TestMethodModel> testMethods;
public SimpleTestClassModel(@NotNull File rootFile, boolean recursive, @NotNull Pattern filenamePattern, @NotNull String doTestMethodName) {
this.rootFile = rootFile;
this.recursive = recursive;
this.filenamePattern = filenamePattern;
this.doTestMethodName = doTestMethodName;
this.testClassName = StringUtil.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(rootFile.getName()));
}
@NotNull
@Override
public Collection<TestClassModel> getInnerTestClasses() {
if (!rootFile.isDirectory() || !recursive) {
return Collections.emptyList();
}
if (innerTestClasses == null) {
List<TestClassModel> children = Lists.newArrayList();
File[] files = rootFile.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
children.add(new SimpleTestClassModel(file, recursive, filenamePattern, doTestMethodName));
}
}
}
Collections.sort(children, BY_NAME);
innerTestClasses = children;
}
return innerTestClasses;
}
@NotNull
@Override
public Collection<TestMethodModel> getTestMethods() {
if (testMethods == null) {
if (!rootFile.isDirectory()) {
testMethods = Collections.<TestMethodModel>singletonList(new SimpleTestMethodModel(rootFile, rootFile, doTestMethodName, filenamePattern));
}
else {
List<TestMethodModel> result = Lists.newArrayList();
result.add(new TestAllFilesPresentMethodModel());
File[] listFiles = rootFile.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (filenamePattern.matcher(file.getName()).matches()) {
result.add(new SimpleTestMethodModel(rootFile, file, doTestMethodName, filenamePattern));
}
}
}
Collections.sort(result, BY_NAME);
testMethods = result;
}
}
return testMethods;
}
@Override
public boolean isEmpty() {
return getTestMethods().size() == 1 && getInnerTestClasses().isEmpty();
}
@Override
public String getDataString() {
return JetTestUtils.getFilePath(rootFile);
}
@Override
public String getName() {
return testClassName;
}
private class TestAllFilesPresentMethodModel implements TestMethodModel {
@Override
public String getName() {
return "testAllFilesPresentIn" + testClassName;
}
@Override
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
String assertTestsPresentStr =
String.format("JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), \"%s\", new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
generatorClassFqName, JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), recursive);
p.println(assertTestsPresentStr);
}
@Override
public String getDataString() {
return null;
}
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2013 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.jet.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.jet.JetTestUtils;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SimpleTestMethodModel implements TestMethodModel {
private final File rootDir;
private final File file;
private final String doTestMethodName;
private final Pattern filenamePattern;
public SimpleTestMethodModel(File rootDir, File file, String doTestMethodName, Pattern filenamePattern) {
this.rootDir = rootDir;
this.file = file;
this.doTestMethodName = doTestMethodName;
this.filenamePattern = filenamePattern;
}
@Override
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
p.println(doTestMethodName, "(\"", JetTestUtils.getFilePath(file), "\");");
}
@Override
public String getDataString() {
return JetTestUtils.getFilePath(new File(FileUtil.getRelativePath(rootDir, file)));
}
@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);
String unescapedName;
if (rootDir.equals(file.getParentFile())) {
unescapedName = extractedName;
}
else {
String relativePath = FileUtil.getRelativePath(rootDir, file.getParentFile());
unescapedName = relativePath + "-" + StringUtil.capitalize(extractedName);
}
return "test" + StringUtil.capitalize(TestGeneratorUtil.escapeForJavaIdentifier(unescapedName));
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
public interface TestClassModel extends TestEntityModel {
@NotNull
Collection<TestClassModel> getInnerTestClasses();
@NotNull
Collection<TestMethodModel> getTestMethods();
boolean isEmpty();
}
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import org.jetbrains.annotations.Nullable;
public interface TestEntityModel {
String getName();
@Nullable
String getDataString();
}
@@ -0,0 +1,236 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.Printer;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class TestGenerator {
private static final List<String> JUNIT3_IMPORTS = Arrays.asList(
"junit.framework.Assert",
"junit.framework.Test",
"junit.framework.TestSuite"
);
private final String baseDir;
private final String suiteClassPackage;
private final String suiteClassName;
private final String baseTestClassPackage;
private final String baseTestClassName;
private final Collection<TestClassModel> testClassModels;
private final String generatorName;
public TestGenerator(
@NotNull String baseDir,
@NotNull String suiteClassPackage,
@NotNull String suiteClassName,
@NotNull Class<? extends TestCase> baseTestClass,
@NotNull Collection<? extends TestClassModel> testClassModels,
@NotNull Class<?> generatorClass
) {
this.baseDir = baseDir;
this.suiteClassPackage = suiteClassPackage;
this.suiteClassName = suiteClassName;
this.baseTestClassPackage = baseTestClass.getPackage().getName();
this.baseTestClassName = baseTestClass.getSimpleName();
this.testClassModels = Lists.newArrayList(testClassModels);
this.generatorName = generatorClass.getCanonicalName();
}
public void generateAndSave() throws IOException {
StringBuilder out = new StringBuilder();
Printer p = new Printer(out);
p.println(FileUtil.loadFile(new File("injector-generator/copyright.txt")));
p.println("package ", suiteClassPackage, ";");
p.println();
for (String importedClassName : JUNIT3_IMPORTS) {
p.println("import ", importedClassName, ";");
}
p.println();
p.println("import java.io.File;");
p.println("import java.util.regex.Pattern;");
p.println("import org.jetbrains.jet.JetTestUtils;");
p.println("import org.jetbrains.jet.test.InnerTestClasses;");
p.println("import org.jetbrains.jet.test.TestMetadata;");
p.println();
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
p.println();
p.println("/** This class is generated by {@link ", generatorName, "}. DO NOT MODIFY MANUALLY */");
generateSuppressAllWarnings(p);
if (testClassModels.size() == 1) {
TestClassModel theOnlyTestClass = testClassModels.iterator().next();
generateTestClass(p, new DelegatingTestClassModel(theOnlyTestClass) {
@Override
public String getName() {
return suiteClassName;
}
}, false);
}
else {
generateTestClass(p, new TestClassModel() {
@NotNull
@Override
public Collection<TestClassModel> getInnerTestClasses() {
return testClassModels;
}
@NotNull
@Override
public Collection<TestMethodModel> getTestMethods() {
return Collections.emptyList();
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public String getName() {
return suiteClassName;
}
@Override
public String getDataString() {
return null;
}
}, false);
}
String testSourceFilePath = baseDir + "/" + suiteClassPackage.replace(".", "/") + "/" + suiteClassName + ".java";
File testSourceFile = new File(testSourceFilePath);
FileUtil.writeToFile(testSourceFile, out.toString());
}
private void generateTestClass(Printer p, TestClassModel testClassModel, boolean isStatic) {
String staticModifier = isStatic ? "static " : "";
generateMetadata(p, testClassModel);
generateInnerClassesAnnotation(p, testClassModel);
p.println("public " + staticModifier + "class ", testClassModel.getName(), " extends ", baseTestClassName, " {");
p.pushIndent();
Collection<TestMethodModel> testMethods = testClassModel.getTestMethods();
for (TestMethodModel testMethodModel : testMethods) {
generateTestMethod(p, testMethodModel);
p.println();
}
Collection<TestClassModel> innerTestClasses = testClassModel.getInnerTestClasses();
for (TestClassModel innerTestClass : innerTestClasses) {
if (innerTestClass.isEmpty()) {
continue;
}
generateTestClass(p, innerTestClass, true);
p.println();
}
if (!innerTestClasses.isEmpty()) {
generateSuiteMethod(p, testClassModel, isStatic);
}
p.popIndent();
p.println("}");
}
private static void generateSuiteMethod(Printer p, TestClassModel testClassModel, boolean innerClass) {
String name = innerClass ? "innerSuite" : "suite";
p.println("public static Test " + name + "() {");
p.pushIndent();
p.println("TestSuite suite = new TestSuite(\"", testClassModel.getName(), "\");");
if (!testClassModel.getTestMethods().isEmpty()) {
p.println("suite.addTestSuite(", testClassModel.getName(), ".class);");
}
for (TestClassModel innerTestClass : testClassModel.getInnerTestClasses()) {
if (innerTestClass.isEmpty()) {
continue;
}
if (innerTestClass.getInnerTestClasses().isEmpty()) {
p.println("suite.addTestSuite(", innerTestClass.getName(), ".class);");
}
else {
p.println("suite.addTest(", innerTestClass.getName(), ".innerSuite());");
}
}
p.println("return suite;");
p.popIndent();
p.println("}");
}
private void generateTestMethod(Printer p, TestMethodModel testMethodModel) {
generateMetadata(p, testMethodModel);
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
p.pushIndent();
testMethodModel.generateBody(p, generatorName);
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 generateInnerClassesAnnotation(Printer p, TestClassModel testClassModel) {
Collection<TestClassModel> innerTestClasses = testClassModel.getInnerTestClasses();
if (innerTestClasses.isEmpty()) return;
p.print("@InnerTestClasses({");
boolean isFirst = true;
for (TestClassModel innerTestClass : innerTestClasses) {
if (!innerTestClass.isEmpty()) {
if (!isFirst) {
p.printWithNoIndent(", ");
}
else {
isFirst = false;
}
p.printWithNoIndent(testClassModel.getName(), ".", innerTestClass.getName(), ".class");
}
}
p.printlnWithNoIndent("})");
}
private static void generateSuppressAllWarnings(Printer p) {
p.println("@SuppressWarnings(\"all\")");
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
public class TestGeneratorUtil {
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();
}
}
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2013 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.jet.generators.tests.generator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.utils.Printer;
public interface TestMethodModel extends TestEntityModel {
void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName);
}