From 87ac436a351c169e4bdcafef7e7f2f57ce0d5429 Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Wed, 7 Jul 2021 09:51:50 +0200 Subject: [PATCH] [Commonizer] Implement HierarchicalCInteropCallableAnnotationCommonizationTest --- ...eropCallableAnnotationCommonizationTest.kt | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalCInteropCallableAnnotationCommonizationTest.kt diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalCInteropCallableAnnotationCommonizationTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalCInteropCallableAnnotationCommonizationTest.kt new file mode 100644 index 00000000000..06b5b263a18 --- /dev/null +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalCInteropCallableAnnotationCommonizationTest.kt @@ -0,0 +1,202 @@ +/* + * Copyright 2010-2021 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.commonizer.hierarchical + +import org.jetbrains.kotlin.commonizer.AbstractInlineSourcesCommonizationTest +import org.jetbrains.kotlin.commonizer.assertCommonized +import org.jetbrains.kotlin.commonizer.utils.InlineSourceBuilder +import org.junit.Test + +class HierarchicalCInteropCallableAnnotationCommonizationTest : AbstractInlineSourcesCommonizationTest() { + + @Test + fun `test ObjCMethod annotation`() { + val result = commonize { + outputTarget("(a, b)", "(c, d)", "(a, b, c, d)") + registerDependency("a", "b", "b", "c", "d", "(a, b)", "(c, d)", "(a, b, c, d)") { + objCAnnotations() + } + + simpleSingleSourceTarget( + "a", """ + import kotlinx.cinterop.* + @ObjCMethod(0) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + import kotlinx.cinterop.* + @ObjCMethod(1) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "c", """ + import kotlinx.cinterop.* + @ObjCMethod(2) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "d", """ + import kotlinx.cinterop.* + @ObjCMethod(2) + fun x() + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + + result.assertCommonized( + "(c, d)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + + result.assertCommonized( + "(a, b, c, d)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + } + + @Test + fun `test only ObjCCallable annotation`() { + val result = commonize { + outputTarget("(a, b)") + registerDependency("a", "b", "(a, b)") { + objCAnnotations() + } + + simpleSingleSourceTarget( + "a", """ + import kotlin.commonizer.* + @ObjCCallable + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + import kotlin.commonizer.* + @ObjCCallable + fun x() + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + } + + @Test + fun `test multiple objC annotations`() { + val result = commonize { + outputTarget("(a, b)", "(c, d)", "(a, b, c, d)") + registerDependency("a", "b", "b", "c", "d", "(a, b)", "(c, d)", "(a, b, c, d)") { + objCAnnotations() + } + + simpleSingleSourceTarget( + "a", """ + import kotlinx.cinterop.* + @ObjCMethod(0) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + import kotlinx.cinterop.* + @ObjCConstructor(1) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "c", """ + import kotlinx.cinterop.* + @ObjCFactory(2) + fun x() + """.trimIndent() + ) + + simpleSingleSourceTarget( + "d", """ + import kotlin.commonizer.* + @ObjCCallable(2) + fun x() + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + + result.assertCommonized( + "(c, d)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + + result.assertCommonized( + "(a, b, c, d)", """ + import kotlin.commonizer.* + @ObjCCallable + expect fun x() + """.trimIndent() + ) + } + +} + +private fun InlineSourceBuilder.ModuleBuilder.objCAnnotations() { + // Register fake kotlinx.cinterop annotations. + // Annotations contain a parameter x which is used to make them "not commonizable" + source( + """ + package kotlinx.cinterop + annotation class ObjCMethod(val x: Int) + annotation class ObjCConstructor(val x: Int) + annotation class ObjCFactory(val x: Int) + """.trimIndent(), + "kotlinx.kt" + ) + + source( + """ + package kotlin.commonizer + annotation class ObjCCallable + """.trimIndent() + ) +} \ No newline at end of file