File: formatter_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.18-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 4,328 kB
  • sloc: sh: 61; makefile: 5
file content (288 lines) | stat: -rw-r--r-- 10,443 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package format

import (
	"reflect"
	"strings"

	"github.com/onsi/ginkgo/v2"
	"github.com/onsi/gomega"
)

// logger *log.Logger.
var (
	ts      *testStruct
	tv      reflect.Value
	nodeMap map[string]Node
)

var _ = ginkgo.Describe("SetConfigField", func() {
	testConfig := testStruct{}
	tt := reflect.TypeOf(testConfig)

	ginkgo.When("updating a struct", func() {
		ginkgo.BeforeEach(func() {
			tsPtr := reflect.New(tt)
			tv = tsPtr.Elem()
			ts = tsPtr.Interface().(*testStruct)
			gomega.Expect(tv.CanSet()).To(gomega.BeTrue())
			gomega.Expect(tv.FieldByName("TestEnum").CanSet()).To(gomega.BeTrue())
			rootNode := getRootNode(ts)
			nodeMap = make(map[string]Node, len(rootNode.Items))
			for _, item := range rootNode.Items {
				field := item.Field()
				nodeMap[field.Name] = item
			}
			gomega.Expect(int(tv.FieldByName("TestEnum").Int())).
				To(gomega.Equal(0), "TestEnum initial value")
		})
		ginkgo.When("setting an integer value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(tv, *nodeMap["Signed"].Field(), "3")
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
					gomega.Expect(ts.Signed).To(gomega.Equal(3))
				})
			})
			ginkgo.When("the value is invalid", func() {
				ginkgo.It("should return an error", func() {
					ts.Signed = 2
					valid, err := SetConfigField(tv, *nodeMap["Signed"].Field(), "z7")
					gomega.Expect(err).To(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeFalse())
					gomega.Expect(ts.Signed).To(gomega.Equal(2))
				})
			})
		})
		ginkgo.When("setting an unsigned integer value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(tv, *nodeMap["Unsigned"].Field(), "6")
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
					gomega.Expect(ts.Unsigned).To(gomega.Equal(uint(6)))
				})
			})
			ginkgo.When("the value is invalid", func() {
				ginkgo.It("should return an error", func() {
					ts.Unsigned = 2
					valid, err := SetConfigField(tv, *nodeMap["Unsigned"].Field(), "-3")
					gomega.Expect(err).To(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeFalse())
					gomega.Expect(ts.Unsigned).To(gomega.Equal(uint(2)))
				})
			})
		})
		ginkgo.When("setting a string slice value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(
						tv,
						*nodeMap["StrSlice"].Field(),
						"meawannowalkalitabitalleh,meawannofeelalitabitstrongah",
					)
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
					gomega.Expect(ts.StrSlice).To(gomega.HaveLen(2))
				})
			})
		})
		ginkgo.When("setting a string array value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(
						tv,
						*nodeMap["StrArray"].Field(),
						"meawannowalkalitabitalleh,meawannofeelalitabitstrongah,meawannothinkalitabitsmartah",
					)
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
				})
			})
			ginkgo.When("the value has too many elements", func() {
				ginkgo.It("should return an error", func() {
					valid, err := SetConfigField(
						tv,
						*nodeMap["StrArray"].Field(),
						"one,two,three,four?",
					)
					gomega.Expect(err).To(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeFalse())
				})
			})
			ginkgo.When("the value has too few elements", func() {
				ginkgo.It("should return an error", func() {
					valid, err := SetConfigField(tv, *nodeMap["StrArray"].Field(), "oneassis,two")
					gomega.Expect(err).To(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeFalse())
				})
			})
		})
		ginkgo.When("setting a struct value", func() {
			ginkgo.When("it doesn't implement ConfigProp", func() {
				ginkgo.It("should return an error", func() {
					valid, err := SetConfigField(tv, *nodeMap["Sub"].Field(), "@awol")
					gomega.Expect(err).To(gomega.HaveOccurred())
					gomega.Expect(valid).NotTo(gomega.BeTrue())
				})
			})
			ginkgo.When("it implements ConfigProp", func() {
				ginkgo.When("the value is valid", func() {
					ginkgo.It("should set it", func() {
						valid, err := SetConfigField(tv, *nodeMap["SubProp"].Field(), "@awol")
						gomega.Expect(err).NotTo(gomega.HaveOccurred())
						gomega.Expect(valid).To(gomega.BeTrue())
						gomega.Expect(ts.SubProp.Value).To(gomega.Equal("awol"))
					})
				})
				ginkgo.When("the value is invalid", func() {
					ginkgo.It("should return an error", func() {
						valid, err := SetConfigField(
							tv,
							*nodeMap["SubProp"].Field(),
							"missing initial at symbol",
						)
						gomega.Expect(err).To(gomega.HaveOccurred())
						gomega.Expect(valid).NotTo(gomega.BeTrue())
					})
				})
			})
		})
		ginkgo.When("setting a struct slice value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(
						tv,
						*nodeMap["SubPropSlice"].Field(),
						"@alice,@merton",
					)
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
					gomega.Expect(ts.SubPropSlice).To(gomega.HaveLen(2))
				})
			})
		})
		ginkgo.When("setting a struct pointer slice value", func() {
			ginkgo.When("the value is valid", func() {
				ginkgo.It("should set it", func() {
					valid, err := SetConfigField(
						tv,
						*nodeMap["SubPropPtrSlice"].Field(),
						"@the,@best",
					)
					gomega.Expect(err).NotTo(gomega.HaveOccurred())
					gomega.Expect(valid).To(gomega.BeTrue())
					gomega.Expect(ts.SubPropPtrSlice).To(gomega.HaveLen(2))
				})
			})
		})
	})
	ginkgo.When("formatting stuct values", func() {
		ginkgo.BeforeEach(func() {
			tsPtr := reflect.New(tt)
			tv = tsPtr.Elem()
			ts = tsPtr.Interface().(*testStruct)
			gomega.Expect(tv.CanSet()).To(gomega.BeTrue())
			gomega.Expect(tv.FieldByName("TestEnum").CanSet()).To(gomega.BeTrue())
			rootNode := getRootNode(ts)
			nodeMap = make(map[string]Node, len(rootNode.Items))
			for _, item := range rootNode.Items {
				field := item.Field()
				nodeMap[field.Name] = item
			}
			gomega.Expect(int(tv.FieldByName("TestEnum").Int())).
				To(gomega.Equal(0), "TestEnum initial value")
		})
		ginkgo.When("setting and formatting", func() {
			ginkgo.It("should format signed integers identical to input", func() {
				testSetAndFormat(tv, nodeMap["Signed"], "-45", "-45")
			})
			ginkgo.It("should format unsigned integers identical to input", func() {
				testSetAndFormat(tv, nodeMap["Unsigned"], "5", "5")
			})
			ginkgo.It("should format structs identical to input", func() {
				testSetAndFormat(tv, nodeMap["SubProp"], "@whoa", "@whoa")
			})
			ginkgo.It("should format enums identical to input", func() {
				testSetAndFormat(tv, nodeMap["TestEnum"], "Foo", "Foo")
			})
			ginkgo.It("should format string slices identical to input", func() {
				testSetAndFormat(
					tv,
					nodeMap["StrSlice"],
					"one,two,three,four",
					"[ one, two, three, four ]",
				)
			})
			ginkgo.It("should format string arrays identical to input", func() {
				testSetAndFormat(tv, nodeMap["StrArray"], "one,two,three", "[ one, two, three ]")
			})
			ginkgo.It("should format prop struct slices identical to input", func() {
				testSetAndFormat(
					tv,
					nodeMap["SubPropSlice"],
					"@be,@the,@best",
					"[ @be, @the, @best ]",
				)
			})
			ginkgo.It("should format prop struct pointer slices identical to input", func() {
				testSetAndFormat(tv, nodeMap["SubPropPtrSlice"], "@diet,@glue", "[ @diet, @glue ]")
			})
			ginkgo.It("should format string maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["StrMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format int maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["IntMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format int8 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Int8Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format int16 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Int16Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format int32 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Int32Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format int64 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Int64Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format uint maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["UintMap"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format uint8 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Uint8Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format uint16 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Uint16Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format uint32 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Uint32Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
			ginkgo.It("should format uint64 maps identical to input", func() {
				testSetAndFormat(tv, nodeMap["Uint64Map"], "a:1,b:2,c:3", "{ a: 1, b: 2, c: 3 }")
			})
		})
	})
})

func testSetAndFormat(tv reflect.Value, node Node, value string, prettyFormat string) {
	field := node.Field()

	valid, err := SetConfigField(tv, *field, value)
	if !valid {
		gomega.Expect(err).To(gomega.HaveOccurred(), "SetConfigField returned false but no error")
	}

	gomega.Expect(err).NotTo(gomega.HaveOccurred(), "SetConfigField error: %v", err)
	gomega.Expect(valid).To(gomega.BeTrue(), "SetConfigField failed")

	formatted, err := GetConfigFieldString(tv, *field)
	gomega.Expect(err).NotTo(gomega.HaveOccurred(), "GetConfigFieldString error: %v", err)
	gomega.Expect(formatted).To(gomega.Equal(value), "Expected %q, got %q", value, formatted)
	node.Update(tv.FieldByName(field.Name))

	sb := strings.Builder{}
	renderer := ConsoleTreeRenderer{}
	renderer.writeNodeValue(&sb, node)
	gomega.Expect(sb.String()).To(gomega.Equal(prettyFormat))
}