Read*BinaryClassTest: compare serialized namespace with hardcopy

This commit is contained in:
Stepan Koltsov
2012-02-15 16:15:20 +04:00
parent 94402c5f3f
commit cf7be620a7
131 changed files with 634 additions and 27 deletions
@@ -16,6 +16,8 @@
package org.jetbrains.jet.compiler;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.codegen.PropertyCodegen;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
@@ -41,9 +43,11 @@ import org.jetbrains.jet.lang.types.Variance;
import org.junit.Assert;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -62,13 +66,28 @@ class NamespaceComparator {
this.includeObject = includeObject;
}
public static void compareNamespaces(@NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb, boolean includeObject) {
new NamespaceComparator(includeObject).doCompareNamespaces(nsa, nsb);
public static void compareNamespaces(@NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb, boolean includeObject,
@NotNull File txtFile) {
String serialized = new NamespaceComparator(includeObject).doCompareNamespaces(nsa, nsb);
try {
// change to true to regenerate files if format changes
if (false) {
Files.write(serialized, txtFile, Charset.forName("utf-8"));
} else {
String expected = Files.toString(txtFile, Charset.forName("utf-8"));
// compare with hardcopy: make sure nothing is lost in output
Assert.assertEquals(expected, serialized);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void doCompareNamespaces(@NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb) {
private String doCompareNamespaces(@NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb) {
StringBuilder sb = new StringBuilder();
Assert.assertEquals(nsa.getName(), nsb.getName());
System.out.println("namespace " + nsa.getName());
sb.append("namespace " + nsa.getName() + "\n\n");
Assert.assertTrue(!nsa.getMemberScope().getAllDescriptors().isEmpty());
@@ -93,13 +112,13 @@ class NamespaceComparator {
ClassifierDescriptor cb = nsb.getMemberScope().getClassifier(name);
Assert.assertTrue(ca != null);
Assert.assertTrue(cb != null);
compareClassifiers(ca, cb);
compareClassifiers(ca, cb, sb);
}
for (String name : propertyNames) {
Set<VariableDescriptor> pa = nsa.getMemberScope().getProperties(name);
Set<VariableDescriptor> pb = nsb.getMemberScope().getProperties(name);
compareDeclarationSets(pa, pb);
compareDeclarationSets(pa, pb, sb);
Assert.assertTrue(nsb.getMemberScope().getFunctions(PropertyCodegen.getterName(name)).isEmpty());
Assert.assertTrue(nsb.getMemberScope().getFunctions(PropertyCodegen.setterName(name)).isEmpty());
@@ -108,15 +127,18 @@ class NamespaceComparator {
for (String name : functionNames) {
Set<FunctionDescriptor> fa = nsa.getMemberScope().getFunctions(name);
Set<FunctionDescriptor> fb = nsb.getMemberScope().getFunctions(name);
compareDeclarationSets(fa, fb);
compareDeclarationSets(fa, fb, sb);
}
return sb.toString();
}
private static void compareDeclarationSets(Set<? extends DeclarationDescriptor> a, Set<? extends DeclarationDescriptor> b) {
private static void compareDeclarationSets(Set<? extends DeclarationDescriptor> a, Set<? extends DeclarationDescriptor> b,
@NotNull StringBuilder sb) {
String at = serializedDeclarationSets(a);
String bt = serializedDeclarationSets(b);
Assert.assertEquals(at, bt);
System.out.print(at);
sb.append(at);
}
private static String serializedDeclarationSets(Collection<? extends DeclarationDescriptor> ds) {
@@ -137,7 +159,7 @@ class NamespaceComparator {
return r.toString();
}
private void compareClassifiers(@NotNull ClassifierDescriptor a, @NotNull ClassifierDescriptor b) {
private void compareClassifiers(@NotNull ClassifierDescriptor a, @NotNull ClassifierDescriptor b, @NotNull StringBuilder sb) {
StringBuilder sba = new StringBuilder();
StringBuilder sbb = new StringBuilder();
@@ -148,23 +170,9 @@ class NamespaceComparator {
String bs = sbb.toString();
Assert.assertEquals(as, bs);
System.out.println(as);
sb.append(as);
}
private void compareDescriptors(@NotNull DeclarationDescriptor a, @NotNull DeclarationDescriptor b) {
StringBuilder sba = new StringBuilder();
StringBuilder sbb = new StringBuilder();
new Serializer(sba).serialize(a);
new Serializer(sbb).serialize(b);
String as = sba.toString();
String bs = sbb.toString();
Assert.assertEquals(as, bs);
System.out.println(as);
}
private static class Serializer {
@@ -56,11 +56,13 @@ public class ReadJavaBinaryClassTest extends TestCaseWithTmpdir {
private final File ktFile;
private final File javaFile;
private final File txtFile;
public ReadJavaBinaryClassTest(File ktFile) {
this.ktFile = ktFile;
Assert.assertTrue(ktFile.getName().endsWith(".kt"));
this.javaFile = new File(ktFile.getPath().replaceFirst("\\.kt$", ".java"));
this.txtFile = new File(ktFile.getPath().replaceFirst("\\.kt$", ".txt"));
setName(javaFile.getName());
}
@@ -69,7 +71,7 @@ public class ReadJavaBinaryClassTest extends TestCaseWithTmpdir {
public void runTest() throws Exception {
NamespaceDescriptor nsa = compileKotlin();
NamespaceDescriptor nsb = compileJava();
NamespaceComparator.compareNamespaces(nsa, nsb, false);
NamespaceComparator.compareNamespaces(nsa, nsb, false, txtFile);
}
private NamespaceDescriptor compileKotlin() throws Exception {
@@ -52,9 +52,11 @@ public class ReadKotlinBinaryClassTest extends TestCaseWithTmpdir {
private JetCoreEnvironment jetCoreEnvironment;
private final File testFile;
private final File txtFile;
public ReadKotlinBinaryClassTest(@NotNull File testFile) {
this.testFile = testFile;
this.txtFile = new File(testFile.getPath().replaceFirst("\\.kt$", ".txt"));
setName(testFile.getName());
}
@@ -94,7 +96,7 @@ public class ReadKotlinBinaryClassTest extends TestCaseWithTmpdir {
JavaDescriptorResolver javaDescriptorResolver = semanticServices.getDescriptorResolver();
NamespaceDescriptor namespaceFromClass = javaDescriptorResolver.resolveNamespace("test");
NamespaceComparator.compareNamespaces(namespaceFromSource, namespaceFromClass, false);
NamespaceComparator.compareNamespaces(namespaceFromSource, namespaceFromClass, false, txtFile);
}
public static Test suite() {