chore: add more tests (#3286)

This commit is contained in:
Kevin Wan
2023-05-26 22:30:03 +08:00
committed by GitHub
parent fa33329a44
commit 8b4382dcec
2 changed files with 16 additions and 14 deletions

View File

@@ -350,7 +350,12 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any)
return emptyValue, errTypeMismatch
}
targetValue.SetMapIndex(key, reflect.ValueOf(v))
val := reflect.ValueOf(v)
if !val.Type().AssignableTo(dereffedElemType) {
return emptyValue, errTypeMismatch
}
targetValue.SetMapIndex(key, val)
case json.Number:
target := reflect.New(dereffedElemType)
if err := setValueFromString(dereffedElemKind, target.Elem(), v.String()); err != nil {

View File

@@ -4400,6 +4400,16 @@ func TestUnmarshalJsonReaderWithTypeString(t *testing.T) {
body := `{"params":{"a":{"a":123}}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
})
t.Run("customized string type", func(t *testing.T) {
type myString string
var req struct {
Params map[string]myString `json:"params"`
}
body := `{"params":{"a":"b"}}`
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
})
}
func TestUnmarshalJsonReaderWithMismatchType(t *testing.T) {
@@ -4862,19 +4872,6 @@ func Test_UnmarshalMap(t *testing.T) {
})
}
// func TestUnmarshalWithFillPrimitives(t *testing.T) {
// t.Run("fill primitives", func(t *testing.T) {
// type St struct {
// A int `key:"a,string,range=[5,10]"`
// }
// var st St
// err := UnmarshalKey(map[string]any{
// "a": "1",
// }, &st)
// assert.ErrorIs(t, err, errNumberRange)
// })
// }
func TestUnmarshaler_Unmarshal(t *testing.T) {
t.Run("not struct", func(t *testing.T) {
var i int