Implement CommonizerTarget isAncestorOf and isDescendentOf

This commit is contained in:
sebastian.sellmair
2021-03-26 10:05:17 +01:00
parent 108debdcc9
commit 35e1f8a520
3 changed files with 45 additions and 6 deletions
@@ -10,10 +10,7 @@ import org.gradle.api.file.FileCollection
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.*
import org.gradle.workers.WorkParameters
import org.jetbrains.kotlin.commonizer.CommonizerTarget
import org.jetbrains.kotlin.commonizer.NativeDistributionCommonizerOutputLayout
import org.jetbrains.kotlin.commonizer.SharedCommonizerTarget
import org.jetbrains.kotlin.commonizer.level
import org.jetbrains.kotlin.commonizer.*
import org.jetbrains.kotlin.compilerRunner.GradleCliCommonizer
import org.jetbrains.kotlin.compilerRunner.konanHome
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtensionOrNull
@@ -210,8 +207,7 @@ private fun removeRedundantParameters(parameters: Set<CInteropCommonizationParam
private operator fun CommonizerTarget.contains(other: CommonizerTarget): Boolean {
if (this == other) return true
if (this !is SharedCommonizerTarget) return false
return targets.any { child -> other in child }
return this.isAncestorOf(other)
}
private fun SharedCommonizerTarget.withAllTransitiveTargets(): Set<CommonizerTarget> {
@@ -100,3 +100,15 @@ public val CommonizerTarget.level: Int
is SharedCommonizerTarget -> if (targets.isNotEmpty()) targets.maxOf { it.level } + 1 else 0
}
}
public infix fun CommonizerTarget.isAncestorOf(other: CommonizerTarget): Boolean {
if (this is SharedCommonizerTarget) {
return targets.any { it == other } || targets.any { it.isAncestorOf(other) }
}
return false
}
public infix fun CommonizerTarget.isDescendentOf(other: CommonizerTarget): Boolean {
return other.isAncestorOf(this)
}
@@ -0,0 +1,31 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.commonizer
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class CommonizerTargetUtilsTest {
@Test
fun `isAncestorOf isDescendentOf`() {
val child = LeafCommonizerTarget("a")
val parent = SharedCommonizerTarget(LeafCommonizerTarget("a"))
assertTrue(child isDescendentOf parent, "Expected child isDescendent of parent")
assertTrue(parent isAncestorOf child, "Expected parent isAncestor of child")
assertFalse(child isDescendentOf child, "Expected same target not being descendent of itself")
assertFalse(parent isDescendentOf parent, "Expected same target not being descendent of itself")
assertFalse(LeafCommonizerTarget("b") isDescendentOf parent, "Expected orphan target not being descendent of parent")
val hierarchicalParent = SharedCommonizerTarget(parent, parseCommonizerTarget("((c, d), e)"))
assertTrue(child isDescendentOf hierarchicalParent, "Expected child being descendent of hierarchical parent")
assertTrue(hierarchicalParent isAncestorOf child, "Expected hierarchicalParent being ancestor of child")
assertTrue(parseCommonizerTarget("(c, d)") isDescendentOf hierarchicalParent)
assertTrue(LeafCommonizerTarget("e") isDescendentOf hierarchicalParent)
}
}