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;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.editor.Document;
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.java.AnalyzerFacadeForJVM;
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.SlicedMap;
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 String generatorClassFqName,
@NotNull File testDataDir,
@NotNull String extension,
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()) {
boolean isTestMethod = method.getName().startsWith("test");
if (isTestMethod) {
methodNames.add(method.getName().toLowerCase() + "." + extension);
TestMetadata testMetadata = method.getAnnotation(TestMetadata.class);
if (testMetadata != null) {
filePaths.add(testMetadata.value());
}
}
for (File file : testDataDir.listFiles()) {
if (file.isDirectory()) {
if (recursive) {
allTestsPresent(testCaseClass, generatorClassFqName, file, extension, recursive);
File[] files = testDataDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
if (recursive) {
assertAllTestsPresentByMetadata(testCaseClass, generatorClassFqName, file, extension, recursive);
}
}
}
else {
String name = file.getName();
if (name.endsWith("." + extension) && !methodNames.contains("test" + name.toLowerCase())) {
String generatorClassSimpleName = generatorClassFqName.substring(generatorClassFqName.lastIndexOf(".") + 1);
junit.framework.Assert.fail("Test data file missing from the generated test class: " +
file +
"\nPlease re-run the generator: " + generatorClassFqName +
"(" + generatorClassSimpleName + ".java:1)");
else {
if (file.getName().endsWith("." + extension)) {
String relativePath = FileUtil.getRelativePath(rootFile, file);
if (!filePaths.contains(relativePath)) {
String generatorClassSimpleName = generatorClassFqName.substring(generatorClassFqName.lastIndexOf(".") + 1);
Assert.fail("Test data file missing from the generated test class: " +
file +
"\nPlease re-run the generator: " + generatorClassFqName +
"(" + 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() {
return delegate.getTestMethods();
}
@Override
public String getDataString() {
return delegate.getDataString();
}
}
@@ -76,15 +76,19 @@ public class SimpleTestClassModel implements TestClassModel {
@Override
public void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName) {
p.println("JetTestUtils.allTestsPresent(" +
p.println("JetTestUtils.assertAllTestsPresentByMetadata(" +
"this.getClass(), " +
"\"", generatorClassFqName, "\", " +
"new File(\"", JetTestUtils.getFilePath(rootFile) + "\"), \"",
extension,
"\", ", recursive,
"\", ", false,
");");
}
@Override
public String getDataString() {
return null;
}
});
File[] listFiles = rootFile.listFiles();
@@ -98,6 +102,11 @@ public class SimpleTestClassModel implements TestClassModel {
return result;
}
@Override
public String getDataString() {
return rootFile.getPath();
}
@Override
public String getName() {
return testClassName;
@@ -42,6 +42,11 @@ public class SimpleTestMethodModel implements TestMethodModel {
p.println(doTestMethodName, "(\"", JetTestUtils.getFilePath(file), "\");");
}
@Override
public String getDataString() {
return FileUtil.getRelativePath(rootDir, file);
}
@Override
public String getName() {
String fileName = FileUtil.getNameWithoutExtension(file.getName());
@@ -21,8 +21,7 @@ import java.util.Collection;
/**
* @author abreslav
*/
public interface TestClassModel {
String getName();
public interface TestClassModel extends TestEntityModel {
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.Set;");
p.println("import org.jetbrains.jet.JetTestUtils;");
p.println("import org.jetbrains.jet.test.TestMetadata;");
p.println();
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
@@ -229,20 +230,14 @@ public class TestGenerator {
private void generateTestClass(Printer p, TestClassModel testDataSource, boolean isStatic) {
String staticModifier = isStatic ? "static " : "";
generateMetadata(p, testDataSource);
p.println("public " + staticModifier + "class ", testDataSource.getName(), " extends ", baseTestClassName, " {");
p.pushIndent();
Collection<TestMethodModel> testMethods = testDataSource.getTestMethods();
for (TestMethodModel testMethodModel : testMethods) {
targetTestFramework.generateTestMethodAnnotations(this, p);
p.println("public void ", testMethodModel.getName(), "() throws Exception {");
p.pushIndent();
testMethodModel.generateBody(p, generatorName);
p.popIndent();
p.println("}");
generateTestMethod(p, testMethodModel);
p.println();
}
@@ -254,4 +249,23 @@ public class TestGenerator {
p.popIndent();
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
*/
public interface TestMethodModel {
String getName();
public interface TestMethodModel extends TestEntityModel {
void generateBody(@NotNull Printer p, @NotNull String generatorClassFqName);
}