diff --git a/decoder_test.go b/decoder_test.go index 9b55c64..6f1ee7e 100644 --- a/decoder_test.go +++ b/decoder_test.go @@ -7,6 +7,7 @@ package schema import ( "encoding/hex" "errors" + "fmt" "reflect" "strings" "testing" @@ -2216,21 +2217,21 @@ func TestRequiredFieldsCannotHaveDefaults(t *testing.T) { func TestInvalidDefaultElementInSliceRaiseError(t *testing.T) { type D struct { - A []int `schema:"a,default:0|won"` - B []bool `schema:"b,default:true|invalid"` - C []*float32 `schema:"c,default:1.1|notAFloat"` + A []int `schema:"a,default:0|notInt"` + B []bool `schema:"b,default:true|notInt"` + C []*float32 `schema:"c,default:1.1|notInt"` // //uint types - D []uint `schema:"d,default:1|notUint"` - E []uint8 `schema:"e,default:2|notUint"` - F []uint16 `schema:"f,default:3|notUint"` - G []uint32 `schema:"g,default:4|notUint"` - H []uint64 `schema:"h,default:5|notUint"` + D []uint `schema:"d,default:1|notInt"` + E []uint8 `schema:"e,default:2|notInt"` + F []uint16 `schema:"f,default:3|notInt"` + G []uint32 `schema:"g,default:4|notInt"` + H []uint64 `schema:"h,default:5|notInt"` // // uint types pointers - I []*uint `schema:"i,default:6|notUint"` - J []*uint8 `schema:"j,default:7|notUint"` - K []*uint16 `schema:"k,default:12|notUint"` - L []*uint32 `schema:"l,default:129|notUint"` - M []*uint64 `schema:"m,default:11111|notUint"` + I []*uint `schema:"i,default:6|notInt"` + J []*uint8 `schema:"j,default:7|notInt"` + K []*uint16 `schema:"k,default:12|notInt"` + L []*uint32 `schema:"l,default:129|notInt"` + M []*uint64 `schema:"m,default:11111|notInt"` // // int types N []int `schema:"n,default:11|notInt"` O []int8 `schema:"o,default:12|notInt"` @@ -2244,20 +2245,40 @@ func TestInvalidDefaultElementInSliceRaiseError(t *testing.T) { V []*int32 `schema:"v,default:22222|notInt"` W []*int64 `schema:"w,default:11111|notInt"` // // float - X []float32 `schema:"c,default:2.2|notAFloat"` - Y []float64 `schema:"c,default:3.3|notAFloat"` - Z []*float64 `schema:"c,default:4.4|notAFloat"` + X []float32 `schema:"c,default:2.2|notInt"` + Y []float64 `schema:"c,default:3.3|notInt"` + Z []*float64 `schema:"c,default:4.4|notInt"` } d := D{} data := map[string][]string{} + pErrMsg := "default option is supported only on: bool, float variants, string, unit variants types or their corresponding pointers or slices" + eng := "ABCDEFGHIJKLMNOPQRSTUVWXYZ" decoder := NewDecoder() err := decoder.Decode(&d, data) if err == nil { - t.Error("if a different type exists, error should be raised.") + t.Error("if a different type exists, error should be raised") + } + + e, ok := err.(MultiError) + if !ok || len(e) != 26 { + t.Errorf("Expected 26 errors, got %#v", err) + } + for _, v := range eng { + fieldKey := "default-" + string(v) + errMsg := fmt.Sprintf("failed setting default: notInt is not compatible with field %s type", string(v)) + if ferr, ok := e[fieldKey]; ok { + // check pointer type error + if ferr.Error() == pErrMsg { + continue + } + if strings.Compare(ferr.Error(), errMsg) != 0 { + t.Errorf("%s: expected %s, got %#v", fieldKey, ferr.Error(), errMsg) + } + } } }