File names stored in the metadata

This is used for better checking of whether a file is present in the test suite
This commit is contained in:
Andrey Breslav
2012-07-09 21:12:47 +04:00
parent e69715723b
commit f64d78c18f
9 changed files with 120 additions and 34 deletions
@@ -17,6 +17,7 @@
package org.jetbrains.jet; package org.jetbrains.jet;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.Disposable; import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
@@ -44,6 +45,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacadeForJVM;
import org.jetbrains.jet.plugin.JetLanguage; import org.jetbrains.jet.plugin.JetLanguage;
import org.jetbrains.jet.test.TestMetadata;
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
import org.jetbrains.jet.util.slicedmap.SlicedMap; import org.jetbrains.jet.util.slicedmap.SlicedMap;
import org.jetbrains.jet.util.slicedmap.WritableSlice; import org.jetbrains.jet.util.slicedmap.WritableSlice;
@@ -362,34 +364,45 @@ public class JetTestUtils {
} }
} }
public static void allTestsPresent( public static void assertAllTestsPresentByMetadata(
@NotNull Class<?> testCaseClass, @NotNull Class<?> testCaseClass,
@NotNull String generatorClassFqName, @NotNull String generatorClassFqName,
@NotNull File testDataDir, @NotNull File testDataDir,
@NotNull String extension, @NotNull String extension,
boolean recursive boolean recursive
) { ) {
Set<String> methodNames = new HashSet<String>(); TestMetadata testClassMetadata = testCaseClass.getAnnotation(TestMetadata.class);
Assert.assertNotNull("No metadata for class: " + testCaseClass, testClassMetadata);
String rootPath = testClassMetadata.value();
File rootFile = new File(rootPath);
Set<String> filePaths = Sets.newHashSet();
for (Method method : testCaseClass.getDeclaredMethods()) { for (Method method : testCaseClass.getDeclaredMethods()) {
boolean isTestMethod = method.getName().startsWith("test"); TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
if (isTestMethod) { if (testMetadata != null) {
methodNames.add(method.getName().toLowerCase() + "." + extension); filePaths.add(testMetadata.value());
} }
} }
for (File file : testDataDir.listFiles()) { File[] files = testDataDir.listFiles();
if (file.isDirectory()) { if (files != null) {
if (recursive) { for (File file : files) {
allTestsPresent(testCaseClass, generatorClassFqName, file, extension, recursive); if (file.isDirectory()) {
if (recursive) {
assertAllTestsPresentByMetadata(testCaseClass, generatorClassFqName, file, extension, recursive);
}
} }
} else {
else { if (file.getName().endsWith("." + extension)) {
String name = file.getName(); String relativePath = FileUtil.getRelativePath(rootFile, file);
if (name.endsWith("." + extension) && !methodNames.contains("test" + name.toLowerCase())) { if (!filePaths.contains(relativePath)) {
String generatorClassSimpleName = generatorClassFqName.substring(generatorClassFqName.lastIndexOf(".") + 1); String generatorClassSimpleName = generatorClassFqName.substring(generatorClassFqName.lastIndexOf(".") + 1);
junit.framework.Assert.fail("Test data file missing from the generated test class: " + Assert.fail("Test data file missing from the generated test class: " +
file + file +
"\nPlease re-run the generator: " + generatorClassFqName + "\nPlease re-run the generator: " + generatorClassFqName +
"(" + generatorClassSimpleName + ".java:1)"); "(" + generatorClassSimpleName + ".java:1)");
}
}
} }
} }
} }
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2012 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.test;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author abreslav
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface TestMetadata {
String value();
}
@@ -42,4 +42,9 @@ public class DelegatingTestClassModel implements TestClassModel {
public Collection<TestMethodModel> getTestMethods() { public Collection<TestMethodModel> getTestMethods() {
return delegate.getTestMethods(); return delegate.getTestMethods();
} }
@Override
public String getDataString() {
return delegate.getDataString();
}
} }
@@ -76,15 +76,19 @@ public class SimpleTestClassModel implements TestClassModel {
@Override @Override
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) { public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
p.println("JetTestUtils.allTestsPresent(" + p.println("JetTestUtils.assertAllTestsPresentByMetadata(" +
"this.getClass(), " + "this.getClass(), " +
"\"", generatorClassFqName, "\", " + "\"", generatorClassFqName, "\", " +
"new File(\"", JetTestUtils.getFilePath(rootFile) + "\"), \"", "new File(\"", JetTestUtils.getFilePath(rootFile) + "\"), \"",
extension, extension,
"\", ", recursive, "\", ", false,
");"); ");");
} }
@Override
public String getDataString() {
return null;
}
}); });
File[] listFiles = rootFile.listFiles(); File[] listFiles = rootFile.listFiles();
@@ -98,6 +102,11 @@ public class SimpleTestClassModel implements TestClassModel {
return result; return result;
} }
@Override
public String getDataString() {
return rootFile.getPath();
}
@Override @Override
public String getName() { public String getName() {
return testClassName; return testClassName;
@@ -42,6 +42,11 @@ public class SimpleTestMethodModel implements TestMethodModel {
p.println(doTestMethodName, "(\"", JetTestUtils.getFilePath(file), "\");"); p.println(doTestMethodName, "(\"", JetTestUtils.getFilePath(file), "\");");
} }
@Override
public String getDataString() {
return FileUtil.getRelativePath(rootDir, file);
}
@Override @Override
public String getName() { public String getName() {
String fileName = FileUtil.getNameWithoutExtension(file.getName()); String fileName = FileUtil.getNameWithoutExtension(file.getName());
@@ -21,8 +21,7 @@ import java.util.Collection;
/** /**
* @author abreslav * @author abreslav
*/ */
public interface TestClassModel { public interface TestClassModel extends TestEntityModel {
String getName();
Collection<TestClassModel> getInnerTestClasses(); Collection<TestClassModel> getInnerTestClasses();
@@ -0,0 +1,13 @@
package org.jetbrains.jet.test.generator;
import org.jetbrains.annotations.Nullable;
/**
* @author abreslav
*/
public interface TestEntityModel {
String getName();
@Nullable
String getDataString();
}
@@ -190,6 +190,7 @@ public class TestGenerator {
p.println("import java.util.HashSet;"); p.println("import java.util.HashSet;");
p.println("import java.util.Set;"); p.println("import java.util.Set;");
p.println("import org.jetbrains.jet.JetTestUtils;"); p.println("import org.jetbrains.jet.JetTestUtils;");
p.println("import org.jetbrains.jet.test.TestMetadata;");
p.println(); p.println();
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";"); p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
@@ -229,20 +230,14 @@ public class TestGenerator {
private void generateTestClass(Printer p, TestClassModel testDataSource, boolean isStatic) { private void generateTestClass(Printer p, TestClassModel testDataSource, boolean isStatic) {
String staticModifier = isStatic ? "static " : ""; String staticModifier = isStatic ? "static " : "";
generateMetadata(p, testDataSource);
p.println("public " + staticModifier + "class ", testDataSource.getName(), " extends ", baseTestClassName, " {"); p.println("public " + staticModifier + "class ", testDataSource.getName(), " extends ", baseTestClassName, " {");
p.pushIndent(); p.pushIndent();
Collection<TestMethodModel> testMethods = testDataSource.getTestMethods(); Collection<TestMethodModel> testMethods = testDataSource.getTestMethods();
for (TestMethodModel testMethodModel : testMethods) { for (TestMethodModel testMethodModel : testMethods) {
targetTestFramework.generateTestMethodAnnotations(this, p); generateTestMethod(p, testMethodModel);
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
p.pushIndent();
testMethodModel.generateBody(p, generatorName);
p.popIndent();
p.println("}");
p.println(); p.println();
} }
@@ -254,4 +249,23 @@ public class TestGenerator {
p.popIndent(); p.popIndent();
p.println("}"); p.println("}");
} }
private void generateTestMethod(Printer p, TestMethodModel testMethodModel) {
targetTestFramework.generateTestMethodAnnotations(this, p);
generateMetadata(p, testMethodModel);
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
p.pushIndent();
testMethodModel.generateBody(p, generatorName);
p.popIndent();
p.println("}");
}
private void generateMetadata(Printer p, TestEntityModel testDataSource) {
String dataString = testDataSource.getDataString();
if (dataString != null) {
p.println("@TestMetadata(\"", dataString, "\")");
}
}
} }
@@ -21,9 +21,6 @@ import org.jetbrains.annotations.NotNull;
/** /**
* @author abreslav * @author abreslav
*/ */
public interface TestMethodModel { public interface TestMethodModel extends TestEntityModel {
String getName();
void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName); void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName);
} }