2020-03-20 06:29:48 -06:00
|
|
|
// Copyright 2020 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
package label_test
|
2020-03-20 06:29:48 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2020-04-20 19:50:02 -06:00
|
|
|
"golang.org/x/tools/internal/event/keys"
|
2020-04-20 13:44:34 -06:00
|
|
|
"golang.org/x/tools/internal/event/label"
|
2020-03-20 06:29:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-04-20 19:50:02 -06:00
|
|
|
AKey = keys.NewString("A", "")
|
|
|
|
BKey = keys.NewString("B", "")
|
|
|
|
CKey = keys.NewString("C", "")
|
2020-03-20 06:29:48 -06:00
|
|
|
A = AKey.Of("a")
|
|
|
|
B = BKey.Of("b")
|
|
|
|
C = CKey.Of("c")
|
2020-04-20 13:44:34 -06:00
|
|
|
all = []label.Label{A, B, C}
|
2020-03-20 06:29:48 -06:00
|
|
|
)
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func TestList(t *testing.T) {
|
2020-03-20 06:29:48 -06:00
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
2020-04-20 13:44:34 -06:00
|
|
|
labels []label.Label
|
2020-03-20 06:29:48 -06:00
|
|
|
expect string
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
}, {
|
|
|
|
name: "single",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a"`,
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{{}},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: ``,
|
|
|
|
}, {
|
|
|
|
name: "two",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, B},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b"`,
|
|
|
|
}, {
|
|
|
|
name: "three",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, B, C},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing A",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{{}, B, C},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing B",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, {}, C},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing C",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, B, {}},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b"`,
|
|
|
|
}, {
|
|
|
|
name: "missing AB",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{{}, {}, C},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing AC",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{{}, B, {}},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `B="b"`,
|
|
|
|
}, {
|
|
|
|
name: "missing BC",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, {}, {}},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a"`,
|
|
|
|
}} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-04-20 13:44:34 -06:00
|
|
|
got := printList(label.NewList(test.labels...))
|
2020-03-20 06:29:48 -06:00
|
|
|
if got != test.expect {
|
|
|
|
t.Errorf("got %q want %q", got, test.expect)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func TestFilter(t *testing.T) {
|
2020-03-20 06:29:48 -06:00
|
|
|
for _, test := range []struct {
|
|
|
|
name string
|
2020-04-20 13:44:34 -06:00
|
|
|
labels []label.Label
|
|
|
|
filters []label.Key
|
2020-03-20 06:29:48 -06:00
|
|
|
expect string
|
|
|
|
}{{
|
|
|
|
name: "no filters",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", C="c"`,
|
|
|
|
}, {
|
2020-04-20 13:44:34 -06:00
|
|
|
name: "no labels",
|
|
|
|
filters: []label.Key{AKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: ``,
|
|
|
|
}, {
|
|
|
|
name: "filter A",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
filters: []label.Key{AKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "filter B",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
filters: []label.Key{BKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "filter C",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
filters: []label.Key{CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b"`,
|
|
|
|
}, {
|
|
|
|
name: "filter AC",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
filters: []label.Key{AKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `B="b"`,
|
|
|
|
}} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-04-20 13:44:34 -06:00
|
|
|
labels := label.NewList(test.labels...)
|
|
|
|
got := printList(label.Filter(labels, test.filters...))
|
2020-03-20 06:29:48 -06:00
|
|
|
if got != test.expect {
|
|
|
|
t.Errorf("got %q want %q", got, test.expect)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func TestMap(t *testing.T) {
|
2020-03-20 06:29:48 -06:00
|
|
|
for _, test := range []struct {
|
2020-03-30 06:04:25 -06:00
|
|
|
name string
|
2020-04-20 13:44:34 -06:00
|
|
|
labels []label.Label
|
|
|
|
keys []label.Key
|
2020-03-30 06:04:25 -06:00
|
|
|
expect string
|
2020-03-20 06:29:48 -06:00
|
|
|
}{{
|
2020-04-20 13:44:34 -06:00
|
|
|
name: "no labels",
|
|
|
|
keys: []label.Key{AKey},
|
2020-03-30 06:04:25 -06:00
|
|
|
expect: `nil`,
|
2020-03-20 06:29:48 -06:00
|
|
|
}, {
|
|
|
|
name: "match A",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
keys: []label.Key{AKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a"`,
|
|
|
|
}, {
|
|
|
|
name: "match B",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
keys: []label.Key{BKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `B="b"`,
|
|
|
|
}, {
|
|
|
|
name: "match C",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
keys: []label.Key{CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "match ABC",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: all,
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing A",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{{}, B, C},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `nil, B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing B",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, {}, C},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", nil, C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "missing C",
|
2020-04-20 13:44:34 -06:00
|
|
|
labels: []label.Label{A, B, {}},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", nil`,
|
|
|
|
}} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-04-20 13:44:34 -06:00
|
|
|
lm := label.NewMap(test.labels...)
|
|
|
|
got := printMap(lm, test.keys)
|
2020-03-20 06:29:48 -06:00
|
|
|
if got != test.expect {
|
|
|
|
t.Errorf("got %q want %q", got, test.expect)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func TestMapMerge(t *testing.T) {
|
2020-03-20 06:29:48 -06:00
|
|
|
for _, test := range []struct {
|
2020-03-30 06:04:25 -06:00
|
|
|
name string
|
2020-04-20 13:44:34 -06:00
|
|
|
maps []label.Map
|
|
|
|
keys []label.Key
|
2020-03-30 06:04:25 -06:00
|
|
|
expect string
|
2020-03-20 06:29:48 -06:00
|
|
|
}{{
|
2020-03-30 06:04:25 -06:00
|
|
|
name: "no maps",
|
2020-04-20 13:44:34 -06:00
|
|
|
keys: []label.Key{AKey},
|
2020-03-30 06:04:25 -06:00
|
|
|
expect: `nil`,
|
2020-03-20 06:29:48 -06:00
|
|
|
}, {
|
|
|
|
name: "one map",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(all...)},
|
|
|
|
keys: []label.Key{AKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a"`,
|
|
|
|
}, {
|
2020-03-30 06:04:25 -06:00
|
|
|
name: "invalid map",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap()},
|
|
|
|
keys: []label.Key{AKey},
|
2020-03-30 06:04:25 -06:00
|
|
|
expect: `nil`,
|
2020-03-20 06:29:48 -06:00
|
|
|
}, {
|
|
|
|
name: "two maps",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(B, C), label.NewMap(A)},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "invalid start map",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(), label.NewMap(B, C)},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `nil, B="b", C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "invalid mid map",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(A), label.NewMap(), label.NewMap(C)},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", nil, C="c"`,
|
|
|
|
}, {
|
|
|
|
name: "invalid end map",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(A, B), label.NewMap()},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-04-20 20:49:14 -06:00
|
|
|
expect: `A="a", B="b", nil`,
|
|
|
|
}, {
|
|
|
|
name: "three maps one nil",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(A), label.NewMap(B), nil},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-04-20 20:49:14 -06:00
|
|
|
expect: `A="a", B="b", nil`,
|
|
|
|
}, {
|
|
|
|
name: "two maps one nil",
|
2020-04-20 13:44:34 -06:00
|
|
|
maps: []label.Map{label.NewMap(A, B), nil},
|
|
|
|
keys: []label.Key{AKey, BKey, CKey},
|
2020-03-20 06:29:48 -06:00
|
|
|
expect: `A="a", B="b", nil`,
|
|
|
|
}} {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2020-04-20 13:44:34 -06:00
|
|
|
tagMap := label.MergeMaps(test.maps...)
|
|
|
|
got := printMap(tagMap, test.keys)
|
2020-03-20 06:29:48 -06:00
|
|
|
if got != test.expect {
|
|
|
|
t.Errorf("got %q want %q", got, test.expect)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func printList(list label.List) string {
|
2020-03-20 06:29:48 -06:00
|
|
|
buf := &bytes.Buffer{}
|
2020-04-20 13:44:34 -06:00
|
|
|
for index := 0; list.Valid(index); index++ {
|
|
|
|
l := list.Label(index)
|
|
|
|
if !l.Valid() {
|
2020-04-03 21:06:57 -06:00
|
|
|
continue
|
|
|
|
}
|
2020-03-20 06:29:48 -06:00
|
|
|
if buf.Len() > 0 {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
fmt.Fprint(buf, l)
|
2020-03-20 06:29:48 -06:00
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func printMap(lm label.Map, keys []label.Key) string {
|
2020-03-20 06:29:48 -06:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
for _, key := range keys {
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
buf.WriteString(", ")
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
fmt.Fprint(buf, lm.Find(key))
|
2020-03-20 06:29:48 -06:00
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|