Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/chartconfig/load_test.go

raw symbols SCIP
1 // Copyright 2023 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 chartconfig_test
6
7 import (
8 "reflect"
9 "testing"
10
11 "golang.org/x/telemetry/internal/chartconfig"
12 )
13
14 func TestLoad(t *testing.T) {
15 // Test that we can actually load the chart config.
16 if _, err := chartconfig.Load(); err != nil {
17 t.Errorf("Load() failed: %v", err)
18 }
19 }
20
21 func TestParse(t *testing.T) {
22 tests := []struct {
23 name string
24 input string
25 want []chartconfig.ChartConfig
26 }{
27 {"empty", "", nil},
28 {"single field", "title: A", []chartconfig.ChartConfig{{Title: "A"}}},
29 {
30 "basic", `
31 title: A
32 description: B
33 type: C
34 program: D
35 counter: E
36 issue: F1
37 issue: F2
38 depth: 2
39 error: 0.1
40 version: v2.0.0
41 `,
42 []chartconfig.ChartConfig{{
43 Title: "A",
44 Description: "B",
45 Type: "C",
46 Program: "D",
47 Counter: "E",
48 Issue: []string{"F1", "F2"},
49 Depth: 2,
50 Error: 0.1,
51 Version: "v2.0.0",
52 }},
53 },
54 {
55 "partial", `
56 title: A
57 description: B
58 `,
59 []chartconfig.ChartConfig{
60 {Title: "A", Description: "B"},
61 },
62 },
63 {
64 "comments and whitespace", `
65 # A comment
66 title: A # a line comment
67
68 # Another comment
69
70 description: B
71
72
73 `,
74 []chartconfig.ChartConfig{
75 {Title: "A", Description: "B"},
76 },
77 },
78 {
79 "multi-record", `
80 # Empty records are skipped
81 ---
82 title: A
83 description: B
84
85 ---
86
87 title: C
88 description: D
89 `,
90 []chartconfig.ChartConfig{
91 {Title: "A", Description: "B"},
92 {Title: "C", Description: "D"},
93 },
94 },
95 {
96 "example", `
97 title: Editor Distribution
98 counter: gopls/editor:{emacs,vim,vscode,other}
99 description: measure editor distribution for gopls users.
100 type: partition
101 issue: TBD
102 program: golang.org/x/tools/gopls
103 `,
104 []chartconfig.ChartConfig{
105 {
106 Title: "Editor Distribution",
107 Description: "measure editor distribution for gopls users.",
108 Counter: "gopls/editor:{emacs,vim,vscode,other}",
109 Type: "partition",
110 Issue: []string{"TBD"},
111 Program: "golang.org/x/tools/gopls",
112 },
113 },
114 },
115 {
116 "multiline counter field", `
117 counter: foo:{
118 bar,
119 baz
120 }
121 `,
122 []chartconfig.ChartConfig{
123 {Counter: "foo:{bar,baz}"},
124 },
125 },
126 {
127 "multiline counter field with braces immediately next to text", `
128 counter: foo:{bar,
129 baz}
130 `,
131 []chartconfig.ChartConfig{
132 {Counter: "foo:{bar,baz}"},
133 },
134 },
135 }
136
137 for _, test := range tests {
138 t.Run(test.name, func(t *testing.T) {
139 got, err := chartconfig.Parse([]byte(test.input))
140 if err != nil {
141 t.Fatalf("Parse(...) failed: %v", err)
142 }
143 if len(got) != len(test.want) {
144 t.Fatalf("Parse(...) returned %d records, want %d", len(got), len(test.want))
145 }
146 for i, got := range got {
147 want := test.want[i]
148 if !reflect.DeepEqual(got, want) {
149 t.Errorf("Parse(...): record %d = %#v, want %#v", i, got, want)
150 }
151 }
152 })
153 }
154 }
155
156 func TestParseErrors(t *testing.T) {
157 tests := []struct {
158 name string
159 input string
160 }{
161 {
162 "leading space",
163 `
164 title: foo
165 `,
166 },
167 {
168 "unknown key",
169 `
170 foo: bar
171 `,
172 },
173 {
174 "bad separator",
175 `
176 title: foo
177 --- # comments aren't allowed after separators
178 title: bar
179 `,
180 },
181 {
182 "invalid depth",
183 `
184 depth: notanint
185 `,
186 },
187 {
188 "open curly brace not in counter field",
189 `
190 title: {
191 `,
192 },
193 {
194 "close curly brace not in counter field",
195 `
196 title: }
197 `,
198 },
199 {
200 "end of record within multiline counter field",
201 `
202 counter: foo{
203 bar
204 ---
205 title: baz
206 `,
207 },
208 {
209 "end of file within multiline counter field",
210 `
211 counter: foo{
212 bar
213 `,
214 },
215 {
216 "close curly before open curly",
217 `
218 counter: }foo{
219 bar
220 }`,
221 },
222 {
223 "open curly after close curly",
224 `
225 counter: foo{
226 bar
227 } {`,
228 },
229 {
230 "open curly after open curly same line",
231 `
232 counter: foo{{
233 bar
234 }`,
235 },
236 {
237 "open curly after open curly different line",
238 `
239 counter: foo{
240 {bar
241 }`,
242 },
243 {
244 "close curly after close curly",
245 `
246 counter: foo{
247 bar
248 } }`,
249 },
250 {
251 "comma right before close curly",
252 `
253 counter: foo{
254 bar,
255 baz,
256 } }`,
257 },
258 }
259
260 for _, test := range tests {
261 t.Run(test.name, func(t *testing.T) {
262 _, err := chartconfig.Parse([]byte(test.input))
263 if err == nil {
264 t.Fatalf("Parse(...) succeeded unexpectedly")
265 }
266 })
267 }
268 }