blob: 1d80676354e6428cf4e8c7b8474718ed94052af1 [file] [log] [blame]
Bob Badoura99ac622021-10-25 16:21:00 -07001// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package compliance
16
17import (
Bob Badoura99ac622021-10-25 16:21:00 -070018 "strings"
19 "testing"
20)
21
Bob Badoura99ac622021-10-25 16:21:00 -070022func TestConditionSet(t *testing.T) {
23 tests := []struct {
Bob Badour103eb0f2022-01-10 13:50:57 -080024 name string
25 conditions []string
26 plus *[]string
27 minus *[]string
28 matchingAny map[string][]string
29 expected []string
Bob Badoura99ac622021-10-25 16:21:00 -070030 }{
31 {
32 name: "empty",
Bob Badour103eb0f2022-01-10 13:50:57 -080033 conditions: []string{},
34 plus: &[]string{},
35 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080036 "notice": []string{},
37 "restricted": []string{},
Bob Badour103eb0f2022-01-10 13:50:57 -080038 "restricted|reciprocal": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070039 },
Colin Cross35f79c32022-01-27 15:18:52 -080040 expected: []string{},
Bob Badour103eb0f2022-01-10 13:50:57 -080041 },
42 {
43 name: "emptyminusnothing",
44 conditions: []string{},
45 minus: &[]string{},
46 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080047 "notice": []string{},
48 "restricted": []string{},
Bob Badour103eb0f2022-01-10 13:50:57 -080049 "restricted|reciprocal": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070050 },
Colin Cross35f79c32022-01-27 15:18:52 -080051 expected: []string{},
Bob Badour103eb0f2022-01-10 13:50:57 -080052 },
53 {
54 name: "emptyminusnotice",
55 conditions: []string{},
56 minus: &[]string{"notice"},
57 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080058 "notice": []string{},
59 "restricted": []string{},
Bob Badour103eb0f2022-01-10 13:50:57 -080060 "restricted|reciprocal": []string{},
61 },
Colin Cross35f79c32022-01-27 15:18:52 -080062 expected: []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070063 },
64 {
Colin Cross35f79c32022-01-27 15:18:52 -080065 name: "noticeonly",
Bob Badour103eb0f2022-01-10 13:50:57 -080066 conditions: []string{"notice"},
67 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080068 "notice": []string{"notice"},
69 "notice|proprietary": []string{"notice"},
70 "restricted": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070071 },
Bob Badour103eb0f2022-01-10 13:50:57 -080072 expected: []string{"notice"},
Bob Badoura99ac622021-10-25 16:21:00 -070073 },
74 {
Colin Cross35f79c32022-01-27 15:18:52 -080075 name: "allnoticeonly",
Bob Badour103eb0f2022-01-10 13:50:57 -080076 conditions: []string{"notice"},
Colin Cross35f79c32022-01-27 15:18:52 -080077 plus: &[]string{"notice"},
Bob Badour103eb0f2022-01-10 13:50:57 -080078 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080079 "notice": []string{"notice"},
80 "notice|proprietary": []string{"notice"},
81 "restricted": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070082 },
Bob Badour103eb0f2022-01-10 13:50:57 -080083 expected: []string{"notice"},
84 },
85 {
Colin Cross35f79c32022-01-27 15:18:52 -080086 name: "emptyplusnotice",
Bob Badour103eb0f2022-01-10 13:50:57 -080087 conditions: []string{},
Colin Cross35f79c32022-01-27 15:18:52 -080088 plus: &[]string{"notice"},
Bob Badour103eb0f2022-01-10 13:50:57 -080089 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -080090 "notice": []string{"notice"},
91 "notice|proprietary": []string{"notice"},
92 "restricted": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -070093 },
Bob Badour103eb0f2022-01-10 13:50:57 -080094 expected: []string{"notice"},
Bob Badoura99ac622021-10-25 16:21:00 -070095 },
96 {
Colin Cross35f79c32022-01-27 15:18:52 -080097 name: "everything",
Bob Badour103eb0f2022-01-10 13:50:57 -080098 conditions: []string{"unencumbered", "permissive", "notice", "reciprocal", "restricted", "proprietary"},
Colin Cross35f79c32022-01-27 15:18:52 -080099 plus: &[]string{"restricted_with_classpath_exception", "restricted_allows_dynamic_linking", "by_exception_only", "not_allowed"},
Bob Badour103eb0f2022-01-10 13:50:57 -0800100 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -0800101 "unencumbered": []string{"unencumbered"},
102 "permissive": []string{"permissive"},
103 "notice": []string{"notice"},
104 "reciprocal": []string{"reciprocal"},
105 "restricted": []string{"restricted"},
106 "restricted_with_classpath_exception": []string{"restricted_with_classpath_exception"},
107 "restricted_allows_dynamic_linking": []string{"restricted_allows_dynamic_linking"},
108 "proprietary": []string{"proprietary"},
109 "by_exception_only": []string{"by_exception_only"},
110 "not_allowed": []string{"not_allowed"},
111 "notice|proprietary": []string{"notice", "proprietary"},
Bob Badoura99ac622021-10-25 16:21:00 -0700112 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800113 expected: []string{
114 "unencumbered",
115 "permissive",
116 "notice",
117 "reciprocal",
118 "restricted",
119 "restricted_with_classpath_exception",
120 "restricted_allows_dynamic_linking",
121 "proprietary",
122 "by_exception_only",
123 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700124 },
125 },
126 {
Bob Badour103eb0f2022-01-10 13:50:57 -0800127 name: "everythingplusminusnothing",
128 conditions: []string{
129 "unencumbered",
130 "permissive",
131 "notice",
132 "reciprocal",
133 "restricted",
134 "restricted_with_classpath_exception",
135 "restricted_allows_dynamic_linking",
136 "proprietary",
137 "by_exception_only",
138 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700139 },
Colin Cross35f79c32022-01-27 15:18:52 -0800140 plus: &[]string{},
Bob Badour103eb0f2022-01-10 13:50:57 -0800141 minus: &[]string{},
142 matchingAny: map[string][]string{
143 "unencumbered|permissive|notice": []string{"unencumbered", "permissive", "notice"},
Colin Cross35f79c32022-01-27 15:18:52 -0800144 "restricted|reciprocal": []string{"reciprocal", "restricted"},
145 "proprietary|by_exception_only": []string{"proprietary", "by_exception_only"},
146 "not_allowed": []string{"not_allowed"},
Bob Badoura99ac622021-10-25 16:21:00 -0700147 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800148 expected: []string{
149 "unencumbered",
150 "permissive",
151 "notice",
152 "reciprocal",
153 "restricted",
154 "restricted_with_classpath_exception",
155 "restricted_allows_dynamic_linking",
156 "proprietary",
157 "by_exception_only",
158 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700159 },
160 },
161 {
Colin Cross35f79c32022-01-27 15:18:52 -0800162 name: "allbutone",
Bob Badour103eb0f2022-01-10 13:50:57 -0800163 conditions: []string{"unencumbered", "permissive", "notice", "reciprocal", "restricted", "proprietary"},
Colin Cross35f79c32022-01-27 15:18:52 -0800164 plus: &[]string{"restricted_allows_dynamic_linking", "by_exception_only", "not_allowed"},
Bob Badour103eb0f2022-01-10 13:50:57 -0800165 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -0800166 "unencumbered": []string{"unencumbered"},
167 "permissive": []string{"permissive"},
168 "notice": []string{"notice"},
169 "reciprocal": []string{"reciprocal"},
170 "restricted": []string{"restricted"},
171 "restricted_with_classpath_exception": []string{},
172 "restricted_allows_dynamic_linking": []string{"restricted_allows_dynamic_linking"},
173 "proprietary": []string{"proprietary"},
174 "by_exception_only": []string{"by_exception_only"},
175 "not_allowed": []string{"not_allowed"},
176 "notice|proprietary": []string{"notice", "proprietary"},
Bob Badoura99ac622021-10-25 16:21:00 -0700177 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800178 expected: []string{
179 "unencumbered",
180 "permissive",
181 "notice",
182 "reciprocal",
183 "restricted",
184 "restricted_allows_dynamic_linking",
185 "proprietary",
186 "by_exception_only",
187 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700188 },
189 },
190 {
Bob Badour103eb0f2022-01-10 13:50:57 -0800191 name: "everythingminusone",
192 conditions: []string{
193 "unencumbered",
194 "permissive",
195 "notice",
196 "reciprocal",
197 "restricted",
198 "restricted_with_classpath_exception",
199 "restricted_allows_dynamic_linking",
200 "proprietary",
201 "by_exception_only",
202 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700203 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800204 minus: &[]string{"restricted_allows_dynamic_linking"},
205 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -0800206 "unencumbered": []string{"unencumbered"},
207 "permissive": []string{"permissive"},
208 "notice": []string{"notice"},
209 "reciprocal": []string{"reciprocal"},
210 "restricted": []string{"restricted"},
211 "restricted_with_classpath_exception": []string{"restricted_with_classpath_exception"},
212 "restricted_allows_dynamic_linking": []string{},
213 "proprietary": []string{"proprietary"},
214 "by_exception_only": []string{"by_exception_only"},
215 "not_allowed": []string{"not_allowed"},
216 "restricted|proprietary": []string{"restricted", "proprietary"},
Bob Badoura99ac622021-10-25 16:21:00 -0700217 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800218 expected: []string{
219 "unencumbered",
220 "permissive",
221 "notice",
222 "reciprocal",
223 "restricted",
224 "restricted_with_classpath_exception",
225 "proprietary",
226 "by_exception_only",
227 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700228 },
229 },
230 {
Bob Badour103eb0f2022-01-10 13:50:57 -0800231 name: "everythingminuseverything",
232 conditions: []string{
233 "unencumbered",
234 "permissive",
235 "notice",
236 "reciprocal",
237 "restricted",
238 "restricted_with_classpath_exception",
239 "restricted_allows_dynamic_linking",
240 "proprietary",
241 "by_exception_only",
242 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700243 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800244 minus: &[]string{
245 "unencumbered",
246 "permissive",
247 "notice",
248 "reciprocal",
249 "restricted",
250 "restricted_with_classpath_exception",
251 "restricted_allows_dynamic_linking",
252 "proprietary",
253 "by_exception_only",
254 "not_allowed",
Bob Badoura99ac622021-10-25 16:21:00 -0700255 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800256 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -0800257 "unencumbered": []string{},
258 "permissive": []string{},
259 "notice": []string{},
260 "reciprocal": []string{},
261 "restricted": []string{},
262 "restricted_with_classpath_exception": []string{},
263 "restricted_allows_dynamic_linking": []string{},
264 "proprietary": []string{},
265 "by_exception_only": []string{},
266 "not_allowed": []string{},
267 "restricted|proprietary": []string{},
Bob Badoura99ac622021-10-25 16:21:00 -0700268 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800269 expected: []string{},
Bob Badoura99ac622021-10-25 16:21:00 -0700270 },
271 {
Colin Cross35f79c32022-01-27 15:18:52 -0800272 name: "restrictedplus",
Bob Badour103eb0f2022-01-10 13:50:57 -0800273 conditions: []string{"restricted", "restricted_with_classpath_exception", "restricted_allows_dynamic_linking"},
Colin Cross35f79c32022-01-27 15:18:52 -0800274 plus: &[]string{"permissive", "notice", "restricted", "proprietary"},
Bob Badour103eb0f2022-01-10 13:50:57 -0800275 matchingAny: map[string][]string{
Colin Cross35f79c32022-01-27 15:18:52 -0800276 "unencumbered": []string{},
277 "permissive": []string{"permissive"},
278 "notice": []string{"notice"},
279 "restricted": []string{"restricted"},
280 "restricted_with_classpath_exception": []string{"restricted_with_classpath_exception"},
281 "restricted_allows_dynamic_linking": []string{"restricted_allows_dynamic_linking"},
282 "proprietary": []string{"proprietary"},
283 "restricted|proprietary": []string{"restricted", "proprietary"},
284 "by_exception_only": []string{},
285 "proprietary|by_exception_only": []string{"proprietary"},
Bob Badoura99ac622021-10-25 16:21:00 -0700286 },
Bob Badour103eb0f2022-01-10 13:50:57 -0800287 expected: []string{"permissive", "notice", "restricted", "restricted_with_classpath_exception", "restricted_allows_dynamic_linking", "proprietary"},
Bob Badoura99ac622021-10-25 16:21:00 -0700288 },
289 }
290 for _, tt := range tests {
Bob Badour103eb0f2022-01-10 13:50:57 -0800291 toConditions := func(names []string) []LicenseCondition {
292 result := make([]LicenseCondition, 0, len(names))
293 for _, name := range names {
294 result = append(result, RecognizedConditionNames[name])
295 }
296 return result
Bob Badoura99ac622021-10-25 16:21:00 -0700297 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800298 populate := func() LicenseConditionSet {
299 testSet := NewLicenseConditionSet(toConditions(tt.conditions)...)
300 if tt.plus != nil {
301 testSet = testSet.Plus(toConditions(*tt.plus)...)
Bob Badoura99ac622021-10-25 16:21:00 -0700302 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800303 if tt.minus != nil {
304 testSet = testSet.Minus(toConditions(*tt.minus)...)
Bob Badoura99ac622021-10-25 16:21:00 -0700305 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800306 return testSet
307 }
308 populateSet := func() LicenseConditionSet {
309 testSet := NewLicenseConditionSet(toConditions(tt.conditions)...)
310 if tt.plus != nil {
311 testSet = testSet.Union(NewLicenseConditionSet(toConditions(*tt.plus)...))
312 }
313 if tt.minus != nil {
314 testSet = testSet.Difference(NewLicenseConditionSet(toConditions(*tt.minus)...))
315 }
316 return testSet
317 }
318 populatePlusSet := func() LicenseConditionSet {
319 testSet := NewLicenseConditionSet(toConditions(tt.conditions)...)
320 if tt.plus != nil {
321 testSet = testSet.Union(NewLicenseConditionSet(toConditions(*tt.plus)...))
322 }
323 if tt.minus != nil {
324 testSet = testSet.Minus(toConditions(*tt.minus)...)
325 }
326 return testSet
327 }
328 populateMinusSet := func() LicenseConditionSet {
329 testSet := NewLicenseConditionSet(toConditions(tt.conditions)...)
330 if tt.plus != nil {
331 testSet = testSet.Plus(toConditions(*tt.plus)...)
332 }
333 if tt.minus != nil {
334 testSet = testSet.Difference(NewLicenseConditionSet(toConditions(*tt.minus)...))
335 }
336 return testSet
337 }
338 checkMatching := func(cs LicenseConditionSet, t *testing.T) {
339 for data, expectedNames := range tt.matchingAny {
340 expectedConditions := toConditions(expectedNames)
341 expected := NewLicenseConditionSet(expectedConditions...)
342 actual := cs.MatchingAny(toConditions(strings.Split(data, "|"))...)
343 actualNames := actual.Names()
Bob Badoura99ac622021-10-25 16:21:00 -0700344
Bob Badour103eb0f2022-01-10 13:50:57 -0800345 t.Logf("MatchingAny(%s): actual set %04x %s", data, actual, actual.String())
346 t.Logf("MatchingAny(%s): expected set %04x %s", data, expected, expected.String())
347
348 if actual != expected {
349 t.Errorf("MatchingAny(%s): got %04x, want %04x", data, actual, expected)
350 continue
351 }
352 if len(actualNames) != len(expectedNames) {
353 t.Errorf("len(MatchinAny(%s).Names()): got %d, want %d",
354 data, len(actualNames), len(expectedNames))
355 } else {
356 for i := 0; i < len(actualNames); i++ {
357 if actualNames[i] != expectedNames[i] {
358 t.Errorf("MatchingAny(%s).Names()[%d]: got %s, want %s",
359 data, i, actualNames[i], expectedNames[i])
360 break
361 }
362 }
363 }
364 actualConditions := actual.AsList()
365 if len(actualConditions) != len(expectedConditions) {
366 t.Errorf("len(MatchingAny(%d).AsList()): got %d, want %d",
367 data, len(actualNames), len(expectedNames))
368 } else {
369 for i := 0; i < len(actualNames); i++ {
370 if actualNames[i] != expectedNames[i] {
371 t.Errorf("MatchingAny(%s).AsList()[%d]: got %s, want %s",
372 data, i, actualNames[i], expectedNames[i])
373 break
374 }
375 }
Bob Badoura99ac622021-10-25 16:21:00 -0700376 }
377 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800378 }
379 checkMatchingSet := func(cs LicenseConditionSet, t *testing.T) {
380 for data, expectedNames := range tt.matchingAny {
381 expected := NewLicenseConditionSet(toConditions(expectedNames)...)
382 actual := cs.MatchingAnySet(NewLicenseConditionSet(toConditions(strings.Split(data, "|"))...))
383 actualNames := actual.Names()
384
385 t.Logf("MatchingAnySet(%s): actual set %04x %s", data, actual, actual.String())
386 t.Logf("MatchingAnySet(%s): expected set %04x %s", data, expected, expected.String())
387
388 if actual != expected {
389 t.Errorf("MatchingAnySet(%s): got %04x, want %04x", data, actual, expected)
390 continue
391 }
392 if len(actualNames) != len(expectedNames) {
393 t.Errorf("len(MatchingAnySet(%s).Names()): got %d, want %d",
394 data, len(actualNames), len(expectedNames))
395 } else {
396 for i := 0; i < len(actualNames); i++ {
397 if actualNames[i] != expectedNames[i] {
398 t.Errorf("MatchingAnySet(%s).Names()[%d]: got %s, want %s",
399 data, i, actualNames[i], expectedNames[i])
400 break
401 }
402 }
403 }
404 expectedConditions := toConditions(expectedNames)
405 actualConditions := actual.AsList()
406 if len(actualConditions) != len(expectedConditions) {
407 t.Errorf("len(MatchingAnySet(%s).AsList()): got %d, want %d",
408 data, len(actualNames), len(expectedNames))
409 } else {
410 for i := 0; i < len(actualNames); i++ {
411 if actualNames[i] != expectedNames[i] {
412 t.Errorf("MatchingAnySet(%s).AsList()[%d]: got %s, want %s",
413 data, i, actualNames[i], expectedNames[i])
414 break
415 }
416 }
417 }
418 }
419 }
420
421 checkExpected := func(actual LicenseConditionSet, t *testing.T) bool {
422 t.Logf("checkExpected{%s}", strings.Join(tt.expected, ", "))
423
424 expectedConditions := toConditions(tt.expected)
425 expected := NewLicenseConditionSet(expectedConditions...)
426
427 actualNames := actual.Names()
428
429 t.Logf("actual license condition set: %04x %s", actual, actual.String())
430 t.Logf("expected license condition set: %04x %s", expected, expected.String())
431
432 if actual != expected {
433 t.Errorf("checkExpected: got %04x, want %04x", actual, expected)
434 return false
435 }
436
437 if len(actualNames) != len(tt.expected) {
438 t.Errorf("len(actual.Names()): got %d, want %d", len(actualNames), len(tt.expected))
Bob Badoura99ac622021-10-25 16:21:00 -0700439 } else {
Bob Badour103eb0f2022-01-10 13:50:57 -0800440 for i := 0; i < len(actualNames); i++ {
441 if actualNames[i] != tt.expected[i] {
442 t.Errorf("actual.Names()[%d]: got %s, want %s", i, actualNames[i], tt.expected[i])
443 break
Bob Badoura99ac622021-10-25 16:21:00 -0700444 }
445 }
446 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800447
448 actualConditions := actual.AsList()
449 if len(actualConditions) != len(expectedConditions) {
450 t.Errorf("len(actual.AsList()): got %d, want %d", len(actualConditions), len(expectedConditions))
451 } else {
452 for i := 0; i < len(actualConditions); i++ {
453 if actualConditions[i] != expectedConditions[i] {
454 t.Errorf("actual.AsList()[%d]: got %s, want %s",
455 i, actualConditions[i], expectedConditions[i])
456 break
457 }
458 }
459 }
460
461 if len(tt.expected) == 0 {
462 if !actual.IsEmpty() {
463 t.Errorf("actual.IsEmpty(): got false, want true")
464 }
465 if actual.HasAny(expectedConditions...) {
466 t.Errorf("actual.HasAny(): got true, want false")
467 }
468 } else {
469 if actual.IsEmpty() {
470 t.Errorf("actual.IsEmpty(): got true, want false")
471 }
472 if !actual.HasAny(expectedConditions...) {
473 t.Errorf("actual.HasAny(all expected): got false, want true")
474 }
475 }
476 if !actual.HasAll(expectedConditions...) {
477 t.Errorf("actual.Hasll(all expected): want true, got false")
478 }
479 for _, expectedCondition := range expectedConditions {
480 if !actual.HasAny(expectedCondition) {
481 t.Errorf("actual.HasAny(%q): got false, want true", expectedCondition.Name())
482 }
483 if !actual.HasAll(expectedCondition) {
484 t.Errorf("actual.HasAll(%q): got false, want true", expectedCondition.Name())
485 }
486 }
487
488 notExpected := (AllLicenseConditions &^ expected)
489 notExpectedList := notExpected.AsList()
490 t.Logf("not expected license condition set: %04x %s", notExpected, notExpected.String())
491
492 if len(tt.expected) == 0 {
493 if actual.HasAny(append(expectedConditions, notExpectedList...)...) {
494 t.Errorf("actual.HasAny(all conditions): want false, got true")
495 }
496 } else {
497 if !actual.HasAny(append(expectedConditions, notExpectedList...)...) {
498 t.Errorf("actual.HasAny(all conditions): want true, got false")
499 }
500 }
501 if len(notExpectedList) == 0 {
502 if !actual.HasAll(append(expectedConditions, notExpectedList...)...) {
503 t.Errorf("actual.HasAll(all conditions): want true, got false")
504 }
505 } else {
506 if actual.HasAll(append(expectedConditions, notExpectedList...)...) {
507 t.Errorf("actual.HasAll(all conditions): want false, got true")
508 }
509 }
510 for _, unexpectedCondition := range notExpectedList {
511 if actual.HasAny(unexpectedCondition) {
512 t.Errorf("actual.HasAny(%q): got true, want false", unexpectedCondition.Name())
513 }
514 if actual.HasAll(unexpectedCondition) {
515 t.Errorf("actual.HasAll(%q): got true, want false", unexpectedCondition.Name())
516 }
517 }
518 return true
Bob Badoura99ac622021-10-25 16:21:00 -0700519 }
520
Bob Badour103eb0f2022-01-10 13:50:57 -0800521 checkExpectedSet := func(actual LicenseConditionSet, t *testing.T) bool {
522 t.Logf("checkExpectedSet{%s}", strings.Join(tt.expected, ", "))
Bob Badoura99ac622021-10-25 16:21:00 -0700523
Bob Badour103eb0f2022-01-10 13:50:57 -0800524 expectedConditions := toConditions(tt.expected)
525 expected := NewLicenseConditionSet(expectedConditions...)
526
527 actualNames := actual.Names()
528
529 t.Logf("actual license condition set: %04x %s", actual, actual.String())
530 t.Logf("expected license condition set: %04x %s", expected, expected.String())
531
532 if actual != expected {
533 t.Errorf("checkExpectedSet: got %04x, want %04x", actual, expected)
534 return false
Bob Badoura99ac622021-10-25 16:21:00 -0700535 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800536
537 if len(actualNames) != len(tt.expected) {
538 t.Errorf("len(actual.Names()): got %d, want %d", len(actualNames), len(tt.expected))
539 } else {
540 for i := 0; i < len(actualNames); i++ {
541 if actualNames[i] != tt.expected[i] {
542 t.Errorf("actual.Names()[%d]: got %s, want %s", i, actualNames[i], tt.expected[i])
543 break
Bob Badoura99ac622021-10-25 16:21:00 -0700544 }
545 }
Bob Badoura99ac622021-10-25 16:21:00 -0700546 }
Bob Badoura99ac622021-10-25 16:21:00 -0700547
Bob Badour103eb0f2022-01-10 13:50:57 -0800548 actualConditions := actual.AsList()
549 if len(actualConditions) != len(expectedConditions) {
550 t.Errorf("len(actual.AsList()): got %d, want %d", len(actualConditions), len(expectedConditions))
551 } else {
552 for i := 0; i < len(actualConditions); i++ {
553 if actualConditions[i] != expectedConditions[i] {
554 t.Errorf("actual.AsList()[%d}: got %s, want %s",
555 i, actualConditions[i], expectedConditions[i])
556 break
557 }
558 }
559 }
560
561 if len(tt.expected) == 0 {
562 if !actual.IsEmpty() {
563 t.Errorf("actual.IsEmpty(): got false, want true")
564 }
565 if actual.MatchesAnySet(expected) {
566 t.Errorf("actual.MatchesAnySet({}): got true, want false")
567 }
568 if actual.MatchesEverySet(expected, expected) {
569 t.Errorf("actual.MatchesEverySet({}, {}): want false, got true")
570 }
571 } else {
572 if actual.IsEmpty() {
573 t.Errorf("actual.IsEmpty(): got true, want false")
574 }
575 if !actual.MatchesAnySet(expected) {
576 t.Errorf("actual.MatchesAnySet({all expected}): want true, got false")
577 }
578 if !actual.MatchesEverySet(expected, expected) {
579 t.Errorf("actual.MatchesEverySet({all expected}, {all expected}): want true, got false")
580 }
581 }
582
583 notExpected := (AllLicenseConditions &^ expected)
584 t.Logf("not expected license condition set: %04x %s", notExpected, notExpected.String())
585
586 if len(tt.expected) == 0 {
587 if actual.MatchesAnySet(expected, notExpected) {
588 t.Errorf("empty actual.MatchesAnySet({expected}, {not expected}): want false, got true")
589 }
590 } else {
591 if !actual.MatchesAnySet(expected, notExpected) {
592 t.Errorf("actual.MatchesAnySet({expected}, {not expected}): want true, got false")
593 }
594 }
595 if actual.MatchesAnySet(notExpected) {
596 t.Errorf("actual.MatchesAnySet({not expected}): want false, got true")
597 }
598 if actual.MatchesEverySet(notExpected) {
599 t.Errorf("actual.MatchesEverySet({not expected}): want false, got true")
600 }
601 if actual.MatchesEverySet(expected, notExpected) {
602 t.Errorf("actual.MatchesEverySet({expected}, {not expected}): want false, got true")
603 }
604
605 if !actual.Difference(expected).IsEmpty() {
606 t.Errorf("actual.Difference({expected}).IsEmpty(): want true, got false")
607 }
608 if expected != actual.Intersection(expected) {
609 t.Errorf("expected == actual.Intersection({expected}): want true, got false (%04x != %04x)", expected, actual.Intersection(expected))
610 }
611 if actual != actual.Intersection(expected) {
612 t.Errorf("actual == actual.Intersection({expected}): want true, got false (%04x != %04x)", actual, actual.Intersection(expected))
613 }
614 return true
615 }
616
Bob Badoura99ac622021-10-25 16:21:00 -0700617 t.Run(tt.name, func(t *testing.T) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800618 cs := populate()
619 if checkExpected(cs, t) {
620 checkMatching(cs, t)
Bob Badoura99ac622021-10-25 16:21:00 -0700621 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800622 if checkExpectedSet(cs, t) {
623 checkMatchingSet(cs, t)
Bob Badoura99ac622021-10-25 16:21:00 -0700624 }
Bob Badour103eb0f2022-01-10 13:50:57 -0800625 })
626
627 t.Run(tt.name+"_sets", func(t *testing.T) {
628 cs := populateSet()
629 if checkExpected(cs, t) {
630 checkMatching(cs, t)
Bob Badoura99ac622021-10-25 16:21:00 -0700631 }
Colin Cross35f79c32022-01-27 15:18:52 -0800632 if checkExpectedSet(cs, t) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800633 checkMatchingSet(cs, t)
634 }
635 })
636
637 t.Run(tt.name+"_plusset", func(t *testing.T) {
638 cs := populatePlusSet()
639 if checkExpected(cs, t) {
640 checkMatching(cs, t)
641 }
Colin Cross35f79c32022-01-27 15:18:52 -0800642 if checkExpectedSet(cs, t) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800643 checkMatchingSet(cs, t)
644 }
645 })
646
647 t.Run(tt.name+"_minusset", func(t *testing.T) {
648 cs := populateMinusSet()
649 if checkExpected(cs, t) {
650 checkMatching(cs, t)
651 }
Colin Cross35f79c32022-01-27 15:18:52 -0800652 if checkExpectedSet(cs, t) {
Bob Badour103eb0f2022-01-10 13:50:57 -0800653 checkMatchingSet(cs, t)
654 }
Bob Badoura99ac622021-10-25 16:21:00 -0700655 })
656 }
657}