Skip to content

Commit

Permalink
fix wrong test & use reflect.IsNil instead of reflect.IsZero for Go 1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Sep 22, 2020
1 parent 834bca7 commit 4ab9470
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 72 deletions.
2 changes: 1 addition & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func SetValueFromString(f reflect.Value, value string) error {

for ft.Kind() == reflect.Ptr {
ft = ft.Elem()
if f.IsZero() {
if f.IsNil() {
f.Set(reflect.New(ft))
}
f = f.Elem()
Expand Down
51 changes: 16 additions & 35 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type ValueStringSliceMapperSample struct {
JBools []bool
KTimes []valueStringTime

LPtrBools []*bool
MPtrPtrBools []**bool
// unsupported
// LPtrBools []*bool
// MPtrPtrBools []**bool

ValueStringSliceMapperSampleInner

Expand Down Expand Up @@ -141,13 +142,13 @@ func TestValueStringSliceMapper(t *testing.T) {
valueStringSliceMapper(target, "IFloat64s", []string{"2.75", "22.75"})
valueStringSliceMapper(target, "JBools", []string{"true", "false"})
valueStringSliceMapper(target, "KTimes", []string{"2016-01-07", "2016-04-05"})
valueStringSliceMapper(target, "LPtrBool", []string{"true", "false"})
valueStringSliceMapper(target, "MPtrPtrBool", []string{"true", "false"})
// valueStringSliceMapper(target, "LPtrBools", []string{"true", "false"})
// valueStringSliceMapper(target, "MPtrPtrBools", []string{"true", "false"})
valueStringSliceMapper(target, "YString", []string{"This is Y"})
valueStringSliceMapper(target, "ZString", []string{})

if len(obj.AStrings) != 2 {
t.Errorf("unexpected A len: %v", len(obj.AStrings))
t.Fatalf("unexpected A len: %v", len(obj.AStrings))
}
if obj.AStrings[0] != "This is A1" {
t.Errorf("unexpected A[0]: %v", obj.AStrings[0])
Expand All @@ -157,7 +158,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.BStrings) != 2 {
t.Errorf("unexpected B len: %v", len(obj.BStrings))
t.Fatalf("unexpected B len: %v", len(obj.BStrings))
}
if obj.BStrings[0] != "This is B1" {
t.Errorf("unexpected B[0]: %v", obj.BStrings[0])
Expand All @@ -167,7 +168,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.CStrings) != 2 {
t.Errorf("unexpected C len: %v", len(obj.CStrings))
t.Fatalf("unexpected C len: %v", len(obj.CStrings))
}
if obj.CStrings[0] != "This is C1" {
t.Errorf("unexpected C[0]: %v", obj.CStrings[0])
Expand All @@ -177,7 +178,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.DInt8s) != 2 {
t.Errorf("unexpected D len: %v", len(obj.DInt8s))
t.Fatalf("unexpected D len: %v", len(obj.DInt8s))
}
if obj.DInt8s[0] != 1 {
t.Errorf("unexpected D[0]: %v", obj.DInt8s[0])
Expand All @@ -187,7 +188,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.EInt64s) != 2 {
t.Errorf("unexpected E len: %v", len(obj.EInt64s))
t.Fatalf("unexpected E len: %v", len(obj.EInt64s))
}
if obj.EInt64s[0] != 2 {
t.Errorf("unexpected E[0]: %v", obj.EInt64s[0])
Expand All @@ -197,7 +198,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.FUint8s) != 2 {
t.Errorf("unexpected F len: %v", len(obj.FUint8s))
t.Fatalf("unexpected F len: %v", len(obj.FUint8s))
}
if obj.FUint8s[0] != 3 {
t.Errorf("unexpected F[0]: %v", obj.FUint8s[0])
Expand All @@ -207,7 +208,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.GUint64s) != 2 {
t.Errorf("unexpected G len: %v", len(obj.GUint64s))
t.Fatalf("unexpected G len: %v", len(obj.GUint64s))
}
if obj.GUint64s[0] != 4 {
t.Errorf("unexpected G[0]: %v", obj.GUint64s[0])
Expand All @@ -217,7 +218,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.HFloat32s) != 2 {
t.Errorf("unexpected H len: %v", len(obj.HFloat32s))
t.Fatalf("unexpected H len: %v", len(obj.HFloat32s))
}
if obj.HFloat32s[0] != 1.25 {
t.Errorf("unexpected H[0]: %v", obj.HFloat32s[0])
Expand All @@ -227,7 +228,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.IFloat64s) != 2 {
t.Errorf("unexpected I len: %v", len(obj.IFloat64s))
t.Fatalf("unexpected I len: %v", len(obj.IFloat64s))
}
if obj.IFloat64s[0] != 2.75 {
t.Errorf("unexpected I[0]: %v", obj.IFloat64s[0])
Expand All @@ -237,7 +238,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.JBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
t.Fatalf("unexpected J len: %v", len(obj.JBools))
}
if obj.JBools[0] != true {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
Expand All @@ -247,7 +248,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.KTimes) != 2 {
t.Errorf("unexpected K len: %v", len(obj.KTimes))
t.Fatalf("unexpected K len: %v", len(obj.KTimes))
}
if y, m, d := time.Time(obj.KTimes[0]).Date(); y != 2016 {
t.Errorf("unexpected KTime[0].y: %v", y)
Expand All @@ -264,26 +265,6 @@ func TestValueStringSliceMapper(t *testing.T) {
t.Errorf("unexpected KTime[1].d: %v", d)
}

if len(obj.LPtrBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
}
if obj.LPtrBools[0] != nil && *obj.LPtrBools[0] {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
}
if obj.LPtrBools[1] != nil && !*obj.LPtrBools[1] {
t.Errorf("unexpected J[1]: %v", obj.JBools[1])
}

if len(obj.MPtrPtrBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
}
if obj.MPtrPtrBools[0] != nil && **obj.MPtrPtrBools[0] {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
}
if obj.MPtrPtrBools[1] != nil && !**obj.MPtrPtrBools[1] {
t.Errorf("unexpected J[1]: %v", obj.JBools[1])
}

if obj.YString != "This is Y" {
t.Errorf("unexpected Y: %v", obj.YString)
}
Expand Down
2 changes: 1 addition & 1 deletion v3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func SetValueFromString(f reflect.Value, value string) error {

for ft.Kind() == reflect.Ptr {
ft = ft.Elem()
if f.IsZero() {
if f.IsNil() {
f.Set(reflect.New(ft))
}
f = f.Elem()
Expand Down
51 changes: 16 additions & 35 deletions v3/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ type ValueStringSliceMapperSample struct {
JBools []bool
KTimes []valueStringTime

LPtrBools []*bool
MPtrPtrBools []**bool
// unsupported
// LPtrBools []*bool
// MPtrPtrBools []**bool

ValueStringSliceMapperSampleInner

Expand Down Expand Up @@ -141,13 +142,13 @@ func TestValueStringSliceMapper(t *testing.T) {
valueStringSliceMapper(target, "IFloat64s", []string{"2.75", "22.75"})
valueStringSliceMapper(target, "JBools", []string{"true", "false"})
valueStringSliceMapper(target, "KTimes", []string{"2016-01-07", "2016-04-05"})
valueStringSliceMapper(target, "LPtrBool", []string{"true", "false"})
valueStringSliceMapper(target, "MPtrPtrBool", []string{"true", "false"})
// valueStringSliceMapper(target, "LPtrBools", []string{"true", "false"})
// valueStringSliceMapper(target, "MPtrPtrBools", []string{"true", "false"})
valueStringSliceMapper(target, "YString", []string{"This is Y"})
valueStringSliceMapper(target, "ZString", []string{})

if len(obj.AStrings) != 2 {
t.Errorf("unexpected A len: %v", len(obj.AStrings))
t.Fatalf("unexpected A len: %v", len(obj.AStrings))
}
if obj.AStrings[0] != "This is A1" {
t.Errorf("unexpected A[0]: %v", obj.AStrings[0])
Expand All @@ -157,7 +158,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.BStrings) != 2 {
t.Errorf("unexpected B len: %v", len(obj.BStrings))
t.Fatalf("unexpected B len: %v", len(obj.BStrings))
}
if obj.BStrings[0] != "This is B1" {
t.Errorf("unexpected B[0]: %v", obj.BStrings[0])
Expand All @@ -167,7 +168,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.CStrings) != 2 {
t.Errorf("unexpected C len: %v", len(obj.CStrings))
t.Fatalf("unexpected C len: %v", len(obj.CStrings))
}
if obj.CStrings[0] != "This is C1" {
t.Errorf("unexpected C[0]: %v", obj.CStrings[0])
Expand All @@ -177,7 +178,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.DInt8s) != 2 {
t.Errorf("unexpected D len: %v", len(obj.DInt8s))
t.Fatalf("unexpected D len: %v", len(obj.DInt8s))
}
if obj.DInt8s[0] != 1 {
t.Errorf("unexpected D[0]: %v", obj.DInt8s[0])
Expand All @@ -187,7 +188,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.EInt64s) != 2 {
t.Errorf("unexpected E len: %v", len(obj.EInt64s))
t.Fatalf("unexpected E len: %v", len(obj.EInt64s))
}
if obj.EInt64s[0] != 2 {
t.Errorf("unexpected E[0]: %v", obj.EInt64s[0])
Expand All @@ -197,7 +198,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.FUint8s) != 2 {
t.Errorf("unexpected F len: %v", len(obj.FUint8s))
t.Fatalf("unexpected F len: %v", len(obj.FUint8s))
}
if obj.FUint8s[0] != 3 {
t.Errorf("unexpected F[0]: %v", obj.FUint8s[0])
Expand All @@ -207,7 +208,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.GUint64s) != 2 {
t.Errorf("unexpected G len: %v", len(obj.GUint64s))
t.Fatalf("unexpected G len: %v", len(obj.GUint64s))
}
if obj.GUint64s[0] != 4 {
t.Errorf("unexpected G[0]: %v", obj.GUint64s[0])
Expand All @@ -217,7 +218,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.HFloat32s) != 2 {
t.Errorf("unexpected H len: %v", len(obj.HFloat32s))
t.Fatalf("unexpected H len: %v", len(obj.HFloat32s))
}
if obj.HFloat32s[0] != 1.25 {
t.Errorf("unexpected H[0]: %v", obj.HFloat32s[0])
Expand All @@ -227,7 +228,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.IFloat64s) != 2 {
t.Errorf("unexpected I len: %v", len(obj.IFloat64s))
t.Fatalf("unexpected I len: %v", len(obj.IFloat64s))
}
if obj.IFloat64s[0] != 2.75 {
t.Errorf("unexpected I[0]: %v", obj.IFloat64s[0])
Expand All @@ -237,7 +238,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.JBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
t.Fatalf("unexpected J len: %v", len(obj.JBools))
}
if obj.JBools[0] != true {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
Expand All @@ -247,7 +248,7 @@ func TestValueStringSliceMapper(t *testing.T) {
}

if len(obj.KTimes) != 2 {
t.Errorf("unexpected K len: %v", len(obj.KTimes))
t.Fatalf("unexpected K len: %v", len(obj.KTimes))
}
if y, m, d := time.Time(obj.KTimes[0]).Date(); y != 2016 {
t.Errorf("unexpected KTime[0].y: %v", y)
Expand All @@ -264,26 +265,6 @@ func TestValueStringSliceMapper(t *testing.T) {
t.Errorf("unexpected KTime[1].d: %v", d)
}

if len(obj.LPtrBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
}
if obj.LPtrBools[0] != nil && *obj.LPtrBools[0] {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
}
if obj.LPtrBools[1] != nil && !*obj.LPtrBools[1] {
t.Errorf("unexpected J[1]: %v", obj.JBools[1])
}

if len(obj.MPtrPtrBools) != 2 {
t.Errorf("unexpected J len: %v", len(obj.JBools))
}
if obj.MPtrPtrBools[0] != nil && **obj.MPtrPtrBools[0] {
t.Errorf("unexpected J[0]: %v", obj.JBools[0])
}
if obj.MPtrPtrBools[1] != nil && !**obj.MPtrPtrBools[1] {
t.Errorf("unexpected J[1]: %v", obj.JBools[1])
}

if obj.YString != "This is Y" {
t.Errorf("unexpected Y: %v", obj.YString)
}
Expand Down

0 comments on commit 4ab9470

Please sign in to comment.