Update .bashrc
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.0 / internal / event / label / label_test.go
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.
4
5 package label_test
6
7 import (
8         "bytes"
9         "fmt"
10         "testing"
11
12         "golang.org/x/tools/internal/event/keys"
13         "golang.org/x/tools/internal/event/label"
14 )
15
16 var (
17         AKey = keys.NewString("A", "")
18         BKey = keys.NewString("B", "")
19         CKey = keys.NewString("C", "")
20         A    = AKey.Of("a")
21         B    = BKey.Of("b")
22         C    = CKey.Of("c")
23         all  = []label.Label{A, B, C}
24 )
25
26 func TestList(t *testing.T) {
27         for _, test := range []struct {
28                 name   string
29                 labels []label.Label
30                 expect string
31         }{{
32                 name: "empty",
33         }, {
34                 name:   "single",
35                 labels: []label.Label{A},
36                 expect: `A="a"`,
37         }, {
38                 name:   "invalid",
39                 labels: []label.Label{{}},
40                 expect: ``,
41         }, {
42                 name:   "two",
43                 labels: []label.Label{A, B},
44                 expect: `A="a", B="b"`,
45         }, {
46                 name:   "three",
47                 labels: []label.Label{A, B, C},
48                 expect: `A="a", B="b", C="c"`,
49         }, {
50                 name:   "missing A",
51                 labels: []label.Label{{}, B, C},
52                 expect: `B="b", C="c"`,
53         }, {
54                 name:   "missing B",
55                 labels: []label.Label{A, {}, C},
56                 expect: `A="a", C="c"`,
57         }, {
58                 name:   "missing C",
59                 labels: []label.Label{A, B, {}},
60                 expect: `A="a", B="b"`,
61         }, {
62                 name:   "missing AB",
63                 labels: []label.Label{{}, {}, C},
64                 expect: `C="c"`,
65         }, {
66                 name:   "missing AC",
67                 labels: []label.Label{{}, B, {}},
68                 expect: `B="b"`,
69         }, {
70                 name:   "missing BC",
71                 labels: []label.Label{A, {}, {}},
72                 expect: `A="a"`,
73         }} {
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)
78                         }
79                 })
80         }
81 }
82
83 func TestFilter(t *testing.T) {
84         for _, test := range []struct {
85                 name    string
86                 labels  []label.Label
87                 filters []label.Key
88                 expect  string
89         }{{
90                 name:   "no filters",
91                 labels: all,
92                 expect: `A="a", B="b", C="c"`,
93         }, {
94                 name:    "no labels",
95                 filters: []label.Key{AKey},
96                 expect:  ``,
97         }, {
98                 name:    "filter A",
99                 labels:  all,
100                 filters: []label.Key{AKey},
101                 expect:  `B="b", C="c"`,
102         }, {
103                 name:    "filter B",
104                 labels:  all,
105                 filters: []label.Key{BKey},
106                 expect:  `A="a", C="c"`,
107         }, {
108                 name:    "filter C",
109                 labels:  all,
110                 filters: []label.Key{CKey},
111                 expect:  `A="a", B="b"`,
112         }, {
113                 name:    "filter AC",
114                 labels:  all,
115                 filters: []label.Key{AKey, CKey},
116                 expect:  `B="b"`,
117         }} {
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)
123                         }
124                 })
125         }
126 }
127
128 func TestMap(t *testing.T) {
129         for _, test := range []struct {
130                 name   string
131                 labels []label.Label
132                 keys   []label.Key
133                 expect string
134         }{{
135                 name:   "no labels",
136                 keys:   []label.Key{AKey},
137                 expect: `nil`,
138         }, {
139                 name:   "match A",
140                 labels: all,
141                 keys:   []label.Key{AKey},
142                 expect: `A="a"`,
143         }, {
144                 name:   "match B",
145                 labels: all,
146                 keys:   []label.Key{BKey},
147                 expect: `B="b"`,
148         }, {
149                 name:   "match C",
150                 labels: all,
151                 keys:   []label.Key{CKey},
152                 expect: `C="c"`,
153         }, {
154                 name:   "match ABC",
155                 labels: all,
156                 keys:   []label.Key{AKey, BKey, CKey},
157                 expect: `A="a", B="b", C="c"`,
158         }, {
159                 name:   "missing A",
160                 labels: []label.Label{{}, B, C},
161                 keys:   []label.Key{AKey, BKey, CKey},
162                 expect: `nil, B="b", C="c"`,
163         }, {
164                 name:   "missing B",
165                 labels: []label.Label{A, {}, C},
166                 keys:   []label.Key{AKey, BKey, CKey},
167                 expect: `A="a", nil, C="c"`,
168         }, {
169                 name:   "missing C",
170                 labels: []label.Label{A, B, {}},
171                 keys:   []label.Key{AKey, BKey, CKey},
172                 expect: `A="a", B="b", nil`,
173         }} {
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)
179                         }
180                 })
181         }
182 }
183
184 func TestMapMerge(t *testing.T) {
185         for _, test := range []struct {
186                 name   string
187                 maps   []label.Map
188                 keys   []label.Key
189                 expect string
190         }{{
191                 name:   "no maps",
192                 keys:   []label.Key{AKey},
193                 expect: `nil`,
194         }, {
195                 name:   "one map",
196                 maps:   []label.Map{label.NewMap(all...)},
197                 keys:   []label.Key{AKey},
198                 expect: `A="a"`,
199         }, {
200                 name:   "invalid map",
201                 maps:   []label.Map{label.NewMap()},
202                 keys:   []label.Key{AKey},
203                 expect: `nil`,
204         }, {
205                 name:   "two maps",
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"`,
209         }, {
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"`,
214         }, {
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"`,
219         }, {
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`,
224         }, {
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`,
229         }, {
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`,
234         }} {
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)
240                         }
241                 })
242         }
243 }
244
245 func printList(list label.List) string {
246         buf := &bytes.Buffer{}
247         for index := 0; list.Valid(index); index++ {
248                 l := list.Label(index)
249                 if !l.Valid() {
250                         continue
251                 }
252                 if buf.Len() > 0 {
253                         buf.WriteString(", ")
254                 }
255                 fmt.Fprint(buf, l)
256         }
257         return buf.String()
258 }
259
260 func printMap(lm label.Map, keys []label.Key) string {
261         buf := &bytes.Buffer{}
262         for _, key := range keys {
263                 if buf.Len() > 0 {
264                         buf.WriteString(", ")
265                 }
266                 fmt.Fprint(buf, lm.Find(key))
267         }
268         return buf.String()
269 }