Remove LocalClassProtoTestGenerated, replace with kotlinp tests

This test uses a hacky mode of the compiler which is not worth it to
support further (especially in K2), `USE_SINGLE_MODULE`, where
everything is compiled in one module. The purpose of the test is just to
check that metadata for local/anonymous classes is written correctly.
So we can replace it with the tests on kotlinp, which uses
kotlinx-metadata-jvm and dumps all loaded metadata to text.

This replacement is not perfect, in particular because it won't check
that the reflection machinery is able to load this metadata, and because
it won't check that annotations are loaded correctly from the bytecode.
But IMHO it's good enough, there are box tests on reflection on local
classes (e.g. `reflection/annotations/localClassLiteral.kt`), so this
way is better than having to support the weird compiler mode for just
one test.
This commit is contained in:
Alexander Udalov
2022-08-26 13:30:56 +02:00
parent 7bf6d64cfb
commit abaffeddab
24 changed files with 178 additions and 263 deletions
@@ -1,25 +0,0 @@
// CLASS_NAME_SUFFIX: A$foo$Local
import kotlin.reflect.KClass
class A {
annotation class Ann(val info: String)
annotation class Bnn(val klass: KClass<*>)
fun foo() {
@Ann("class")
class Local {
@Ann("fun")
fun foo(): Local = this
@field:Ann("val")
@Bnn(Local::class)
val x = foo()
@Ann("inner")
@Bnn(Array<Array<out Local>>::class)
inner class Inner
}
}
}
@@ -1,9 +0,0 @@
@A.Ann(info = "class") local final class `A$foo$Local`
public constructor `A$foo$Local`()
@A.Bnn(klass = A$foo$Local::class) @field:A.Ann(info = "val") public final val x: `A$foo$Local`
@A.Ann(info = "fun") public final fun foo(): `A$foo$Local`
@A.Ann(info = "inner") @A.Bnn(klass = kotlin.Array<kotlin.Array<A$foo$Local>>::class) local final inner class Inner {
public constructor Inner()
}
@@ -1,5 +0,0 @@
// CLASS_NAME_SUFFIX: $main$1
fun main() = object : Runnable {
override fun run() { }
}
@@ -1,3 +0,0 @@
local final class `AnonymousObjectKt$main$1` : java.lang.Runnable
public open override /*1*/ fun run(): kotlin.Unit
@@ -1,7 +0,0 @@
local final inner class Deepest
public constructor Deepest()
public final fun deep(): test.`DeepInnerChainKt$main$Local$Inner$prop$1$foo$1$DeepLocal`
public final fun deepest(): test.`DeepInnerChainKt$main$Local$Inner$prop$1$foo$1$DeepLocal`.Deepest?
public final fun inner(): test.`DeepInnerChainKt$main$Local`.Inner
public final fun local(): test.`DeepInnerChainKt$main$Local`
-10
View File
@@ -1,10 +0,0 @@
// CLASS_NAME_SUFFIX: main$Local$Inner
fun main() {
class Local {
inner class Inner {
fun local(l: Local) {}
fun inner(i: Inner) {}
}
}
}
@@ -1,5 +0,0 @@
local final inner class Inner
public constructor Inner()
public final fun inner(/*0*/ i: `InnerOfLocalKt$main$Local`.Inner): kotlin.Unit
public final fun local(/*0*/ l: `InnerOfLocalKt$main$Local`): kotlin.Unit
@@ -1,7 +0,0 @@
local open class `LocalClassInSignatureKt$main$Local`
public constructor `LocalClassInSignatureKt$main$Local`()
public final val returnType: `LocalClassInSignatureKt$main$Local`
public final fun </*0*/ T : `LocalClassInSignatureKt$main$Local`, /*1*/ U : T> generic(/*0*/ t: T): U
public final fun param(/*0*/ l: `LocalClassInSignatureKt$main$Local`): kotlin.Unit
public final fun `LocalClassInSignatureKt$main$Local`.receiver(): `LocalClassInSignatureKt$main$Local`
@@ -1,7 +0,0 @@
// CLASS_NAME_SUFFIX: A$foo$Local
class A {
fun foo() {
class Local
}
}
@@ -1,3 +0,0 @@
local final class `A$foo$Local`
public constructor `A$foo$Local`()
@@ -1,5 +0,0 @@
// CLASS_NAME_SUFFIX: $main$Local
fun main() {
class Local
}
@@ -1,3 +0,0 @@
local final class `SimpleInTopLevelFunctionKt$main$Local`
public constructor `SimpleInTopLevelFunctionKt$main$Local`()
@@ -1,90 +0,0 @@
/*
* Copyright 2010-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.serialization
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.container.get
import org.jetbrains.kotlin.jvm.compiler.LoadDescriptorUtil
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.kotlin.DeserializationComponentsForJava
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil
import org.jetbrains.kotlin.test.*
import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator
import org.jetbrains.kotlin.test.util.RecursiveDescriptorComparatorAdaptor
import java.io.File
import java.net.URLClassLoader
abstract class AbstractLocalClassProtoTest : TestCaseWithTmpdir() {
protected fun doTest(filename: String) {
val source = File(filename)
KotlinTestUtils.createEnvironmentWithMockJdkAndIdeaAnnotations(testRootDisposable).let { environment ->
LoadDescriptorUtil.compileKotlinToDirAndGetModule(listOf(source), tmpdir, environment)
}
val classNameSuffix = InTextDirectivesUtils.findStringWithPrefixes(source.readText(), "// CLASS_NAME_SUFFIX: ")
?: error("CLASS_NAME_SUFFIX directive not found in test data")
val classLoader = URLClassLoader(arrayOf(tmpdir.toURI().toURL()), ForTestCompileRuntime.runtimeAndReflectJarClassLoader())
val classFile = tmpdir.walkTopDown().singleOrNull { it.path.endsWith("$classNameSuffix.class") }
?: error("Local class with suffix `$classNameSuffix` is not found in: ${tmpdir.listFiles().toList()}")
val clazz = classLoader.loadClass(classFile.toRelativeString(tmpdir).substringBeforeLast(".class").replace('/', '.').replace('\\', '.'))
assertHasAnnotationData(clazz)
val configuration = KotlinTestUtils.newConfiguration(ConfigurationKind.ALL, TestJdkKind.MOCK_JDK, tmpdir).apply {
// This is needed because otherwise local classes, being binary classes, are not resolved via source module's container.
// They could only be resolved via the container of the _dependency_ module.
// This can be improved as soon as there's an API to get the container of the dependency module.
put(JVMConfigurationKeys.USE_SINGLE_MODULE, true)
}
val environment = KotlinCoreEnvironment.createForTests(testRootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
val components = JvmResolveUtil.createContainer(environment).get<DeserializationComponentsForJava>().components
val classDescriptor = components.classDeserializer.deserializeClass(clazz.classId)
?: error("Class is not resolved: $clazz (classId = ${clazz.classId})")
RecursiveDescriptorComparatorAdaptor.validateAndCompareDescriptorWithFile(
classDescriptor,
RecursiveDescriptorComparator.DONT_INCLUDE_METHODS_OF_OBJECT,
KotlinTestUtils.replaceExtension(source, "txt")
)
}
@Suppress("UNCHECKED_CAST")
private fun assertHasAnnotationData(clazz: Class<*>) {
checkNotNull(clazz.getAnnotation(
clazz.classLoader.loadClass(JvmAnnotationNames.METADATA_FQ_NAME.asString()) as Class<Annotation>
)) { "Metadata annotation is not found for class $clazz" }
}
private val Class<*>.classId: ClassId
get() = when {
enclosingMethod != null || enclosingConstructor != null || simpleName.isEmpty() -> {
val fqName = FqName(name)
ClassId(fqName.parent(), FqName.topLevel(fqName.shortName()), true)
}
else -> declaringClass?.classId?.createNestedClassId(Name.identifier(simpleName)) ?: ClassId.topLevel(FqName(name))
}
}
@@ -47,7 +47,6 @@ import org.jetbrains.kotlin.repl.AbstractReplInterpreterTest
import org.jetbrains.kotlin.resolve.AbstractResolveTest
import org.jetbrains.kotlin.resolve.calls.AbstractResolvedCallsTest
import org.jetbrains.kotlin.resolve.calls.AbstractResolvedConstructorDelegationCallsTests
import org.jetbrains.kotlin.serialization.AbstractLocalClassProtoTest
import org.jetbrains.kotlin.test.TargetBackend
import org.jetbrains.kotlin.types.AbstractTypeBindingTest
@@ -286,10 +285,6 @@ fun generateJUnit3CompilerTests(args: Array<String>) {
model("type/binding")
}
testClass<AbstractLocalClassProtoTest> {
model("serialization/local")
}
testClass<AbstractKDocLexerTest> {
model("lexer/kdoc")
}
@@ -1,66 +0,0 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.serialization;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.JUnit3RunnerWithInners;
import org.jetbrains.kotlin.test.KotlinTestUtils;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.runner.RunWith;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/serialization/local")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public class LocalClassProtoTestGenerated extends AbstractLocalClassProtoTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInLocal() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/serialization/local"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("annotationsInLocalClass.kt")
public void testAnnotationsInLocalClass() throws Exception {
runTest("compiler/testData/serialization/local/annotationsInLocalClass.kt");
}
@TestMetadata("anonymousObject.kt")
public void testAnonymousObject() throws Exception {
runTest("compiler/testData/serialization/local/anonymousObject.kt");
}
@TestMetadata("deepInnerChain.kt")
public void testDeepInnerChain() throws Exception {
runTest("compiler/testData/serialization/local/deepInnerChain.kt");
}
@TestMetadata("innerOfLocal.kt")
public void testInnerOfLocal() throws Exception {
runTest("compiler/testData/serialization/local/innerOfLocal.kt");
}
@TestMetadata("localClassInSignature.kt")
public void testLocalClassInSignature() throws Exception {
runTest("compiler/testData/serialization/local/localClassInSignature.kt");
}
@TestMetadata("simpleInMemberFunction.kt")
public void testSimpleInMemberFunction() throws Exception {
runTest("compiler/testData/serialization/local/simpleInMemberFunction.kt");
}
@TestMetadata("simpleInTopLevelFunction.kt")
public void testSimpleInTopLevelFunction() throws Exception {
runTest("compiler/testData/serialization/local/simpleInTopLevelFunction.kt");
}
}
@@ -59,11 +59,6 @@ public class KotlinpTestGenerated extends AbstractKotlinpTest {
runTest("libraries/tools/kotlinp/testData/Lambda.kt");
}
@TestMetadata("LocalClass.kt")
public void testLocalClass() throws Exception {
runTest("libraries/tools/kotlinp/testData/LocalClass.kt");
}
@TestMetadata("LocalDelegatedProperties.kt")
public void testLocalDelegatedProperties() throws Exception {
runTest("libraries/tools/kotlinp/testData/LocalDelegatedProperties.kt");
@@ -156,4 +151,37 @@ public class KotlinpTestGenerated extends AbstractKotlinpTest {
runTest("libraries/tools/kotlinp/testData/jvmDefault/withoutCompatibility.kt");
}
}
@TestMetadata("libraries/tools/kotlinp/testData/localClasses")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class LocalClasses extends AbstractKotlinpTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, this, testDataFilePath);
}
public void testAllFilesPresentInLocalClasses() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("libraries/tools/kotlinp/testData/localClasses"), Pattern.compile("^(.+)\\.kt$"), null, true);
}
@TestMetadata("AnonymousObject.kt")
public void testAnonymousObject() throws Exception {
runTest("libraries/tools/kotlinp/testData/localClasses/AnonymousObject.kt");
}
@TestMetadata("DeepInnerLocalChain.kt")
public void testDeepInnerLocalChain() throws Exception {
runTest("libraries/tools/kotlinp/testData/localClasses/DeepInnerLocalChain.kt");
}
@TestMetadata("LocalClassInConstructor.kt")
public void testLocalClassInConstructor() throws Exception {
runTest("libraries/tools/kotlinp/testData/localClasses/LocalClassInConstructor.kt");
}
@TestMetadata("LocalClassInSignature.kt")
public void testLocalClassInSignature() throws Exception {
runTest("libraries/tools/kotlinp/testData/localClasses/LocalClassInSignature.kt");
}
}
}
@@ -0,0 +1,3 @@
fun test() = object : Runnable {
override fun run() { }
}
@@ -0,0 +1,23 @@
// AnonymousObjectKt.class
// ------------------------------------------
package {
// signature: test()Ljava/lang/Runnable;
public final fun test(): java/lang/Runnable
}
// AnonymousObjectKt$test$1.class
// ------------------------------------------
local final class .AnonymousObjectKt$test$1 : java/lang/Runnable {
// signature: run()V
public open fun run(): kotlin/Unit
// module name: test-module
}
// META-INF/test-module.kotlin_module
// ------------------------------------------
module {
package <root> {
AnonymousObjectKt
}
}
@@ -1,8 +1,4 @@
package test
// CLASS_NAME_SUFFIX: Deepest
fun main() {
fun test() {
class Local {
inner class Inner {
val prop = object {
@@ -0,0 +1,80 @@
// DeepInnerLocalChainKt.class
// ------------------------------------------
package {
// signature: test()V
public final fun test(): kotlin/Unit
}
// DeepInnerLocalChainKt$test$Local.class
// ------------------------------------------
local final class .DeepInnerLocalChainKt$test$Local : kotlin/Any {
// signature: <init>()V
public constructor()
// nested class: Inner
// module name: test-module
}
// DeepInnerLocalChainKt$test$Local$Inner.class
// ------------------------------------------
local final inner class .DeepInnerLocalChainKt$test$Local.Inner : kotlin/Any {
// signature: <init>(LDeepInnerLocalChainKt$test$Local;)V
public constructor()
// field: prop:LDeepInnerLocalChainKt$test$Local$Inner$prop$1;
// getter: getProp()LDeepInnerLocalChainKt$test$Local$Inner$prop$1;
public final val prop: .DeepInnerLocalChainKt$test$Local$Inner$prop$1
public final get
// module name: test-module
}
// DeepInnerLocalChainKt$test$Local$Inner$prop$1.class
// ------------------------------------------
local final class .DeepInnerLocalChainKt$test$Local$Inner$prop$1 : kotlin/Any {
// signature: foo()V
public final fun foo(): kotlin/Unit
// module name: test-module
}
// DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal.class
// ------------------------------------------
local final class .DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal : kotlin/Any {
// signature: <init>(LDeepInnerLocalChainKt$test$Local;)V
public constructor()
// nested class: Deepest
// module name: test-module
}
// DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal$Deepest.class
// ------------------------------------------
local final inner class .DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal.Deepest : kotlin/Any {
// signature: <init>(LDeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal;)V
public constructor()
// signature: deep()LDeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal;
public final fun deep(): .DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal
// signature: deepest()LDeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal$Deepest;
public final fun deepest(): .DeepInnerLocalChainKt$test$Local$Inner$prop$1$foo$bar$DeepLocal.Deepest?
// signature: inner()LDeepInnerLocalChainKt$test$Local$Inner;
public final fun inner(): .DeepInnerLocalChainKt$test$Local.Inner
// signature: local()LDeepInnerLocalChainKt$test$Local;
public final fun local(): .DeepInnerLocalChainKt$test$Local
// module name: test-module
}
// META-INF/test-module.kotlin_module
// ------------------------------------------
module {
package <root> {
DeepInnerLocalChainKt
}
}
@@ -1,6 +1,4 @@
// CLASS_NAME_SUFFIX: $main$Local
fun main() {
fun test() {
open class Local {
fun param(l: Local) {}
@@ -0,0 +1,37 @@
// LocalClassInSignatureKt.class
// ------------------------------------------
package {
// signature: test()V
public final fun test(): kotlin/Unit
}
// LocalClassInSignatureKt$test$Local.class
// ------------------------------------------
local open class .LocalClassInSignatureKt$test$Local : kotlin/Any {
// signature: <init>()V
public constructor()
// signature: generic(LLocalClassInSignatureKt$test$Local;)LLocalClassInSignatureKt$test$Local;
public final fun <T#0 /* T */ : .LocalClassInSignatureKt$test$Local, T#1 /* U */ : T#0> generic(t: T#0): T#1
// signature: param(LLocalClassInSignatureKt$test$Local;)V
public final fun param(l: .LocalClassInSignatureKt$test$Local): kotlin/Unit
// signature: receiver(LLocalClassInSignatureKt$test$Local;)LLocalClassInSignatureKt$test$Local;
public final fun .LocalClassInSignatureKt$test$Local.receiver(): .LocalClassInSignatureKt$test$Local
// field: returnType:LLocalClassInSignatureKt$test$Local;
// getter: getReturnType()LLocalClassInSignatureKt$test$Local;
public final val returnType: .LocalClassInSignatureKt$test$Local
public final get
// module name: test-module
}
// META-INF/test-module.kotlin_module
// ------------------------------------------
module {
package <root> {
LocalClassInSignatureKt
}
}