Skip to content

Commit

Permalink
fix(spdx-utils): Fix offersChoice() for equal OR-operands
Browse files Browse the repository at this point in the history
If there is a "choice" between "a" or "a", that is not really a choice.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Dec 18, 2024
1 parent 8965839 commit a72d6b3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
8 changes: 1 addition & 7 deletions utils/spdx/src/main/kotlin/SpdxExpression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions utils/spdx/src/test/kotlin/SpdxExpressionChoiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a72d6b3

Please sign in to comment.