feat(Go-StudyExample):2021/02/09:新增merge源码阅读和单测代码
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package mergo_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/imdario/mergo"
|
||||
)
|
||||
|
||||
func TestIssue89Boolean(t *testing.T) {
|
||||
type Foo struct {
|
||||
Bar bool `json:"bar"`
|
||||
}
|
||||
|
||||
src := Foo{Bar: true}
|
||||
dst := Foo{Bar: false}
|
||||
|
||||
if err := mergo.Merge(&dst, src); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if dst.Bar == false {
|
||||
t.Errorf("expected true, got false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssue89MergeWithEmptyValue(t *testing.T) {
|
||||
p1 := map[string]interface{}{
|
||||
"A": 3, "B": "note", "C": true,
|
||||
}
|
||||
p2 := map[string]interface{}{
|
||||
"B": "", "C": false,
|
||||
}
|
||||
if err := mergo.Merge(&p1, p2, mergo.WithOverwriteWithEmptyValue); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
testCases := []struct {
|
||||
key string
|
||||
expected interface{}
|
||||
}{
|
||||
{
|
||||
"A",
|
||||
3,
|
||||
},
|
||||
{
|
||||
"B",
|
||||
"",
|
||||
},
|
||||
{
|
||||
"C",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tC := range testCases {
|
||||
if p1[tC.key] != tC.expected {
|
||||
t.Errorf("expected %v in p1[%q], got %v", tC.expected, tC.key, p1[tC.key])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user