[Tests] Add tests for additional stub info in .knm files

KTIJ-28668
This commit is contained in:
Pavel Kirpichenkov
2024-02-15 18:56:50 +02:00
committed by Space Team
parent 604dec7a87
commit 1c1bdae973
11 changed files with 145 additions and 8 deletions
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2024 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.analysis.decompiler.konan
import com.intellij.util.indexing.FileContentImpl
import org.jetbrains.kotlin.analysis.decompiler.stub.files.extractAdditionalStubInfo
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.test.directives.model.Directive
import org.jetbrains.kotlin.test.directives.model.DirectiveApplicability
import org.jetbrains.kotlin.test.directives.model.SimpleDirectivesContainer
import java.nio.file.Path
import kotlin.io.path.name
abstract class AbstractAdditionalStubInfoKnmTest : AbstractDecompiledKnmFileTest() {
private object Directives : SimpleDirectivesContainer() {
val KNM_K2_IGNORE by directive(
description = "Ignore test for KNM files with K2 K/N Decompiler",
applicability = DirectiveApplicability.Global,
)
}
override val ignoreDirective: Directive
get() = Directives.KNM_K2_IGNORE
override fun doTest(testDirectoryPath: Path) {
val stubBuilder = K2KotlinNativeMetadataDecompiler().stubBuilder
val knmFiles = compileToKnmFiles(testDirectoryPath)
val knmFile = knmFiles.singleOrNull { "root_package" !in it.path }
?: error("Expected a single non-root .knm file, but received:${System.lineSeparator()}" +
knmFiles.joinToString(separator = System.lineSeparator()) { it.path }
)
val stub = stubBuilder.buildFileStub(FileContentImpl.createByFile(knmFile, environment.project))!!
KotlinTestUtils.assertEqualsToFile(
testDirectoryPath.resolve("${testDirectoryPath.name}.txt"),
extractAdditionalStubInfo(stub)
)
}
}
@@ -29,6 +29,14 @@ abstract class AbstractDecompiledKnmFileTest : KotlinTestWithEnvironment() {
protected abstract fun doTest(testDirectoryPath: Path)
override fun setUp() {
super.setUp()
environment.projectEnvironment.environment.registerFileType(
KlibMetaFileType, KlibMetaFileType.defaultExtension
)
}
override fun createEnvironment(): KotlinCoreEnvironment {
return KotlinCoreEnvironment.createForTests(
ApplicationEnvironmentDisposer.ROOT_DISPOSABLE,
@@ -67,14 +67,6 @@ abstract class AbstractDecompiledKnmStubConsistencyK2Test : AbstractDecompiledKn
abstract class AbstractDecompiledKnmStubConsistencyTest : AbstractDecompiledKnmFileTest() {
abstract fun createDecompiler(): KlibMetadataDecompiler<*>
override fun setUp() {
super.setUp()
environment.projectEnvironment.environment.registerFileType(
KlibMetaFileType, KlibMetaFileType.defaultExtension
)
}
override fun doTest(testDirectoryPath: Path) {
val files = compileToKnmFiles(testDirectoryPath)
@@ -0,0 +1,68 @@
/*
* Copyright 2010-2024 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.analysis.decompiler.konan;
import com.intellij.testFramework.TestDataPath;
import org.jetbrains.kotlin.test.util.KtTestUtil;
import org.jetbrains.kotlin.test.TestMetadata;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.regex.Pattern;
/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.analysis.api.GenerateAnalysisApiTestsKt}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo")
@TestDataPath("$PROJECT_ROOT")
public class AdditionalStubInfoKnmTestGenerated extends AbstractAdditionalStubInfoKnmTest {
@Test
public void testAllFilesPresentInAdditionalClsStubInfo() throws Exception {
KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo"), Pattern.compile("^([^\\.]+)$"), null, false);
}
@Test
@TestMetadata("AnnotatedFlexibleTypes")
public void testAnnotatedFlexibleTypes() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/AnnotatedFlexibleTypes/");
}
@Test
@TestMetadata("AnnotationValues")
public void testAnnotationValues() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/AnnotationValues/");
}
@Test
@TestMetadata("Contracts")
public void testContracts() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/Contracts/");
}
@Test
@TestMetadata("OuterClassesWithFlexibleArgs")
public void testOuterClassesWithFlexibleArgs() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/OuterClassesWithFlexibleArgs/");
}
@Test
@TestMetadata("ParameterName")
public void testParameterName() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/ParameterName/");
}
@Test
@TestMetadata("PropertyInitializer")
public void testPropertyInitializer() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/PropertyInitializer/");
}
@Test
@TestMetadata("TypeParametersInFlexibleTypes")
public void testTypeParametersInFlexibleTypes() throws Exception {
runTest("analysis/decompiled/decompiler-to-file-stubs/testData/additionalClsStubInfo/TypeParametersInFlexibleTypes/");
}
}
@@ -1,3 +1,6 @@
/* Java interop */
// KNM_K2_IGNORE
package test
public class AnnotatedFlexibleTypes(val javaClass: d.JavaClass) {
@@ -1,3 +1,7 @@
/* K2 is skipped because of the test data: multiple KNM parts are produced */
// KNM_K2_IGNORE
package test
import test.E.E1
@@ -1,3 +1,5 @@
/* KTIJ-28668 */
// KNM_K2_IGNORE
// JVM_FILE_NAME: ContractsKt
@file:OptIn(ExperimentalContracts::class)
@@ -1,3 +1,6 @@
/* Java interop */
// KNM_K2_IGNORE
package test
public class OuterClassesWithFlexibleArgs<K, L>(k: K, l: L) {
@@ -1,3 +1,5 @@
// KNM_K2_IGNORE
package test
import kotlin.reflect.KClass
@@ -1,3 +1,6 @@
/* Java interop */
// KNM_K2_IGNORE
package test
public class TypeParametersInFlexibleTypes<A, D>(val javaClass: d.JavaClass<A>, val t: D & Any) {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.generators.tests.analysis.api
import org.jetbrains.kotlin.analysis.decompiler.konan.AbstractAdditionalStubInfoKnmTest
import org.jetbrains.kotlin.analysis.decompiler.konan.AbstractDecompiledKnmStubConsistencyFe10Test
import org.jetbrains.kotlin.analysis.decompiler.konan.AbstractDecompiledKnmStubConsistencyK2Test
import org.jetbrains.kotlin.analysis.decompiler.psi.AbstractByDecompiledPsiStubBuilderK2CompilerTest
@@ -66,4 +67,13 @@ internal fun TestGroupSuite.generateDecompiledTests() {
model("clsFileStubBuilder", extension = null, recursive = false)
}
}
testGroup(
"analysis/decompiled/decompiler-native/tests",
"analysis/decompiled/decompiler-to-file-stubs/testData",
) {
testClass<AbstractAdditionalStubInfoKnmTest> {
model("additionalClsStubInfo", extension = null, recursive = false)
}
}
}