1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 "golang.org/x/tools/internal/event/keys"
13 "golang.org/x/tools/internal/event/label"
17 AKey = keys.NewString("A", "")
18 BKey = keys.NewString("B", "")
19 CKey = keys.NewString("C", "")
23 all = []label.Label{A, B, C}
26 func TestList(t *testing.T) {
27 for _, test := range []struct {
35 labels: []label.Label{A},
39 labels: []label.Label{{}},
43 labels: []label.Label{A, B},
44 expect: `A="a", B="b"`,
47 labels: []label.Label{A, B, C},
48 expect: `A="a", B="b", C="c"`,
51 labels: []label.Label{{}, B, C},
52 expect: `B="b", C="c"`,
55 labels: []label.Label{A, {}, C},
56 expect: `A="a", C="c"`,
59 labels: []label.Label{A, B, {}},
60 expect: `A="a", B="b"`,
63 labels: []label.Label{{}, {}, C},
67 labels: []label.Label{{}, B, {}},
71 labels: []label.Label{A, {}, {}},
74 t.Run(test.name, func(t *testing.T) {
75 got := printList(label.NewList(test.labels...))
76 if got != test.expect {
77 t.Errorf("got %q want %q", got, test.expect)
83 func TestFilter(t *testing.T) {
84 for _, test := range []struct {
92 expect: `A="a", B="b", C="c"`,
95 filters: []label.Key{AKey},
100 filters: []label.Key{AKey},
101 expect: `B="b", C="c"`,
105 filters: []label.Key{BKey},
106 expect: `A="a", C="c"`,
110 filters: []label.Key{CKey},
111 expect: `A="a", B="b"`,
115 filters: []label.Key{AKey, CKey},
118 t.Run(test.name, func(t *testing.T) {
119 labels := label.NewList(test.labels...)
120 got := printList(label.Filter(labels, test.filters...))
121 if got != test.expect {
122 t.Errorf("got %q want %q", got, test.expect)
128 func TestMap(t *testing.T) {
129 for _, test := range []struct {
136 keys: []label.Key{AKey},
141 keys: []label.Key{AKey},
146 keys: []label.Key{BKey},
151 keys: []label.Key{CKey},
156 keys: []label.Key{AKey, BKey, CKey},
157 expect: `A="a", B="b", C="c"`,
160 labels: []label.Label{{}, B, C},
161 keys: []label.Key{AKey, BKey, CKey},
162 expect: `nil, B="b", C="c"`,
165 labels: []label.Label{A, {}, C},
166 keys: []label.Key{AKey, BKey, CKey},
167 expect: `A="a", nil, C="c"`,
170 labels: []label.Label{A, B, {}},
171 keys: []label.Key{AKey, BKey, CKey},
172 expect: `A="a", B="b", nil`,
174 t.Run(test.name, func(t *testing.T) {
175 lm := label.NewMap(test.labels...)
176 got := printMap(lm, test.keys)
177 if got != test.expect {
178 t.Errorf("got %q want %q", got, test.expect)
184 func TestMapMerge(t *testing.T) {
185 for _, test := range []struct {
192 keys: []label.Key{AKey},
196 maps: []label.Map{label.NewMap(all...)},
197 keys: []label.Key{AKey},
201 maps: []label.Map{label.NewMap()},
202 keys: []label.Key{AKey},
206 maps: []label.Map{label.NewMap(B, C), label.NewMap(A)},
207 keys: []label.Key{AKey, BKey, CKey},
208 expect: `A="a", B="b", C="c"`,
210 name: "invalid start map",
211 maps: []label.Map{label.NewMap(), label.NewMap(B, C)},
212 keys: []label.Key{AKey, BKey, CKey},
213 expect: `nil, B="b", C="c"`,
215 name: "invalid mid map",
216 maps: []label.Map{label.NewMap(A), label.NewMap(), label.NewMap(C)},
217 keys: []label.Key{AKey, BKey, CKey},
218 expect: `A="a", nil, C="c"`,
220 name: "invalid end map",
221 maps: []label.Map{label.NewMap(A, B), label.NewMap()},
222 keys: []label.Key{AKey, BKey, CKey},
223 expect: `A="a", B="b", nil`,
225 name: "three maps one nil",
226 maps: []label.Map{label.NewMap(A), label.NewMap(B), nil},
227 keys: []label.Key{AKey, BKey, CKey},
228 expect: `A="a", B="b", nil`,
230 name: "two maps one nil",
231 maps: []label.Map{label.NewMap(A, B), nil},
232 keys: []label.Key{AKey, BKey, CKey},
233 expect: `A="a", B="b", nil`,
235 t.Run(test.name, func(t *testing.T) {
236 tagMap := label.MergeMaps(test.maps...)
237 got := printMap(tagMap, test.keys)
238 if got != test.expect {
239 t.Errorf("got %q want %q", got, test.expect)
245 func printList(list label.List) string {
246 buf := &bytes.Buffer{}
247 for index := 0; list.Valid(index); index++ {
248 l := list.Label(index)
253 buf.WriteString(", ")
260 func printMap(lm label.Map, keys []label.Key) string {
261 buf := &bytes.Buffer{}
262 for _, key := range keys {
264 buf.WriteString(", ")
266 fmt.Fprint(buf, lm.Find(key))