[kotlin-tooling-core] linearClosure & withLinearClosure: Support loops
KT-54312
This commit is contained in:
committed by
Space Team
parent
8016225519
commit
42009431d1
+2
-2
@@ -115,7 +115,7 @@ inline fun <reified T : Any> T.linearClosure(next: (T) -> T?): Set<T> {
|
||||
while (enqueued != null) {
|
||||
if (enqueued != this && results.add(enqueued)) {
|
||||
enqueued = next(enqueued)
|
||||
}
|
||||
} else break
|
||||
}
|
||||
|
||||
return results
|
||||
@@ -134,7 +134,7 @@ inline fun <reified T : Any> T.withLinearClosure(next: (T) -> T?): Set<T> {
|
||||
while (enqueued != null) {
|
||||
if (results.add(enqueued)) {
|
||||
enqueued = next(enqueued)
|
||||
}
|
||||
} else break
|
||||
}
|
||||
|
||||
return results
|
||||
|
||||
+30
@@ -288,6 +288,21 @@ class ClosureTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `linearClosure - loop`() {
|
||||
val a = Node("a")
|
||||
val b = Node("b")
|
||||
val c = Node("c")
|
||||
|
||||
c.parent = b
|
||||
b.parent = a
|
||||
a.parent = c
|
||||
|
||||
assertEquals(
|
||||
listOf("b", "a"), c.linearClosure { it.parent }.map { it.value },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `linearClosure on empty`() {
|
||||
assertSame(
|
||||
@@ -310,6 +325,21 @@ class ClosureTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withLinearClosure - loop`() {
|
||||
val a = Node("a")
|
||||
val b = Node("b")
|
||||
val c = Node("c")
|
||||
|
||||
c.parent = b
|
||||
b.parent = a
|
||||
a.parent = c
|
||||
|
||||
assertEquals(
|
||||
listOf("c", "b", "a"), c.withLinearClosure { it.parent }.map { it.value },
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `withLinearClosure on empty`() {
|
||||
assertEquals(
|
||||
|
||||
Reference in New Issue
Block a user