提交 ac966ad1 编写于 作者: D Dmitriy Dolovov

[Commonizer] Add getParentEntityId() method to CirEntityId

上级 4bab505c
......@@ -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 {
......
......@@ -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())
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册