Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | import unittest |
| 2 | import oe.license |
| 3 | |
| 4 | class SeenVisitor(oe.license.LicenseVisitor): |
| 5 | def __init__(self): |
| 6 | self.seen = [] |
| 7 | oe.license.LicenseVisitor.__init__(self) |
| 8 | |
| 9 | def visit_Str(self, node): |
| 10 | self.seen.append(node.s) |
| 11 | |
| 12 | class TestSingleLicense(unittest.TestCase): |
| 13 | licenses = [ |
| 14 | "GPLv2", |
| 15 | "LGPL-2.0", |
| 16 | "Artistic", |
| 17 | "MIT", |
| 18 | "GPLv3+", |
| 19 | "FOO_BAR", |
| 20 | ] |
| 21 | invalid_licenses = ["GPL/BSD"] |
| 22 | |
| 23 | @staticmethod |
| 24 | def parse(licensestr): |
| 25 | visitor = SeenVisitor() |
| 26 | visitor.visit_string(licensestr) |
| 27 | return visitor.seen |
| 28 | |
| 29 | def test_single_licenses(self): |
| 30 | for license in self.licenses: |
| 31 | licenses = self.parse(license) |
| 32 | self.assertListEqual(licenses, [license]) |
| 33 | |
| 34 | def test_invalid_licenses(self): |
| 35 | for license in self.invalid_licenses: |
| 36 | with self.assertRaises(oe.license.InvalidLicense) as cm: |
| 37 | self.parse(license) |
| 38 | self.assertEqual(cm.exception.license, license) |
| 39 | |
| 40 | class TestSimpleCombinations(unittest.TestCase): |
| 41 | tests = { |
| 42 | "FOO&BAR": ["FOO", "BAR"], |
| 43 | "BAZ & MOO": ["BAZ", "MOO"], |
| 44 | "ALPHA|BETA": ["ALPHA"], |
| 45 | "BAZ&MOO|FOO": ["FOO"], |
| 46 | "FOO&BAR|BAZ": ["FOO", "BAR"], |
| 47 | } |
| 48 | preferred = ["ALPHA", "FOO", "BAR"] |
| 49 | |
| 50 | def test_tests(self): |
| 51 | def choose(a, b): |
| 52 | if all(lic in self.preferred for lic in b): |
| 53 | return b |
| 54 | else: |
| 55 | return a |
| 56 | |
| 57 | for license, expected in self.tests.items(): |
| 58 | licenses = oe.license.flattened_licenses(license, choose) |
| 59 | self.assertListEqual(licenses, expected) |
| 60 | |
| 61 | class TestComplexCombinations(TestSimpleCombinations): |
| 62 | tests = { |
| 63 | "FOO & (BAR | BAZ)&MOO": ["FOO", "BAR", "MOO"], |
| 64 | "(ALPHA|(BETA&THETA)|OMEGA)&DELTA": ["OMEGA", "DELTA"], |
| 65 | "((ALPHA|BETA)&FOO)|BAZ": ["BETA", "FOO"], |
| 66 | "(GPL-2.0|Proprietary)&BSD-4-clause&MIT": ["GPL-2.0", "BSD-4-clause", "MIT"], |
| 67 | } |
| 68 | preferred = ["BAR", "OMEGA", "BETA", "GPL-2.0"] |