From ac966ad1d27c4000321b1ac416a431966e066171 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Wed, 10 Feb 2021 12:27:13 +0300 Subject: [PATCH] [Commonizer] Add getParentEntityId() method to CirEntityId --- .../descriptors/commonizer/cir/CirName.kt | 5 +- .../commonizer/core/CirEntityIdTest.kt | 60 +++++++++++++------ 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirName.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirName.kt index e5c697fec07..e32ac086848 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirName.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirName.kt @@ -102,9 +102,12 @@ class CirEntityId private constructor(val packageName: CirPackageName, val relat relativeNameSegments.joinTo(this, ".") } + val isNestedEntity: Boolean get() = relativeNameSegments.size > 1 + fun createNestedEntityId(entityName: CirName): CirEntityId = create(packageName, relativeNameSegments + entityName) - val isNestedEntity: Boolean get() = relativeNameSegments.size > 1 + fun getParentEntityId(): CirEntityId? = + if (isNestedEntity) create(packageName, relativeNameSegments.copyOfRange(0, relativeNameSegments.size - 1)) else null companion object { fun create(entityId: String): CirEntityId { diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/CirEntityIdTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/CirEntityIdTest.kt index 49de0701ad9..c0e93bea0f8 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/CirEntityIdTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/CirEntityIdTest.kt @@ -170,6 +170,30 @@ class CirEntityIdTest { } } + @Test + fun isNested() { + listOf( + "" to false, + "/" to false, + "foo/" to false, + "foo/bar/" to false, + "My" to false, + "My.Test" to true, + "My.Test.Class" to true, + "/My" to false, + "/My.Test" to true, + "/My.Test.Class" to true, + "foo/My" to false, + "foo/My.Test" to true, + "foo/My.Test.Class" to true, + "foo/bar/My" to false, + "foo/bar/My.Test" to true, + "foo/bar/My.Test.Class" to true, + ).forEach { (rawEntityId, isNested) -> + assertEquals(isNested, CirEntityId.create(rawEntityId).isNestedEntity) + } + } + @Test fun createNested() { val nested1 = CirName.create("Nested1") @@ -203,26 +227,24 @@ class CirEntityIdTest { } @Test - fun isNested() { + fun createParent() { listOf( - "" to false, - "/" to false, - "foo/" to false, - "foo/bar/" to false, - "My" to false, - "My.Test" to true, - "My.Test.Class" to true, - "/My" to false, - "/My.Test" to true, - "/My.Test.Class" to true, - "foo/My" to false, - "foo/My.Test" to true, - "foo/My.Test.Class" to true, - "foo/bar/My" to false, - "foo/bar/My.Test" to true, - "foo/bar/My.Test.Class" to true, - ).forEach { (rawEntityId, isNested) -> - assertEquals(isNested, CirEntityId.create(rawEntityId).isNestedEntity) + "" to null, + "foo/" to null, + "foo/bar/" to null, + "Outer" to null, + "foo/Outer" to null, + "foo/bar/Outer" to null, + "Outer.Nested1" to "Outer", + "foo/Outer.Nested1" to "foo/Outer", + "foo/bar/Outer.Nested1" to "foo/bar/Outer", + "Outer.Nested1.Nested2" to "Outer.Nested1", + "foo/Outer.Nested1.Nested2" to "foo/Outer.Nested1", + "foo/bar/Outer.Nested1.Nested2" to "foo/bar/Outer.Nested1" + ).forEach { (rawEntityId, rawParentEntityId) -> + val entityId = CirEntityId.create(rawEntityId) + val parentEntityId = rawParentEntityId?.let(CirEntityId::create) + assertSame(parentEntityId, entityId.getParentEntityId()) } } }