structs: add tests for FillMap

This commit is contained in:
Fatih Arslan 2016-03-14 13:56:43 +02:00
parent 0a6321f45c
commit ff332a1b02
2 changed files with 61 additions and 2 deletions

View File

@ -78,7 +78,8 @@ func (s *Struct) Map() map[string]interface{} {
return out return out
} }
// FillMap is the same as Map. Instead of returning the output, it fills the given input // FillMap is the same as Map. Instead of returning the output, it fills the
// given map.
func (s *Struct) FillMap(out map[string]interface{}) { func (s *Struct) FillMap(out map[string]interface{}) {
if out == nil { if out == nil {
return return
@ -434,7 +435,8 @@ func Map(s interface{}) map[string]interface{} {
return New(s).Map() return New(s).Map()
} }
// FillMap is the same as Map. Instead of returning the output, it fills the given input // FillMap is the same as Map. Instead of returning the output, it fills the
// given map.
func FillMap(s interface{}, out map[string]interface{}) { func FillMap(s interface{}, out map[string]interface{}) {
New(s).FillMap(out) New(s).FillMap(out)
} }

View File

@ -310,6 +310,63 @@ func TestMap_TimeField(t *testing.T) {
} }
} }
func TestFillMap(t *testing.T) {
var T = struct {
A string
B int
C bool
}{
A: "a-value",
B: 2,
C: true,
}
a := make(map[string]interface{}, 0)
FillMap(T, a)
// we have three fields
if len(a) != 3 {
t.Errorf("FillMap should fill a map of len 3, got: %d", len(a))
}
inMap := func(val interface{}) bool {
for _, v := range a {
if reflect.DeepEqual(v, val) {
return true
}
}
return false
}
for _, val := range []interface{}{"a-value", 2, true} {
if !inMap(val) {
t.Errorf("FillMap should have the value %v", val)
}
}
}
func TestFillMap_Nil(t *testing.T) {
var T = struct {
A string
B int
C bool
}{
A: "a-value",
B: 2,
C: true,
}
defer func() {
err := recover()
if err != nil {
t.Error("FillMap should not panic if a nil map is passed")
}
}()
// nil should no
FillMap(T, nil)
}
func TestStruct(t *testing.T) { func TestStruct(t *testing.T) {
var T = struct{}{} var T = struct{}{}