From ff332a1b029d57daa56b9a7a7ec95f4e5f0f7d0e Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Mon, 14 Mar 2016 13:56:43 +0200 Subject: [PATCH] structs: add tests for FillMap --- structs.go | 6 ++++-- structs_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/structs.go b/structs.go index 6cc59e0..408d50f 100644 --- a/structs.go +++ b/structs.go @@ -78,7 +78,8 @@ func (s *Struct) Map() map[string]interface{} { 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{}) { if out == nil { return @@ -434,7 +435,8 @@ func Map(s interface{}) map[string]interface{} { 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{}) { New(s).FillMap(out) } diff --git a/structs_test.go b/structs_test.go index 7402bc0..30e3115 100644 --- a/structs_test.go +++ b/structs_test.go @@ -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) { var T = struct{}{}