Fix test code for debug

This commit is contained in:
Masaaki Goshima 2021-03-18 18:29:18 +09:00
parent 75d7b8e673
commit e0812246ef
1 changed files with 69 additions and 67 deletions

View File

@ -1786,80 +1786,82 @@ func equalError(a, b error) bool {
func TestUnmarshal(t *testing.T) { func TestUnmarshal(t *testing.T) {
for i, tt := range unmarshalTests { for i, tt := range unmarshalTests {
in := []byte(tt.in) t.Run(fmt.Sprintf("%d_%q", i, tt.in), func(t *testing.T) {
if tt.ptr == nil { in := []byte(tt.in)
continue if tt.ptr == nil {
} return
typ := reflect.TypeOf(tt.ptr)
if typ.Kind() != reflect.Ptr {
t.Errorf("#%d: unmarshalTest.ptr %T is not a pointer type", i, tt.ptr)
continue
}
typ = typ.Elem()
// v = new(right-type)
v := reflect.New(typ)
if !reflect.DeepEqual(tt.ptr, v.Interface()) {
// There's no reason for ptr to point to non-zero data,
// as we decode into new(right-type), so the data is
// discarded.
// This can easily mean tests that silently don't test
// what they should. To test decoding into existing
// data, see TestPrefilled.
t.Errorf("#%d: unmarshalTest.ptr %#v is not a pointer to a zero value", i, tt.ptr)
continue
}
dec := json.NewDecoder(bytes.NewReader(in))
if tt.useNumber {
dec.UseNumber()
}
if tt.disallowUnknownFields {
dec.DisallowUnknownFields()
}
if err := dec.Decode(v.Interface()); !equalError(err, tt.err) {
t.Errorf("#%d: %v, want %v", i, err, tt.err)
continue
} else if err != nil {
continue
}
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out)
data, _ := json.Marshal(v.Elem().Interface())
println(string(data))
data, _ = json.Marshal(tt.out)
println(string(data))
continue
}
// Check round trip also decodes correctly.
if tt.err == nil {
enc, err := json.Marshal(v.Interface())
if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err)
continue
} }
if tt.golden && !bytes.Equal(enc, in) {
t.Errorf("#%d: remarshal mismatch:\nhave: %s\nwant: %s", i, enc, in) typ := reflect.TypeOf(tt.ptr)
if typ.Kind() != reflect.Ptr {
t.Errorf("#%d: unmarshalTest.ptr %T is not a pointer type", i, tt.ptr)
return
} }
vv := reflect.New(reflect.TypeOf(tt.ptr).Elem()) typ = typ.Elem()
dec = json.NewDecoder(bytes.NewReader(enc))
// v = new(right-type)
v := reflect.New(typ)
if !reflect.DeepEqual(tt.ptr, v.Interface()) {
// There's no reason for ptr to point to non-zero data,
// as we decode into new(right-type), so the data is
// discarded.
// This can easily mean tests that silently don't test
// what they should. To test decoding into existing
// data, see TestPrefilled.
t.Errorf("#%d: unmarshalTest.ptr %#v is not a pointer to a zero value", i, tt.ptr)
return
}
dec := json.NewDecoder(bytes.NewReader(in))
if tt.useNumber { if tt.useNumber {
dec.UseNumber() dec.UseNumber()
} }
if err := dec.Decode(vv.Interface()); err != nil { if tt.disallowUnknownFields {
t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err) dec.DisallowUnknownFields()
continue
} }
if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) { if err := dec.Decode(v.Interface()); !equalError(err, tt.err) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface()) t.Errorf("#%d: %v, want %v", i, err, tt.err)
t.Errorf(" In: %q", strings.Map(noSpace, string(in))) return
t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc))) } else if err != nil {
continue return
} }
} if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), tt.out)
data, _ := json.Marshal(v.Elem().Interface())
println(string(data))
data, _ = json.Marshal(tt.out)
println(string(data))
return
}
// Check round trip also decodes correctly.
if tt.err == nil {
enc, err := json.Marshal(v.Interface())
if err != nil {
t.Errorf("#%d: error re-marshaling: %v", i, err)
return
}
if tt.golden && !bytes.Equal(enc, in) {
t.Errorf("#%d: remarshal mismatch:\nhave: %s\nwant: %s", i, enc, in)
}
vv := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec = json.NewDecoder(bytes.NewReader(enc))
if tt.useNumber {
dec.UseNumber()
}
if err := dec.Decode(vv.Interface()); err != nil {
t.Errorf("#%d: error re-unmarshaling %#q: %v", i, enc, err)
return
}
if !reflect.DeepEqual(v.Elem().Interface(), vv.Elem().Interface()) {
t.Errorf("#%d: mismatch\nhave: %#+v\nwant: %#+v", i, v.Elem().Interface(), vv.Elem().Interface())
t.Errorf(" In: %q", strings.Map(noSpace, string(in)))
t.Errorf("Marshal: %q", strings.Map(noSpace, string(enc)))
return
}
}
})
} }
} }