[Commonizer] Add getParentEntityId() method to CirEntityId
This commit is contained in:
@@ -102,9 +102,12 @@ class CirEntityId private constructor(val packageName: CirPackageName, val relat
|
|||||||
relativeNameSegments.joinTo(this, ".")
|
relativeNameSegments.joinTo(this, ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val isNestedEntity: Boolean get() = relativeNameSegments.size > 1
|
||||||
|
|
||||||
fun createNestedEntityId(entityName: CirName): CirEntityId = create(packageName, relativeNameSegments + entityName)
|
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 {
|
companion object {
|
||||||
fun create(entityId: String): CirEntityId {
|
fun create(entityId: String): CirEntityId {
|
||||||
|
|||||||
+41
-19
@@ -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
|
@Test
|
||||||
fun createNested() {
|
fun createNested() {
|
||||||
val nested1 = CirName.create("Nested1")
|
val nested1 = CirName.create("Nested1")
|
||||||
@@ -203,26 +227,24 @@ class CirEntityIdTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun isNested() {
|
fun createParent() {
|
||||||
listOf(
|
listOf(
|
||||||
"" to false,
|
"" to null,
|
||||||
"/" to false,
|
"foo/" to null,
|
||||||
"foo/" to false,
|
"foo/bar/" to null,
|
||||||
"foo/bar/" to false,
|
"Outer" to null,
|
||||||
"My" to false,
|
"foo/Outer" to null,
|
||||||
"My.Test" to true,
|
"foo/bar/Outer" to null,
|
||||||
"My.Test.Class" to true,
|
"Outer.Nested1" to "Outer",
|
||||||
"/My" to false,
|
"foo/Outer.Nested1" to "foo/Outer",
|
||||||
"/My.Test" to true,
|
"foo/bar/Outer.Nested1" to "foo/bar/Outer",
|
||||||
"/My.Test.Class" to true,
|
"Outer.Nested1.Nested2" to "Outer.Nested1",
|
||||||
"foo/My" to false,
|
"foo/Outer.Nested1.Nested2" to "foo/Outer.Nested1",
|
||||||
"foo/My.Test" to true,
|
"foo/bar/Outer.Nested1.Nested2" to "foo/bar/Outer.Nested1"
|
||||||
"foo/My.Test.Class" to true,
|
).forEach { (rawEntityId, rawParentEntityId) ->
|
||||||
"foo/bar/My" to false,
|
val entityId = CirEntityId.create(rawEntityId)
|
||||||
"foo/bar/My.Test" to true,
|
val parentEntityId = rawParentEntityId?.let(CirEntityId::create)
|
||||||
"foo/bar/My.Test.Class" to true,
|
assertSame(parentEntityId, entityId.getParentEntityId())
|
||||||
).forEach { (rawEntityId, isNested) ->
|
|
||||||
assertEquals(isNested, CirEntityId.create(rawEntityId).isNestedEntity)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user