Skip to content

Commit 2e6a4b4

Browse files
committed
cm: reorganize List JSON tests
1 parent a22a157 commit 2e6a4b4

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

cm/list_test.go

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,75 +21,14 @@ func TestListMethods(t *testing.T) {
2121
}
2222
}
2323

24-
type listTestItem struct {
25-
Name string `json:"name"`
26-
Age int `json:"age"`
27-
}
28-
29-
type listTestInvalid struct {
30-
Name string `json:"name"`
31-
Age int `json:"age"`
32-
}
33-
34-
type listTestWrapper[T comparable] struct {
35-
raw string
36-
outerList List[T]
37-
innerList []T
38-
err bool
39-
}
40-
41-
func (w *listTestWrapper[T]) wantErr() bool {
42-
return w.err
43-
}
44-
45-
func (w *listTestWrapper[T]) outer() any {
46-
return &w.outerList
47-
}
48-
49-
func (w *listTestWrapper[T]) outerSlice() any {
50-
return w.outerList.Slice()
51-
}
52-
53-
func (w *listTestWrapper[T]) inner() any {
54-
return w.innerList
55-
}
56-
57-
func (w *listTestWrapper[T]) rawData() string {
58-
return w.raw
59-
}
60-
61-
func newListEncoder[T comparable](raw string, want []T, wantErr bool) *listTestWrapper[T] {
62-
return &listTestWrapper[T]{raw: raw, outerList: ToList(want), err: wantErr}
63-
}
64-
65-
func newListDecoder[T comparable](raw string, want []T, wantErr bool) *listTestWrapper[T] {
66-
return &listTestWrapper[T]{raw: raw, innerList: want, err: wantErr}
67-
}
68-
69-
type listTester interface {
70-
outer() any
71-
inner() any
72-
outerSlice() any
73-
wantErr() bool
74-
rawData() string
75-
}
76-
77-
func (_ listTestInvalid) MarshalJSON() ([]byte, error) {
78-
return nil, fmt.Errorf("can't encode")
79-
}
80-
81-
func (_ *listTestInvalid) UnmarshalJSON(_ []byte) error {
82-
return fmt.Errorf("can't decode")
83-
}
84-
8524
func TestListMarshalJSON(t *testing.T) {
8625
tests := []struct {
8726
name string
8827
w listTester
8928
}{
9029
{
9130
name: "encode error",
92-
w: newListEncoder(``, []listTestInvalid{{}}, true),
31+
w: newListEncoder(``, []errorEntry{{}}, true),
9332
},
9433
{
9534
name: "f32 nan",
@@ -161,7 +100,7 @@ func TestListMarshalJSON(t *testing.T) {
161100
},
162101
{
163102
name: "struct",
164-
w: newListEncoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []listTestItem{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
103+
w: newListEncoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
165104
},
166105
{
167106
name: "list",
@@ -204,7 +143,7 @@ func TestListUnmarshalJSON(t *testing.T) {
204143
}{
205144
{
206145
name: "decode error",
207-
w: newListDecoder(`["joe"]`, []listTestInvalid{}, true),
146+
w: newListDecoder(`["joe"]`, []errorEntry{}, true),
208147
},
209148
{
210149
name: "invalid json",
@@ -297,7 +236,7 @@ func TestListUnmarshalJSON(t *testing.T) {
297236
},
298237
{
299238
name: "struct",
300-
w: newListDecoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []listTestItem{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
239+
w: newListDecoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
301240
},
302241
{
303242
name: "list",
@@ -327,3 +266,64 @@ func TestListUnmarshalJSON(t *testing.T) {
327266
})
328267
}
329268
}
269+
270+
type listTester interface {
271+
outer() any
272+
inner() any
273+
outerSlice() any
274+
wantErr() bool
275+
rawData() string
276+
}
277+
278+
type listWrapper[T comparable] struct {
279+
raw string
280+
outerList List[T]
281+
innerList []T
282+
err bool
283+
}
284+
285+
func (w *listWrapper[T]) wantErr() bool {
286+
return w.err
287+
}
288+
289+
func (w *listWrapper[T]) outer() any {
290+
return &w.outerList
291+
}
292+
293+
func (w *listWrapper[T]) outerSlice() any {
294+
return w.outerList.Slice()
295+
}
296+
297+
func (w *listWrapper[T]) inner() any {
298+
return w.innerList
299+
}
300+
301+
func (w *listWrapper[T]) rawData() string {
302+
return w.raw
303+
}
304+
305+
func newListEncoder[T comparable](raw string, want []T, wantErr bool) *listWrapper[T] {
306+
return &listWrapper[T]{raw: raw, outerList: ToList(want), err: wantErr}
307+
}
308+
309+
func newListDecoder[T comparable](raw string, want []T, wantErr bool) *listWrapper[T] {
310+
return &listWrapper[T]{raw: raw, innerList: want, err: wantErr}
311+
}
312+
313+
type testEntry struct {
314+
Name string `json:"name"`
315+
Age int `json:"age"`
316+
}
317+
318+
type errorEntry struct {
319+
Name string `json:"name"`
320+
Age int `json:"age"`
321+
}
322+
323+
func (errorEntry) MarshalJSON() ([]byte, error) {
324+
return nil, fmt.Errorf("can't encode")
325+
}
326+
327+
func (*errorEntry) UnmarshalJSON(_ []byte) error {
328+
return fmt.Errorf("can't decode")
329+
}

0 commit comments

Comments
 (0)