1// 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 (
18	"testing"
19)
20
21func TestConditionSetHas(t *testing.T) {
22	impliesShare := ImpliesShared
23
24	t.Logf("testing with imliesShare=%#v", impliesShare)
25
26	if impliesShare.HasAny(NoticeCondition) {
27		t.Errorf("impliesShare.HasAny(\"notice\"=%#v) got true, want false", NoticeCondition)
28	}
29
30	if !impliesShare.HasAny(RestrictedCondition) {
31		t.Errorf("impliesShare.HasAny(\"restricted\"=%#v) got false, want true", RestrictedCondition)
32	}
33
34	if !impliesShare.HasAny(ReciprocalCondition) {
35		t.Errorf("impliesShare.HasAny(\"reciprocal\"=%#v) got false, want true", ReciprocalCondition)
36	}
37
38	if impliesShare.HasAny(LicenseCondition(0x0000)) {
39		t.Errorf("impliesShare.HasAny(nil=%#v) got true, want false", LicenseCondition(0x0000))
40	}
41}
42
43func TestConditionName(t *testing.T) {
44	for expected, condition := range RecognizedConditionNames {
45		actual := condition.Name()
46		if expected != actual {
47			t.Errorf("unexpected name for condition %#v: got %s, want %s", condition, actual, expected)
48		}
49	}
50}
51
52func TestConditionName_InvalidCondition(t *testing.T) {
53	panicked := false
54	var lc LicenseCondition
55	func() {
56		defer func() {
57			if err := recover(); err != nil {
58				panicked = true
59			}
60		}()
61		name := lc.Name()
62		t.Errorf("invalid condition unexpected name: got %s, wanted panic", name)
63	}()
64	if !panicked {
65		t.Errorf("no expected panic for %#v.Name(): got no panic, wanted panic", lc)
66	}
67}
68