Implement light classes for Kotlin scripts
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -14,63 +14,50 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.asJava;
|
||||
package org.jetbrains.kotlin.asJava
|
||||
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.kotlin.asJava.finder.JavaElementFinder;
|
||||
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava;
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment;
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind;
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils;
|
||||
import org.jetbrains.kotlin.utils.ExceptionUtilsKt;
|
||||
import com.intellij.psi.PsiClass
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import org.jetbrains.kotlin.asJava.finder.JavaElementFinder
|
||||
import org.jetbrains.kotlin.checkers.KotlinMultiFileTestWithJava
|
||||
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||
import java.io.File
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
abstract class AbstractCompilerLightClassTest : KotlinMultiFileTestWithJava<Void, Void>() {
|
||||
override fun getConfigurationKind(): ConfigurationKind = ConfigurationKind.ALL
|
||||
|
||||
public abstract class AbstractCompilerLightClassTest extends KotlinMultiFileTestWithJava<Void, Void> {
|
||||
@NotNull
|
||||
@Override
|
||||
protected ConfigurationKind getConfigurationKind() {
|
||||
return ConfigurationKind.ALL;
|
||||
override fun isKotlinSourceRootNeeded(): Boolean = true
|
||||
|
||||
override fun doMultiFileTest(file: File, modules: MutableMap<String, ModuleAndDependencies>, files: MutableList<Void>) {
|
||||
val environment = createEnvironment(file)
|
||||
val expectedFile = KotlinTestUtils.replaceExtension(file, "java")
|
||||
LightClassTestCommon.testLightClass(
|
||||
expectedFile,
|
||||
file,
|
||||
{ fqname -> findLightClass(environment, fqname) },
|
||||
LightClassTestCommon::removeEmptyDefaultImpls
|
||||
)
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isKotlinSourceRootNeeded() {
|
||||
return true;
|
||||
}
|
||||
override fun createTestModule(name: String): Void? = null
|
||||
|
||||
@NotNull
|
||||
public static JavaElementFinder createFinder(@NotNull KotlinCoreEnvironment environment) throws IOException {
|
||||
// We need to resolve all the files in order too fill in the trace that sits inside LightClassGenerationSupport
|
||||
KotlinTestUtils.resolveAllKotlinFiles(environment);
|
||||
override fun createTestFile(module: Void?, fileName: String, text: String, directives: Map<String, String>): Void? = null
|
||||
|
||||
return JavaElementFinder.getInstance(environment.getProject());
|
||||
}
|
||||
companion object {
|
||||
fun findLightClass(environment: KotlinCoreEnvironment, fqname: String): PsiClass? {
|
||||
KotlinTestUtils.resolveAllKotlinFiles(environment)
|
||||
|
||||
@Override
|
||||
protected void doMultiFileTest(File file, Map<String, ModuleAndDependencies> modules, List<Void> files) throws IOException {
|
||||
KotlinCoreEnvironment environment = createEnvironment(file);
|
||||
File expectedFile = KotlinTestUtils.replaceExtension(file, "java");
|
||||
LightClassTestCommon.INSTANCE.testLightClass(expectedFile, file, s -> {
|
||||
try {
|
||||
return createFinder(environment).findClass(s, GlobalSearchScope.allScope(environment.getProject()));
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw ExceptionUtilsKt.rethrow(e);
|
||||
}
|
||||
}, LightClassTestCommon.INSTANCE::removeEmptyDefaultImpls);
|
||||
}
|
||||
val lightCLassForScript = LightClassGenerationSupport
|
||||
.getInstance(environment.project)
|
||||
.getScriptClasses(FqName(fqname), GlobalSearchScope.allScope(environment.project))
|
||||
.firstOrNull()
|
||||
|
||||
@Override
|
||||
protected Void createTestModule(@NotNull String name) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void createTestFile(Void module, String fileName, String text, Map<String, String> directives) {
|
||||
return null;
|
||||
return lightCLassForScript ?: JavaElementFinder
|
||||
.getInstance(environment.project)
|
||||
.findClass(fqname, GlobalSearchScope.allScope(environment.project))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ import java.util.regex.Pattern;
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInLightClasses() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true, "local", "ideRegression");
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true, "local", "ideRegression");
|
||||
}
|
||||
|
||||
@TestMetadata("AnnotationClass.kt")
|
||||
@@ -155,7 +155,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class CompilationErrors extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInCompilationErrors() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/compilationErrors"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/compilationErrors"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("AnnotationModifiers.kt")
|
||||
@@ -206,7 +206,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Delegation extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInDelegation() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/delegation"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/delegation"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("Function.kt")
|
||||
@@ -220,12 +220,6 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/delegation/Property.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("WithPlatformTypes.NoCompile.kt")
|
||||
public void testWithPlatformTypes_NoCompile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/delegation/WithPlatformTypes.NoCompile.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/asJava/lightClasses/facades")
|
||||
@@ -233,13 +227,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Facades extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInFacades() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/facades"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("EmptyFile.NoCompile.kt")
|
||||
public void testEmptyFile_NoCompile() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/facades/EmptyFile.NoCompile.kt");
|
||||
doTest(fileName);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/facades"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("MultiFile.kt")
|
||||
@@ -266,7 +254,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class NullabilityAnnotations extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInNullabilityAnnotations() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/nullabilityAnnotations"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/nullabilityAnnotations"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("Class.kt")
|
||||
@@ -383,7 +371,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Object extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInObject() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/object"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/object"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("SimpleObject.kt")
|
||||
@@ -398,7 +386,7 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class PublicField extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInPublicField() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/publicField"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/publicField"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("CompanionObject.kt")
|
||||
@@ -413,4 +401,25 @@ public class CompilerLightClassTestGenerated extends AbstractCompilerLightClassT
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/asJava/lightClasses/script")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Script extends AbstractCompilerLightClassTest {
|
||||
public void testAllFilesPresentInScript() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/asJava/lightClasses/script"), Pattern.compile("^([^.]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("HelloWorld.kts")
|
||||
public void testHelloWorld() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/script/HelloWorld.kts");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("InnerClasses.kts")
|
||||
public void testInnerClasses() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/asJava/lightClasses/script/InnerClasses.kts");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,9 @@ public abstract class KotlinAsJavaTestBase extends KotlinTestWithEnvironment {
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
finder = AbstractCompilerLightClassTest.createFinder(getEnvironment());
|
||||
KotlinCoreEnvironment environment = getEnvironment();
|
||||
KotlinTestUtils.resolveAllKotlinFiles(environment);
|
||||
finder = JavaElementFinder.getInstance(environment.getProject());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user