From 888a6c0c9b603d1d9c8d742333d1ba5072a85954 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Tue, 18 Mar 2014 17:04:55 +0400 Subject: [PATCH] Add tests on Kotlin against compiled Java+Kotlin The original purpose was to create a test that parameter names are inherited on a K-J-K hierarchy when "K-J" is compiled to bytecode, but that's not possible right now because of KT-4509 --- .../InheritParameterName.kt | 8 ++++ .../InheritParameterName.txt | 21 ++++++++++ .../InheritParameterName/A.java | 5 +++ .../tests/org/jetbrains/jet/JetTestUtils.java | 25 ++++++++++- .../AbstractCompileJavaAgainstKotlinTest.java | 18 ++------ .../jvm/compiler/AbstractLoadJavaTest.java | 41 ++++++++++++++++++- .../jvm/compiler/LoadJavaTestGenerated.java | 16 +++++++- .../jet/generators/tests/GenerateTests.kt | 1 + 8 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.kt create mode 100644 compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.txt create mode 100644 compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName/A.java diff --git a/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.kt b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.kt new file mode 100644 index 00000000000..e6a67265e60 --- /dev/null +++ b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.kt @@ -0,0 +1,8 @@ +package test + +trait B { + fun foo(kotlinName: Int) +} + +class ZAB : A, B +class ZBA : B, A diff --git a/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.txt b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.txt new file mode 100644 index 00000000000..da594bb3448 --- /dev/null +++ b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.txt @@ -0,0 +1,21 @@ +package test + +public /*synthesized*/ fun A(/*0*/ function: (kotlin.Int) -> kotlin.Unit): test.A + +public trait A : java.lang.Object { + public abstract fun foo(/*0*/ p0: kotlin.Int): kotlin.Unit +} + +internal trait B { + internal abstract fun foo(/*0*/ kotlinName: kotlin.Int): kotlin.Unit +} + +internal final class ZAB : test.A, test.B { + public constructor ZAB() + public abstract override /*2*/ /*fake_override*/ fun foo(/*0*/ kotlinName: kotlin.Int): kotlin.Unit +} + +internal final class ZBA : test.B, test.A { + public constructor ZBA() + public abstract override /*2*/ /*fake_override*/ fun foo(/*0*/ kotlinName: kotlin.Int): kotlin.Unit +} diff --git a/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName/A.java b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName/A.java new file mode 100644 index 00000000000..f68ab15fb7d --- /dev/null +++ b/compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName/A.java @@ -0,0 +1,5 @@ +package test; + +public interface A { + void foo(int javaName); +} diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 968a18a5659..96d2e189cf0 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -73,6 +73,7 @@ import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; import org.jetbrains.jet.util.slicedmap.SlicedMap; import org.jetbrains.jet.util.slicedmap.WritableSlice; import org.jetbrains.jet.utils.PathUtil; +import org.jetbrains.jet.utils.UtilsPackage; import org.junit.Assert; import javax.tools.*; @@ -89,6 +90,7 @@ import static org.jetbrains.jet.ConfigurationKind.ALL; import static org.jetbrains.jet.ConfigurationKind.JDK_AND_ANNOTATIONS; import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.ANNOTATIONS_PATH_KEY; import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY; +import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.compileKotlinToDirAndGetAnalyzeExhaust; public class JetTestUtils { private static final Pattern KT_FILES = Pattern.compile(".*?.kt"); @@ -443,7 +445,28 @@ public class JetTestUtils { } } catch (IOException e) { - throw new RuntimeException(e); + throw UtilsPackage.rethrow(e); + } + } + + public static void compileKotlinWithJava( + @NotNull List javaFiles, + @NotNull List ktFiles, + @NotNull File outDir, + @NotNull Disposable disposable + ) throws IOException { + if (!ktFiles.isEmpty()) { + compileKotlinToDirAndGetAnalyzeExhaust(ktFiles, outDir, disposable, ALL); + } + else { + boolean mkdirs = outDir.mkdirs(); + assert mkdirs : "Not created: " + outDir; + } + if (!javaFiles.isEmpty()) { + compileJavaFiles(javaFiles, Arrays.asList( + "-classpath", outDir.getPath() + File.pathSeparator + ForTestCompileRuntime.runtimeJarForTests(), + "-d", outDir.getPath() + )); } } diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java index e2075f02672..6642c428176 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractCompileJavaAgainstKotlinTest.java @@ -16,31 +16,21 @@ package org.jetbrains.jet.jvm.compiler; -import org.jetbrains.jet.ConfigurationKind; -import org.jetbrains.jet.JetTestUtils; -import org.jetbrains.jet.codegen.forTestCompile.ForTestCompileRuntime; import org.jetbrains.jet.test.TestCaseWithTmpdir; import org.junit.Assert; import java.io.File; -import java.util.Arrays; +import java.io.IOException; import java.util.Collections; -import java.util.List; -import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.compileKotlinToDirAndGetAnalyzeExhaust; +import static org.jetbrains.jet.JetTestUtils.compileKotlinWithJava; @SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "JUnitTestCaseWithNoTests"}) public abstract class AbstractCompileJavaAgainstKotlinTest extends TestCaseWithTmpdir { - protected void doTest(String ktFilePath) throws Exception { + protected void doTest(String ktFilePath) throws IOException { Assert.assertTrue(ktFilePath.endsWith(".kt")); File ktFile = new File(ktFilePath); File javaFile = new File(ktFilePath.replaceFirst("\\.kt", ".java")); - compileKotlinToDirAndGetAnalyzeExhaust(Collections.singletonList(ktFile), tmpdir, getTestRootDisposable(), ConfigurationKind.ALL); - - List options = Arrays.asList( - "-classpath", tmpdir.getPath() + System.getProperty("path.separator") + ForTestCompileRuntime.runtimeJarForTests(), - "-d", tmpdir.getPath() - ); - JetTestUtils.compileJavaFiles(Collections.singleton(javaFile), options); + compileKotlinWithJava(Collections.singletonList(javaFile), Collections.singletonList(ktFile), tmpdir, getTestRootDisposable()); } } diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java index b5664b60918..43978aec73a 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/AbstractLoadJavaTest.java @@ -23,6 +23,7 @@ import com.intellij.psi.PsiFile; import junit.framework.ComparisonFailure; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.ConfigurationKind; +import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.TestJdkKind; import org.jetbrains.jet.analyzer.AnalyzeExhaust; @@ -35,7 +36,12 @@ import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedClass import org.jetbrains.jet.descriptors.serialization.descriptors.MemberFilter; import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJvm; import org.jetbrains.jet.lang.descriptors.*; -import org.jetbrains.jet.lang.resolve.*; +import org.jetbrains.jet.lang.psi.JetFile; +import org.jetbrains.jet.lang.resolve.BindingContext; +import org.jetbrains.jet.lang.resolve.BindingTrace; +import org.jetbrains.jet.lang.resolve.DescriptorUtils; +import org.jetbrains.jet.lang.resolve.TopDownAnalysisParameters; +import org.jetbrains.jet.lang.resolve.lazy.JvmResolveUtil; import org.jetbrains.jet.storage.ExceptionTracker; import org.jetbrains.jet.storage.LockBasedStorageManager; import org.jetbrains.jet.test.TestCaseWithTmpdir; @@ -51,6 +57,7 @@ import java.util.List; import java.util.Map; import java.util.regex.Pattern; +import static org.jetbrains.jet.JetTestUtils.*; import static org.jetbrains.jet.jvm.compiler.LoadDescriptorUtil.*; import static org.jetbrains.jet.test.util.DescriptorValidator.ValidationVisitor.ALLOW_ERROR_TYPES; import static org.jetbrains.jet.test.util.RecursiveDescriptorComparator.*; @@ -116,7 +123,8 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir { RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT .checkPrimaryConstructors(true) .checkPropertyAccessors(true), - txtFile); + txtFile + ); } protected void doTestJavaAgainstKotlin(String expectedFileName) throws Exception { @@ -165,6 +173,35 @@ public abstract class AbstractLoadJavaTest extends TestCaseWithTmpdir { checkJavaPackage(expectedFile, packageView, trace.getBindingContext(), DONT_INCLUDE_METHODS_OF_OBJECT); } + // TODO: add more tests on inherited parameter names, but currently impossible because of KT-4509 + protected void doTestKotlinAgainstCompiledJavaWithKotlin(@NotNull String expectedFileName) throws Exception { + File kotlinSrc = new File(expectedFileName); + File librarySrc = new File(expectedFileName.replaceFirst("\\.kt$", "")); + File expectedFile = new File(expectedFileName.replaceFirst("\\.kt$", ".txt")); + + File libraryOut = new File(tmpdir, "libraryOut"); + compileKotlinWithJava( + Arrays.asList(librarySrc.listFiles(JetTestCaseBuilder.filterByExtension("java"))), + Arrays.asList(librarySrc.listFiles(JetTestCaseBuilder.filterByExtension("kt"))), + libraryOut, + getTestRootDisposable() + ); + + JetCoreEnvironment environment = JetCoreEnvironment.createForTests( + getTestRootDisposable(), compilerConfigurationForTests(ConfigurationKind.JDK_ONLY, TestJdkKind.MOCK_JDK, + getAnnotationsJar(), libraryOut) + ); + JetFile jetFile = JetTestUtils.createFile(kotlinSrc.getPath(), FileUtil.loadFile(kotlinSrc, true), environment.getProject()); + + AnalyzeExhaust exhaust = JvmResolveUtil.analyzeFilesWithJavaIntegration( + environment.getProject(), Collections.singleton(jetFile), Predicates.alwaysTrue() + ); + PackageViewDescriptor packageView = exhaust.getModuleDescriptor().getPackage(TEST_PACKAGE_FQNAME); + assertNotNull(packageView); + + validateAndCompareDescriptorWithFile(packageView, DONT_INCLUDE_METHODS_OF_OBJECT, expectedFile); + } + protected void doTestSourceJava(@NotNull String javaFileName) throws Exception { File originalJavaFile = new File(javaFileName); File expectedFile = getTxtFile(javaFileName); diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java index 4f28b2e3dbe..8398fbe8eb4 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadJavaTestGenerated.java @@ -30,7 +30,7 @@ import org.jetbrains.jet.jvm.compiler.AbstractLoadJavaTest; /** This class is generated by {@link org.jetbrains.jet.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ @SuppressWarnings("all") -@InnerTestClasses({LoadJavaTestGenerated.CompiledJava.class, LoadJavaTestGenerated.CompiledJavaAndKotlin.class, LoadJavaTestGenerated.CompiledJavaCompareWithKotlin.class, LoadJavaTestGenerated.CompiledJavaIncludeObjectMethods.class, LoadJavaTestGenerated.CompiledKotlin.class, LoadJavaTestGenerated.JavaAgainstKotlin.class, LoadJavaTestGenerated.SourceJava.class}) +@InnerTestClasses({LoadJavaTestGenerated.CompiledJava.class, LoadJavaTestGenerated.CompiledJavaAndKotlin.class, LoadJavaTestGenerated.CompiledJavaCompareWithKotlin.class, LoadJavaTestGenerated.CompiledJavaIncludeObjectMethods.class, LoadJavaTestGenerated.CompiledKotlin.class, LoadJavaTestGenerated.JavaAgainstKotlin.class, LoadJavaTestGenerated.KotlinAgainstCompiledJavaWithKotlin.class, LoadJavaTestGenerated.SourceJava.class}) public class LoadJavaTestGenerated extends AbstractLoadJavaTest { @TestMetadata("compiler/testData/loadJava/compiledJava") @InnerTestClasses({CompiledJava.Annotations.class, CompiledJava.ProtectedPackage.class, CompiledJava.ProtectedStatic.class, CompiledJava.Sam.class, CompiledJava.SignaturePropagation.class, CompiledJava.Static.class}) @@ -3133,6 +3133,19 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest { } } + @TestMetadata("compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin") + public static class KotlinAgainstCompiledJavaWithKotlin extends AbstractLoadJavaTest { + public void testAllFilesPresentInKotlinAgainstCompiledJavaWithKotlin() throws Exception { + JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin"), Pattern.compile("^(.+)\\.kt$"), false); + } + + @TestMetadata("InheritParameterName.kt") + public void testInheritParameterName() throws Exception { + doTestKotlinAgainstCompiledJavaWithKotlin("compiler/testData/loadJava/kotlinAgainstCompiledJavaWithKotlin/InheritParameterName.kt"); + } + + } + @TestMetadata("compiler/testData/loadJava/sourceJava") public static class SourceJava extends AbstractLoadJavaTest { public void testAllFilesPresentInSourceJava() throws Exception { @@ -3179,6 +3192,7 @@ public class LoadJavaTestGenerated extends AbstractLoadJavaTest { suite.addTestSuite(CompiledJavaIncludeObjectMethods.class); suite.addTest(CompiledKotlin.innerSuite()); suite.addTest(JavaAgainstKotlin.innerSuite()); + suite.addTestSuite(KotlinAgainstCompiledJavaWithKotlin.class); suite.addTestSuite(SourceJava.class); return suite; } diff --git a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt index a828bbb6b2a..42e851bc559 100644 --- a/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/jet/generators/tests/GenerateTests.kt @@ -194,6 +194,7 @@ fun main(args: Array) { model("loadJava/compiledJavaIncludeObjectMethods", extension = "java", testMethod = "doTestCompiledJavaIncludeObjectMethods") model("loadJava/compiledKotlin", testMethod = "doTestCompiledKotlin") model("loadJava/javaAgainstKotlin", extension = "txt", testMethod = "doTestJavaAgainstKotlin") + model("loadJava/kotlinAgainstCompiledJavaWithKotlin", extension = "kt", testMethod = "doTestKotlinAgainstCompiledJavaWithKotlin", recursive = false) model("loadJava/sourceJava", extension = "java", testMethod = "doTestSourceJava") }