Debian Codegraph @

spdxexp/license_test.go

raw symbols SCIP
1 package spdxexp
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestActiveLicense(t *testing.T) {
10 tests := []struct {
11 name string
12 inputID string
13 outputID string
14 result bool
15 }{
16 {"active license - direct match", "Apache-2.0", "Apache-2.0", true},
17 {"active license - all upper", "APACHE-2.0", "Apache-2.0", true},
18 {"active license - all lower", "apache-2.0", "Apache-2.0", true},
19 {"active license - mixed case", "apACHe-2.0", "Apache-2.0", true},
20 {"deprecated license - direct match", "eCos-2.0", "eCos-2.0", false},
21 {"deprecated license - all upper", "ECOS-2.0", "eCos-2.0", false},
22 {"deprecated license - all lower", "ecos-2.0", "eCos-2.0", false},
23 {"deprecated license - mixed case", "ECos-2.0", "eCos-2.0", false},
24 {"exception license - direct match", "Bison-exception-2.2", "Bison-exception-2.2", false},
25 {"exception license - all upper", "BISON-EXCEPTION-2.2", "Bison-exception-2.2", false},
26 {"exception license - all lower", "bison-exception-2.2", "Bison-exception-2.2", false},
27 {"exception license - mixed case", "BisoN-Exception-2.2", "Bison-exception-2.2", false},
28 }
29
30 for _, test := range tests {
31 test := test
32 t.Run(test.name, func(t *testing.T) {
33 result, license := activeLicense((test.inputID))
34 assert.Equal(t, test.result, result)
35 if result {
36 // updated to the proper case if found
37 assert.Equal(t, test.outputID, license)
38 } else {
39 // no change in case if not found
40 assert.Equal(t, test.inputID, license)
41 }
42 })
43 }
44 }
45
46 func TestDeprecatedLicense(t *testing.T) {
47 tests := []struct {
48 name string
49 inputID string
50 outputID string
51 result bool
52 }{
53 {"active license - direct match", "Apache-2.0", "Apache-2.0", false},
54 {"active license - all upper", "APACHE-2.0", "Apache-2.0", false},
55 {"active license - all lower", "apache-2.0", "Apache-2.0", false},
56 {"active license - mixed case", "apACHe-2.0", "Apache-2.0", false},
57 {"deprecated license - direct match", "eCos-2.0", "eCos-2.0", true},
58 {"deprecated license - all upper", "ECOS-2.0", "eCos-2.0", true},
59 {"deprecated license - all lower", "ecos-2.0", "eCos-2.0", true},
60 {"deprecated license - mixed case", "ECos-2.0", "eCos-2.0", true},
61 {"exception license - direct match", "Bison-exception-2.2", "Bison-exception-2.2", false},
62 {"exception license - all upper", "BISON-EXCEPTION-2.2", "Bison-exception-2.2", false},
63 {"exception license - all lower", "bison-exception-2.2", "Bison-exception-2.2", false},
64 {"exception license - mixed case", "BisoN-Exception-2.2", "Bison-exception-2.2", false},
65 }
66
67 for _, test := range tests {
68 test := test
69 t.Run(test.name, func(t *testing.T) {
70 result, license := deprecatedLicense((test.inputID))
71 assert.Equal(t, test.result, result)
72 if result {
73 // updated to the proper case if found
74 assert.Equal(t, test.outputID, license)
75 } else {
76 // no change in case if not found
77 assert.Equal(t, test.inputID, license)
78 }
79 })
80 }
81 }
82
83 func TestExceptionLicense(t *testing.T) {
84 tests := []struct {
85 name string
86 inputID string
87 outputID string
88 result bool
89 }{
90 {"active license - direct match", "Apache-2.0", "Apache-2.0", false},
91 {"active license - all upper", "APACHE-2.0", "Apache-2.0", false},
92 {"active license - all lower", "apache-2.0", "Apache-2.0", false},
93 {"active license - mixed case", "apACHe-2.0", "Apache-2.0", false},
94 {"deprecated license - direct match", "eCos-2.0", "eCos-2.0", false},
95 {"deprecated license - all upper", "ECOS-2.0", "eCos-2.0", false},
96 {"deprecated license - all lower", "ecos-2.0", "eCos-2.0", false},
97 {"deprecated license - mixed case", "ECos-2.0", "eCos-2.0", false},
98 {"exception license - direct match", "Bison-exception-2.2", "Bison-exception-2.2", true},
99 {"exception license - all upper", "BISON-EXCEPTION-2.2", "Bison-exception-2.2", true},
100 {"exception license - all lower", "bison-exception-2.2", "Bison-exception-2.2", true},
101 {"exception license - mixed case", "BisoN-Exception-2.2", "Bison-exception-2.2", true},
102 }
103
104 for _, test := range tests {
105 test := test
106 t.Run(test.name, func(t *testing.T) {
107 result, license := exceptionLicense((test.inputID))
108 assert.Equal(t, test.result, result)
109 if result {
110 // updated to the proper case if found
111 assert.Equal(t, test.outputID, license)
112 } else {
113 // no change in case if not found
114 assert.Equal(t, test.inputID, license)
115 }
116 })
117 }
118 }
119
120 func TestGetLicenseRange(t *testing.T) {
121 tests := []struct {
122 name string
123 id string
124 licenseRange *licenseRange
125 }{
126 {"no multi-element ranges", "Apache-2.0", &licenseRange{
127 licenses: []string{"Apache-2.0"},
128 location: map[uint8]int{licenseGroup: 2, versionGroup: 2, licenseIndex: 0}}},
129 {"multi-element ranges", "GFDL-1.2-only", &licenseRange{
130 licenses: []string{"GFDL-1.2", "GFDL-1.2-only"},
131 location: map[uint8]int{licenseGroup: 21, versionGroup: 1, licenseIndex: 1}}},
132 {"no range", "Bison-exception-2.2", nil},
133 }
134
135 for _, test := range tests {
136 test := test
137 t.Run(test.name, func(t *testing.T) {
138 licenseRange := getLicenseRange(test.id)
139 assert.Equal(t, test.licenseRange, licenseRange)
140 })
141 }
142 }