From e33e1413d185c92b0d4b367f2c7e3eeb57f6f309 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Wed, 18 Dec 2024 17:45:19 +0100 Subject: [PATCH] fix(spdx-utils): Fix `offersChoice()` for equal `OR`-operands If there is a "choice" between "a" or "a", that is not really a choice. Signed-off-by: Sebastian Schuberth --- utils/spdx/src/main/kotlin/SpdxExpression.kt | 8 +------- utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt | 4 ++++ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/utils/spdx/src/main/kotlin/SpdxExpression.kt b/utils/spdx/src/main/kotlin/SpdxExpression.kt index 36f78ac50b482..9598150720ab3 100644 --- a/utils/spdx/src/main/kotlin/SpdxExpression.kt +++ b/utils/spdx/src/main/kotlin/SpdxExpression.kt @@ -164,7 +164,7 @@ sealed class SpdxExpression { * Return true if this expression offers a license choice. This can only be true if this expression contains the * [OR operator][SpdxOperator.OR]. */ - open fun offersChoice(): Boolean = false + fun offersChoice(): Boolean = validChoices().size > 1 /** * Apply a license [choice], optionally limited to the given [subExpression], and return an [SpdxExpression] where @@ -332,12 +332,6 @@ class SpdxCompoundExpression( } } - override fun offersChoice(): Boolean = - when (operator) { - SpdxOperator.OR -> true - SpdxOperator.AND -> children.any { it.offersChoice() } - } - override fun applyChoice(choice: SpdxExpression, subExpression: SpdxExpression): SpdxExpression { if (!subExpression.isSubExpression(choice)) { throw InvalidLicenseChoiceException( diff --git a/utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt b/utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt index 1eafc42e2ddc8..907d6a07745ea 100644 --- a/utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt +++ b/utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt @@ -286,6 +286,10 @@ class SpdxExpressionChoiceTest : WordSpec({ "a AND b".toSpdx().offersChoice() shouldBe false "a AND b AND c".toSpdx().offersChoice() shouldBe false } + + "return false if the expression contains the OR operator with equal operands" { + "a OR a".toSpdx().offersChoice() shouldBe false + } } "validChoices()" should {