From 3512922899064ea7ce7ffae589d01e6cf0aa9820 Mon Sep 17 00:00:00 2001 From: Wayne Beaton Date: Mon, 8 Jun 2020 23:18:54 -0400 Subject: [PATCH] Recursively rework precedence. --- .../dash/licenses/spdx/SpdxBinaryOperation.java | 7 ++++--- .../licenses/tests/SpdxExpressionParserTests.java | 14 +++++++++++++- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/eclipse/dash/licenses/spdx/SpdxBinaryOperation.java b/src/main/java/org/eclipse/dash/licenses/spdx/SpdxBinaryOperation.java index ab9d692f..2817d6f3 100644 --- a/src/main/java/org/eclipse/dash/licenses/spdx/SpdxBinaryOperation.java +++ b/src/main/java/org/eclipse/dash/licenses/spdx/SpdxBinaryOperation.java @@ -66,6 +66,7 @@ public int getPrecedence() { @Override public boolean matchesApproved(SpdxBinaryOperation operation, Collection approved) { + // TODO Implement this. return false; } @@ -115,9 +116,9 @@ public static SpdxBinaryOperation create(Operator operator, SpdxExpression left, } public static SpdxBinaryOperation create(Operator operator, SpdxExpression left, SpdxBinaryOperation right) { - if (operator.getPrecedence() > right.operator.getPrecedence()) { - SpdxBinaryOperation primary = new SpdxBinaryOperation(operator, left, right.left); - return new SpdxBinaryOperation(right.operator, primary, right.right); + if (operator.getPrecedence() >= right.operator.getPrecedence()) { + SpdxBinaryOperation primary = create(operator, left, right.left); + return create(right.operator, primary, right.right); } return new SpdxBinaryOperation(operator, left, right); } diff --git a/src/test/java/org/eclipse/dash/licenses/tests/SpdxExpressionParserTests.java b/src/test/java/org/eclipse/dash/licenses/tests/SpdxExpressionParserTests.java index 0034e4c0..77580075 100644 --- a/src/test/java/org/eclipse/dash/licenses/tests/SpdxExpressionParserTests.java +++ b/src/test/java/org/eclipse/dash/licenses/tests/SpdxExpressionParserTests.java @@ -39,7 +39,7 @@ void testConjunction() { @Test void testMultipleConjunction() { - assertEquals("(EPL-1.0 AND (Apache-2.0 AND MIT))", + assertEquals("((EPL-1.0 AND Apache-2.0) AND MIT)", new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 AND MIT").toString()); } @@ -127,6 +127,18 @@ void testExceptionPrecedence2() { new SpdxExpressionParser().parse("EPL-1.0 WITH Exception OR MIT").toString()); } + @Test + void testExceptionPrecedence3() { + assertEquals("(((EPL-1.0 AND Apache-2.0) OR MIT) OR BSD0)", + new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 OR MIT OR BSD0").toString()); + } + + @Test + void testExceptionPrecedence4() { + assertEquals("(((EPL-1.0 AND Apache-2.0) OR (MIT WITH STUFF)) OR BSD0)", + new SpdxExpressionParser().parse("EPL-1.0 AND Apache-2.0 OR MIT WITH STUFF OR BSD0").toString()); + } + @Test void testMatchSimple() { assertTrue(new SpdxExpressionParser().parse("EPL-2.0").matchesApproved(Collections.singleton("EPL-2.0")));