mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-03-10 08:31:29 +00:00
Implement CommonizerTarget.allLeaves
This commit is contained in:
@@ -111,6 +111,10 @@ public fun CommonizerTarget.withAllAncestors(): Set<CommonizerTarget> {
|
||||
}
|
||||
}
|
||||
|
||||
public fun CommonizerTarget.allLeaves(): Set<LeafCommonizerTarget> {
|
||||
return withAllAncestors().filterIsInstance<LeafCommonizerTarget>().toSet()
|
||||
}
|
||||
|
||||
public infix fun CommonizerTarget.isAncestorOf(other: CommonizerTarget): Boolean {
|
||||
if (this is SharedCommonizerTarget) {
|
||||
return targets.any { it == other } || targets.any { it.isAncestorOf(other) }
|
||||
|
||||
@@ -53,5 +53,28 @@ class CommonizerTargetUtilsTest {
|
||||
"Expected all targets present"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allLeaves() {
|
||||
val target = parseCommonizerTarget("((a, b), (c, d), (e, (f, g)))")
|
||||
assertEquals(
|
||||
setOf(
|
||||
LeafCommonizerTarget("a"),
|
||||
LeafCommonizerTarget("b"),
|
||||
LeafCommonizerTarget("c"),
|
||||
LeafCommonizerTarget("d"),
|
||||
LeafCommonizerTarget("e"),
|
||||
LeafCommonizerTarget("f"),
|
||||
LeafCommonizerTarget("g")
|
||||
),
|
||||
target.allLeaves(),
|
||||
"Expected leaf targets present"
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
setOf(LeafCommonizerTarget("a")), LeafCommonizerTarget("a").allLeaves(),
|
||||
"Expected LeafCommonizerTarget returns itself in 'allLeaves'"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user