Debian Codegraph @

combination_approvals.go

raw
1 package approvaltests
2
3 import (
4 "fmt"
5 "strings"
6
7 "reflect"
8 )
9
10 type emptyType struct{}
11
12 var (
13 // SkipThisCombination should be returned if you do not want to process a particular combination
14 SkipThisCombination = "♬ SKIP THIS COMBINATION ♬"
15
16 empty = emptyType{}
17 emptyCollection = []emptyType{empty}
18 )
19
20 // VerifyAllCombinationsFor1 Example:
21 // VerifyAllCombinationsFor1(t, "uppercase", func(x interface{}) string { return strings.ToUpper(x.(string)) }, []string("dog", "cat"})
22 func VerifyAllCombinationsFor1(t Failable, header string, transform func(interface{}) string, collection1 interface{}) error {
23 transform2 := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
24 return transform(p1)
25 }
26
27 return VerifyAllCombinationsFor9(t, header, transform2, collection1,
28 emptyCollection,
29 emptyCollection,
30 emptyCollection,
31 emptyCollection,
32 emptyCollection,
33 emptyCollection,
34 emptyCollection,
35 emptyCollection)
36 }
37
38 // VerifyAllCombinationsFor2 calls the transform function with all combinations
39 // from collection 1 and collection 2. The resulting received file contains all
40 // inputs and the resulting outputs. The received file is then compared to the
41 // approved version. If the transform function returns SkipThisCombination the
42 // output of this combination won't be displayed inside the received file.
43 func VerifyAllCombinationsFor2(t Failable, header string, transform func(interface{}, interface{}) string, collection1 interface{}, collection2 interface{}) error {
44 transform2 := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
45 return transform(p1, p2)
46 }
47
48 return VerifyAllCombinationsFor9(t, header, transform2, collection1,
49 collection2,
50 emptyCollection,
51 emptyCollection,
52 emptyCollection,
53 emptyCollection,
54 emptyCollection,
55 emptyCollection,
56 emptyCollection)
57 }
58
59 // VerifyAllCombinationsFor3 is for combinations of 3.
60 func VerifyAllCombinationsFor3(
61 t Failable,
62 header string,
63 transform func(p1, p2, p3 interface{}) string,
64 collection1, collection2, collection3 interface{}) error {
65
66 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
67 return transform(p1, p2, p3)
68 }
69
70 return VerifyAllCombinationsFor9(t, header, kerning,
71 collection1,
72 collection2,
73 collection3,
74 emptyCollection,
75 emptyCollection,
76 emptyCollection,
77 emptyCollection,
78 emptyCollection,
79 emptyCollection)
80 }
81
82 // VerifyAllCombinationsFor4 is for combinations of 4.
83 func VerifyAllCombinationsFor4(
84 t Failable,
85 header string,
86 transform func(p1, p2, p3, p4 interface{}) string,
87 collection1, collection2, collection3, collection4 interface{}) error {
88
89 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
90 return transform(p1, p2, p3, p4)
91 }
92
93 return VerifyAllCombinationsFor9(t, header, kerning,
94 collection1,
95 collection2,
96 collection3,
97 collection4,
98 emptyCollection,
99 emptyCollection,
100 emptyCollection,
101 emptyCollection,
102 emptyCollection)
103 }
104
105 // VerifyAllCombinationsFor5 is for combinations of 5.
106 func VerifyAllCombinationsFor5(
107 t Failable,
108 header string,
109 transform func(p1, p2, p3, p4, p5 interface{}) string,
110 collection1, collection2, collection3, collection4, collection5 interface{}) error {
111
112 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
113 return transform(p1, p2, p3, p4, p5)
114 }
115
116 return VerifyAllCombinationsFor9(t, header, kerning,
117 collection1,
118 collection2,
119 collection3,
120 collection4,
121 collection5,
122 emptyCollection,
123 emptyCollection,
124 emptyCollection,
125 emptyCollection)
126 }
127
128 // VerifyAllCombinationsFor6 is for combinations of 6.
129 func VerifyAllCombinationsFor6(
130 t Failable,
131 header string,
132 transform func(p1, p2, p3, p4, p5, p6 interface{}) string,
133 collection1, collection2, collection3, collection4, collection5, collection6 interface{}) error {
134
135 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
136 return transform(p1, p2, p3, p4, p5, p6)
137 }
138
139 return VerifyAllCombinationsFor9(t, header, kerning,
140 collection1,
141 collection2,
142 collection3,
143 collection4,
144 collection5,
145 collection6,
146 emptyCollection,
147 emptyCollection,
148 emptyCollection)
149 }
150
151 // VerifyAllCombinationsFor7 is for combinations of 7.
152 func VerifyAllCombinationsFor7(
153 t Failable,
154 header string,
155 transform func(p1, p2, p3, p4, p5, p6, p7 interface{}) string,
156 collection1, collection2, collection3, collection4, collection5, collection6, collection7 interface{}) error {
157
158 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
159 return transform(p1, p2, p3, p4, p5, p6, p7)
160 }
161
162 return VerifyAllCombinationsFor9(t, header, kerning,
163 collection1,
164 collection2,
165 collection3,
166 collection4,
167 collection5,
168 collection6,
169 collection7,
170 emptyCollection,
171 emptyCollection)
172 }
173
174 // VerifyAllCombinationsFor8 is for combinations of 8.
175 func VerifyAllCombinationsFor8(
176 t Failable,
177 header string,
178 transform func(p1, p2, p3, p4, p5, p6, p7, p8 interface{}) string,
179 collection1, collection2, collection3, collection4, collection5, collection6, collection7, collection8 interface{}) error {
180
181 kerning := func(p1, p2, p3, p4, p5, p6, p7, p8, p9 interface{}) string {
182 return transform(p1, p2, p3, p4, p5, p6, p7, p8)
183 }
184
185 return VerifyAllCombinationsFor9(t, header, kerning,
186 collection1,
187 collection2,
188 collection3,
189 collection4,
190 collection5,
191 collection6,
192 collection7,
193 collection8,
194 emptyCollection)
195 }
196
197 // VerifyAllCombinationsFor9 is for combinations of 9.
198 func VerifyAllCombinationsFor9(
199 t Failable,
200 header string,
201 transform func(a, b, c, d, e, f, g, h, i interface{}) string,
202 collection1,
203 collection2,
204 collection3,
205 collection4,
206 collection5,
207 collection6,
208 collection7,
209 collection8,
210 collection9 interface{}) error {
211
212 if len(header) != 0 {
213 header = fmt.Sprintf("%s\n\n\n", header)
214 }
215
216 var mapped []string
217
218 slice1 := reflect.ValueOf(collection1)
219 slice2 := reflect.ValueOf(collection2)
220 slice3 := reflect.ValueOf(collection3)
221 slice4 := reflect.ValueOf(collection4)
222 slice5 := reflect.ValueOf(collection5)
223 slice6 := reflect.ValueOf(collection6)
224 slice7 := reflect.ValueOf(collection7)
225 slice8 := reflect.ValueOf(collection8)
226 slice9 := reflect.ValueOf(collection9)
227
228 for i1 := 0; i1 < slice1.Len(); i1++ {
229 for i2 := 0; i2 < slice2.Len(); i2++ {
230 for i3 := 0; i3 < slice3.Len(); i3++ {
231 for i4 := 0; i4 < slice4.Len(); i4++ {
232 for i5 := 0; i5 < slice5.Len(); i5++ {
233 for i6 := 0; i6 < slice6.Len(); i6++ {
234 for i7 := 0; i7 < slice7.Len(); i7++ {
235 for i8 := 0; i8 < slice8.Len(); i8++ {
236 for i9 := 0; i9 < slice9.Len(); i9++ {
237 p1 := slice1.Index(i1).Interface()
238 p2 := slice2.Index(i2).Interface()
239 p3 := slice3.Index(i3).Interface()
240 p4 := slice4.Index(i4).Interface()
241 p5 := slice5.Index(i5).Interface()
242 p6 := slice6.Index(i6).Interface()
243 p7 := slice7.Index(i7).Interface()
244 p8 := slice8.Index(i8).Interface()
245 p9 := slice9.Index(i9).Interface()
246
247 parameterText := getParameterText(p1, p2, p3, p4, p5, p6, p7, p8, p9)
248 transformText := getTransformText(transform, p1, p2, p3, p4, p5, p6, p7, p8, p9)
249 if transformText != SkipThisCombination {
250 mapped = append(mapped, fmt.Sprintf("%s => %s", parameterText, transformText))
251 }
252 }
253 }
254 }
255 }
256 }
257 }
258 }
259 }
260 }
261
262 outputText := header + strings.Join(mapped, "\n")
263 return VerifyString(t, outputText)
264 }
265
266 func getParameterText(args ...interface{}) string {
267 parameterText := "["
268 for _, x := range args {
269 if x != empty {
270 parameterText += fmt.Sprintf("%v,", x)
271 }
272 }
273
274 parameterText = parameterText[0 : len(parameterText)-1]
275 parameterText += "]"
276
277 return parameterText
278 }
279
280 func getTransformText(
281 transform func(a, b, c, d, e, f, g, h, i interface{}) string,
282 p1,
283 p2,
284 p3,
285 p4,
286 p5,
287 p6,
288 p7,
289 p8,
290 p9 interface{}) (s string) {
291 defer func() {
292 r := recover()
293 if r != nil {
294 s = "panic occurred"
295 }
296 }()
297
298 return transform(p1, p2, p3, p4, p5, p6, p7, p8, p9)
299 }