Skip to content

Commit 4aa733a

Browse files
committed
test(spew): fully refactored of spew tests
Signed-off-by: Frederic BIDON <[email protected]>
1 parent 405d74c commit 4aa733a

File tree

10 files changed

+1951
-1623
lines changed

10 files changed

+1951
-1623
lines changed

internal/spew/common_test.go

Lines changed: 131 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -27,136 +27,6 @@ import (
2727
"github.com/go-openapi/testify/v2/internal/spew"
2828
)
2929

30-
// custom type to test Stinger interface on non-pointer receiver.
31-
type stringer string
32-
33-
// String implements the Stringer interface for testing invocation of custom
34-
// stringers on types with non-pointer receivers.
35-
func (s stringer) String() string {
36-
return "stringer " + string(s)
37-
}
38-
39-
// custom type to test Stinger interface on pointer receiver.
40-
type pstringer string
41-
42-
// String implements the Stringer interface for testing invocation of custom
43-
// stringers on types with only pointer receivers.
44-
func (s *pstringer) String() string {
45-
return "stringer " + string(*s)
46-
}
47-
48-
// xref1 and xref2 are cross referencing structs for testing circular reference
49-
// detection.
50-
type xref1 struct {
51-
ps2 *xref2
52-
}
53-
type xref2 struct {
54-
ps1 *xref1
55-
}
56-
57-
// indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
58-
// reference for testing detection.
59-
type indirCir1 struct {
60-
ps2 *indirCir2
61-
}
62-
type indirCir2 struct {
63-
ps3 *indirCir3
64-
}
65-
type indirCir3 struct {
66-
ps1 *indirCir1
67-
}
68-
69-
// embed is used to test embedded structures.
70-
type embed struct {
71-
a string
72-
}
73-
74-
// embedwrap is used to test embedded structures.
75-
type embedwrap struct {
76-
*embed
77-
78-
e *embed
79-
}
80-
81-
// panicer is used to intentionally cause a panic for testing spew properly
82-
// handles them.
83-
type panicer int
84-
85-
func (p panicer) String() string {
86-
panic("test panic")
87-
}
88-
89-
// customError is used to test custom error interface invocation.
90-
type customError int
91-
92-
func (e customError) Error() string {
93-
return fmt.Sprintf("error: %d", int(e))
94-
}
95-
96-
// stringizeWants verts a slice of wanted test output into a format suitable
97-
// for a test error message.
98-
func stringizeWants(wants []string) string {
99-
s := ""
100-
var sSb97 strings.Builder
101-
for i, want := range wants {
102-
if i > 0 {
103-
sSb97.WriteString(fmt.Sprintf("want%d: %s", i+1, want))
104-
} else {
105-
sSb97.WriteString("want: " + want)
106-
}
107-
}
108-
s += sSb97.String()
109-
return s
110-
}
111-
112-
// testFailed returns whether or not a test failed by checking if the result
113-
// of the test is in the slice of wanted strings.
114-
func testFailed(result string, wants []string) bool {
115-
return !slices.Contains(wants, result)
116-
}
117-
118-
type sortableStruct struct {
119-
x int
120-
}
121-
122-
func (ss sortableStruct) String() string {
123-
return fmt.Sprintf("ss.%d", ss.x)
124-
}
125-
126-
type unsortableStruct struct {
127-
x int
128-
}
129-
130-
type sortTestCase struct {
131-
input []reflect.Value
132-
expected []reflect.Value
133-
}
134-
135-
func helpTestSortValues(t *testing.T, tests []sortTestCase, cs *spew.ConfigState) {
136-
t.Helper()
137-
138-
getInterfaces := func(values []reflect.Value) []any {
139-
interfaces := []any{}
140-
for _, v := range values {
141-
interfaces = append(interfaces, v.Interface())
142-
}
143-
return interfaces
144-
}
145-
146-
for _, test := range tests {
147-
spew.SortValues(test.input, cs)
148-
// reflect.DeepEqual cannot really make sense of reflect.Value,
149-
// probably because of all the pointer tricks. For instance,
150-
// v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{}
151-
// instead.
152-
input := getInterfaces(test.input)
153-
expected := getInterfaces(test.expected)
154-
if !reflect.DeepEqual(input, expected) {
155-
t.Errorf("Sort mismatch:\n %v != %v\n\n%#v != %#v", input, expected, input, expected)
156-
}
157-
}
158-
}
159-
16030
// TestSortValues ensures the sort functionality for relect.Value based sorting
16131
// works as intended.
16232
func TestSortValues(t *testing.T) {
@@ -273,6 +143,7 @@ func TestSortValues(t *testing.T) {
273143
},
274144
}
275145
cs := spew.ConfigState{DisableMethods: true, SpewKeys: false}
146+
276147
helpTestSortValues(t, tests, &cs)
277148
}
278149

@@ -382,6 +253,136 @@ func TestSortTimeValuesWithString(t *testing.T) {
382253
helpTestSortValues(t, tests, &cs)
383254
}
384255

256+
// custom type to test Stinger interface on non-pointer receiver.
257+
type stringer string
258+
259+
// String implements the Stringer interface for testing invocation of custom
260+
// stringers on types with non-pointer receivers.
261+
func (s stringer) String() string {
262+
return "stringer " + string(s)
263+
}
264+
265+
// custom type to test Stinger interface on pointer receiver.
266+
type pstringer string
267+
268+
// String implements the Stringer interface for testing invocation of custom
269+
// stringers on types with only pointer receivers.
270+
func (s *pstringer) String() string {
271+
return "stringer " + string(*s)
272+
}
273+
274+
// xref1 and xref2 are cross referencing structs for testing circular reference
275+
// detection.
276+
type xref1 struct {
277+
ps2 *xref2
278+
}
279+
type xref2 struct {
280+
ps1 *xref1
281+
}
282+
283+
// indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
284+
// reference for testing detection.
285+
type indirCir1 struct {
286+
ps2 *indirCir2
287+
}
288+
type indirCir2 struct {
289+
ps3 *indirCir3
290+
}
291+
type indirCir3 struct {
292+
ps1 *indirCir1
293+
}
294+
295+
// embed is used to test embedded structures.
296+
type embed struct {
297+
a string
298+
}
299+
300+
// embedwrap is used to test embedded structures.
301+
type embedwrap struct {
302+
*embed
303+
304+
e *embed
305+
}
306+
307+
// panicer is used to intentionally cause a panic for testing spew properly
308+
// handles them.
309+
type panicer int
310+
311+
func (p panicer) String() string {
312+
panic("test panic")
313+
}
314+
315+
// customError is used to test custom error interface invocation.
316+
type customError int
317+
318+
func (e customError) Error() string {
319+
return fmt.Sprintf("error: %d", int(e))
320+
}
321+
322+
// stringizeWants verts a slice of wanted test output into a format suitable
323+
// for a test error message.
324+
func stringizeWants(wants []string) string {
325+
s := ""
326+
var sSb97 strings.Builder
327+
for i, want := range wants {
328+
if i > 0 {
329+
sSb97.WriteString(fmt.Sprintf("want%d: %s", i+1, want))
330+
} else {
331+
sSb97.WriteString("want: " + want)
332+
}
333+
}
334+
s += sSb97.String()
335+
return s
336+
}
337+
338+
// testFailed returns whether or not a test failed by checking if the result
339+
// of the test is in the slice of wanted strings.
340+
func testFailed(result string, wants []string) bool {
341+
return !slices.Contains(wants, result)
342+
}
343+
344+
type sortableStruct struct {
345+
x int
346+
}
347+
348+
func (ss sortableStruct) String() string {
349+
return fmt.Sprintf("ss.%d", ss.x)
350+
}
351+
352+
type unsortableStruct struct {
353+
x int
354+
}
355+
356+
type sortTestCase struct {
357+
input []reflect.Value
358+
expected []reflect.Value
359+
}
360+
361+
func helpTestSortValues(t *testing.T, tests []sortTestCase, cs *spew.ConfigState) {
362+
t.Helper()
363+
364+
getInterfaces := func(values []reflect.Value) []any {
365+
interfaces := make([]any, 0, len(values))
366+
for _, v := range values {
367+
interfaces = append(interfaces, v.Interface())
368+
}
369+
return interfaces
370+
}
371+
372+
for _, test := range tests {
373+
spew.SortValues(test.input, cs)
374+
// reflect.DeepEqual cannot really make sense of reflect.Value,
375+
// probably because of all the pointer tricks. For instance,
376+
// v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{}
377+
// instead.
378+
input := getInterfaces(test.input)
379+
expected := getInterfaces(test.expected)
380+
if !reflect.DeepEqual(input, expected) {
381+
t.Errorf("Sort mismatch:\n %v != %v\n\n%#v != %#v", input, expected, input, expected)
382+
}
383+
}
384+
}
385+
385386
func testTimings() (t0, t1, t2 time.Time) {
386387
t0 = time.Now()
387388
t1 = t0.Add(time.Hour)

0 commit comments

Comments
 (0)