Skip to content

Commit 411bd9a

Browse files
committed
cm: revise List JSON tests
1 parent 921df28 commit 411bd9a

File tree

1 file changed

+85
-81
lines changed

1 file changed

+85
-81
lines changed

cm/list_test.go

Lines changed: 85 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -28,108 +28,110 @@ func TestListMarshalJSON(t *testing.T) {
2828
}{
2929
{
3030
name: "encode error",
31-
w: newListEncoder(``, []errorEntry{{}}, true),
31+
w: listMarshalTest(``, []errorEntry{{}}, true),
3232
},
3333
{
3434
name: "f32 nan",
35-
w: newListEncoder(``, []float32{float32(math.NaN())}, true),
35+
w: listMarshalTest(``, []float32{float32(math.NaN())}, true),
3636
},
3737
{
3838
name: "f64 nan",
39-
w: newListEncoder(``, []float64{float64(math.NaN())}, true),
39+
w: listMarshalTest(``, []float64{float64(math.NaN())}, true),
4040
},
4141
{
4242
name: "nil",
43-
w: newListEncoder[string](`[]`, nil, false),
43+
w: listMarshalTest[string](`[]`, nil, false),
4444
},
4545
{
4646
name: "empty",
47-
w: newListEncoder(`[]`, []string{}, false),
47+
w: listMarshalTest(`[]`, []string{}, false),
4848
},
4949
{
5050
name: "bool",
51-
w: newListEncoder(`[true,false]`, []bool{true, false}, false),
51+
w: listMarshalTest(`[true,false]`, []bool{true, false}, false),
5252
},
5353
{
5454
name: "string",
55-
w: newListEncoder(`["one","two","three"]`, []string{"one", "two", "three"}, false),
55+
w: listMarshalTest(`["one","two","three"]`, []string{"one", "two", "three"}, false),
5656
},
5757
{
5858
name: "char",
59-
w: newListEncoder(`[104,105,127942]`, []rune{'h', 'i', '🏆'}, false),
59+
w: listMarshalTest(`[104,105,127942]`, []rune{'h', 'i', '🏆'}, false),
6060
},
6161
{
6262
name: "s8",
63-
w: newListEncoder(`[123,-123,127]`, []int8{123, -123, math.MaxInt8}, false),
63+
w: listMarshalTest(`[123,-123,127]`, []int8{123, -123, math.MaxInt8}, false),
6464
},
6565
{
6666
name: "u8",
67-
w: newListEncoder(`[123,0,255]`, []uint8{123, 0, math.MaxUint8}, false),
67+
w: listMarshalTest(`[123,0,255]`, []uint8{123, 0, math.MaxUint8}, false),
6868
},
6969
{
7070
name: "s16",
71-
w: newListEncoder(`[123,-123,32767]`, []int16{123, -123, math.MaxInt16}, false),
71+
w: listMarshalTest(`[123,-123,32767]`, []int16{123, -123, math.MaxInt16}, false),
7272
},
7373
{
7474
name: "u16",
75-
w: newListEncoder(`[123,0,65535]`, []uint16{123, 0, math.MaxUint16}, false),
75+
w: listMarshalTest(`[123,0,65535]`, []uint16{123, 0, math.MaxUint16}, false),
7676
},
7777
{
7878
name: "s32",
79-
w: newListEncoder(`[123,-123,2147483647]`, []int32{123, -123, math.MaxInt32}, false),
79+
w: listMarshalTest(`[123,-123,2147483647]`, []int32{123, -123, math.MaxInt32}, false),
8080
},
8181
{
8282
name: "u32",
83-
w: newListEncoder(`[123,0,4294967295]`, []uint32{123, 0, math.MaxUint32}, false),
83+
w: listMarshalTest(`[123,0,4294967295]`, []uint32{123, 0, math.MaxUint32}, false),
8484
},
8585
{
8686
name: "s64",
87-
w: newListEncoder(`[123,-123,9223372036854775807]`, []int64{123, -123, math.MaxInt64}, false),
87+
w: listMarshalTest(`[123,-123,9223372036854775807]`, []int64{123, -123, math.MaxInt64}, false),
8888
},
8989
{
9090
name: "u64",
91-
w: newListEncoder(`[123,0,18446744073709551615]`, []uint64{123, 0, math.MaxUint64}, false),
91+
w: listMarshalTest(`[123,0,18446744073709551615]`, []uint64{123, 0, math.MaxUint64}, false),
9292
},
9393
{
9494
name: "f32",
95-
w: newListEncoder(`[1.01,2,3.4028235e+38]`, []float32{1.01, 2, math.MaxFloat32}, false),
95+
w: listMarshalTest(`[1.01,2,3.4028235e+38]`, []float32{1.01, 2, math.MaxFloat32}, false),
9696
},
9797
{
9898
name: "f64",
99-
w: newListEncoder(`[1.01,2,1.7976931348623157e+308]`, []float64{1.01, 2, math.MaxFloat64}, false),
99+
w: listMarshalTest(`[1.01,2,1.7976931348623157e+308]`, []float64{1.01, 2, math.MaxFloat64}, false),
100100
},
101101
{
102102
name: "struct",
103-
w: newListEncoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
103+
w: listMarshalTest(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
104104
},
105105
{
106106
name: "list",
107-
w: newListEncoder(`[["one","two","three"],["four","five","six"]]`, []List[string]{ToList([]string{"one", "two", "three"}), ToList([]string{"four", "five", "six"})}, false),
107+
w: listMarshalTest(`[["one","two","three"],["four","five","six"]]`, []List[string]{ToList([]string{"one", "two", "three"}), ToList([]string{"four", "five", "six"})}, false),
108108
},
109109
}
110110

111111
for _, tt := range tests {
112112
t.Run(tt.name, func(t *testing.T) {
113113
// NOTE(lxf): skip marshal errors in tinygo as it uses 'defer'
114114
// needs tinygo 0.35-dev
115-
if tt.w.wantErr() && runtime.Compiler == "tinygo" && strings.Contains(runtime.GOARCH, "wasm") {
115+
if tt.w.WantErr() && runtime.Compiler == "tinygo" && strings.Contains(runtime.GOARCH, "wasm") {
116116
return
117117
}
118118

119-
data, err := json.Marshal(tt.w.outer())
119+
data, err := json.Marshal(tt.w.List())
120120
if err != nil {
121-
if tt.w.wantErr() {
121+
if tt.w.WantErr() {
122122
return
123123
}
124124

125-
t.Fatal(err)
125+
t.Error(err)
126+
return
126127
}
127128

128-
if tt.w.wantErr() {
129-
t.Fatalf("expect error, but got none. got (%s)", string(data))
129+
if tt.w.WantErr() {
130+
t.Errorf("expected error, but got none. got (%s)", string(data))
131+
return
130132
}
131133

132-
if got, want := data, []byte(tt.w.rawData()); !bytes.Equal(got, want) {
134+
if got, want := data, tt.w.JSON(); !bytes.Equal(got, want) {
133135
t.Errorf("got (%v) != want (%v)", string(got), string(want))
134136
}
135137
})
@@ -143,171 +145,173 @@ func TestListUnmarshalJSON(t *testing.T) {
143145
}{
144146
{
145147
name: "decode error",
146-
w: newListDecoder(`["joe"]`, []errorEntry{}, true),
148+
w: listUnmarshalTest(`["joe"]`, []errorEntry{}, true),
147149
},
148150
{
149151
name: "invalid json",
150-
w: newListDecoder(`[joe]`, []string{}, true),
152+
w: listUnmarshalTest(`[joe]`, []string{}, true),
151153
},
152154
{
153155
name: "incompatible type",
154-
w: newListDecoder(`[123,456]`, []string{}, true),
156+
w: listUnmarshalTest(`[123,456]`, []string{}, true),
155157
},
156158
{
157159
name: "incompatible bool",
158-
w: newListDecoder(`["true","false"]`, []bool{true, false}, true),
160+
w: listUnmarshalTest(`["true","false"]`, []bool{true, false}, true),
159161
},
160162
{
161163
name: "incompatible s32",
162-
w: newListDecoder(`["123","-123","2147483647"]`, []int32{}, true),
164+
w: listUnmarshalTest(`["123","-123","2147483647"]`, []int32{}, true),
163165
},
164166
{
165167
name: "incompatible u32",
166-
w: newListDecoder(`["123","0","4294967295"]`, []uint32{}, true),
168+
w: listUnmarshalTest(`["123","0","4294967295"]`, []uint32{}, true),
167169
},
168170

169171
{
170172
name: "null",
171-
w: newListDecoder[string](`null`, nil, false),
173+
w: listUnmarshalTest[string](`null`, nil, false),
172174
},
173175
{
174176
name: "empty",
175-
w: newListDecoder(`[]`, []string{}, false),
177+
w: listUnmarshalTest(`[]`, []string{}, false),
176178
},
177179
{
178180
name: "bool",
179-
w: newListDecoder(`[true,false]`, []bool{true, false}, false),
181+
w: listUnmarshalTest(`[true,false]`, []bool{true, false}, false),
180182
},
181183
{
182184
name: "string",
183-
w: newListDecoder(`["one","two","three"]`, []string{"one", "two", "three"}, false),
185+
w: listUnmarshalTest(`["one","two","three"]`, []string{"one", "two", "three"}, false),
184186
},
185187
{
186188
name: "char",
187-
w: newListDecoder(`[104,105,127942]`, []rune{'h', 'i', '🏆'}, false),
189+
w: listUnmarshalTest(`[104,105,127942]`, []rune{'h', 'i', '🏆'}, false),
188190
},
189191
{
190192
name: "s8",
191-
w: newListDecoder(`[123,-123,127]`, []int8{123, -123, math.MaxInt8}, false),
193+
w: listUnmarshalTest(`[123,-123,127]`, []int8{123, -123, math.MaxInt8}, false),
192194
},
193195
{
194196
name: "u8",
195-
w: newListDecoder(`[123,0,255]`, []uint8{123, 0, math.MaxUint8}, false),
197+
w: listUnmarshalTest(`[123,0,255]`, []uint8{123, 0, math.MaxUint8}, false),
196198
},
197199
{
198200
name: "s16",
199-
w: newListDecoder(`[123,-123,32767]`, []int16{123, -123, math.MaxInt16}, false),
201+
w: listUnmarshalTest(`[123,-123,32767]`, []int16{123, -123, math.MaxInt16}, false),
200202
},
201203
{
202204
name: "u16",
203-
w: newListDecoder(`[123,0,65535]`, []uint16{123, 0, math.MaxUint16}, false),
205+
w: listUnmarshalTest(`[123,0,65535]`, []uint16{123, 0, math.MaxUint16}, false),
204206
},
205207
{
206208
name: "s32",
207-
w: newListDecoder(`[123,-123,2147483647]`, []int32{123, -123, math.MaxInt32}, false),
209+
w: listUnmarshalTest(`[123,-123,2147483647]`, []int32{123, -123, math.MaxInt32}, false),
208210
},
209211
{
210212
name: "u32",
211-
w: newListDecoder(`[123,0,4294967295]`, []uint32{123, 0, math.MaxUint32}, false),
213+
w: listUnmarshalTest(`[123,0,4294967295]`, []uint32{123, 0, math.MaxUint32}, false),
212214
},
213215
{
214216
name: "s64",
215-
w: newListDecoder(`[123,-123,9223372036854775807]`, []int64{123, -123, math.MaxInt64}, false),
217+
w: listUnmarshalTest(`[123,-123,9223372036854775807]`, []int64{123, -123, math.MaxInt64}, false),
216218
},
217219
{
218220
name: "u64",
219-
w: newListDecoder(`[123,0,18446744073709551615]`, []uint64{123, 0, math.MaxUint64}, false),
221+
w: listUnmarshalTest(`[123,0,18446744073709551615]`, []uint64{123, 0, math.MaxUint64}, false),
220222
},
221223
{
222224
name: "f32",
223-
w: newListDecoder(`[1.01,2,3.4028235e+38]`, []float32{1.01, 2, math.MaxFloat32}, false),
225+
w: listUnmarshalTest(`[1.01,2,3.4028235e+38]`, []float32{1.01, 2, math.MaxFloat32}, false),
224226
},
225227
{
226228
name: "f32 nan",
227-
w: newListDecoder(`[null]`, []float32{0}, false),
229+
w: listUnmarshalTest(`[null]`, []float32{0}, false),
228230
},
229231
{
230232
name: "f64",
231-
w: newListDecoder(`[1.01,2,1.7976931348623157e+308]`, []float64{1.01, 2, math.MaxFloat64}, false),
233+
w: listUnmarshalTest(`[1.01,2,1.7976931348623157e+308]`, []float64{1.01, 2, math.MaxFloat64}, false),
232234
},
233235
{
234236
name: "f64 nan",
235-
w: newListDecoder(`[null]`, []float64{0}, false),
237+
w: listUnmarshalTest(`[null]`, []float64{0}, false),
236238
},
237239
{
238240
name: "struct",
239-
w: newListDecoder(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
241+
w: listUnmarshalTest(`[{"name":"joe","age":10},{"name":"jane","age":20}]`, []testEntry{{Name: "joe", Age: 10}, {Name: "jane", Age: 20}}, false),
240242
},
241243
{
242244
name: "list",
243-
w: newListDecoder(`[["one","two","three"],["four","five","six"]]`, []List[string]{ToList([]string{"one", "two", "three"}), ToList([]string{"four", "five", "six"})}, false),
245+
w: listUnmarshalTest(`[["one","two","three"],["four","five","six"]]`, []List[string]{ToList([]string{"one", "two", "three"}), ToList([]string{"four", "five", "six"})}, false),
244246
},
245247
// tuple, result, option, and variant needs json implementation
246248
}
247249

248250
for _, tt := range tests {
249251
t.Run(tt.name, func(t *testing.T) {
250-
err := json.Unmarshal([]byte(tt.w.rawData()), tt.w.outer())
252+
err := json.Unmarshal(tt.w.JSON(), tt.w.List())
251253
if err != nil {
252-
if tt.w.wantErr() {
254+
if tt.w.WantErr() {
253255
return
254256
}
255257

256-
t.Fatal(err)
258+
t.Error(err)
259+
return
257260
}
258261

259-
if tt.w.wantErr() {
260-
t.Fatalf("expect error, but got none. got (%v)", tt.w.outerSlice())
262+
if tt.w.WantErr() {
263+
t.Errorf("expected error, but got none. got (%v)", tt.w.Slice())
264+
return
261265
}
262266

263-
if got, want := tt.w.outerSlice(), tt.w.inner(); !reflect.DeepEqual(got, want) {
267+
if got, want := tt.w.Slice(), tt.w.WantSlice(); !reflect.DeepEqual(got, want) {
264268
t.Errorf("got (%v) != want (%v)", got, want)
265269
}
266270
})
267271
}
268272
}
269273

270274
type listTester interface {
271-
outer() any
272-
inner() any
273-
outerSlice() any
274-
wantErr() bool
275-
rawData() string
275+
List() any
276+
WantSlice() any
277+
Slice() any
278+
WantErr() bool
279+
JSON() []byte
276280
}
277281

278282
type listWrapper[T comparable] struct {
279-
raw string
280-
outerList List[T]
281-
innerList []T
282-
err bool
283+
json string
284+
list List[T]
285+
slice []T
286+
wantErr bool
283287
}
284288

285-
func (w *listWrapper[T]) wantErr() bool {
286-
return w.err
289+
func (w *listWrapper[T]) WantErr() bool {
290+
return w.wantErr
287291
}
288292

289-
func (w *listWrapper[T]) outer() any {
290-
return &w.outerList
293+
func (w *listWrapper[T]) List() any {
294+
return &w.list
291295
}
292296

293-
func (w *listWrapper[T]) outerSlice() any {
294-
return w.outerList.Slice()
297+
func (w *listWrapper[T]) Slice() any {
298+
return w.list.Slice()
295299
}
296300

297-
func (w *listWrapper[T]) inner() any {
298-
return w.innerList
301+
func (w *listWrapper[T]) WantSlice() any {
302+
return w.slice
299303
}
300304

301-
func (w *listWrapper[T]) rawData() string {
302-
return w.raw
305+
func (w *listWrapper[T]) JSON() []byte {
306+
return []byte(w.json)
303307
}
304308

305-
func newListEncoder[T comparable](raw string, want []T, wantErr bool) *listWrapper[T] {
306-
return &listWrapper[T]{raw: raw, outerList: ToList(want), err: wantErr}
309+
func listMarshalTest[T comparable](json string, want []T, wantErr bool) *listWrapper[T] {
310+
return &listWrapper[T]{json: json, list: ToList(want), wantErr: wantErr}
307311
}
308312

309-
func newListDecoder[T comparable](raw string, want []T, wantErr bool) *listWrapper[T] {
310-
return &listWrapper[T]{raw: raw, innerList: want, err: wantErr}
313+
func listUnmarshalTest[T comparable](json string, want []T, wantErr bool) *listWrapper[T] {
314+
return &listWrapper[T]{json: json, slice: want, wantErr: wantErr}
311315
}
312316

313317
type testEntry struct {

0 commit comments

Comments
 (0)