Skip to content

Commit baa56a6

Browse files
Add tests for composite rule annotation script
1 parent c1c8858 commit baa56a6

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# tests for annotate_composites.py
2+
import sys
3+
from pathlib import Path
4+
5+
sys.path.insert(0, str(Path(__file__).parent))
6+
from annotate_composites import strip_version_suffix, get_candidate_names, find_in_text
7+
8+
9+
class TestStripVersionSuffix:
10+
11+
def test_strips_trailing_version(self):
12+
assert strip_version_suffix('GNU General Public License 3.0') == 'GNU General Public License'
13+
14+
def test_strips_version_with_v_prefix(self):
15+
assert strip_version_suffix('Mozilla Public License v2.0') == 'Mozilla Public License'
16+
17+
def test_strips_or_later(self):
18+
result = strip_version_suffix('GNU Lesser General Public License 2.1 or later')
19+
assert result == 'GNU Lesser General Public License'
20+
21+
def test_returns_none_when_result_too_short(self):
22+
assert strip_version_suffix('EPL 1.0') is None
23+
24+
def test_returns_none_when_no_version(self):
25+
assert strip_version_suffix('Boost Software License') is None
26+
27+
28+
class FakeLicense:
29+
"""minimal mock for License objects"""
30+
def __init__(self, key, name=None, short_name=None, spdx_license_key=None):
31+
self.key = key
32+
self.name = name
33+
self.short_name = short_name
34+
self.spdx_license_key = spdx_license_key
35+
36+
37+
class TestGetCandidateNames:
38+
39+
def test_collects_all_fields(self):
40+
lic = FakeLicense(
41+
key='apache-2.0',
42+
name='Apache License 2.0',
43+
short_name='Apache 2.0',
44+
spdx_license_key='Apache-2.0',
45+
)
46+
names = get_candidate_names(lic)
47+
assert 'Apache License 2.0' in names
48+
assert 'Apache 2.0' in names
49+
assert 'Apache-2.0' in names
50+
assert 'apache-2.0' in names
51+
52+
def test_includes_extra_names_for_known_keys(self):
53+
lic = FakeLicense(key='gpl-2.0', name='GNU General Public License 2.0')
54+
names = get_candidate_names(lic)
55+
assert 'GPLv2' in names
56+
assert 'GPL 2.0' in names
57+
58+
def test_includes_version_stripped_base(self):
59+
lic = FakeLicense(key='agpl-3.0', name='GNU Affero General Public License 3.0')
60+
names = get_candidate_names(lic)
61+
assert 'GNU Affero General Public License' in names
62+
63+
def test_sorted_longest_first(self):
64+
lic = FakeLicense(key='mit', name='MIT License', short_name='MIT')
65+
names = get_candidate_names(lic)
66+
for i in range(len(names) - 1):
67+
assert len(names[i]) >= len(names[i + 1])
68+
69+
def test_no_duplicates(self):
70+
lic = FakeLicense(key='isc', name='ISC License', short_name='ISC License',
71+
spdx_license_key='ISC')
72+
names = get_candidate_names(lic)
73+
assert len(names) == len(set(names))
74+
75+
76+
class TestFindInText:
77+
78+
def test_basic_match(self):
79+
text = 'Licensed under the Apache License, Version 2.0'
80+
assert find_in_text(text, ['Apache License']) == 'Apache License'
81+
82+
def test_case_insensitive(self):
83+
text = 'distributed under the gnu general public license'
84+
result = find_in_text(text, ['GNU General Public License'])
85+
assert result == 'gnu general public license'
86+
87+
def test_not_found(self):
88+
text = 'this software uses the BSD license'
89+
assert find_in_text(text, ['Apache License', 'MIT License']) is None
90+
91+
def test_skips_very_short_candidates(self):
92+
assert find_in_text('released under XY terms', ['XY']) is None
93+
94+
def test_longer_candidate_matched_first(self):
95+
text = 'Released under the MIT License'
96+
assert find_in_text(text, ['MIT License', 'MIT']) == 'MIT License'

0 commit comments

Comments
 (0)