Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 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 | |
| 15 | package bazel |
| 16 | |
| 17 | import ( |
| 18 | "reflect" |
| 19 | "testing" |
| 20 | ) |
| 21 | |
| 22 | func 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 { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 42 | actualUniqueLabels := UniqueSortedBazelLabels(tc.originalLabels) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 43 | if !reflect.DeepEqual(tc.expectedUniqueLabels, actualUniqueLabels) { |
| 44 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabels, actualUniqueLabels) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 49 | func 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 | |
| 77 | func 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 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame^] | 125 | func TestFirstUniqueBazelLabelList(t *testing.T) { |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 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 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame^] | 160 | actualUniqueLabelList := FirstUniqueBazelLabelList(tc.originalLabelList) |
| 161 | if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) { |
| 162 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestUniqueSortedBazelLabelList(t *testing.T) { |
| 168 | testCases := []struct { |
| 169 | originalLabelList LabelList |
| 170 | expectedUniqueLabelList LabelList |
| 171 | }{ |
| 172 | { |
| 173 | originalLabelList: LabelList{ |
| 174 | Includes: []Label{ |
| 175 | {Label: "c"}, |
| 176 | {Label: "a"}, |
| 177 | {Label: "a"}, |
| 178 | {Label: "b"}, |
| 179 | }, |
| 180 | Excludes: []Label{ |
| 181 | {Label: "y"}, |
| 182 | {Label: "z"}, |
| 183 | {Label: "x"}, |
| 184 | {Label: "x"}, |
| 185 | }, |
| 186 | }, |
| 187 | expectedUniqueLabelList: LabelList{ |
| 188 | Includes: []Label{ |
| 189 | {Label: "a"}, |
| 190 | {Label: "b"}, |
| 191 | {Label: "c"}, |
| 192 | }, |
| 193 | Excludes: []Label{ |
| 194 | {Label: "x"}, |
| 195 | {Label: "y"}, |
| 196 | {Label: "z"}, |
| 197 | }, |
| 198 | }, |
| 199 | }, |
| 200 | } |
| 201 | for _, tc := range testCases { |
| 202 | actualUniqueLabelList := UniqueSortedBazelLabelList(tc.originalLabelList) |
Rupert Shuttleworth | 2e4219b | 2021-03-12 11:04:21 +0000 | [diff] [blame] | 203 | if !reflect.DeepEqual(tc.expectedUniqueLabelList, actualUniqueLabelList) { |
| 204 | t.Fatalf("Expected %v, got %v", tc.expectedUniqueLabelList, actualUniqueLabelList) |
| 205 | } |
| 206 | } |
| 207 | } |