structure: add anonymous fields test, closes #5

This commit is contained in:
Fatih Arslan 2014-07-30 21:08:34 +03:00
parent 4d9abb0de1
commit 0ec62843ec
2 changed files with 113 additions and 5 deletions

View File

@ -129,7 +129,6 @@ func Fields(s interface{}) []string {
keys := make([]string, 0) keys := make([]string, 0)
for i, field := range fields { for i, field := range fields {
val := v.Field(i) val := v.Field(i)
if val.Kind() == reflect.Struct { if val.Kind() == reflect.Struct {
// look out for embedded structs, and convert them to a // look out for embedded structs, and convert them to a

View File

@ -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 { type A struct {
Name string Name string
} }
@ -106,6 +106,34 @@ func TestMap_Embedded(t *testing.T) {
t.Errorf("Map should return a map type, got: %v", typ) 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{}) in, ok := m["A"].(map[string]interface{})
if !ok { if !ok {
t.Error("Embedded structs is not available in the map") 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 { type A struct {
Name string 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) { func TestFields(t *testing.T) {
var T = struct { var T = struct {
A string 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 { type A struct {
Name string 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) { func TestIsValid(t *testing.T) {
var T = struct { var T = struct {
A string 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 { type A struct {
Name string Name string
D string D string
@ -301,7 +391,26 @@ func TestIsValid_Embedded(t *testing.T) {
if ok { if ok {
t.Error("IsValid should return false because D is not initialized") 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) { func TestName(t *testing.T) {