Implement CommonizerTarget.allLeaves

This commit is contained in:
sebastian.sellmair
2021-03-26 10:46:14 +01:00
parent a78645e858
commit 46a20c81cc
2 changed files with 27 additions and 0 deletions

View File

@@ -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) }

View File

@@ -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'"
)
}
}