[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, ".")
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
+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
|
||||
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())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user