1919from json import load as json_load
2020from unittest import TestCase
2121
22- from ddt import data , ddt , idata , unpack
22+ from ddt import ddt , idata , unpack
2323
2424from cyclonedx import spdx
2525from cyclonedx .schema ._res import SPDX_JSON
2626
2727# rework access
2828with open (SPDX_JSON ) as spdx_schema :
29- KNOWN_SPDX_IDS = json_load (spdx_schema )['enum' ]
29+ KNOWN_SPDX_IDS = set (json_load (spdx_schema )['enum' ])
30+
31+ # for valid test data see the spec: https://spdx.github.io/spdx-spec/v3.0.1/annexes/spdx-license-expressions/
32+ VALID_EXPRESSIONS = {
33+ # region Simple license expressions
34+ 'CDDL-1.0' ,
35+ # region not supported yet #110 - https://github.com/aboutcode-org/license-expression/issues/110
36+ # 'CDDL-1.0+',
37+ # endregion region not supported yet #110
38+ # region not supported yet #109 - https://github.com/aboutcode-org/license-expression/issues/109
39+ # 'LicenseRef-23',
40+ # 'LicenseRef-MIT-Style-1',
41+ # 'DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2',
42+ # endregion region not supported yet #109
43+ # endregion Simple license expressions
44+ # region Composite license expressions
45+ 'LGPL-2.1-only OR MIT' ,
46+ 'MIT or LGPL-2.1-only' ,
47+ '(MIT OR LGPL-2.1-only)' ,
48+ 'LGPL-2.1-only OR MIT OR BSD-3-Clause' ,
49+ 'LGPL-2.1-only AND MIT' ,
50+ 'MIT AND LGPL-2.1-only' ,
51+ 'MIT and LGPL-2.1-only' ,
52+ '(MIT AND LGPL-2.1-only)' ,
53+ 'LGPL-2.1-only AND MIT AND BSD-2-Clause' ,
54+ 'GPL-2.0-or-later WITH Bison-exception-2.2' ,
55+ 'LGPL-2.1-only OR BSD-3-Clause AND MIT' ,
56+ 'MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)' ,
57+ # endregion Composite license expressions
58+ # region examples from CDX spec
59+ 'Apache-2.0 AND (MIT OR GPL-2.0-only)' ,
60+ 'GPL-3.0-only WITH Classpath-exception-2.0' ,
61+ # endregion examples from CDX spec
62+ }
63+
64+ INVALID_EXPRESSIONS = {
65+ 'MIT AND Apache-2.0 OR something-unknown'
66+ 'something invalid' ,
67+ '(c) John Doe' ,
68+ 'Apache License, Version 2.0' ,
69+ }
3070
31- VALID_COMPOUND_EXPRESSIONS = {
32- # for valid test data see the spec: https://spdx.github.io/spdx-spec/v2.3/SPDX-license-expressions/
33- '(MIT AND Apache-2.0)' ,
34- 'BSD-2-Clause OR Apache-2.0' ,
71+ UNKNOWN_SPDX_IDS = {
72+ '' ,
73+ 'something unsupported' , 'something unfixable' ,
74+ 'Apache 2.0' ,
75+ 'LicenseRef-custom-identifier' ,
76+ * (VALID_EXPRESSIONS - KNOWN_SPDX_IDS ),
77+ * INVALID_EXPRESSIONS ,
3578}
3679
3780
@@ -43,12 +86,11 @@ def test_positive(self, supported_value: str) -> None:
4386 actual = spdx .is_supported_id (supported_value )
4487 self .assertTrue (actual )
4588
46- @data (
47- 'something unsupported' ,
48- # somehow case-twisted values
49- 'MiT' ,
50- 'mit' ,
51- )
89+ @idata (chain (UNKNOWN_SPDX_IDS , (
90+ # region somehow case-twisted values
91+ 'MiT' , 'mit' ,
92+ # endregion somehow case-twisted values
93+ )))
5294 def test_negative (self , unsupported_value : str ) -> None :
5395 actual = spdx .is_supported_id (unsupported_value )
5496 self .assertFalse (actual )
@@ -60,37 +102,31 @@ class TestSpdxFixup(TestCase):
60102 @idata (chain (
61103 # original value
62104 ((v , v ) for v in KNOWN_SPDX_IDS ),
63- # somehow case-twisted values
105+ # region somehow case-twisted values
64106 ((v .lower (), v ) for v in KNOWN_SPDX_IDS ),
65107 ((v .upper (), v ) for v in KNOWN_SPDX_IDS )
108+ # endregion somehow case-twisted values
66109 ))
67110 @unpack
68111 def test_positive (self , fixable : str , expected_fixed : str ) -> None :
69112 actual = spdx .fixup_id (fixable )
70113 self .assertEqual (expected_fixed , actual )
71114
72- @data (
73- 'something unfixable' ,
74- )
115+ @idata (UNKNOWN_SPDX_IDS )
75116 def test_negative (self , unfixable : str ) -> None :
76117 actual = spdx .fixup_id (unfixable )
77118 self .assertIsNone (actual )
78119
79120
80121@ddt
81- class TestSpdxIsCompoundExpression (TestCase ):
122+ class TestSpdxIsExpression (TestCase ):
82123
83- @idata (VALID_COMPOUND_EXPRESSIONS )
124+ @idata (VALID_EXPRESSIONS )
84125 def test_positive (self , valid_expression : str ) -> None :
85126 actual = spdx .is_expression (valid_expression )
86127 self .assertTrue (actual )
87128
88- @data (
89- 'MIT AND Apache-2.0 OR something-unknown'
90- 'something invalid' ,
91- '(c) John Doe' ,
92- 'Apache License, Version 2.0'
93- )
129+ @idata (INVALID_EXPRESSIONS )
94130 def test_negative (self , invalid_expression : str ) -> None :
95131 actual = spdx .is_expression (invalid_expression )
96132 self .assertFalse (actual )
0 commit comments