Comparing tests generated from test data

For better debugging
This commit is contained in:
Andrey Breslav
2012-06-06 22:12:40 +04:00
parent acee1f5df1
commit 9da5fe11d9
7 changed files with 285 additions and 32 deletions
@@ -0,0 +1 @@
fun <T> foo(t : T) : T {}
@@ -0,0 +1,3 @@
namespace <root>
final fun </*0*/ T : jet.Any?>foo(/*0*/ t: T): T
@@ -0,0 +1,17 @@
// FILE: a.kt
class A {}
// FILE: b.kt
package p; class C {fun f() {}}
// FILE: c.kt
package p; open class G<T> {open fun f(): T {} fun a() {}}
// FILE: d.kt
package p; class G2<E> : G<E> { fun g() : E {} override fun f() : T {}}
// FILE: e.kt
package p; fun foo() {}
// FILE: f.kt
package p; fun foo(a: C) {}
@@ -1,5 +1,26 @@
namespace <root>
// <namespace name="p">
namespace p
final class p.C : jet.Any {
final /*constructor*/ fun <init>(): p.C
final fun f(): jet.Tuple0
}
open class p.G</*0*/ T : jet.Any?> : jet.Any {
final /*constructor*/ fun </*0*/ T : jet.Any?><init>(): p.G<T>
final fun a(): jet.Tuple0
open fun f(): T
}
final class p.G2</*0*/ E : jet.Any?> : p.G<E> {
final /*constructor*/ fun </*0*/ E : jet.Any?><init>(): p.G2<E>
final override /*1*/ fun a(): jet.Tuple0
open override /*1*/ fun f(): <root>
final fun g(): E
}
final fun foo(): jet.Tuple0
final fun foo(/*0*/ a: p.C): jet.Tuple0
// </namespace name="p">
final class A : jet.Any {
final /*constructor*/ fun <init>(): A
}
@@ -20,8 +20,10 @@ import com.google.common.base.Predicates;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.PsiFile;
import org.jetbrains.jet.CompileCompilerDependenciesTest;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.cli.jvm.compiler.JetCoreEnvironment;
import org.jetbrains.jet.cli.jvm.compiler.NamespaceComparator;
import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJvm;
@@ -29,7 +31,6 @@ import org.jetbrains.jet.lang.descriptors.ModuleDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetPsiFactory;
import org.jetbrains.jet.lang.resolve.AnalyzerScriptParameter;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import org.jetbrains.jet.lang.resolve.TopDownAnalysisParameters;
import org.jetbrains.jet.lang.resolve.java.CompilerDependencies;
@@ -37,17 +38,16 @@ import org.jetbrains.jet.lang.resolve.java.CompilerSpecialMode;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
import java.util.Arrays;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
/**
* @author abreslav
*/
public class LazyResolveComparingTest {
public abstract class AbstractLazyResolveComparingTest {
private final Disposable rootDisposable = new Disposable() {
@Override
public void dispose() {
@@ -58,14 +58,6 @@ public class LazyResolveComparingTest {
compilerDependencies = CompileCompilerDependenciesTest.compilerDependenciesForTests(CompilerSpecialMode.REGULAR, true);
private final JetCoreEnvironment jetCoreEnvironment = new JetCoreEnvironment(rootDisposable, compilerDependencies);
private final Project project = jetCoreEnvironment.getProject();
private final List<JetFile> files = Arrays.asList(
JetPsiFactory.createFile(project, "class A {}"),
JetPsiFactory.createFile(project, "package p; class C {fun f() {}}"),
JetPsiFactory.createFile(project, "package p; open class G<T> {open fun f(): T {} fun a() {}}"),
JetPsiFactory.createFile(project, "package p; class G2<E> : G<E> { fun g() : E {} override fun f() : T {}}"),
JetPsiFactory.createFile(project, "package p; fun foo() {}"),
JetPsiFactory.createFile(project, "package p; fun foo(a: C) {}")
);
@BeforeClass
public static void setUp() throws Exception {
@@ -77,36 +69,29 @@ public class LazyResolveComparingTest {
Disposer.dispose(rootDisposable);
}
@Test
public void testNamespacesAreEqual() throws Exception {
protected void doTest(String testFileName) throws IOException {
TopDownAnalysisParameters params = new TopDownAnalysisParameters(
Predicates.<PsiFile>alwaysTrue(), false, false, Collections.<AnalyzerScriptParameter>emptyList());
ModuleDescriptor module = new ModuleDescriptor(Name.special("<test module>"));
InjectorForTopDownAnalyzerForJvm
injector = new InjectorForTopDownAnalyzerForJvm(project, params, new BindingTraceContext(), module, compilerDependencies);
injector.getTopDownAnalyzer().analyzeFiles(files, Collections.<AnalyzerScriptParameter>emptyList());
final BindingTrace trace = injector.getBindingTrace();
//for (JetFile file : files) {
// for (final JetDeclaration declaration : file.getDeclarations()) {
// declaration.accept(new JetTreeVisitor<Void>() {
// @Override
// public Void visitDeclaration(JetDeclaration dcl, Void data) {
// DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, dcl);
// System.out.println(descriptor);
// return super.visitDeclaration(dcl, data);
// }
// }, null);
// }
//}
List<JetFile> files = JetTestUtils
.createTestFiles(testFileName, FileUtil.loadFile(new File(testFileName)), new JetTestUtils.TestFileFactory<JetFile>() {
@Override
public JetFile create(String fileName, String text) {
return JetPsiFactory.createFile(project, fileName, text);
}
});
injector.getTopDownAnalyzer().analyzeFiles(files, Collections.<AnalyzerScriptParameter>emptyList());
ModuleDescriptor lazyModule = new ModuleDescriptor(Name.special("<lazy module>"));
ResolveSession session = new ResolveSession(project, lazyModule, new FileBasedDeclarationProviderFactory(files));
NamespaceComparator.compareNamespaces(lazyModule.getRootNamespace(), module.getRootNamespace(), true,
new File("compiler/testData/lazyResolve/simpleClass.txt"));
//NamespaceComparator.compareNamespaces(lazyModule.getRootNamespace().getMemberScope().getNamespace(Name.identifier("p")),
// module.getRootNamespace().getMemberScope().getNamespace(Name.identifier("p")), true, new File("log1.txt"));
NamespaceComparator.compareNamespaces(module.getRootNamespace(), lazyModule.getRootNamespace(), true,
new File(FileUtil.getNameWithoutExtension(testFileName) + ".txt"));
}
}
@@ -0,0 +1,60 @@
/*
* 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.lang.resolve.lazy;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileFilter;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
/* This class is generated by LazyResolveComparingTestGenerator. DO NOT MODIFY MANUALLY */
public class LazyResolveComparingTestGenerated extends AbstractLazyResolveComparingTest {
@Test
public void testGenericFunction() throws Exception {
doTest("compiler/testData/lazyResolve/genericFunction.kt");
}
@Test
public void testSimpleClass() throws Exception {
doTest("compiler/testData/lazyResolve/simpleClass.kt");
}
@Test
public void allTestsPresent() {
Set<String> methodNames = new HashSet<String>();
for (Method method : LazyResolveComparingTestGenerated.class.getDeclaredMethods()) {
if (method.isAnnotationPresent(Test.class)) {
methodNames.add(method.getName().toLowerCase() + ".kt");
}
}
File[] testDataFiles = new File("compiler/testData/lazyResolve").listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".kt");
}
});
for (File testDataFile : testDataFiles) {
if (!methodNames.contains("test" + testDataFile.getName().toLowerCase())) {
Assert.fail("Test data file missing from the generated test class: " + testDataFile + "\nPlease re-run the generator: LazyResolveComparingTestGenerated");
}
}
}
}
@@ -0,0 +1,166 @@
/*
* 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.lang.resolve.lazy;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
/**
* @author abreslav
*/
public class LazyResolveComparingTestGenerator {
public static void main(String[] args) throws IOException {
new LazyResolveComparingTestGenerator().generateAndSave();
}
private final String baseDir;
private final String testDataFileExtension;
private final String testClassPackage;
private final String testClassName;
private final String baseTestClassPackage;
private final String baseTestClassName;
private final File testDataDir;
private final String generatorName;
public LazyResolveComparingTestGenerator() {
baseDir = "compiler/tests/";
testDataFileExtension = "kt";
testClassPackage = "org.jetbrains.jet.lang.resolve.lazy";
testClassName = "LazyResolveComparingTestGenerated";
baseTestClassPackage = "org.jetbrains.jet.lang.resolve.lazy";
baseTestClassName = "AbstractLazyResolveComparingTest";
testDataDir = new File("compiler/testData/lazyResolve");
generatorName = "LazyResolveComparingTestGenerator";
}
public void generateAndSave() throws IOException {
StringBuilder out = new StringBuilder();
Printer p = new Printer(out);
p.print(FileUtil.loadFile(new File("injector-generator/copyright.txt")));
p.println("package ", testClassPackage, ";");
p.println();
p.println("import org.junit.Assert;");
p.println("import org.junit.Test;");
p.println();
p.println("import java.io.File;");
p.println("import java.io.FileFilter;");
p.println("import java.lang.reflect.Method;");
p.println("import java.util.HashSet;");
p.println("import java.util.Set;");
p.println();
p.println("import ", baseTestClassPackage, ".", baseTestClassName, ";");
p.println();
p.println("/* This class is generated by " + generatorName + ". DO NOT MODIFY MANUALLY */");
p.println("public class ", testClassName, " extends ", baseTestClassName, " {");
p.pushIndent();
File[] ktFiles = testDataDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith("." + testDataFileExtension);
}
});
for (File file : ktFiles) {
p.println("@Test");
p.println("public void test", FileUtil.getNameWithoutExtension(StringUtil.capitalize(file.getName())), "() throws Exception {");
p.pushIndent();
p.println("doTest(\"", file.getPath(), "\");");
p.popIndent();
p.println("}");
p.println();
}
generateAllTestsPresent(p);
p.popIndent();
p.println("}");
String testSourceFilePath = baseDir + testClassPackage.replace(".", "/") + "/" + testClassName + ".java";
FileUtil.writeToFile(new File(testSourceFilePath), out.toString());
}
private void generateAllTestsPresent(Printer p) {
String methodText =
"@Test\n" +
" public void allTestsPresent() {\n" +
" Set<String> methodNames = new HashSet<String>();\n" +
" for (Method method : " + testClassName + ".class.getDeclaredMethods()) {\n" +
" if (method.isAnnotationPresent(Test.class)) {\n" +
" methodNames.add(method.getName().toLowerCase() + \"." + testDataFileExtension + "\");\n" +
" }\n" +
" }\n" +
" File[] testDataFiles = new File(\"" + testDataDir + "\").listFiles(new FileFilter() {\n" +
" @Override\n" +
" public boolean accept(File pathname) {\n" +
" return pathname.getName().endsWith(\"." + testDataFileExtension + "\");\n" +
" }\n" +
" });\n" +
" for (File testDataFile : testDataFiles) {\n" +
" if (!methodNames.contains(\"test\" + testDataFile.getName().toLowerCase())) {\n" +
" Assert.fail(\"Test data file missing from the generated test class: \" + testDataFile + \"\\nPlease re-run the generator: " + testClassName + "\");\n" +
" }\n" +
" }\n" +
" }\n";
p.println(methodText);
}
private static class Printer {
private static final String INDENTATION_UNIT = " ";
private String indent = "";
private final StringBuilder out;
private Printer(@NotNull StringBuilder out) {
this.out = out;
}
public void println(Object... objects) {
print(objects);
out.append("\n");
}
public void print(Object... objects) {
out.append(indent);
for (Object object : objects) {
out.append(object);
}
}
public void pushIndent() {
indent += INDENTATION_UNIT;
}
public void popIndent() {
if (indent.length() < INDENTATION_UNIT.length()) {
throw new IllegalStateException("No indentation to pop");
}
indent = indent.substring(INDENTATION_UNIT.length());
}
}
}