From 32bea765f0f8b09c7614a54e7e02dc2ddb240443 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Mon, 18 Nov 2019 16:14:47 +0700 Subject: [PATCH] [Interop] Add tests for StubIr to metadata translation. --- .../interop/gen/StubIrToMetadataTests.kt | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Interop/StubGenerator/src/test/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrToMetadataTests.kt diff --git a/Interop/StubGenerator/src/test/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrToMetadataTests.kt b/Interop/StubGenerator/src/test/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrToMetadataTests.kt new file mode 100644 index 00000000000..a4e4fe8409e --- /dev/null +++ b/Interop/StubGenerator/src/test/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrToMetadataTests.kt @@ -0,0 +1,104 @@ +package org.jetbrains.kotlin.native.interop.gen + +import kotlinx.metadata.KmAnnotationArgument +import kotlinx.metadata.KmClassifier +import kotlinx.metadata.KmModuleFragment +import kotlinx.metadata.klib.compileTimeValue +import kotlinx.metadata.klib.uniqId +import org.jetbrains.kotlin.native.interop.indexer.FunctionDecl +import org.jetbrains.kotlin.native.interop.indexer.IntegerConstantDef +import org.jetbrains.kotlin.native.interop.indexer.IntegerType +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class StubIrToMetadataTests { + + companion object { + val intStubType = ClassifierStubType(Classifier.topLevel("kotlin", "Int")) + val intType = IntegerType(4, true, "int") + } + + private fun createTrivialFunction(name: String): FunctionStub { + val cDeclaration = FunctionDecl(name, emptyList(), intType, "", false, false) + val origin = StubOrigin.Function(cDeclaration) + return FunctionStub( + name = cDeclaration.name, + returnType = intStubType, + parameters = listOf(), + origin = origin, + annotations = emptyList(), + external = true, + receiver = null, + modality = MemberStubModality.FINAL + ) + } + + private fun createTrivialIntegerConstantProperty(name: String, value: Long): PropertyStub { + val origin = StubOrigin.Constant(IntegerConstantDef(name, intType, value)) + return PropertyStub( + name = name, + type = intStubType, + kind = PropertyStub.Kind.Constant(IntegralConstantStub(value, intType.size, true)), + origin = origin + ) + } + + private fun createMetadata( + fqName: String, + functions: List = emptyList(), + properties: List = emptyList() + ) = ModuleMetadataEmitter(fqName).let { + val stubs = SimpleStubContainer(functions = functions, properties = properties) + stubs.accept(it, null) + it.writeModule() + }.also(this::checkUniqIdPresence) + + private fun checkUniqIdPresence(metadata: KmModuleFragment) { + metadata.classes.forEach { assertNotNull(it.uniqId) } + metadata.pkg?.let { pkg -> + pkg.functions.forEach { assertNotNull(it.uniqId) } + pkg.properties.forEach { assertNotNull(it.uniqId) } + pkg.typeAliases.forEach { assertNotNull(it.uniqId) } + } + } + + @Test + fun `single simple function`() { + val packageName = "single_function" + val function = createTrivialFunction("hello") + val metadata = createMetadata(packageName, functions = listOf(function)) + with (metadata) { + assertEquals(packageName, packageName) + assertTrue(classes.isEmpty()) + assertNotNull(pkg) + assertTrue(pkg!!.functions.size == 1) + + val kmFunction = pkg!!.functions[0] + assertEquals(kmFunction.name, function.name) + assertEquals(0, kmFunction.valueParameters.size) + val returnTypeClassifier = kmFunction.returnType.classifier + assertTrue(returnTypeClassifier is KmClassifier.Class) + assertEquals("kotlin/Int", returnTypeClassifier.name) + } + } + + @Test + fun `single constant`() { + val property = createTrivialIntegerConstantProperty("meaning", 42) + val metadata = createMetadata("single_property", properties = listOf(property)) + with (metadata) { + assertNotNull(pkg) + assertTrue(pkg!!.properties.size == 1) + + val kmProperty = pkg!!.properties[0] + assertEquals(kmProperty.name, property.name) + + val compileTimeValue = kmProperty.compileTimeValue + assertNotNull(compileTimeValue) + assertTrue(compileTimeValue is KmAnnotationArgument.IntValue) + assertEquals(42, compileTimeValue.value) + } + } +} \ No newline at end of file