Code cleanup in NamespaceComparator.
This commit is contained in:
+1
-1
@@ -56,7 +56,7 @@ public abstract class AbstractLazyResolveDiagnosticsTest extends AbstractJetDiag
|
|||||||
final Set<Name> names = LazyResolveTestUtil.getTopLevelPackagesFromFileList(jetFiles);
|
final Set<Name> names = LazyResolveTestUtil.getTopLevelPackagesFromFileList(jetFiles);
|
||||||
NamespaceComparator.compareNamespaces(
|
NamespaceComparator.compareNamespaces(
|
||||||
expected, actual,
|
expected, actual,
|
||||||
NamespaceComparator.RECURSIVE.filterRecusion(new Predicate<FqNameUnsafe>() {
|
NamespaceComparator.RECURSIVE.filterRecursion(new Predicate<FqNameUnsafe>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(FqNameUnsafe fqName) {
|
public boolean apply(FqNameUnsafe fqName) {
|
||||||
if (fqName.isRoot()) return true;
|
if (fqName.isRoot()) return true;
|
||||||
|
|||||||
+1
-1
@@ -72,7 +72,7 @@ public abstract class AbstractLazyResolveNamespaceComparingTest extends KotlinTe
|
|||||||
File serializeResultsTo = new File(FileUtil.getNameWithoutExtension(testFileName) + ".txt");
|
File serializeResultsTo = new File(FileUtil.getNameWithoutExtension(testFileName) + ".txt");
|
||||||
|
|
||||||
NamespaceComparator.compareNamespaces(expected, actual,
|
NamespaceComparator.compareNamespaces(expected, actual,
|
||||||
NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT.filterRecusion(
|
NamespaceComparator.DONT_INCLUDE_METHODS_OF_OBJECT.filterRecursion(
|
||||||
new Predicate<FqNameUnsafe>() {
|
new Predicate<FqNameUnsafe>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(FqNameUnsafe fqName) {
|
public boolean apply(FqNameUnsafe fqName) {
|
||||||
|
|||||||
+1
-1
@@ -61,7 +61,7 @@ public class LazyResolveStdlibLoadingTest extends KotlinTestWithEnvironmentManag
|
|||||||
for (Name name : namespaceShortNames) {
|
for (Name name : namespaceShortNames) {
|
||||||
NamespaceDescriptor a = module.getRootNamespace().getMemberScope().getNamespace(name);
|
NamespaceDescriptor a = module.getRootNamespace().getMemberScope().getNamespace(name);
|
||||||
NamespaceDescriptor b = lazyModule.getRootNamespace().getMemberScope().getNamespace(name);
|
NamespaceDescriptor b = lazyModule.getRootNamespace().getMemberScope().getNamespace(name);
|
||||||
NamespaceComparator.assertNamespacesEqual(a, b, NamespaceComparator.RECURSIVE);
|
NamespaceComparator.compareNamespaces(a, b, NamespaceComparator.RECURSIVE, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import com.google.common.collect.Lists;
|
|||||||
import com.intellij.openapi.util.io.FileUtil;
|
import com.intellij.openapi.util.io.FileUtil;
|
||||||
import com.intellij.util.containers.ContainerUtil;
|
import com.intellij.util.containers.ContainerUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.jvm.compiler.ExpectedLoadErrorsUtil;
|
import org.jetbrains.jet.jvm.compiler.ExpectedLoadErrorsUtil;
|
||||||
import org.jetbrains.jet.lang.descriptors.*;
|
import org.jetbrains.jet.lang.descriptors.*;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
@@ -44,89 +45,21 @@ import java.util.List;
|
|||||||
* @author Stepan Koltsov
|
* @author Stepan Koltsov
|
||||||
*/
|
*/
|
||||||
public class NamespaceComparator {
|
public class NamespaceComparator {
|
||||||
|
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, Predicates.<FqNameUnsafe>alwaysTrue());
|
||||||
|
public static final Configuration RECURSIVE = new Configuration(false, true, Predicates.<FqNameUnsafe>alwaysTrue());
|
||||||
|
|
||||||
private static final DescriptorRenderer RENDERER = new DescriptorRendererBuilder()
|
private static final DescriptorRenderer RENDERER = new DescriptorRendererBuilder()
|
||||||
.setWithDefinedIn(false)
|
.setWithDefinedIn(false)
|
||||||
.setExcludedAnnotationClasses(Arrays.asList(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)))
|
.setExcludedAnnotationClasses(Arrays.asList(new FqName(ExpectedLoadErrorsUtil.ANNOTATION_CLASS_NAME)))
|
||||||
.setVerbose(true).build();
|
.setVerbose(true).build();
|
||||||
|
|
||||||
public static void compareNamespaces(
|
|
||||||
@NotNull NamespaceDescriptor expectedNamespace,
|
|
||||||
@NotNull NamespaceDescriptor actualNamespace,
|
|
||||||
@NotNull Configuration configuration,
|
|
||||||
@NotNull File txtFile
|
|
||||||
) {
|
|
||||||
String serializedFormWithDemarkedNames = assertNamespacesEqual(expectedNamespace, actualNamespace, configuration);
|
|
||||||
// The serializer puts "!" in front of the name because it allows for speedy sorting of members
|
|
||||||
// see MemberComparator.normalize()
|
|
||||||
String serialized = serializedFormWithDemarkedNames.replace("!", "");
|
|
||||||
try {
|
|
||||||
if (!txtFile.exists()) {
|
|
||||||
FileUtil.writeToFile(txtFile, serialized);
|
|
||||||
Assert.fail("Expected data file did not exist. Generating: " + txtFile);
|
|
||||||
}
|
|
||||||
String expected = FileUtil.loadFile(txtFile, true);
|
|
||||||
|
|
||||||
// compare with hardcopy: make sure nothing is lost in output
|
|
||||||
Assert.assertEquals(expected, serialized);
|
|
||||||
}
|
|
||||||
catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String assertNamespacesEqual(
|
|
||||||
NamespaceDescriptor expectedNamespace,
|
|
||||||
NamespaceDescriptor actualNamespace,
|
|
||||||
@NotNull Configuration configuration
|
|
||||||
) {
|
|
||||||
NamespaceComparator comparator = new NamespaceComparator(configuration);
|
|
||||||
String serialized = comparator.doCompareNamespaces(expectedNamespace, actualNamespace);
|
|
||||||
return serialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, Predicates.<FqNameUnsafe>alwaysTrue());
|
|
||||||
public static final Configuration RECURSIVE = new Configuration(false, true, Predicates.<FqNameUnsafe>alwaysTrue());
|
|
||||||
|
|
||||||
public static class Configuration {
|
|
||||||
|
|
||||||
private final boolean checkPrimaryConstructors;
|
|
||||||
private final boolean includeObject;
|
|
||||||
private final Predicate<FqNameUnsafe> recurseIntoPackage;
|
|
||||||
|
|
||||||
public Configuration(
|
|
||||||
boolean checkPrimaryConstructors,
|
|
||||||
boolean includeObject,
|
|
||||||
Predicate<FqNameUnsafe> recurseIntoPackage
|
|
||||||
) {
|
|
||||||
this.checkPrimaryConstructors = checkPrimaryConstructors;
|
|
||||||
this.includeObject = includeObject;
|
|
||||||
this.recurseIntoPackage = recurseIntoPackage;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Configuration filterRecusion(@NotNull Predicate<FqNameUnsafe> recurseIntoPackage) {
|
|
||||||
return new Configuration(checkPrimaryConstructors, includeObject, recurseIntoPackage);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) {
|
|
||||||
return new Configuration(checkPrimaryConstructors, includeObject, recurseIntoPackage);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final Configuration conf;
|
private final Configuration conf;
|
||||||
|
|
||||||
private NamespaceComparator(@NotNull Configuration conf) {
|
private NamespaceComparator(@NotNull Configuration conf) {
|
||||||
this.conf = conf;
|
this.conf = conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String doCompareNamespaces(@NotNull NamespaceDescriptor expectedNamespace, @NotNull NamespaceDescriptor actualNamespace) {
|
private String serializeRecursively(@NotNull DeclarationDescriptor declarationDescriptor) {
|
||||||
String expectedSerialized = recursiveSerialize(expectedNamespace);
|
|
||||||
String actualSerialized = recursiveSerialize(actualNamespace);
|
|
||||||
|
|
||||||
Assert.assertEquals(expectedSerialized, actualSerialized);
|
|
||||||
return actualSerialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
private String recursiveSerialize(@NotNull DeclarationDescriptor declarationDescriptor) {
|
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
appendDeclarationRecursively(declarationDescriptor, new Printer(result, 1), true);
|
appendDeclarationRecursively(declarationDescriptor, new Printer(result, 1), true);
|
||||||
return result.toString();
|
return result.toString();
|
||||||
@@ -172,7 +105,7 @@ public class NamespaceComparator {
|
|||||||
Collections.sort(subDescriptors, MemberComparator.INSTANCE);
|
Collections.sort(subDescriptors, MemberComparator.INSTANCE);
|
||||||
|
|
||||||
for (DeclarationDescriptor subDescriptor : subDescriptors) {
|
for (DeclarationDescriptor subDescriptor : subDescriptors) {
|
||||||
if (!conf.includeObject) {
|
if (!conf.includeMethodsOfJavaObject) {
|
||||||
// TODO regexp check? oh dear
|
// TODO regexp check? oh dear
|
||||||
if (subDescriptor.getName().getName().matches("equals|hashCode|finalize|wait|notify(All)?|toString|clone|getClass")) {
|
if (subDescriptor.getName().getName().matches("equals|hashCode|finalize|wait|notify(All)?|toString|clone|getClass")) {
|
||||||
continue;
|
continue;
|
||||||
@@ -192,4 +125,58 @@ public class NamespaceComparator {
|
|||||||
printer.printlnWithNoIndent();
|
printer.printlnWithNoIndent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void compareNamespaces(
|
||||||
|
@NotNull NamespaceDescriptor expectedNamespace,
|
||||||
|
@NotNull NamespaceDescriptor actualNamespace,
|
||||||
|
@NotNull Configuration configuration,
|
||||||
|
@Nullable File txtFile
|
||||||
|
) {
|
||||||
|
NamespaceComparator comparator = new NamespaceComparator(configuration);
|
||||||
|
|
||||||
|
String expectedSerialized = comparator.serializeRecursively(expectedNamespace);
|
||||||
|
String actualSerialized = comparator.serializeRecursively(actualNamespace);
|
||||||
|
|
||||||
|
Assert.assertEquals("Expected and actual namespaces differ", expectedSerialized, actualSerialized);
|
||||||
|
|
||||||
|
if (txtFile != null) {
|
||||||
|
try {
|
||||||
|
if (!txtFile.exists()) {
|
||||||
|
FileUtil.writeToFile(txtFile, actualSerialized);
|
||||||
|
Assert.fail("Expected data file did not exist. Generating: " + txtFile);
|
||||||
|
}
|
||||||
|
String expected = FileUtil.loadFile(txtFile, true);
|
||||||
|
|
||||||
|
// compare with hardcopy: make sure nothing is lost in output
|
||||||
|
Assert.assertEquals("Expected and actual namespaces differ from " + txtFile.getName(), expected, actualSerialized);
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Configuration {
|
||||||
|
private final boolean checkPrimaryConstructors;
|
||||||
|
private final boolean includeMethodsOfJavaObject;
|
||||||
|
private final Predicate<FqNameUnsafe> recurseIntoPackage;
|
||||||
|
|
||||||
|
public Configuration(
|
||||||
|
boolean checkPrimaryConstructors,
|
||||||
|
boolean includeMethodsOfJavaObject,
|
||||||
|
Predicate<FqNameUnsafe> recurseIntoPackage
|
||||||
|
) {
|
||||||
|
this.checkPrimaryConstructors = checkPrimaryConstructors;
|
||||||
|
this.includeMethodsOfJavaObject = includeMethodsOfJavaObject;
|
||||||
|
this.recurseIntoPackage = recurseIntoPackage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Configuration filterRecursion(@NotNull Predicate<FqNameUnsafe> recurseIntoPackage) {
|
||||||
|
return new Configuration(checkPrimaryConstructors, includeMethodsOfJavaObject, recurseIntoPackage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) {
|
||||||
|
return new Configuration(checkPrimaryConstructors, includeMethodsOfJavaObject, recurseIntoPackage);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user