Support rendering contents of different modules in load* tests

This is needed because otherwise with separate modules turned on in the
compiler, these tests do not display the contents of classes from the
dependency module in the rendered output
This commit is contained in:
Alexander Udalov
2016-09-28 14:46:24 +03:00
parent 3314725700
commit 9d778711d2
5 changed files with 56 additions and 46 deletions
@@ -43,7 +43,6 @@ import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.TestCaseWithTmpdir; import org.jetbrains.kotlin.test.TestCaseWithTmpdir;
import org.jetbrains.kotlin.test.TestJdkKind; import org.jetbrains.kotlin.test.TestJdkKind;
import org.jetbrains.kotlin.test.util.DescriptorValidator; import org.jetbrains.kotlin.test.util.DescriptorValidator;
import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator;
import org.junit.Assert; import org.junit.Assert;
import java.io.File; import java.io.File;
@@ -65,8 +64,11 @@ import static org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.*;
The generated test compares package descriptors loaded from kotlin sources and read from compiled java. The generated test compares package descriptors loaded from kotlin sources and read from compiled java.
*/ */
public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir { public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
// There are two modules in each test case (sources and dependencies), so we should render declarations from both of them
public static final Configuration COMPARATOR_CONFIGURATION = DONT_INCLUDE_METHODS_OF_OBJECT.renderDeclarationsFromOtherModules(true);
protected void doTestCompiledJava(@NotNull String javaFileName) throws Exception { protected void doTestCompiledJava(@NotNull String javaFileName) throws Exception {
doTestCompiledJava(javaFileName, DONT_INCLUDE_METHODS_OF_OBJECT); doTestCompiledJava(javaFileName, COMPARATOR_CONFIGURATION);
} }
// Java-Kotlin dependencies are not supported in this method for simplicity // Java-Kotlin dependencies are not supported in this method for simplicity
@@ -84,11 +86,11 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
javaSources, tmpdir, ConfigurationKind.JDK_ONLY javaSources, tmpdir, ConfigurationKind.JDK_ONLY
); );
checkJavaPackage(expectedFile, binaryPackageAndContext.first, binaryPackageAndContext.second, DONT_INCLUDE_METHODS_OF_OBJECT); checkJavaPackage(expectedFile, binaryPackageAndContext.first, binaryPackageAndContext.second, COMPARATOR_CONFIGURATION);
} }
protected void doTestCompiledJavaIncludeObjectMethods(@NotNull String javaFileName) throws Exception { protected void doTestCompiledJavaIncludeObjectMethods(@NotNull String javaFileName) throws Exception {
doTestCompiledJava(javaFileName, RECURSIVE); doTestCompiledJava(javaFileName, RECURSIVE.renderDeclarationsFromOtherModules(true));
} }
protected void doTestCompiledKotlin(@NotNull String ktFileName) throws Exception { protected void doTestCompiledKotlin(@NotNull String ktFileName) throws Exception {
@@ -132,9 +134,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
DescriptorValidator.validate(errorTypesForbidden(), packageFromSource); DescriptorValidator.validate(errorTypesForbidden(), packageFromSource);
DescriptorValidator.validate(new DeserializedScopeValidationVisitor(), packageFromBinary); DescriptorValidator.validate(new DeserializedScopeValidationVisitor(), packageFromBinary);
Configuration comparatorConfiguration = RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT Configuration comparatorConfiguration = COMPARATOR_CONFIGURATION.checkPrimaryConstructors(true).checkPropertyAccessors(true);
.checkPrimaryConstructors(true)
.checkPropertyAccessors(true);
compareDescriptors(packageFromSource, packageFromBinary, comparatorConfiguration, txtFile); compareDescriptors(packageFromSource, packageFromBinary, comparatorConfiguration, txtFile);
} }
@@ -168,7 +168,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
); );
PackageViewDescriptor packageView = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME); PackageViewDescriptor packageView = result.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME);
checkJavaPackage(expectedFile, packageView, result.getBindingContext(), DONT_INCLUDE_METHODS_OF_OBJECT); checkJavaPackage(expectedFile, packageView, result.getBindingContext(), COMPARATOR_CONFIGURATION);
} }
// TODO: add more tests on inherited parameter names, but currently impossible because of KT-4509 // TODO: add more tests on inherited parameter names, but currently impossible because of KT-4509
@@ -198,7 +198,7 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
PackageViewDescriptor packageView = module.getPackage(TEST_PACKAGE_FQNAME); PackageViewDescriptor packageView = module.getPackage(TEST_PACKAGE_FQNAME);
assertFalse(packageView.isEmpty()); assertFalse(packageView.isEmpty());
validateAndCompareDescriptorWithFile(packageView, DONT_INCLUDE_METHODS_OF_OBJECT.withValidationStrategy( validateAndCompareDescriptorWithFile(packageView, COMPARATOR_CONFIGURATION.withValidationStrategy(
new DeserializedScopeValidationVisitor() new DeserializedScopeValidationVisitor()
), expectedFile); ), expectedFile);
} }
@@ -220,8 +220,10 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir {
tmpdir, getTestRootDisposable(), getJdkKind(), ConfigurationKind.JDK_ONLY, false tmpdir, getTestRootDisposable(), getJdkKind(), ConfigurationKind.JDK_ONLY, false
); );
checkJavaPackage(expectedFile, javaPackageAndContext.first, javaPackageAndContext.second, checkJavaPackage(
DONT_INCLUDE_METHODS_OF_OBJECT.withValidationStrategy(errorTypesAllowed())); expectedFile, javaPackageAndContext.first, javaPackageAndContext.second,
COMPARATOR_CONFIGURATION.withValidationStrategy(errorTypesAllowed())
);
} }
private void doTestCompiledJava(@NotNull String javaFileName, Configuration configuration) throws Exception { private void doTestCompiledJava(@NotNull String javaFileName, Configuration configuration) throws Exception {
@@ -96,6 +96,7 @@ abstract class AbstractJvmRuntimeDescriptorLoaderTest : TestCaseWithTmpdir() {
/* checkPrimaryConstructors = */ fileName.endsWith(".kt"), /* checkPrimaryConstructors = */ fileName.endsWith(".kt"),
/* checkPropertyAccessors = */ true, /* checkPropertyAccessors = */ true,
/* includeMethodsOfKotlinAny = */ false, /* includeMethodsOfKotlinAny = */ false,
/* renderDeclarationsFromOtherModules = */ true,
// Skip Java annotation constructors because order of their parameters is not retained at runtime // Skip Java annotation constructors because order of their parameters is not retained at runtime
{ descriptor -> !descriptor!!.isJavaAnnotationConstructor() }, { descriptor -> !descriptor!!.isJavaAnnotationConstructor() },
errorTypesForbidden(), renderer errorTypesForbidden(), renderer
@@ -62,14 +62,14 @@ public class RecursiveDescriptorComparator {
} }
); );
public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, false, public static final Configuration DONT_INCLUDE_METHODS_OF_OBJECT = new Configuration(false, false, false, false,
Predicates.<DeclarationDescriptor>alwaysTrue(), Predicates.<DeclarationDescriptor>alwaysTrue(),
errorTypesForbidden(), DEFAULT_RENDERER); errorTypesForbidden(), DEFAULT_RENDERER);
public static final Configuration RECURSIVE = new Configuration(false, false, true, public static final Configuration RECURSIVE = new Configuration(false, false, true, false,
Predicates.<DeclarationDescriptor>alwaysTrue(), Predicates.<DeclarationDescriptor>alwaysTrue(),
errorTypesForbidden(), DEFAULT_RENDERER); errorTypesForbidden(), DEFAULT_RENDERER);
public static final Configuration RECURSIVE_ALL = new Configuration(true, true, true, public static final Configuration RECURSIVE_ALL = new Configuration(true, true, true, false,
Predicates.<DeclarationDescriptor>alwaysTrue(), Predicates.<DeclarationDescriptor>alwaysTrue(),
errorTypesForbidden(), DEFAULT_RENDERER); errorTypesForbidden(), DEFAULT_RENDERER);
@@ -189,7 +189,7 @@ public class RecursiveDescriptorComparator {
@NotNull Collection<? extends DeclarationDescriptor> extraSubDescriptors, @NotNull Collection<? extends DeclarationDescriptor> extraSubDescriptors,
@NotNull Printer printer @NotNull Printer printer
) { ) {
if (!module.equals(DescriptorUtils.getContainingModule(descriptor))) { if (!conf.renderDeclarationsFromOtherModules && !module.equals(DescriptorUtils.getContainingModule(descriptor))) {
printer.println(String.format("// -- Module: %s --", DescriptorUtils.getContainingModule(descriptor).getName())); printer.println(String.format("// -- Module: %s --", DescriptorUtils.getContainingModule(descriptor).getName()));
return; return;
} }
@@ -274,15 +274,16 @@ public class RecursiveDescriptorComparator {
private final boolean checkPrimaryConstructors; private final boolean checkPrimaryConstructors;
private final boolean checkPropertyAccessors; private final boolean checkPropertyAccessors;
private final boolean includeMethodsOfKotlinAny; private final boolean includeMethodsOfKotlinAny;
private final boolean renderDeclarationsFromOtherModules;
private final Predicate<DeclarationDescriptor> recursiveFilter; private final Predicate<DeclarationDescriptor> recursiveFilter;
private final DescriptorRenderer renderer; private final DescriptorRenderer renderer;
private final DescriptorValidator.ValidationVisitor validationStrategy; private final DescriptorValidator.ValidationVisitor validationStrategy;
public Configuration( public Configuration(
boolean checkPrimaryConstructors, boolean checkPrimaryConstructors,
boolean checkPropertyAccessors, boolean checkPropertyAccessors,
boolean includeMethodsOfKotlinAny, boolean includeMethodsOfKotlinAny,
boolean renderDeclarationsFromOtherModules,
Predicate<DeclarationDescriptor> recursiveFilter, Predicate<DeclarationDescriptor> recursiveFilter,
DescriptorValidator.ValidationVisitor validationStrategy, DescriptorValidator.ValidationVisitor validationStrategy,
DescriptorRenderer renderer DescriptorRenderer renderer
@@ -290,39 +291,46 @@ public class RecursiveDescriptorComparator {
this.checkPrimaryConstructors = checkPrimaryConstructors; this.checkPrimaryConstructors = checkPrimaryConstructors;
this.checkPropertyAccessors = checkPropertyAccessors; this.checkPropertyAccessors = checkPropertyAccessors;
this.includeMethodsOfKotlinAny = includeMethodsOfKotlinAny; this.includeMethodsOfKotlinAny = includeMethodsOfKotlinAny;
this.renderDeclarationsFromOtherModules = renderDeclarationsFromOtherModules;
this.recursiveFilter = recursiveFilter; this.recursiveFilter = recursiveFilter;
this.validationStrategy = validationStrategy; this.validationStrategy = validationStrategy;
this.renderer = renderer; this.renderer = renderer;
} }
public Configuration filterRecursion(@NotNull Predicate<DeclarationDescriptor> stepIntoFilter) { public Configuration filterRecursion(@NotNull Predicate<DeclarationDescriptor> stepIntoFilter) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, stepIntoFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
renderDeclarationsFromOtherModules, stepIntoFilter,
validationStrategy.withStepIntoFilter(stepIntoFilter), renderer); validationStrategy.withStepIntoFilter(stepIntoFilter), renderer);
} }
public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) { public Configuration checkPrimaryConstructors(boolean checkPrimaryConstructors) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, recursiveFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
validationStrategy, renderer); renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
} }
public Configuration checkPropertyAccessors(boolean checkPropertyAccessors) { public Configuration checkPropertyAccessors(boolean checkPropertyAccessors) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, recursiveFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
validationStrategy, renderer); renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
} }
public Configuration includeMethodsOfKotlinAny(boolean includeMethodsOfKotlinAny) { public Configuration includeMethodsOfKotlinAny(boolean includeMethodsOfKotlinAny) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, recursiveFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
validationStrategy, renderer); renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
}
public Configuration renderDeclarationsFromOtherModules(boolean renderDeclarationsFromOtherModules) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
} }
public Configuration withValidationStrategy(@NotNull DescriptorValidator.ValidationVisitor validationStrategy) { public Configuration withValidationStrategy(@NotNull DescriptorValidator.ValidationVisitor validationStrategy) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, recursiveFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
validationStrategy, renderer); renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
} }
public Configuration withRenderer(@NotNull DescriptorRenderer renderer) { public Configuration withRenderer(@NotNull DescriptorRenderer renderer) {
return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny, recursiveFilter, return new Configuration(checkPrimaryConstructors, checkPropertyAccessors, includeMethodsOfKotlinAny,
validationStrategy, renderer); renderDeclarationsFromOtherModules, recursiveFilter, validationStrategy, renderer);
} }
} }
} }
@@ -40,28 +40,28 @@ import java.lang.annotation.Retention;
import java.util.Collections; import java.util.Collections;
import static org.jetbrains.kotlin.test.KotlinTestUtils.*; import static org.jetbrains.kotlin.test.KotlinTestUtils.*;
import static org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT;
import static org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile; import static org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile;
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "JUnitTestCaseWithNoTests"}) @SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "JUnitTestCaseWithNoTests"})
public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithTmpdir { public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithTmpdir {
// Do not render parameter names because there are test cases where classes inherit from JDK collections, // Do not render parameter names because there are test cases where classes inherit from JDK collections,
// and some versions of JDK have debug information in the class files (including parameter names), and some don't // and some versions of JDK have debug information in the class files (including parameter names), and some don't
private static final RecursiveDescriptorComparator.Configuration CONFIGURATION = DONT_INCLUDE_METHODS_OF_OBJECT.withRenderer( private static final RecursiveDescriptorComparator.Configuration CONFIGURATION =
DescriptorRenderer.Companion.withOptions( AbstractLoadJavaTest.COMPARATOR_CONFIGURATION.withRenderer(
new Function1<DescriptorRendererOptions, Unit>() { DescriptorRenderer.Companion.withOptions(
@Override new Function1<DescriptorRendererOptions, Unit>() {
public Unit invoke(DescriptorRendererOptions options) { @Override
options.setWithDefinedIn(false); public Unit invoke(DescriptorRendererOptions options) {
options.setParameterNameRenderingPolicy(ParameterNameRenderingPolicy.NONE); options.setWithDefinedIn(false);
options.setVerbose(true); options.setParameterNameRenderingPolicy(ParameterNameRenderingPolicy.NONE);
options.setExcludedAnnotationClasses(Collections.singleton(new FqName(Retention.class.getName()))); options.setVerbose(true);
options.setModifiers(DescriptorRendererModifier.ALL); options.setExcludedAnnotationClasses(Collections.singleton(new FqName(Retention.class.getName())));
return Unit.INSTANCE; options.setModifiers(DescriptorRendererModifier.ALL);
} return Unit.INSTANCE;
} }
) }
); )
);
protected void doTest(String ktFilePath) throws IOException { protected void doTest(String ktFilePath) throws IOException {
Assert.assertTrue(ktFilePath.endsWith(".kt")); Assert.assertTrue(ktFilePath.endsWith(".kt"));
@@ -99,9 +99,8 @@ public class CompileKotlinAgainstCustomBinariesTest extends TestCaseWithTmpdir {
private void doTestWithTxt(@NotNull File... extraClassPath) throws Exception { private void doTestWithTxt(@NotNull File... extraClassPath) throws Exception {
PackageViewDescriptor packageView = analyzeFileToPackageView(extraClassPath); PackageViewDescriptor packageView = analyzeFileToPackageView(extraClassPath);
RecursiveDescriptorComparator.Configuration comparator = RecursiveDescriptorComparator.Configuration comparator = AbstractLoadJavaTest.COMPARATOR_CONFIGURATION
RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT.withValidationStrategy( .withValidationStrategy(DescriptorValidator.ValidationVisitor.errorTypesAllowed());
DescriptorValidator.ValidationVisitor.errorTypesAllowed());
validateAndCompareDescriptorWithFile(packageView, comparator, getTestDataFileWithExtension("txt")); validateAndCompareDescriptorWithFile(packageView, comparator, getTestDataFileWithExtension("txt"));
} }