Debian Codegraph @

pkg/format/formatter_test.go

raw
1 package format
2
3 import (
4 "reflect"
5 "strings"
6 "time"
7
8 "github.com/onsi/ginkgo/v2"
9 "github.com/onsi/gomega"
10 )
11
12 // logger *log.Logger.
13 var (
14 ts *testStruct
15 tv reflect.Value
16 nodeMap map[string]Node
17 )
18
19 var _ = ginkgo.Describe("SetConfigField", func() {
20 tt := reflect.TypeFor[testStruct]()
21
22 ginkgo.When("updating a struct", func() {
23 ginkgo.BeforeEach(func() {
24 tsPtr := reflect.New(tt)
25 tv = tsPtr.Elem()
26 ts = tsPtr.Interface().(*testStruct)
27
28 gomega.Expect(tv.CanSet()).To(gomega.BeTrue())
29 gomega.Expect(tv.FieldByName("TestEnum").CanSet()).To(gomega.BeTrue())
30
31 rootNode := getRootNode(ts)
32
33 nodeMap = make(map[string]Node, len(rootNode.Items))
34 for _, item := range rootNode.Items {
35 field := item.Field()
36 nodeMap[field.Name] = item
37 }
38
39 gomega.Expect(int(tv.FieldByName("TestEnum").Int())).
40 To(gomega.Equal(0), "TestEnum initial value")
41 })
42 ginkgo.When("setting an integer value", func() {
43 ginkgo.When("the value is valid", func() {
44 ginkgo.It("should set it", func() {
45 valid, err := SetConfigField(tv, nodeMap["Signed"].Field(), "3")
46 gomega.Expect(err).NotTo(gomega.HaveOccurred())
47 gomega.Expect(valid).To(gomega.BeTrue())
48 gomega.Expect(ts.Signed).To(gomega.Equal(3))
49 })
50 })
51 ginkgo.When("the value is invalid", func() {
52 ginkgo.It("should return an error", func() {
53 ts.Signed = 2
54 valid, err := SetConfigField(tv, nodeMap["Signed"].Field(), "z7")
55 gomega.Expect(err).To(gomega.HaveOccurred())
56 gomega.Expect(valid).To(gomega.BeFalse())
57 gomega.Expect(ts.Signed).To(gomega.Equal(2))
58 })
59 })
60 })
61 ginkgo.When("setting an unsigned integer value", func() {
62 ginkgo.When("the value is valid", func() {
63 ginkgo.It("should set it", func() {
64 valid, err := SetConfigField(tv, nodeMap["Unsigned"].Field(), "6")
65 gomega.Expect(err).NotTo(gomega.HaveOccurred())
66 gomega.Expect(valid).To(gomega.BeTrue())
67 gomega.Expect(ts.Unsigned).To(gomega.Equal(uint(6)))
68 })
69 })
70 ginkgo.When("the value is invalid", func() {
71 ginkgo.It("should return an error", func() {
72 ts.Unsigned = 2
73 valid, err := SetConfigField(tv, nodeMap["Unsigned"].Field(), "-3")
74 gomega.Expect(err).To(gomega.HaveOccurred())
75 gomega.Expect(valid).To(gomega.BeFalse())
76 gomega.Expect(ts.Unsigned).To(gomega.Equal(uint(2)))
77 })
78 })
79 })
80 ginkgo.When("setting a duration value", func() {
81 ginkgo.When("the value has a unit", func() {
82 ginkgo.It("should set it", func() {
83 valid, err := SetConfigField(tv, nodeMap["Duration"].Field(), "1h2m3s")
84 gomega.Expect(err).NotTo(gomega.HaveOccurred())
85 gomega.Expect(valid).To(gomega.BeTrue())
86 gomega.Expect(ts.Duration).
87 To(gomega.Equal(time.Hour + 2*time.Minute + 3*time.Second))
88 })
89 })
90 ginkgo.When("the value is a bare non-zero number", func() {
91 ginkgo.It("should return an error rather than assume nanoseconds", func() {
92 ts.Duration = time.Second
93 valid, err := SetConfigField(tv, nodeMap["Duration"].Field(), "10")
94 gomega.Expect(err).To(gomega.MatchError(ErrParseDurationFailed))
95 gomega.Expect(valid).To(gomega.BeFalse())
96 gomega.Expect(ts.Duration).To(gomega.Equal(time.Second))
97 })
98 })
99 ginkgo.When("the value is a bare zero", func() {
100 ginkgo.It("should be accepted as a zero duration", func() {
101 ts.Duration = time.Second
102 valid, err := SetConfigField(tv, nodeMap["Duration"].Field(), "0")
103 gomega.Expect(err).NotTo(gomega.HaveOccurred())
104 gomega.Expect(valid).To(gomega.BeTrue())
105 gomega.Expect(ts.Duration).To(gomega.BeZero())
106 })
107 })
108 ginkgo.When("the value is invalid", func() {
109 ginkgo.It("should return an error", func() {
110 ts.Duration = time.Second
111 valid, err := SetConfigField(tv, nodeMap["Duration"].Field(), "z7")
112 gomega.Expect(err).To(gomega.MatchError(ErrParseDurationFailed))
113 gomega.Expect(valid).To(gomega.BeFalse())
114 gomega.Expect(ts.Duration).To(gomega.Equal(time.Second))
115 })
116 })
117 })
118 ginkgo.When("setting a string slice value", func() {
119 ginkgo.When("the value is valid", func() {
120 ginkgo.It("should set it", func() {
121 valid, err := SetConfigField(
122 tv,
123 nodeMap["StrSlice"].Field(),
124 "meawannowalkalitabitalleh,meawannofeelalitabitstrongah",
125 )
126 gomega.Expect(err).NotTo(gomega.HaveOccurred())
127 gomega.Expect(valid).To(gomega.BeTrue())
128 gomega.Expect(ts.StrSlice).To(gomega.HaveLen(2))
129 })
130 })
131 })
132 ginkgo.When("setting a string array value", func() {
133 ginkgo.When("the value is valid", func() {
134 ginkgo.It("should set it", func() {
135 valid, err := SetConfigField(
136 tv,
137 nodeMap["StrArray"].Field(),
138 "meawannowalkalitabitalleh,meawannofeelalitabitstrongah,meawannothinkalitabitsmartah",
139 )
140 gomega.Expect(err).NotTo(gomega.HaveOccurred())
141 gomega.Expect(valid).To(gomega.BeTrue())
142 })
143 })
144 ginkgo.When("the value has too many elements", func() {
145 ginkgo.It("should return an error", func() {
146 valid, err := SetConfigField(
147 tv,
148 nodeMap["StrArray"].Field(),
149 "one,two,three,four?",
150 )
151 gomega.Expect(err).To(gomega.HaveOccurred())
152 gomega.Expect(valid).To(gomega.BeFalse())
153 })
154 })
155 ginkgo.When("the value has too few elements", func() {
156 ginkgo.It("should return an error", func() {
157 valid, err := SetConfigField(tv, nodeMap["StrArray"].Field(), "oneassis,two")
158 gomega.Expect(err).To(gomega.HaveOccurred())
159 gomega.Expect(valid).To(gomega.BeFalse())
160 })
161 })
162 })
163 ginkgo.When("setting a struct value", func() {
164 ginkgo.When("it doesn't implement ConfigProp", func() {
165 ginkgo.It("should return an error", func() {
166 valid, err := SetConfigField(tv, nodeMap["Sub"].Field(), "@awol")
167 gomega.Expect(err).To(gomega.HaveOccurred())
168 gomega.Expect(valid).NotTo(gomega.BeTrue())
169 })
170 })
171 ginkgo.When("it implements ConfigProp", func() {
172 ginkgo.When("the value is valid", func() {
173 ginkgo.It("should set it", func() {
174 valid, err := SetConfigField(tv, nodeMap["SubProp"].Field(), "@awol")
175 gomega.Expect(err).NotTo(gomega.HaveOccurred())
176 gomega.Expect(valid).To(gomega.BeTrue())
177 gomega.Expect(ts.SubProp.Value).To(gomega.Equal("awol"))
178 })
179 })
180 ginkgo.When("the value is invalid", func() {
181 ginkgo.It("should return an error", func() {
182 valid, err := SetConfigField(
183 tv,
184 nodeMap["SubProp"].Field(),
185 "missing initial at symbol",
186 )
187 gomega.Expect(err).To(gomega.HaveOccurred())
188 gomega.Expect(valid).NotTo(gomega.BeTrue())
189 })
190 })
191 })
192 })
193 ginkgo.When("setting a struct slice value", func() {
194 ginkgo.When("the value is valid", func() {
195 ginkgo.It("should set it", func() {
196 valid, err := SetConfigField(
197 tv,
198 nodeMap["SubPropSlice"].Field(),
199 "@alice,@merton",
200 )
201 gomega.Expect(err).NotTo(gomega.HaveOccurred())
202 gomega.Expect(valid).To(gomega.BeTrue())
203 gomega.Expect(ts.SubPropSlice).To(gomega.HaveLen(2))
204 })
205 })
206 })
207 ginkgo.When("setting a struct pointer slice value", func() {
208 ginkgo.When("the value is valid", func() {
209 ginkgo.It("should set it", func() {
210 valid, err := SetConfigField(
211 tv,
212 nodeMap["SubPropPtrSlice"].Field(),
213 "@the,@best",
214 )
215 gomega.Expect(err).NotTo(gomega.HaveOccurred())
216 gomega.Expect(valid).To(gomega.BeTrue())
217 gomega.Expect(ts.SubPropPtrSlice).To(gomega.HaveLen(2))
218 })
219 })
220 })
221 })
222 ginkgo.When("formatting stuct values", func() {
223 ginkgo.BeforeEach(func() {
224 tsPtr := reflect.New(tt)
225 tv = tsPtr.Elem()
226 ts = tsPtr.Interface().(*testStruct)
227
228 gomega.Expect(tv.CanSet()).To(gomega.BeTrue())
229 gomega.Expect(tv.FieldByName("TestEnum").CanSet()).To(gomega.BeTrue())
230
231 rootNode := getRootNode(ts)
232
233 nodeMap = make(map[string]Node, len(rootNode.Items))
234 for _, item := range rootNode.Items {
235 field := item.Field()
236 nodeMap[field.Name] = item
237 }
238
239 gomega.Expect(int(tv.FieldByName("TestEnum").Int())).
240 To(gomega.Equal(0), "TestEnum initial value")
241 })
242 ginkgo.When("setting and formatting", func() {
243 ginkgo.It("should format signed integers identical to input", func() {
244 testSetAndFormat(tv, nodeMap["Signed"], "-45", "-45")
245 })
246 ginkgo.It("should format unsigned integers identical to input", func() {
247 testSetAndFormat(tv, nodeMap["Unsigned"], "5", "5")
248 })
249 ginkgo.It("should format durations identical to input", func() {
250 testSetAndFormat(tv, nodeMap["Duration"], "1h2m3s", "1h2m3s")
251 })
252 ginkgo.It("should format structs identical to input", func() {
253 testSetAndFormat(tv, nodeMap["SubProp"], "@whoa", "@whoa")
254 })
255 ginkgo.It("should format enums identical to input", func() {
256 testSetAndFormat(tv, nodeMap["TestEnum"], "Foo", "Foo")
257 })
258 ginkgo.It("should format string slices identical to input", func() {
259 testSetAndFormat(
260 tv,
261 nodeMap["StrSlice"],
262 "one,two,three,four",
263 "[ one, two, three, four ]",
264 )
265 })
266 ginkgo.It("should format string arrays identical to input", func() {
267 testSetAndFormat(tv, nodeMap["StrArray"], "one,two,three", "[ one, two, three ]")
268 })
269 ginkgo.It("should format prop struct slices identical to input", func() {
270 testSetAndFormat(
271 tv,
272 nodeMap["SubPropSlice"],
273 "@be,@the,@best",
274 "[ @be, @the, @best ]",
275 )
276 })
277 ginkgo.It("should format prop struct pointer slices identical to input", func() {
278 testSetAndFormat(tv, nodeMap["SubPropPtrSlice"], "@diet,@glue", "[ @diet, @glue ]")
279 })
280 ginkgo.It("should format string maps identical to input", func() {
281 testSetAndFormat(tv, nodeMap["StrMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
282 })
283 ginkgo.It("should format int maps identical to input", func() {
284 testSetAndFormat(tv, nodeMap["IntMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
285 })
286 ginkgo.It("should format int8 maps identical to input", func() {
287 testSetAndFormat(tv, nodeMap["Int8Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
288 })
289 ginkgo.It("should format int16 maps identical to input", func() {
290 testSetAndFormat(tv, nodeMap["Int16Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
291 })
292 ginkgo.It("should format int32 maps identical to input", func() {
293 testSetAndFormat(tv, nodeMap["Int32Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
294 })
295 ginkgo.It("should format int64 maps identical to input", func() {
296 testSetAndFormat(tv, nodeMap["Int64Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
297 })
298 ginkgo.It("should format uint maps identical to input", func() {
299 testSetAndFormat(tv, nodeMap["UintMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
300 })
301 ginkgo.It("should format uint8 maps identical to input", func() {
302 testSetAndFormat(tv, nodeMap["Uint8Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
303 })
304 ginkgo.It("should format uint16 maps identical to input", func() {
305 testSetAndFormat(tv, nodeMap["Uint16Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
306 })
307 ginkgo.It("should format uint32 maps identical to input", func() {
308 testSetAndFormat(tv, nodeMap["Uint32Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
309 })
310 ginkgo.It("should format uint64 maps identical to input", func() {
311 testSetAndFormat(tv, nodeMap["Uint64Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
312 })
313 })
314 })
315 })
316
317 func testSetAndFormat(tv reflect.Value, node Node, value, prettyFormat string) {
318 field := node.Field()
319
320 valid, err := SetConfigField(tv, field, value)
321 if !valid {
322 gomega.Expect(err).To(gomega.HaveOccurred(), "SetConfigField returned false but no error")
323 }
324
325 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "SetConfigField error: %v", err)
326 gomega.Expect(valid).To(gomega.BeTrue(), "SetConfigField failed")
327
328 formatted, err := GetConfigFieldString(tv, field)
329 gomega.Expect(err).NotTo(gomega.HaveOccurred(), "GetConfigFieldString error: %v", err)
330 gomega.Expect(formatted).To(gomega.Equal(value), "Expected %q, got %q", value, formatted)
331 node.Update(tv.FieldByName(field.Name))
332
333 sb := strings.Builder{}
334 renderer := ConsoleTreeRenderer{}
335 renderer.writeNodeValue(&sb, node)
336 gomega.Expect(sb.String()).To(gomega.Equal(prettyFormat))
337 }