1// Copyright 2021 Google Inc. All rights reserved.
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 bazel
16
17import (
18	"reflect"
19	"testing"
20)
21
22func TestUniqueBazelLabels(t *testing.T) {
23	testCases := []struct {
24		originalLabels       []Label
25		expectedUniqueLabels []Label
26	}{
27		{
28			originalLabels: []Label{
29				{Label: "a"},
30				{Label: "b"},
31				{Label: "a"},
32				{Label: "c"},
33			},
34			expectedUniqueLabels: []Label{
35				{Label: "a"},
36				{Label: "b"},
37				{Label: "c"},
38			},
39		},
40	}
41	for _, tc := range testCases {
42		actualUniqueLabels := UniqueSortedBazelLabels(tc.originalLabels)
43		if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) {
44			t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels)
45		}
46	}
47}
48
49func TestSubtractStrings(t *testing.T) {
50	testCases := []struct {
51		haystack       []string
52		needle         []string
53		expectedResult []string
54	}{
55		{
56			haystack: []string{
57				"a",
58				"b",
59				"c",
60			},
61			needle: []string{
62				"a",
63			},
64			expectedResult: []string{
65				"b", "c",
66			},
67		},
68	}
69	for _, tc := range testCases {
70		actualResult := SubtractStrings(tc.haystack, tc.needle)
71		if !reflect.DeepEqual(tc.expectedResult, actualResult) {
72			t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult)
73		}
74	}
75}
76
77func TestSubtractBazelLabelList(t *testing.T) {
78	testCases := []struct {
79		haystack       LabelList
80		needle         LabelList
81		expectedResult LabelList
82	}{
83		{
84			haystack: LabelList{
85				Includes: []Label{
86					{Label: "a"},
87					{Label: "b"},
88					{Label: "c"},
89				},
90				Excludes: []Label{
91					{Label: "x"},
92					{Label: "y"},
93					{Label: "z"},
94				},
95			},
96			needle: LabelList{
97				Includes: []Label{
98					{Label: "a"},
99				},
100				Excludes: []Label{
101					{Label: "z"},
102				},
103			},
104			// NOTE: Excludes are intentionally not subtracted
105			expectedResult: LabelList{
106				Includes: []Label{
107					{Label: "b"},
108					{Label: "c"},
109				},
110				Excludes: []Label{
111					{Label: "x"},
112					{Label: "y"},
113					{Label: "z"},
114				},
115			},
116		},
117	}
118	for _, tc := range testCases {
119		actualResult := SubtractBazelLabelList(tc.haystack, tc.needle)
120		if !reflect.DeepEqual(tc.expectedResult, actualResult) {
121			t.Fatalf("Expected %v, got %v", tc.expectedResult, actualResult)
122		}
123	}
124}
125func TestUniqueBazelLabelList(t *testing.T) {
126	testCases := []struct {
127		originalLabelList       LabelList
128		expectedUniqueLabelList LabelList
129	}{
130		{
131			originalLabelList: LabelList{
132				Includes: []Label{
133					{Label: "a"},
134					{Label: "b"},
135					{Label: "a"},
136					{Label: "c"},
137				},
138				Excludes: []Label{
139					{Label: "x"},
140					{Label: "x"},
141					{Label: "y"},
142					{Label: "z"},
143				},
144			},
145			expectedUniqueLabelList: LabelList{
146				Includes: []Label{
147					{Label: "a"},
148					{Label: "b"},
149					{Label: "c"},
150				},
151				Excludes: []Label{
152					{Label: "x"},
153					{Label: "y"},
154					{Label: "z"},
155				},
156			},
157		},
158	}
159	for _, tc := range testCases {
160		actualUniqueLabelList := UniqueBazelLabelList(tc.originalLabelList)
161		if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) {
162			t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList)
163		}
164	}
165}
166