Skip to content

Commit

Permalink
refactor(spdx-utils): Move tests out of SpdxExpressionParserTest
Browse files Browse the repository at this point in the history
These tests do not test the SPDX parser in the grammar sense, but the
`parse()` function of the `SpdxExpression` class. As such, move these
tests to `SpdxExpressionTest`. This also avoids confusion with

    utils/spdx/src/test/kotlin/parser/SpdxExpressionParserTest.kt

of the same name.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Feb 13, 2024
1 parent da63723 commit 5ca5643
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 206 deletions.
206 changes: 0 additions & 206 deletions utils/spdx/src/test/kotlin/SpdxExpressionParserTest.kt

This file was deleted.

179 changes: 179 additions & 0 deletions utils/spdx/src/test/kotlin/SpdxExpressionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.ossreviewtoolkit.utils.spdx.SpdxExpression.Strictness
import org.ossreviewtoolkit.utils.spdx.SpdxLicense.*
import org.ossreviewtoolkit.utils.spdx.SpdxLicenseException.*

@Suppress("LargeClass")
class SpdxExpressionTest : WordSpec({

Check warning on line 41 in utils/spdx/src/test/kotlin/SpdxExpressionTest.kt

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused symbol

Class "SpdxExpressionTest" is never used
"toString()" should {
"return the textual SPDX expression" {
Expand Down Expand Up @@ -262,6 +263,184 @@ class SpdxExpressionTest : WordSpec({
}

"The expression parser" should {
"parse a license id correctly" {
val actualExpression = "spdx.license-id".toSpdx()
val expectedExpression = SpdxLicenseIdExpression("spdx.license-id")

actualExpression shouldBe expectedExpression
}

"parse a license id starting with a digit correctly" {
val actualExpression = "0license".toSpdx()
val expectedExpression = SpdxLicenseIdExpression("0license")

actualExpression shouldBe expectedExpression
}

"parse a license id with any later version correctly" {
val actualExpression = "license+".toSpdx()
val expectedExpression = SpdxLicenseIdExpression("license", orLaterVersion = true)

actualExpression shouldBe expectedExpression
}

"parse a document ref correctly" {
val actualExpression = "DocumentRef-document:LicenseRef-license".toSpdx()
val expectedExpression = SpdxLicenseReferenceExpression("DocumentRef-document:LicenseRef-license")

actualExpression shouldBe expectedExpression
}

"parse a license ref correctly" {
val actualExpression = "LicenseRef-license".toSpdx()
val expectedExpression = SpdxLicenseReferenceExpression("LicenseRef-license")

actualExpression shouldBe expectedExpression
}

"parse a complex expression correctly" {
val actualExpression = SpdxExpression.parse(
"license1+ and ((license2 with exception1) OR license3+ AND license4 WITH exception2)"
)
val expectedExpression = SpdxCompoundExpression(
SpdxLicenseIdExpression("license1", orLaterVersion = true),
SpdxOperator.AND,
SpdxCompoundExpression(
SpdxLicenseWithExceptionExpression(
SpdxLicenseIdExpression("license2"),
"exception1"
),
SpdxOperator.OR,
SpdxCompoundExpression(
SpdxLicenseIdExpression("license3", orLaterVersion = true),
SpdxOperator.AND,
SpdxLicenseWithExceptionExpression(
SpdxLicenseIdExpression("license4"),
"exception2"
)
)
)
)

actualExpression shouldBe expectedExpression
}

"bind + stronger than WITH" {
val actualExpression = "license+ WITH exception".toSpdx()
val expectedExpression = SpdxLicenseWithExceptionExpression(
SpdxLicenseIdExpression("license", orLaterVersion = true),
"exception"
)

actualExpression shouldBe expectedExpression
}

"bind WITH stronger than AND" {
val actualExpression = "license1 AND license2 WITH exception".toSpdx()
val expectedExpression = SpdxCompoundExpression(
SpdxLicenseIdExpression("license1"),
SpdxOperator.AND,
SpdxLicenseWithExceptionExpression(
SpdxLicenseIdExpression("license2"),
"exception"
)
)

actualExpression shouldBe expectedExpression
}

"bind AND stronger than OR" {
val actualExpression = "license1 OR license2 AND license3".toSpdx()
val expectedExpression = SpdxCompoundExpression(
SpdxLicenseIdExpression("license1"),
SpdxOperator.OR,
SpdxCompoundExpression(
SpdxLicenseIdExpression("license2"),
SpdxOperator.AND,
SpdxLicenseIdExpression("license3")
)
)

actualExpression shouldBe expectedExpression
}

"bind the and operator left associative" {
val actualExpression = "license1 AND license2 AND license3".toSpdx()
val expectedExpression = SpdxCompoundExpression(
SpdxCompoundExpression(
SpdxLicenseIdExpression("license1"),
SpdxOperator.AND,
SpdxLicenseIdExpression("license2")
),
SpdxOperator.AND,
SpdxLicenseIdExpression("license3")
)

actualExpression shouldBe expectedExpression
}

"bind the or operator left associative" {
val actualExpression = "license1 OR license2 OR license3".toSpdx()
val expectedExpression = SpdxCompoundExpression(
SpdxCompoundExpression(
SpdxLicenseIdExpression("license1"),
SpdxOperator.OR,
SpdxLicenseIdExpression("license2")
),
SpdxOperator.OR,
SpdxLicenseIdExpression("license3")
)

actualExpression shouldBe expectedExpression
}

"respect parentheses for binding strength of operators" {
val actualExpression = "(license1 OR license2) AND license3".toSpdx()
val expectedExpression = SpdxCompoundExpression(
SpdxCompoundExpression(
SpdxLicenseIdExpression("license1"),
SpdxOperator.OR,
SpdxLicenseIdExpression("license2")
),
SpdxOperator.AND,
SpdxLicenseIdExpression("license3")
)

actualExpression shouldBe expectedExpression
}

"fail if + is used in an exception expression" {
val exception = shouldThrow<SpdxException> {
"license WITH exception+".toSpdx()
}

exception.message shouldBe "Unexpected token 'PLUS(position=23)'."
}

"fail if a compound expression is used before WITH" {
val exception = shouldThrow<SpdxException> {
"(license1 AND license2) WITH exception".toSpdx()
}

exception.message shouldBe "Unexpected token 'WITH(position=25)'."
}

"fail on an invalid symbol" {
val exception = shouldThrow<SpdxException> {
"/".toSpdx()
}

exception.message shouldBe "Unexpected character '/' at position 1."
}

"fail on a syntax error" {
val exception = shouldThrow<SpdxException> {
"((".toSpdx()
}

exception.message shouldBe "Unexpected token 'null'."
}

"work for deprecated license identifiers" {
assertSoftly {
"eCos-2.0".toSpdx() shouldBe SpdxLicenseIdExpression("eCos-2.0")
Expand Down

0 comments on commit 5ca5643

Please sign in to comment.