Auto generate test for rename
This commit is contained in:
committed by
Nikolay Krasko
parent
6935ad4437
commit
87fac21310
@@ -32,6 +32,9 @@ import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.impl.PsiFileFactoryImpl;
|
||||
import com.intellij.rt.execution.junit.FileComparisonFailure;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.Processor;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import junit.framework.TestCase;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -548,14 +551,8 @@ public class JetTestUtils {
|
||||
String rootPath = testClassMetadata.value();
|
||||
File rootFile = new File(rootPath);
|
||||
|
||||
Set<String> filePaths = collectPathsMetadata(testCaseClass);
|
||||
|
||||
Set<String> filePaths = Sets.newHashSet();
|
||||
for (Method method : testCaseClass.getDeclaredMethods()) {
|
||||
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
|
||||
if (testMetadata != null) {
|
||||
filePaths.add(testMetadata.value());
|
||||
}
|
||||
}
|
||||
File[] files = testDataDir.listFiles();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
@@ -564,20 +561,70 @@ public class JetTestUtils {
|
||||
assertTestClassPresentByMetadata(testCaseClass, generatorClassFqName, file);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (filenamePattern.matcher(file.getName()).matches()) {
|
||||
String relativePath = FileUtil.getRelativePath(rootFile, file);
|
||||
if (!filePaths.contains(relativePath)) {
|
||||
Assert.fail("Test data file missing from the generated test class: " +
|
||||
file +
|
||||
pleaseReRunGenerator(generatorClassFqName));
|
||||
}
|
||||
}
|
||||
else if (filenamePattern.matcher(file.getName()).matches()) {
|
||||
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void assertAllTestsPresentInSingleGeneratedClass(
|
||||
@NotNull Class<?> testCaseClass,
|
||||
@NotNull final String generatorClassFqName,
|
||||
@NotNull File testDataDir,
|
||||
@NotNull final Pattern filenamePattern) {
|
||||
TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
|
||||
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
|
||||
String rootPath = testClassMetadata.value();
|
||||
final File rootFile = new File(rootPath);
|
||||
|
||||
final Set<String> filePaths = collectPathsMetadata(testCaseClass);
|
||||
|
||||
FileUtil.processFilesRecursively(testDataDir, new Processor<File>() {
|
||||
@Override
|
||||
public boolean process(File file) {
|
||||
if (file.isFile() && filenamePattern.matcher(file.getName()).matches()) {
|
||||
assertFilePathPresent(file, rootFile, filePaths, generatorClassFqName);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void assertFilePathPresent(File file, File rootFile, Set<String> filePaths, String generatorClassFqName) {
|
||||
String path = FileUtil.getRelativePath(rootFile, file);
|
||||
if (path != null) {
|
||||
String relativePath = FileUtil.nameToCompare(path);
|
||||
if (!filePaths.contains(relativePath)) {
|
||||
Assert.fail("Test data file missing from the generated test class: " +
|
||||
file +
|
||||
pleaseReRunGenerator(generatorClassFqName));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Set<String> collectPathsMetadata(Class<?> testCaseClass) {
|
||||
return ContainerUtil.newHashSet(
|
||||
ContainerUtil.map(collectMethodsMetadata(testCaseClass), new Function<String, String>() {
|
||||
@Override
|
||||
public String fun(String pathData) {
|
||||
return FileUtil.nameToCompare(pathData);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private static Set<String> collectMethodsMetadata(Class<?> testCaseClass) {
|
||||
Set<String> filePaths = Sets.newHashSet();
|
||||
for (Method method : testCaseClass.getDeclaredMethods()) {
|
||||
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
|
||||
if (testMetadata != null) {
|
||||
filePaths.add(testMetadata.value());
|
||||
}
|
||||
}
|
||||
return filePaths;
|
||||
}
|
||||
|
||||
private static boolean containsTestData(File dir, Pattern filenamePattern) {
|
||||
File[] files = dir.listFiles();
|
||||
assert files != null;
|
||||
|
||||
Reference in New Issue
Block a user