Test generator: inline the constant FQ name
This commit is contained in:
@@ -93,6 +93,8 @@ import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.compileKotlinToD
|
|||||||
import static org.jetbrains.jet.lang.psi.PsiPackage.JetPsiFactory;
|
import static org.jetbrains.jet.lang.psi.PsiPackage.JetPsiFactory;
|
||||||
|
|
||||||
public class JetTestUtils {
|
public class JetTestUtils {
|
||||||
|
public static final String TEST_GENERATOR_NAME = "org.jetbrains.jet.generators.tests.TestsPackage";
|
||||||
|
|
||||||
private static final Pattern KT_FILES = Pattern.compile(".*?.kt");
|
private static final Pattern KT_FILES = Pattern.compile(".*?.kt");
|
||||||
private static final List<File> filesToDelete = new ArrayList<File>();
|
private static final List<File> filesToDelete = new ArrayList<File>();
|
||||||
|
|
||||||
@@ -676,7 +678,6 @@ public class JetTestUtils {
|
|||||||
|
|
||||||
public static void assertAllTestsPresentByMetadata(
|
public static void assertAllTestsPresentByMetadata(
|
||||||
@NotNull Class<?> testCaseClass,
|
@NotNull Class<?> testCaseClass,
|
||||||
@NotNull String generatorClassFqName,
|
|
||||||
@NotNull File testDataDir,
|
@NotNull File testDataDir,
|
||||||
@NotNull Pattern filenamePattern,
|
@NotNull Pattern filenamePattern,
|
||||||
boolean recursive
|
boolean recursive
|
||||||
@@ -693,11 +694,11 @@ public class JetTestUtils {
|
|||||||
for (File file : files) {
|
for (File file : files) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
if (recursive && containsTestData(file, filenamePattern)) {
|
if (recursive && containsTestData(file, filenamePattern)) {
|
||||||
assertTestClassPresentByMetadata(testCaseClass, generatorClassFqName, file);
|
assertTestClassPresentByMetadata(testCaseClass, file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (filenamePattern.matcher(file.getName()).matches()) {
|
else if (filenamePattern.matcher(file.getName()).matches()) {
|
||||||
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
|
assertFilePathPresent(file, rootFile, filePaths);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -705,13 +706,12 @@ public class JetTestUtils {
|
|||||||
|
|
||||||
public static void assertAllTestsPresentInSingleGeneratedClass(
|
public static void assertAllTestsPresentInSingleGeneratedClass(
|
||||||
@NotNull Class<?> testCaseClass,
|
@NotNull Class<?> testCaseClass,
|
||||||
@NotNull final String generatorClassFqName,
|
|
||||||
@NotNull File testDataDir,
|
@NotNull File testDataDir,
|
||||||
@NotNull final Pattern filenamePattern) {
|
@NotNull final Pattern filenamePattern
|
||||||
|
) {
|
||||||
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
|
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
|
||||||
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
|
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
|
||||||
String rootPath = testClassMetadata.value();
|
final File rootFile = new File(testClassMetadata.value());
|
||||||
final File rootFile = new File(rootPath);
|
|
||||||
|
|
||||||
final Set<String> filePaths = collectPathsMetadata(testCaseClass);
|
final Set<String> filePaths = collectPathsMetadata(testCaseClass);
|
||||||
|
|
||||||
@@ -719,7 +719,7 @@ public class JetTestUtils {
|
|||||||
@Override
|
@Override
|
||||||
public boolean process(File file) {
|
public boolean process(File file) {
|
||||||
if (file.isFile() && filenamePattern.matcher(file.getName()).matches()) {
|
if (file.isFile() && filenamePattern.matcher(file.getName()).matches()) {
|
||||||
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
|
assertFilePathPresent(file, rootFile, filePaths);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -727,14 +727,14 @@ public class JetTestUtils {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertFilePathPresent(File file, File rootFile, Set<String> filePaths, String generatorClassFqName) {
|
private static void assertFilePathPresent(File file, File rootFile, Set<String> filePaths) {
|
||||||
String path = FileUtil.getRelativePath(rootFile, file);
|
String path = FileUtil.getRelativePath(rootFile, file);
|
||||||
if (path != null) {
|
if (path != null) {
|
||||||
String relativePath = FileUtil.nameToCompare(path);
|
String relativePath = FileUtil.nameToCompare(path);
|
||||||
if (!filePaths.contains(relativePath)) {
|
if (!filePaths.contains(relativePath)) {
|
||||||
Assert.fail("Test data file missing from the generated test class: " +
|
Assert.fail("Test data file missing from the generated test class: " +
|
||||||
file +
|
file +
|
||||||
pleaseReRunGenerator(generatorClassFqName));
|
pleaseReRunGenerator());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -780,7 +780,6 @@ public class JetTestUtils {
|
|||||||
|
|
||||||
private static void assertTestClassPresentByMetadata(
|
private static void assertTestClassPresentByMetadata(
|
||||||
@NotNull Class<?> outerClass,
|
@NotNull Class<?> outerClass,
|
||||||
@NotNull String generatorClassFqName,
|
|
||||||
@NotNull File testDataDir
|
@NotNull File testDataDir
|
||||||
) {
|
) {
|
||||||
InnerTestClasses innerClassesAnnotation = outerClass.getAnnotation(InnerTestClasses.class);
|
InnerTestClasses innerClassesAnnotation = outerClass.getAnnotation(InnerTestClasses.class);
|
||||||
@@ -793,20 +792,19 @@ public class JetTestUtils {
|
|||||||
}
|
}
|
||||||
Assert.fail("Test data directory missing from the generated test class: " +
|
Assert.fail("Test data directory missing from the generated test class: " +
|
||||||
testDataDir +
|
testDataDir +
|
||||||
pleaseReRunGenerator(generatorClassFqName));
|
pleaseReRunGenerator());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String pleaseReRunGenerator(String generatorClassFqName) {
|
private static String pleaseReRunGenerator() {
|
||||||
return "\nPlease re-run the generator: " + generatorClassFqName +
|
return "\nPlease re-run the generator: " + TEST_GENERATOR_NAME + getLocationFormattedForConsole();
|
||||||
getLocationFormattedForConsole(generatorClassFqName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getLocationFormattedForConsole(String generatorClassFqName) {
|
private static String getLocationFormattedForConsole() {
|
||||||
return "(" + getSimpleName(generatorClassFqName) + ".java:1)";
|
return "(" + getSimpleName() + ".java:1)";
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getSimpleName(String generatorClassFqName) {
|
private static String getSimpleName() {
|
||||||
return generatorClassFqName.substring(generatorClassFqName.lastIndexOf(".") + 1);
|
return TEST_GENERATOR_NAME.substring(TEST_GENERATOR_NAME.lastIndexOf(".") + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
@@ -614,8 +614,8 @@ private class TestGroup(val testsRoot: String, val testDataRoot: String) {
|
|||||||
fun testClass(
|
fun testClass(
|
||||||
baseTestClass: Class<out TestCase>,
|
baseTestClass: Class<out TestCase>,
|
||||||
suiteTestClass: String = getDefaultSuiteTestClass(baseTestClass),
|
suiteTestClass: String = getDefaultSuiteTestClass(baseTestClass),
|
||||||
init: TestClass.() -> Unit) {
|
init: TestClass.() -> Unit
|
||||||
|
) {
|
||||||
val testClass = TestClass()
|
val testClass = TestClass()
|
||||||
testClass.init()
|
testClass.init()
|
||||||
|
|
||||||
@@ -624,13 +624,11 @@ private class TestGroup(val testsRoot: String, val testDataRoot: String) {
|
|||||||
baseTestClass.getPackage()!!.getName()!!,
|
baseTestClass.getPackage()!!.getName()!!,
|
||||||
suiteTestClass,
|
suiteTestClass,
|
||||||
baseTestClass,
|
baseTestClass,
|
||||||
testClass.testModels,
|
testClass.testModels
|
||||||
"org.jetbrains.jet.generators.tests.TestsPackage"
|
|
||||||
).generateAndSave()
|
).generateAndSave()
|
||||||
}
|
}
|
||||||
|
|
||||||
inner class TestClass() {
|
inner class TestClass {
|
||||||
|
|
||||||
val testModels = ArrayList<TestClassModel>()
|
val testModels = ArrayList<TestClassModel>()
|
||||||
|
|
||||||
fun model(
|
fun model(
|
||||||
|
|||||||
+3
-3
@@ -168,10 +168,10 @@ public class SimpleTestClassModel implements TestClassModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
|
public void generateBody(@NotNull Printer p) {
|
||||||
String assertTestsPresentStr =
|
String assertTestsPresentStr =
|
||||||
String.format("JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), \"%s\", new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
|
String.format("JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"), %s);",
|
||||||
generatorClassFqName, JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), recursive);
|
JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()), recursive);
|
||||||
p.println(assertTestsPresentStr);
|
p.println(assertTestsPresentStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ public class SimpleTestMethodModel implements TestMethodModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
|
public void generateBody(@NotNull Printer p) {
|
||||||
String filePath = JetTestUtils.getFilePath(file) + (file.isDirectory() ? "/" : "");
|
String filePath = JetTestUtils.getFilePath(file) + (file.isDirectory() ? "/" : "");
|
||||||
p.println(doTestMethodName, "(\"", filePath, "\");");
|
p.println(doTestMethodName, "(\"", filePath, "\");");
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -117,10 +117,11 @@ public class SingleClassTestModel implements TestClassModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
|
public void generateBody(@NotNull Printer p) {
|
||||||
String assertTestsPresentStr = String.format(
|
String assertTestsPresentStr = String.format(
|
||||||
"JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), \"%s\", new File(\"%s\"), Pattern.compile(\"%s\"));",
|
"JetTestUtils.assertAllTestsPresentInSingleGeneratedClass(this.getClass(), new File(\"%s\"), Pattern.compile(\"%s\"));",
|
||||||
generatorClassFqName, JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern()));
|
JetTestUtils.getFilePath(rootFile), StringUtil.escapeStringCharacters(filenamePattern.pattern())
|
||||||
|
);
|
||||||
p.println(assertTestsPresentStr);
|
p.println(assertTestsPresentStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.intellij.openapi.util.io.FileUtil;
|
|||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.jet.JetTestUtils;
|
||||||
import org.jetbrains.jet.di.GeneratorsFileUtil;
|
import org.jetbrains.jet.di.GeneratorsFileUtil;
|
||||||
import org.jetbrains.jet.utils.Printer;
|
import org.jetbrains.jet.utils.Printer;
|
||||||
|
|
||||||
@@ -38,13 +39,10 @@ public class TestGenerator {
|
|||||||
|
|
||||||
private static final Set<String> GENERATED_FILES = ContainerUtil.newHashSet();
|
private static final Set<String> GENERATED_FILES = ContainerUtil.newHashSet();
|
||||||
|
|
||||||
private final String baseDir;
|
|
||||||
private final String suiteClassPackage;
|
private final String suiteClassPackage;
|
||||||
private final String suiteClassName;
|
private final String suiteClassName;
|
||||||
private final String baseTestClassPackage;
|
|
||||||
private final String baseTestClassName;
|
private final String baseTestClassName;
|
||||||
private final Collection<TestClassModel> testClassModels;
|
private final Collection<TestClassModel> testClassModels;
|
||||||
private final String generatorName;
|
|
||||||
private final String testSourceFilePath;
|
private final String testSourceFilePath;
|
||||||
|
|
||||||
public TestGenerator(
|
public TestGenerator(
|
||||||
@@ -52,18 +50,14 @@ public class TestGenerator {
|
|||||||
@NotNull String suiteClassPackage,
|
@NotNull String suiteClassPackage,
|
||||||
@NotNull String suiteClassName,
|
@NotNull String suiteClassName,
|
||||||
@NotNull Class<? extends TestCase> baseTestClass,
|
@NotNull Class<? extends TestCase> baseTestClass,
|
||||||
@NotNull Collection<? extends TestClassModel> testClassModels,
|
@NotNull Collection<? extends TestClassModel> testClassModels
|
||||||
@NotNull String generatorClass
|
|
||||||
) {
|
) {
|
||||||
this.baseDir = baseDir;
|
|
||||||
this.suiteClassPackage = suiteClassPackage;
|
this.suiteClassPackage = suiteClassPackage;
|
||||||
this.suiteClassName = suiteClassName;
|
this.suiteClassName = suiteClassName;
|
||||||
this.baseTestClassPackage = baseTestClass.getPackage().getName();
|
|
||||||
this.baseTestClassName = baseTestClass.getSimpleName();
|
this.baseTestClassName = baseTestClass.getSimpleName();
|
||||||
this.testClassModels = Lists.newArrayList(testClassModels);
|
this.testClassModels = Lists.newArrayList(testClassModels);
|
||||||
this.generatorName = generatorClass;
|
|
||||||
|
|
||||||
this.testSourceFilePath = this.baseDir + "/" + this.suiteClassPackage.replace(".", "/") + "/" + this.suiteClassName + ".java";
|
this.testSourceFilePath = baseDir + "/" + this.suiteClassPackage.replace(".", "/") + "/" + this.suiteClassName + ".java";
|
||||||
|
|
||||||
if (!GENERATED_FILES.add(testSourceFilePath)) {
|
if (!GENERATED_FILES.add(testSourceFilePath)) {
|
||||||
throw new IllegalArgumentException("Same test file already generated in current session: " + testSourceFilePath);
|
throw new IllegalArgumentException("Same test file already generated in current session: " + testSourceFilePath);
|
||||||
@@ -91,8 +85,7 @@ public class TestGenerator {
|
|||||||
|
|
||||||
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
|
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
|
||||||
p.println();
|
p.println();
|
||||||
|
p.println("/** This class is generated by {@link ", JetTestUtils.TEST_GENERATOR_NAME, "}. DO NOT MODIFY MANUALLY */");
|
||||||
p.println("/** This class is generated by {@link ", generatorName, "}. DO NOT MODIFY MANUALLY */");
|
|
||||||
|
|
||||||
generateSuppressAllWarnings(p);
|
generateSuppressAllWarnings(p);
|
||||||
if (testClassModels.size() == 1) {
|
if (testClassModels.size() == 1) {
|
||||||
@@ -198,12 +191,12 @@ public class TestGenerator {
|
|||||||
p.println("}");
|
p.println("}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateTestMethod(Printer p, TestMethodModel testMethodModel) {
|
private static void generateTestMethod(Printer p, TestMethodModel testMethodModel) {
|
||||||
generateMetadata(p, testMethodModel);
|
generateMetadata(p, testMethodModel);
|
||||||
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
|
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
|
||||||
p.pushIndent();
|
p.pushIndent();
|
||||||
|
|
||||||
testMethodModel.generateBody(p, generatorName);
|
testMethodModel.generateBody(p);
|
||||||
|
|
||||||
p.popIndent();
|
p.popIndent();
|
||||||
p.println("}");
|
p.println("}");
|
||||||
|
|||||||
@@ -20,5 +20,5 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.jet.utils.Printer;
|
import org.jetbrains.jet.utils.Printer;
|
||||||
|
|
||||||
public interface TestMethodModel extends TestEntityModel {
|
public interface TestMethodModel extends TestEntityModel {
|
||||||
void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName);
|
void generateBody(@NotNull Printer p);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user