@@ -28,108 +28,110 @@ func TestListMarshalJSON(t *testing.T) {
28
28
}{
29
29
{
30
30
name : "encode error" ,
31
- w : newListEncoder (`` , []errorEntry {{}}, true ),
31
+ w : listMarshalTest (`` , []errorEntry {{}}, true ),
32
32
},
33
33
{
34
34
name : "f32 nan" ,
35
- w : newListEncoder (`` , []float32 {float32 (math .NaN ())}, true ),
35
+ w : listMarshalTest (`` , []float32 {float32 (math .NaN ())}, true ),
36
36
},
37
37
{
38
38
name : "f64 nan" ,
39
- w : newListEncoder (`` , []float64 {float64 (math .NaN ())}, true ),
39
+ w : listMarshalTest (`` , []float64 {float64 (math .NaN ())}, true ),
40
40
},
41
41
{
42
42
name : "nil" ,
43
- w : newListEncoder [string ](`[]` , nil , false ),
43
+ w : listMarshalTest [string ](`[]` , nil , false ),
44
44
},
45
45
{
46
46
name : "empty" ,
47
- w : newListEncoder (`[]` , []string {}, false ),
47
+ w : listMarshalTest (`[]` , []string {}, false ),
48
48
},
49
49
{
50
50
name : "bool" ,
51
- w : newListEncoder (`[true,false]` , []bool {true , false }, false ),
51
+ w : listMarshalTest (`[true,false]` , []bool {true , false }, false ),
52
52
},
53
53
{
54
54
name : "string" ,
55
- w : newListEncoder (`["one","two","three"]` , []string {"one" , "two" , "three" }, false ),
55
+ w : listMarshalTest (`["one","two","three"]` , []string {"one" , "two" , "three" }, false ),
56
56
},
57
57
{
58
58
name : "char" ,
59
- w : newListEncoder (`[104,105,127942]` , []rune {'h' , 'i' , '🏆' }, false ),
59
+ w : listMarshalTest (`[104,105,127942]` , []rune {'h' , 'i' , '🏆' }, false ),
60
60
},
61
61
{
62
62
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 ),
64
64
},
65
65
{
66
66
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 ),
68
68
},
69
69
{
70
70
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 ),
72
72
},
73
73
{
74
74
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 ),
76
76
},
77
77
{
78
78
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 ),
80
80
},
81
81
{
82
82
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 ),
84
84
},
85
85
{
86
86
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 ),
88
88
},
89
89
{
90
90
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 ),
92
92
},
93
93
{
94
94
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 ),
96
96
},
97
97
{
98
98
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 ),
100
100
},
101
101
{
102
102
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 ),
104
104
},
105
105
{
106
106
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 ),
108
108
},
109
109
}
110
110
111
111
for _ , tt := range tests {
112
112
t .Run (tt .name , func (t * testing.T ) {
113
113
// NOTE(lxf): skip marshal errors in tinygo as it uses 'defer'
114
114
// 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" ) {
116
116
return
117
117
}
118
118
119
- data , err := json .Marshal (tt .w .outer ())
119
+ data , err := json .Marshal (tt .w .List ())
120
120
if err != nil {
121
- if tt .w .wantErr () {
121
+ if tt .w .WantErr () {
122
122
return
123
123
}
124
124
125
- t .Fatal (err )
125
+ t .Error (err )
126
+ return
126
127
}
127
128
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
130
132
}
131
133
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 ) {
133
135
t .Errorf ("got (%v) != want (%v)" , string (got ), string (want ))
134
136
}
135
137
})
@@ -143,171 +145,173 @@ func TestListUnmarshalJSON(t *testing.T) {
143
145
}{
144
146
{
145
147
name : "decode error" ,
146
- w : newListDecoder (`["joe"]` , []errorEntry {}, true ),
148
+ w : listUnmarshalTest (`["joe"]` , []errorEntry {}, true ),
147
149
},
148
150
{
149
151
name : "invalid json" ,
150
- w : newListDecoder (`[joe]` , []string {}, true ),
152
+ w : listUnmarshalTest (`[joe]` , []string {}, true ),
151
153
},
152
154
{
153
155
name : "incompatible type" ,
154
- w : newListDecoder (`[123,456]` , []string {}, true ),
156
+ w : listUnmarshalTest (`[123,456]` , []string {}, true ),
155
157
},
156
158
{
157
159
name : "incompatible bool" ,
158
- w : newListDecoder (`["true","false"]` , []bool {true , false }, true ),
160
+ w : listUnmarshalTest (`["true","false"]` , []bool {true , false }, true ),
159
161
},
160
162
{
161
163
name : "incompatible s32" ,
162
- w : newListDecoder (`["123","-123","2147483647"]` , []int32 {}, true ),
164
+ w : listUnmarshalTest (`["123","-123","2147483647"]` , []int32 {}, true ),
163
165
},
164
166
{
165
167
name : "incompatible u32" ,
166
- w : newListDecoder (`["123","0","4294967295"]` , []uint32 {}, true ),
168
+ w : listUnmarshalTest (`["123","0","4294967295"]` , []uint32 {}, true ),
167
169
},
168
170
169
171
{
170
172
name : "null" ,
171
- w : newListDecoder [string ](`null` , nil , false ),
173
+ w : listUnmarshalTest [string ](`null` , nil , false ),
172
174
},
173
175
{
174
176
name : "empty" ,
175
- w : newListDecoder (`[]` , []string {}, false ),
177
+ w : listUnmarshalTest (`[]` , []string {}, false ),
176
178
},
177
179
{
178
180
name : "bool" ,
179
- w : newListDecoder (`[true,false]` , []bool {true , false }, false ),
181
+ w : listUnmarshalTest (`[true,false]` , []bool {true , false }, false ),
180
182
},
181
183
{
182
184
name : "string" ,
183
- w : newListDecoder (`["one","two","three"]` , []string {"one" , "two" , "three" }, false ),
185
+ w : listUnmarshalTest (`["one","two","three"]` , []string {"one" , "two" , "three" }, false ),
184
186
},
185
187
{
186
188
name : "char" ,
187
- w : newListDecoder (`[104,105,127942]` , []rune {'h' , 'i' , '🏆' }, false ),
189
+ w : listUnmarshalTest (`[104,105,127942]` , []rune {'h' , 'i' , '🏆' }, false ),
188
190
},
189
191
{
190
192
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 ),
192
194
},
193
195
{
194
196
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 ),
196
198
},
197
199
{
198
200
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 ),
200
202
},
201
203
{
202
204
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 ),
204
206
},
205
207
{
206
208
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 ),
208
210
},
209
211
{
210
212
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 ),
212
214
},
213
215
{
214
216
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 ),
216
218
},
217
219
{
218
220
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 ),
220
222
},
221
223
{
222
224
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 ),
224
226
},
225
227
{
226
228
name : "f32 nan" ,
227
- w : newListDecoder (`[null]` , []float32 {0 }, false ),
229
+ w : listUnmarshalTest (`[null]` , []float32 {0 }, false ),
228
230
},
229
231
{
230
232
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 ),
232
234
},
233
235
{
234
236
name : "f64 nan" ,
235
- w : newListDecoder (`[null]` , []float64 {0 }, false ),
237
+ w : listUnmarshalTest (`[null]` , []float64 {0 }, false ),
236
238
},
237
239
{
238
240
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 ),
240
242
},
241
243
{
242
244
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 ),
244
246
},
245
247
// tuple, result, option, and variant needs json implementation
246
248
}
247
249
248
250
for _ , tt := range tests {
249
251
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 ())
251
253
if err != nil {
252
- if tt .w .wantErr () {
254
+ if tt .w .WantErr () {
253
255
return
254
256
}
255
257
256
- t .Fatal (err )
258
+ t .Error (err )
259
+ return
257
260
}
258
261
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
261
265
}
262
266
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 ) {
264
268
t .Errorf ("got (%v) != want (%v)" , got , want )
265
269
}
266
270
})
267
271
}
268
272
}
269
273
270
274
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
276
280
}
277
281
278
282
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
283
287
}
284
288
285
- func (w * listWrapper [T ]) wantErr () bool {
286
- return w .err
289
+ func (w * listWrapper [T ]) WantErr () bool {
290
+ return w .wantErr
287
291
}
288
292
289
- func (w * listWrapper [T ]) outer () any {
290
- return & w .outerList
293
+ func (w * listWrapper [T ]) List () any {
294
+ return & w .list
291
295
}
292
296
293
- func (w * listWrapper [T ]) outerSlice () any {
294
- return w .outerList .Slice ()
297
+ func (w * listWrapper [T ]) Slice () any {
298
+ return w .list .Slice ()
295
299
}
296
300
297
- func (w * listWrapper [T ]) inner () any {
298
- return w .innerList
301
+ func (w * listWrapper [T ]) WantSlice () any {
302
+ return w .slice
299
303
}
300
304
301
- func (w * listWrapper [T ]) rawData () string {
302
- return w . raw
305
+ func (w * listWrapper [T ]) JSON () [] byte {
306
+ return [] byte ( w . json )
303
307
}
304
308
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 }
307
311
}
308
312
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 }
311
315
}
312
316
313
317
type testEntry struct {
0 commit comments