Debian Codegraph @

pkg/format/formatter_test.go

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