diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/ParameterizedTypesCommonizationTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/ParameterizedTypesCommonizationTest.kt index 3f2a2b9f1df..e23d406df4a 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/ParameterizedTypesCommonizationTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/ParameterizedTypesCommonizationTest.kt @@ -5,6 +5,211 @@ package org.jetbrains.kotlin.commonizer.hierarchical +import org.jetbrains.kotlin.commonizer.AbstractInlineSourcesCommonizationTest +import org.jetbrains.kotlin.commonizer.assertCommonized -class ParameterizedTypesCommonizationTest { -} \ No newline at end of file + +class ParameterizedTypesCommonizationTest : AbstractInlineSourcesCommonizationTest() { + + fun `test simple parameterized class`() { + val result = commonize { + outputTarget("(a, b)") + simpleSingleSourceTarget( + "a", """ + class X + fun x1(x: X) + fun x2(x: X) + fun x3(x: X) + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + class X + fun x1(x: X) + fun x2(x: X) + fun x3(x: X) + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + expect class X() + expect fun x1(x: X) + expect fun x2(x: X) + expect fun x3(x: X) + """.trimIndent() + ) + } + + fun `test parameterized type alias - 0`() { + val result = commonize { + outputTarget("(a, b)") + + simpleSingleSourceTarget( + "a", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + fun x1(x: TA3) + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + fun x1(x: TA3) + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + expect fun x1(x: TA3) + """.trimIndent() + ) + } + + fun `test parameterized type alias - 1`() { + val result = commonize { + outputTarget("(a, b)") + + simpleSingleSourceTarget( + "a", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + fun x1(x: TA3) + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + fun x1(x: TA1) + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + typealias TA1 = Map + typealias TA2 = TA1 + typealias TA3 = TA2 + + /* + Desirable solution below! + Test ensures that no wrong commonization is emitted for now. + */ + //expect fun x1(x: TA1) + """.trimIndent() + ) + } + + fun `test parameterized type alias - 2`() { + val result = commonize { + outputTarget("(a, b)") + + simpleSingleSourceTarget( + "a", """ + class Triple + typealias T1 = Triple + typealias T2 = Triple + typealias T3 = Triple + typealias T4 = Triple + + fun x1(x: T4) + fun x2(x: T3) + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + class Triple + typealias T1 = Triple + typealias T2 = Triple + typealias T3 = Triple + typealias T4 = Triple + + fun x1(x: T3) // NOTE: T3 & T4 flipped + fun x2(x: T4) // NOTE: T3 & T4 flipped + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + expect class Triple() + typealias T1 = Triple + typealias T2 = Triple + typealias T3 = Triple + typealias T4 = Triple + + /* Note: No hard requirement whether T4 or T3 is chosen! */ + expect fun x1(x: T4) + expect fun x2(x: T4) + """.trimIndent() + ) + } + + fun `test type alias from dependencies parameterized with library source type`() { + val result = commonize { + outputTarget("(a, b)") + + registerDependency("(a, b)") { + source( + """ + class Box + typealias TA = Box + """.trimIndent() + ) + } + + simpleSingleSourceTarget( + "a", """ + class MyClass + typealias X = TA + + fun x(x: X) + fun x2(x: TA) + fun x3(x: TA) + """.trimIndent() + ) + + simpleSingleSourceTarget( + "b", """ + class MyClass + typealias X = TA + + fun x(x: X) + fun x2(x: TA) + fun x3(x: TA) + """.trimIndent() + ) + } + + result.assertCommonized( + "(a, b)", """ + expect class MyClass() + typealias X = TA + + expect fun x(x: X) + expect fun x2(x: TA) + expect fun x3(x: TA) + """.trimIndent() + ) + } +}