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
This commit is contained in:
Alexander Udalov
2014-03-18 17:04:55 +04:00
committed by Andrey Breslav
parent 291741754b
commit 888a6c0c9b
8 changed files with 117 additions and 18 deletions
@@ -0,0 +1,8 @@
package test
trait B {
fun foo(kotlinName: Int)
}
class ZAB : A, B
class ZBA : B, A
@@ -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
}
@@ -0,0 +1,5 @@
package test;
public interface A {
void foo(int javaName);
}
@@ -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<File> javaFiles,
@NotNull List<File> 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()
));
}
}
@@ -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<String> 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());
}
}
@@ -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.<PsiFile>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);
@@ -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;
}
@@ -194,6 +194,7 @@ fun main(args: Array<String>) {
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")
}