From 0ec62843ecfec440ad7d63158cc8e92560da3991 Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 30 Jul 2014 21:08:34 +0300 Subject: [PATCH] structure: add anonymous fields test, closes #5 --- structure.go | 1 - structure_test.go | 117 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 113 insertions(+), 5 deletions(-) diff --git a/structure.go b/structure.go index 34c7698..b1eaa6a 100644 --- a/structure.go +++ b/structure.go @@ -129,7 +129,6 @@ func Fields(s interface{}) []string { keys := make([]string, 0) for i, field := range fields { - val := v.Field(i) if val.Kind() == reflect.Struct { // look out for embedded structs, and convert them to a diff --git a/structure_test.go b/structure_test.go index 4828ce8..6e69df9 100644 --- a/structure_test.go +++ b/structure_test.go @@ -89,7 +89,7 @@ func TestMap_Tag(t *testing.T) { } -func TestMap_Embedded(t *testing.T) { +func TestMap_Nested(t *testing.T) { type A struct { Name string } @@ -106,6 +106,34 @@ func TestMap_Embedded(t *testing.T) { t.Errorf("Map should return a map type, got: %v", typ) } + in, ok := m["A"].(map[string]interface{}) + if !ok { + t.Error("Map nested structs is not available in the map") + } + + if name := in["Name"].(string); name != "example" { + t.Error("Map nested struct's name field should give example, got: %s", name) + } +} + +func TestMap_Anonymous(t *testing.T) { + type A struct { + Name string + } + a := A{Name: "example"} + + type B struct { + A + } + b := &B{} + b.A = a + + m := Map(b) + + if typ := reflect.TypeOf(m).Kind(); typ != reflect.Map { + t.Errorf("Map should return a map type, got: %v", typ) + } + in, ok := m["A"].(map[string]interface{}) if !ok { t.Error("Embedded structs is not available in the map") @@ -162,7 +190,7 @@ func TestValues(t *testing.T) { } } -func TestValues_Embedded(t *testing.T) { +func TestValues_Nested(t *testing.T) { type A struct { Name string } @@ -192,6 +220,37 @@ func TestValues_Embedded(t *testing.T) { } } +func TestValues_Anonymous(t *testing.T) { + type A struct { + Name string + } + a := A{Name: "example"} + + type B struct { + A + C int + } + b := &B{C: 123} + b.A = a + + s := Values(b) + + inSlice := func(val interface{}) bool { + for _, v := range s { + if reflect.DeepEqual(v, val) { + return true + } + } + return false + } + + for _, val := range []interface{}{"example", 123} { + if !inSlice(val) { + t.Errorf("Values should have the value %v", val) + } + } +} + func TestFields(t *testing.T) { var T = struct { A string @@ -225,7 +284,7 @@ func TestFields(t *testing.T) { } } -func TestFields_Embedded(t *testing.T) { +func TestFields_Nested(t *testing.T) { type A struct { Name string } @@ -255,6 +314,37 @@ func TestFields_Embedded(t *testing.T) { } } +func TestFields_Anonymous(t *testing.T) { + type A struct { + Name string + } + a := A{Name: "example"} + + type B struct { + A + C int + } + b := &B{C: 123} + b.A = a + + s := Fields(b) + + inSlice := func(val interface{}) bool { + for _, v := range s { + if reflect.DeepEqual(v, val) { + return true + } + } + return false + } + + for _, val := range []interface{}{"Name", "A", "C"} { + if !inSlice(val) { + t.Errorf("Fields should have the value %v", val) + } + } +} + func TestIsValid(t *testing.T) { var T = struct { A string @@ -284,7 +374,7 @@ func TestIsValid(t *testing.T) { } } -func TestIsValid_Embedded(t *testing.T) { +func TestIsValid_Nested(t *testing.T) { type A struct { Name string D string @@ -301,7 +391,26 @@ func TestIsValid_Embedded(t *testing.T) { if ok { t.Error("IsValid should return false because D is not initialized") } +} +func TestIsValid_Anonymous(t *testing.T) { + type A struct { + Name string + D string + } + a := A{Name: "example"} + + type B struct { + A + C int + } + b := &B{C: 123} + b.A = a + + ok := IsValid(b) + if ok { + t.Error("IsValid should return false because D is not initialized") + } } func TestName(t *testing.T) {