Skip to content

Commit 819554f

Browse files
Add tests and simplify comments
Signed-off-by: Ayan Sinha Mahapatra <asmahapatra@aboutcode.org>
1 parent ca2a42a commit 819554f

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/license_expression/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,12 @@ def dedup(self, expression):
716716
717717
The deduplication:
718718
719+
- Performs the deduplication recusively for all sub-expressions.
720+
719721
- Does not sort the licenses of sub-expression in an expression. They
720-
stay in the same order as in the original expression.
722+
stay in the same order as in the original expression. In case of two
723+
similar expressions joined by AND, sorted differently, the sort order
724+
of the first expression is retained.
721725
722726
- Choices (as in "MIT or GPL") are kept as-is and not treated as
723727
simplifiable. This avoids droping important choice options in complex
@@ -751,8 +755,6 @@ def dedup(self, expression):
751755
):
752756
relation = exp.__class__.__name__
753757
# Flatten nested 'AND' expressions only (not OR) to maintain precedence
754-
# Example: (A AND B) AND (C OR D) will become A AND B AND (C OR D)
755-
# The OR will not be flattened to avoid changing the expression logic
756758
if relation == "AND":
757759
flattened = []
758760
for e in expressions:

tests/test_license_expression.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,20 @@ def test_dedup_expressions_logically_equivalent_4(self):
735735
expected = l.parse("gpl AND mit")
736736
assert result == expected
737737

738+
def test_dedup_expressions_logically_equivalent_5(self):
739+
l = Licensing()
740+
exp = "(gpl OR mit) AND (mit OR gpl) AND ((gpl OR mit) AND (mit OR gpl))"
741+
result = l.dedup(exp)
742+
expected = l.parse("gpl OR mit")
743+
assert result == expected
744+
745+
def test_dedup_expressions_logically_equivalent_6(self):
746+
l = Licensing()
747+
exp = "(gpl OR mit) AND (mit OR gpl) AND ((gpl OR mit) OR (mit OR gpl))"
748+
result = l.dedup(exp)
749+
expected = l.parse("gpl OR mit")
750+
assert result == expected
751+
738752
def test_dedup_expressions_multiple_occurrences(self):
739753
l = Licensing()
740754
exp = " GPL-2.0 or (mit and LGPL-2.1) or bsd Or GPL-2.0 or (mit and LGPL-2.1)"

0 commit comments

Comments
 (0)