From e8f744e2d97d12cba92319b97c9cec06bd2dd780 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dario=20Casta=C3=B1=C3=A9?= Date: Sat, 18 Jul 2020 13:18:36 +0200 Subject: [PATCH] Tests isolated in mergo_test package --- go.mod | 5 +-- go.sum | 26 +----------- issue100_test.go | 10 +++-- issue104_test.go | 8 +++- issue121_test.go | 6 ++- issue123_test.go | 6 ++- issue125_test.go | 6 ++- issue129_test.go | 8 ++-- issue131_test.go | 6 ++- issue136_test.go | 20 +++++---- issue138_test.go | 8 ++-- issue143_test.go | 12 +++--- issue149_test.go | 6 ++- issue17_test.go | 6 ++- issue23_test.go | 6 ++- issue33_test.go | 8 ++-- issue38_test.go | 10 +++-- issue50_test.go | 6 ++- issue52_test.go | 14 ++++--- issue61_test.go | 6 ++- issue64_test.go | 6 ++- issue66_test.go | 8 ++-- issue83_test.go | 10 +++-- issue84_test.go | 10 +++-- issue89_test.go | 8 ++-- issueXXX_test.go | 6 ++- merge_test.go | 14 ++++--- mergo_test.go | 105 ++++++++++++++++++++++++---------------------- pr80_test.go | 6 ++- pr81_test.go | 6 ++- v039_bugs_test.go | 10 +++-- 31 files changed, 207 insertions(+), 165 deletions(-) diff --git a/go.mod b/go.mod index 115dc6e..3d689d9 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,4 @@ module github.com/imdario/mergo go 1.13 -require ( - golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6 // indirect - gopkg.in/yaml.v2 v2.3.0 -) +require gopkg.in/yaml.v2 v2.3.0 diff --git a/go.sum b/go.sum index 57ed6f0..168980d 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +1,4 @@ -github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg= -github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= -github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4= -golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6 h1:nULzSsKgihxFGLnQFv2T7lE5vIhOtg8ZPpJHapEt7o0= -golang.org/x/tools v0.0.0-20200717024301-6ddee64345a6/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/issue100_test.go b/issue100_test.go index 3584024..039123a 100644 --- a/issue100_test.go +++ b/issue100_test.go @@ -1,6 +1,10 @@ -package mergo +package mergo_test -import "testing" +import ( + "testing" + + "github.com/imdario/mergo" +) type issue100s struct { Member interface{} @@ -11,7 +15,7 @@ func TestIssue100(t *testing.T) { m["Member"] = "anything" st := &issue100s{} - if err := Map(st, m); err != nil { + if err := mergo.Map(st, m); err != nil { t.Error(err) } } diff --git a/issue104_test.go b/issue104_test.go index cc405af..c07e5a8 100644 --- a/issue104_test.go +++ b/issue104_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "reflect" "testing" + + "github.com/imdario/mergo" ) type Record struct { @@ -23,7 +25,9 @@ func StructToRecord(in interface{}) *Record { } } - Map(&rec.Data, in) + if err := mergo.Map(&rec.Data, in); err != nil { + panic(err) + } return &rec } diff --git a/issue121_test.go b/issue121_test.go index bcec280..0f36efd 100644 --- a/issue121_test.go +++ b/issue121_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) func TestIssue121WithSliceDeepCopy(t *testing.T) { @@ -19,7 +21,7 @@ func TestIssue121WithSliceDeepCopy(t *testing.T) { }, } - if err := Merge(&dst, src, WithSliceDeepCopy); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithSliceDeepCopy); err != nil { t.Errorf("Error during the merge: %v", err) } diff --git a/issue123_test.go b/issue123_test.go index 1681dd1..ae62d7b 100644 --- a/issue123_test.go +++ b/issue123_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) func TestIssue123(t *testing.T) { @@ -17,7 +19,7 @@ func TestIssue123(t *testing.T) { } // Expected behavior - if err := Merge(&dst, src, WithOverride); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil { t.Fatal(err) } testCases := []struct { diff --git a/issue125_test.go b/issue125_test.go index 69965cc..4e5cb63 100644 --- a/issue125_test.go +++ b/issue125_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "encoding/json" "testing" + + "github.com/imdario/mergo" ) type settings struct { @@ -24,7 +26,7 @@ func TestIssue125MergeWithOverwrite(t *testing.T) { t.Errorf("Error while Unmarshalling maprequest: %s", err) } - if err := Merge(&something, defaultSettings, WithOverrideEmptySlice); err != nil { + if err := mergo.Merge(&something, defaultSettings, mergo.WithOverrideEmptySlice); err != nil { t.Errorf("Error while merging: %s", err) } diff --git a/issue129_test.go b/issue129_test.go index 273bc12..3a1c691 100644 --- a/issue129_test.go +++ b/issue129_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) func TestIssue129Boolean(t *testing.T) { @@ -20,7 +22,7 @@ func TestIssue129Boolean(t *testing.T) { } // Standard behavior - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.Error(err) } if dst.A != true { @@ -35,7 +37,7 @@ func TestIssue129Boolean(t *testing.T) { A: false, B: true, } - if err := Merge(&dst, src, WithOverwriteWithEmptyValue); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithOverwriteWithEmptyValue); err != nil { t.Error(err) } if dst.A != true { diff --git a/issue131_test.go b/issue131_test.go index 4f2d6ca..5ca24cf 100644 --- a/issue131_test.go +++ b/issue131_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type foz struct { @@ -18,7 +20,7 @@ func TestIssue131MergeWithOverwriteWithEmptyValue(t *testing.T) { A: func(v bool) *bool { return &v }(true), B: "dest", } - if err := Merge(&dest, src, WithOverwriteWithEmptyValue); err != nil { + if err := mergo.Merge(&dest, src, mergo.WithOverwriteWithEmptyValue); err != nil { t.Error(err) } if *src.A != *dest.A { diff --git a/issue136_test.go b/issue136_test.go index 02d6c0c..4fe81c7 100644 --- a/issue136_test.go +++ b/issue136_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type embeddedTestA struct { @@ -15,13 +17,17 @@ type embeddedTestB struct { } func TestMergeEmbedded(t *testing.T) { - a := &embeddedTestA{ - "Suwon", 16, - } + var ( + err error + a = &embeddedTestA{ + "Suwon", 16, + } + b = &embeddedTestB{} + ) - b := &embeddedTestB{} - - err := Merge(&b.embeddedTestA, *a) + if err := mergo.Merge(&b.embeddedTestA, *a); err != nil { + t.Error(err) + } if b.Name != "Suwon" { t.Errorf("%v %v", b.Name, err) diff --git a/issue138_test.go b/issue138_test.go index 43437cb..70d7b29 100644 --- a/issue138_test.go +++ b/issue138_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "encoding/json" "testing" + + "github.com/imdario/mergo" ) const issue138configuration string = ` @@ -24,7 +26,7 @@ func TestIssue138(t *testing.T) { // https://golang.org/pkg/encoding/json/#Unmarshal json.Unmarshal([]byte(issue138configuration), &foo) - err := Map(&config{}, foo) + err := mergo.Map(&config{}, foo) if err == nil { t.Error("expected type mismatch error, got nil") } else { @@ -34,7 +36,7 @@ func TestIssue138(t *testing.T) { } c := compatibleConfig{} - if err := Map(&c, foo); err != nil { + if err := mergo.Map(&c, foo); err != nil { t.Error(err) } } diff --git a/issue143_test.go b/issue143_test.go index 1bde211..8cca4d8 100644 --- a/issue143_test.go +++ b/issue143_test.go @@ -1,17 +1,19 @@ -package mergo +package mergo_test import ( "fmt" "testing" + + "github.com/imdario/mergo" ) func TestIssue143(t *testing.T) { testCases := []struct { - options []func(*Config) + options []func(*mergo.Config) expected func(map[string]interface{}) error }{ { - options: []func(*Config){WithOverride}, + options: []func(*mergo.Config){mergo.WithOverride}, expected: func(m map[string]interface{}) error { properties := m["properties"].(map[string]interface{}) if properties["field1"] != "wrong" { @@ -21,7 +23,7 @@ func TestIssue143(t *testing.T) { }, }, { - options: []func(*Config){}, + options: []func(*mergo.Config){}, expected: func(m map[string]interface{}) error { properties := m["properties"].(map[string]interface{}) if properties["field1"] == "wrong" { @@ -40,7 +42,7 @@ func TestIssue143(t *testing.T) { }, } - err := Map( + err := mergo.Map( &base, map[string]interface{}{ "properties": map[string]interface{}{ diff --git a/issue149_test.go b/issue149_test.go index c6b5c0c..b6857c9 100644 --- a/issue149_test.go +++ b/issue149_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type user struct { @@ -25,7 +27,7 @@ func TestIssue149(t *testing.T) { User: nil, Token: &tokenValue, } - if err := Merge(dest, src, WithOverwriteWithEmptyValue); err != nil { + if err := mergo.Merge(dest, src, mergo.WithOverwriteWithEmptyValue); err != nil { t.Error(err) } if dest.User != nil { diff --git a/issue17_test.go b/issue17_test.go index e0abf04..97bbd7d 100644 --- a/issue17_test.go +++ b/issue17_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "encoding/json" "testing" + + "github.com/imdario/mergo" ) func TestIssue17MergeWithOverwrite(t *testing.T) { @@ -20,7 +22,7 @@ func TestIssue17MergeWithOverwrite(t *testing.T) { t.Errorf("Error while Unmarshalling maprequest: %s", err) } - if err := MergeWithOverwrite(&something, maprequest); err != nil { + if err := mergo.MergeWithOverwrite(&something, maprequest); err != nil { t.Errorf("Error while merging: %s", err) } } diff --git a/issue23_test.go b/issue23_test.go index 65bf7a5..d91bd79 100644 --- a/issue23_test.go +++ b/issue23_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "testing" "time" + + "github.com/imdario/mergo" ) type document struct { @@ -19,7 +21,7 @@ func TestIssue23MergeWithOverwrite(t *testing.T) { &expected, } - if err := MergeWithOverwrite(&dst, src); err != nil { + if err := mergo.MergeWithOverwrite(&dst, src); err != nil { t.Errorf("Error while merging %s", err) } diff --git a/issue33_test.go b/issue33_test.go index b4fea46..cf92735 100644 --- a/issue33_test.go +++ b/issue33_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type Foo struct { @@ -16,7 +18,7 @@ func TestIssue33Merge(t *testing.T) { Bslice: []byte{1, 2}, } - if err := Merge(&dest, toMerge); err != nil { + if err := mergo.Merge(&dest, toMerge); err != nil { t.Errorf("Error while merging: %s", err) } // Merge doesn't overwrite an attribute if in destination it doesn't have a zero value. @@ -25,7 +27,7 @@ func TestIssue33Merge(t *testing.T) { t.Errorf("dest.Str should have not been override as it has a non-zero value: dest.Str(%v) != 'a'", dest.Str) } // If we want to override, we must use MergeWithOverwrite or Merge using WithOverride. - if err := Merge(&dest, toMerge, WithOverride); err != nil { + if err := mergo.Merge(&dest, toMerge, mergo.WithOverride); err != nil { t.Errorf("Error while merging: %s", err) } diff --git a/issue38_test.go b/issue38_test.go index 69ad8fa..fdee7ff 100644 --- a/issue38_test.go +++ b/issue38_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "testing" "time" + + "github.com/imdario/mergo" ) type structWithoutTimePointer struct { @@ -19,7 +21,7 @@ func TestIssue38Merge(t *testing.T) { expected, } - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.Errorf("Error while merging %s", err) } @@ -36,7 +38,7 @@ func TestIssue38MergeEmptyStruct(t *testing.T) { expected, } - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.Errorf("Error while merging %s", err) } @@ -55,7 +57,7 @@ func TestIssue38MergeWithOverwrite(t *testing.T) { expected, } - if err := MergeWithOverwrite(&dst, src); err != nil { + if err := mergo.MergeWithOverwrite(&dst, src); err != nil { t.Errorf("Error while merging %s", err) } diff --git a/issue50_test.go b/issue50_test.go index 8a6aaa6..2d32579 100644 --- a/issue50_test.go +++ b/issue50_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "testing" "time" + + "github.com/imdario/mergo" ) type testStruct struct { @@ -13,7 +15,7 @@ func TestIssue50Merge(t *testing.T) { to := testStruct{} from := testStruct{} - if err := Merge(&to, from); err != nil { + if err := mergo.Merge(&to, from); err != nil { t.Fail() } } diff --git a/issue52_test.go b/issue52_test.go index 6aeb9e8..af65471 100644 --- a/issue52_test.go +++ b/issue52_test.go @@ -1,9 +1,11 @@ -package mergo +package mergo_test import ( "reflect" "testing" "time" + + "github.com/imdario/mergo" ) type structWithTime struct { @@ -45,7 +47,7 @@ func TestOverwriteZeroSrcTime(t *testing.T) { dst := structWithTime{now} src := structWithTime{} - if err := MergeWithOverwrite(&dst, src); err != nil { + if err := mergo.MergeWithOverwrite(&dst, src); err != nil { t.FailNow() } @@ -59,7 +61,7 @@ func TestOverwriteZeroSrcTimeWithTransformer(t *testing.T) { dst := structWithTime{now} src := structWithTime{} - if err := MergeWithOverwrite(&dst, src, WithTransformers(timeTransfomer{true})); err != nil { + if err := mergo.MergeWithOverwrite(&dst, src, mergo.WithTransformers(timeTransfomer{true})); err != nil { t.FailNow() } @@ -73,7 +75,7 @@ func TestOverwriteZeroDstTime(t *testing.T) { dst := structWithTime{} src := structWithTime{now} - if err := MergeWithOverwrite(&dst, src); err != nil { + if err := mergo.MergeWithOverwrite(&dst, src); err != nil { t.FailNow() } @@ -87,7 +89,7 @@ func TestZeroDstTime(t *testing.T) { dst := structWithTime{} src := structWithTime{now} - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.FailNow() } @@ -101,7 +103,7 @@ func TestZeroDstTimeWithTransformer(t *testing.T) { dst := structWithTime{} src := structWithTime{now} - if err := Merge(&dst, src, WithTransformers(timeTransfomer{})); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithTransformers(timeTransfomer{})); err != nil { t.FailNow() } diff --git a/issue61_test.go b/issue61_test.go index 380881d..c94d693 100644 --- a/issue61_test.go +++ b/issue61_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "reflect" "testing" + + "github.com/imdario/mergo" ) func TestIssue61MergeNilMap(t *testing.T) { @@ -12,7 +14,7 @@ func TestIssue61MergeNilMap(t *testing.T) { t1 := T{} t2 := T{I: map[string][]string{"hi": {"there"}}} - if err := Merge(&t1, t2); err != nil { + if err := mergo.Merge(&t1, t2); err != nil { t.Fail() } diff --git a/issue64_test.go b/issue64_test.go index 02395f9..c3d2772 100644 --- a/issue64_test.go +++ b/issue64_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type Student struct { @@ -26,7 +28,7 @@ func issue64Data() []issue64TestData { func TestIssue64MergeSliceWithOverride(t *testing.T) { for _, data := range issue64Data() { - err := Merge(&data.S2, data.S1, WithOverride) + err := mergo.Merge(&data.S2, data.S1, mergo.WithOverride) if err != nil { t.Errorf("Error while merging %s", err) } diff --git a/issue66_test.go b/issue66_test.go index 6fad293..643d4bb 100644 --- a/issue66_test.go +++ b/issue66_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type PrivateSliceTest66 struct { @@ -18,7 +20,7 @@ func TestPrivateSlice(t *testing.T) { PublicStrings: []string{"six", "seven"}, } - if err := Merge(&p1, p2); err != nil { + if err := mergo.Merge(&p1, p2); err != nil { t.Errorf("Error during the merge: %v", err) } @@ -40,7 +42,7 @@ func TestPrivateSliceWithAppendSlice(t *testing.T) { PublicStrings: []string{"six", "seven"}, } - if err := Merge(&p1, p2, WithAppendSlice); err != nil { + if err := mergo.Merge(&p1, p2, mergo.WithAppendSlice); err != nil { t.Errorf("Error during the merge: %v", err) } diff --git a/issue83_test.go b/issue83_test.go index 9d80789..d10e892 100644 --- a/issue83_test.go +++ b/issue83_test.go @@ -1,6 +1,10 @@ -package mergo +package mergo_test -import "testing" +import ( + "testing" + + "github.com/imdario/mergo" +) type issue83My struct { Data []int @@ -9,7 +13,7 @@ type issue83My struct { func TestIssue83(t *testing.T) { dst := issue83My{Data: []int{1, 2, 3}} new := issue83My{} - if err := Merge(&dst, new, WithOverwriteWithEmptyValue); err != nil { + if err := mergo.Merge(&dst, new, mergo.WithOverwriteWithEmptyValue); err != nil { t.Error(err) } if len(dst.Data) > 0 { diff --git a/issue84_test.go b/issue84_test.go index 422442f..b01ebf4 100644 --- a/issue84_test.go +++ b/issue84_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type DstStructIssue84 struct { @@ -28,7 +30,7 @@ func TestIssue84MergeMapWithNilValueToStructWithOverride(t *testing.T) { "A": 3, "B": 4, "C": 0, } - if err := Map(&p1, p2, WithOverride); err != nil { + if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil { t.Errorf("Error during the merge: %v", err) } @@ -45,7 +47,7 @@ func TestIssue84MergeMapWithoutKeyExistsToStructWithOverride(t *testing.T) { "A": 3, "B": 4, } - if err := Map(&p1, p2, WithOverride); err != nil { + if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil { t.Errorf("Error during the merge: %v", err) } @@ -70,7 +72,7 @@ func TestIssue84MergeNestedMapWithNilValueToStructWithOverride(t *testing.T) { }, "B": 4, "C": 0, } - if err := Map(&p1, p2, WithOverride); err != nil { + if err := mergo.Map(&p1, p2, mergo.WithOverride); err != nil { t.Errorf("Error during the merge: %v", err) } diff --git a/issue89_test.go b/issue89_test.go index afae669..0a38994 100644 --- a/issue89_test.go +++ b/issue89_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) func TestIssue89Boolean(t *testing.T) { @@ -12,7 +14,7 @@ func TestIssue89Boolean(t *testing.T) { src := Foo{Bar: true} dst := Foo{Bar: false} - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.Error(err) } if dst.Bar == false { @@ -27,7 +29,7 @@ func TestIssue89MergeWithEmptyValue(t *testing.T) { p2 := map[string]interface{}{ "B": "", "C": false, } - if err := Merge(&p1, p2, WithOverwriteWithEmptyValue); err != nil { + if err := mergo.Merge(&p1, p2, mergo.WithOverwriteWithEmptyValue); err != nil { t.Error(err) } testCases := []struct { diff --git a/issueXXX_test.go b/issueXXX_test.go index 2390e40..e5f424f 100644 --- a/issueXXX_test.go +++ b/issueXXX_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) var testDataS = []struct { @@ -17,7 +19,7 @@ var testDataS = []struct { func TestMergeSliceWithOverrideWithAppendSlice(t *testing.T) { for _, data := range testDataS { - err := Merge(&data.S2, data.S1, WithOverride, WithAppendSlice) + err := mergo.Merge(&data.S2, data.S1, mergo.WithOverride, mergo.WithAppendSlice) if err != nil { t.Errorf("Error while merging %s", err) } diff --git a/merge_test.go b/merge_test.go index 356d623..dd51dab 100644 --- a/merge_test.go +++ b/merge_test.go @@ -1,8 +1,10 @@ -package mergo +package mergo_test import ( "reflect" "testing" + + "github.com/imdario/mergo" ) type transformer struct { @@ -30,7 +32,7 @@ func TestMergeWithTransformerNilStruct(t *testing.T) { a := foo{s: "foo"} b := foo{Bar: &bar{i: 2, s: map[string]string{"foo": "bar"}}} - if err := Merge(&a, &b, WithOverride, WithTransformers(&transformer{ + if err := mergo.Merge(&a, &b, mergo.WithOverride, mergo.WithTransformers(&transformer{ m: map[reflect.Type]func(dst, src reflect.Value) error{ reflect.TypeOf(&bar{}): func(dst, src reflect.Value) error { // Do sthg with Elem @@ -62,9 +64,9 @@ func TestMergeNonPointer(t *testing.T) { "a": "1", }, } - want := ErrNonPointerAgument + want := mergo.ErrNonPointerAgument - if got := merge(dst, src); got != want { + if got := mergo.Merge(dst, src); got != want { t.Errorf("want: %s, got: %s", want, got) } } @@ -79,8 +81,8 @@ func TestMapNonPointer(t *testing.T) { }, }, } - want := ErrNonPointerAgument - if got := merge(dst, src); got != want { + want := mergo.ErrNonPointerAgument + if got := mergo.Merge(dst, src); got != want { t.Errorf("want: %s, got: %s", want, got) } } diff --git a/mergo_test.go b/mergo_test.go index d63c876..d69714c 100644 --- a/mergo_test.go +++ b/mergo_test.go @@ -3,7 +3,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package mergo +package mergo_test import ( "io/ioutil" @@ -12,6 +12,7 @@ import ( "testing" "time" + "github.com/imdario/mergo" "gopkg.in/yaml.v2" ) @@ -75,7 +76,9 @@ func TestKb(t *testing.T) { expected.Name = "B" expected.KeyValue = ekv - Merge(&b, a) + if err := mergo.Merge(&b, a); err != nil { + t.Error(err) + } if !reflect.DeepEqual(b, expected) { t.Errorf("Actual: %#v did not match \nExpected: %#v", b, expected) @@ -83,7 +86,7 @@ func TestKb(t *testing.T) { } func TestNil(t *testing.T) { - if err := Merge(nil, nil); err != ErrNilArguments { + if err := mergo.Merge(nil, nil); err != mergo.ErrNilArguments { t.Fail() } } @@ -91,7 +94,7 @@ func TestNil(t *testing.T) { func TestDifferentTypes(t *testing.T) { a := simpleTest{42} b := 42 - if err := Merge(&a, b); err != ErrDifferentArgumentsTypes { + if err := mergo.Merge(&a, b); err != mergo.ErrDifferentArgumentsTypes { t.Fail() } } @@ -99,7 +102,7 @@ func TestDifferentTypes(t *testing.T) { func TestSimpleStruct(t *testing.T) { a := simpleTest{} b := simpleTest{42} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.FailNow() } if a.Value != 42 { @@ -114,7 +117,7 @@ func TestComplexStruct(t *testing.T) { a := complexTest{} a.ID = "athing" b := complexTest{simpleTest{42}, 1, "bthing"} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.FailNow() } if a.St.Value != 42 { @@ -133,7 +136,7 @@ func TestComplexStructWithOverwrite(t *testing.T) { b := complexTest{simpleTest{42}, 2, ""} expect := complexTest{simpleTest{42}, 1, "do-not-overwrite-with-empty-value"} - if err := MergeWithOverwrite(&a, b); err != nil { + if err := mergo.MergeWithOverwrite(&a, b); err != nil { t.FailNow() } @@ -147,7 +150,7 @@ func TestPointerStruct(t *testing.T) { s2 := simpleTest{19} a := pointerTest{&s1} b := pointerTest{&s2} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.FailNow() } if a.C.Value != b.C.Value { @@ -205,7 +208,7 @@ func TestEmbeddedStruct(t *testing.T) { } for _, test := range tests { - err := Merge(&test.dst, test.src) + err := mergo.Merge(&test.dst, test.src) if err != nil { t.Errorf("unexpected error: %v", err) continue @@ -219,7 +222,7 @@ func TestEmbeddedStruct(t *testing.T) { func TestPointerStructNil(t *testing.T) { a := pointerTest{nil} b := pointerTest{&simpleTest{19}} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.FailNow() } if a.C.Value != b.C.Value { @@ -227,13 +230,13 @@ func TestPointerStructNil(t *testing.T) { } } -func testSlice(t *testing.T, a []int, b []int, e []int, opts ...func(*Config)) { +func testSlice(t *testing.T, a []int, b []int, e []int, opts ...func(*mergo.Config)) { t.Helper() bc := b sa := sliceTest{a} sb := sliceTest{b} - if err := Merge(&sa, sb, opts...); err != nil { + if err := mergo.Merge(&sa, sb, opts...); err != nil { t.FailNow() } if !reflect.DeepEqual(sb.S, bc) { @@ -245,7 +248,7 @@ func testSlice(t *testing.T, a []int, b []int, e []int, opts ...func(*Config)) { ma := map[string][]int{"S": a} mb := map[string][]int{"S": b} - if err := Merge(&ma, mb, opts...); err != nil { + if err := mergo.Merge(&ma, mb, opts...); err != nil { t.FailNow() } if !reflect.DeepEqual(mb["S"], bc) { @@ -259,7 +262,7 @@ func testSlice(t *testing.T, a []int, b []int, e []int, opts ...func(*Config)) { // test case with missing dst key ma := map[string][]int{} mb := map[string][]int{"S": b} - if err := Merge(&ma, mb); err != nil { + if err := mergo.Merge(&ma, mb); err != nil { t.FailNow() } if !reflect.DeepEqual(mb["S"], bc) { @@ -274,7 +277,7 @@ func testSlice(t *testing.T, a []int, b []int, e []int, opts ...func(*Config)) { // test case with missing src key ma := map[string][]int{"S": a} mb := map[string][]int{} - if err := Merge(&ma, mb); err != nil { + if err := mergo.Merge(&ma, mb); err != nil { t.FailNow() } if !reflect.DeepEqual(mb["S"], bc) { @@ -292,12 +295,12 @@ func TestSlice(t *testing.T) { testSlice(t, []int{1}, []int{2, 3}, []int{1}) testSlice(t, []int{1}, []int{}, []int{1}) testSlice(t, []int{1}, nil, []int{1}) - testSlice(t, nil, []int{1, 2, 3}, []int{1, 2, 3}, WithAppendSlice) - testSlice(t, []int{}, []int{1, 2, 3}, []int{1, 2, 3}, WithAppendSlice) - testSlice(t, []int{1}, []int{2, 3}, []int{1, 2, 3}, WithAppendSlice) - testSlice(t, []int{1}, []int{2, 3}, []int{1, 2, 3}, WithAppendSlice, WithOverride) - testSlice(t, []int{1}, []int{}, []int{1}, WithAppendSlice) - testSlice(t, []int{1}, nil, []int{1}, WithAppendSlice) + testSlice(t, nil, []int{1, 2, 3}, []int{1, 2, 3}, mergo.WithAppendSlice) + testSlice(t, []int{}, []int{1, 2, 3}, []int{1, 2, 3}, mergo.WithAppendSlice) + testSlice(t, []int{1}, []int{2, 3}, []int{1, 2, 3}, mergo.WithAppendSlice) + testSlice(t, []int{1}, []int{2, 3}, []int{1, 2, 3}, mergo.WithAppendSlice, mergo.WithOverride) + testSlice(t, []int{1}, []int{}, []int{1}, mergo.WithAppendSlice) + testSlice(t, []int{1}, nil, []int{1}, mergo.WithAppendSlice) } func TestEmptyMaps(t *testing.T) { @@ -305,7 +308,7 @@ func TestEmptyMaps(t *testing.T) { b := mapTest{ map[int]int{}, } - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.Fail() } if !reflect.DeepEqual(a, b) { @@ -316,7 +319,7 @@ func TestEmptyMaps(t *testing.T) { func TestEmptyToEmptyMaps(t *testing.T) { a := mapTest{} b := mapTest{} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.Fail() } if !reflect.DeepEqual(a, b) { @@ -336,7 +339,7 @@ func TestEmptyToNotEmptyMaps(t *testing.T) { b := mapTest{ map[int]int{}, } - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.Fail() } if !reflect.DeepEqual(a, aa) { @@ -365,7 +368,7 @@ func TestMapsWithOverwrite(t *testing.T) { "e": {14}, } - if err := MergeWithOverwrite(&m, n); err != nil { + if err := mergo.MergeWithOverwrite(&m, n); err != nil { t.Errorf(err.Error()) } @@ -395,7 +398,7 @@ func TestMapWithEmbeddedStructPointer(t *testing.T) { "e": {14}, } - if err := Merge(&m, n, WithOverride); err != nil { + if err := mergo.Merge(&m, n, mergo.WithOverride); err != nil { t.Errorf(err.Error()) } @@ -521,9 +524,9 @@ func TestMergeUsingStructAndMap(t *testing.T) { t.Run(tc.name, func(t *testing.T) { var err error if tc.overwrite { - err = Merge(tc.target, *tc.changes, WithOverride) + err = mergo.Merge(tc.target, *tc.changes, mergo.WithOverride) } else { - err = Merge(tc.target, *tc.changes) + err = mergo.Merge(tc.target, *tc.changes) } if err != nil { t.Error(err) @@ -555,7 +558,7 @@ func TestMaps(t *testing.T) { "e": {14}, } - if err := Merge(&m, n); err != nil { + if err := mergo.Merge(&m, n); err != nil { t.Errorf(err.Error()) } @@ -588,7 +591,7 @@ func TestMapsWithNilPointer(t *testing.T) { "c": nil, } - if err := Merge(&m, n, WithOverride); err != nil { + if err := mergo.Merge(&m, n, mergo.WithOverride); err != nil { t.Errorf(err.Error()) } @@ -604,7 +607,7 @@ func TestYAMLMaps(t *testing.T) { fl := license["fields"].(map[interface{}]interface{}) // license has one extra field (site) and another already existing in thing (author) that Mergo won't override. expectedLength := len(ft) + len(fl) - 1 - if err := Merge(&license, thing); err != nil { + if err := mergo.Merge(&license, thing); err != nil { t.Error(err.Error()) } currentLength := len(license["fields"].(map[interface{}]interface{})) @@ -620,7 +623,7 @@ func TestYAMLMaps(t *testing.T) { func TestTwoPointerValues(t *testing.T) { a := &simpleTest{} b := &simpleTest{42} - if err := Merge(a, b); err != nil { + if err := mergo.Merge(a, b); err != nil { t.Errorf(`Boom. You crossed the streams: %s`, err) } } @@ -641,7 +644,7 @@ func TestMap(t *testing.T) { "zt": simpleTest{299}, // Mapping a missing field (zt doesn't exist) "nt": simpleTest{3}, } - if err := Map(&c, b); err != nil { + if err := mergo.Map(&c, b); err != nil { t.FailNow() } m := b["ct"].(map[string]interface{}) @@ -670,7 +673,7 @@ func TestSimpleMap(t *testing.T) { b := map[string]interface{}{ "value": 42, } - if err := Map(&a, b); err != nil { + if err := mergo.Map(&a, b); err != nil { t.FailNow() } if a.Value != 42 { @@ -681,7 +684,7 @@ func TestSimpleMap(t *testing.T) { func TestIfcMap(t *testing.T) { a := ifcTest{} b := ifcTest{42} - if err := Map(&a, b); err != nil { + if err := mergo.Map(&a, b); err != nil { t.FailNow() } if a.I != 42 { @@ -695,7 +698,7 @@ func TestIfcMap(t *testing.T) { func TestIfcMapNoOverwrite(t *testing.T) { a := ifcTest{13} b := ifcTest{42} - if err := Map(&a, b); err != nil { + if err := mergo.Map(&a, b); err != nil { t.FailNow() } if a.I != 13 { @@ -706,7 +709,7 @@ func TestIfcMapNoOverwrite(t *testing.T) { func TestIfcMapWithOverwrite(t *testing.T) { a := ifcTest{13} b := ifcTest{42} - if err := MapWithOverwrite(&a, b); err != nil { + if err := mergo.MapWithOverwrite(&a, b); err != nil { t.FailNow() } if a.I != 42 { @@ -726,7 +729,7 @@ type pointerMapTest struct { func TestBackAndForth(t *testing.T) { pt := pointerMapTest{42, 1, &simpleTest{66}} m := make(map[string]interface{}) - if err := Map(&m, pt); err != nil { + if err := mergo.Map(&m, pt); err != nil { t.FailNow() } var ( @@ -744,7 +747,7 @@ func TestBackAndForth(t *testing.T) { t.Errorf("something went wrong while mapping pt on m, B wasn't copied") } bpt := pointerMapTest{} - if err := Map(&bpt, m); err != nil { + if err := mergo.Map(&bpt, m); err != nil { t.Error(err) } if bpt.A != pt.A { @@ -771,7 +774,7 @@ func TestEmbeddedPointerUnpacking(t *testing.T) { } for _, test := range tests { pt := test.input - if err := MapWithOverwrite(&pt, m); err != nil { + if err := mergo.MapWithOverwrite(&pt, m); err != nil { t.FailNow() } if pt.B.Value != newValue { @@ -794,7 +797,7 @@ func TestTime(t *testing.T) { "Birth": &now, } b := structWithTimePointer{} - if err := Merge(&b, dataStruct); err != nil { + if err := mergo.Merge(&b, dataStruct); err != nil { t.FailNow() } if b.Birth.IsZero() { @@ -804,7 +807,7 @@ func TestTime(t *testing.T) { t.Errorf("time.Time not merged in properly: b.Birth(%v) != dataStruct['Birth'](%v)", b.Birth, dataStruct.Birth) } b = structWithTimePointer{} - if err := Map(&b, dataMap); err != nil { + if err := mergo.Map(&b, dataMap); err != nil { t.FailNow() } if b.Birth.IsZero() { @@ -833,7 +836,7 @@ func TestNestedPtrValueInMap(t *testing.T) { "x": {}, }, } - if err := Map(dst, src); err != nil { + if err := mergo.Map(dst, src); err != nil { t.FailNow() } if dst.NestedPtrValue["x"].A == 0 { @@ -868,7 +871,7 @@ func TestUnexportedProperty(t *testing.T) { t.Errorf("Should not have panicked") } }() - Merge(&a, b) + mergo.Merge(&a, b) } type structWithBoolPointer struct { @@ -883,7 +886,7 @@ func TestBooleanPointer(t *testing.T) { dst := structWithBoolPointer{ &bf, } - if err := Merge(&dst, src); err != nil { + if err := mergo.Merge(&dst, src); err != nil { t.FailNow() } if dst.C == src.C { @@ -897,17 +900,17 @@ func TestBooleanPointer(t *testing.T) { func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) { testCases := []struct { name string - options []func(*Config) + options []func(*mergo.Config) err string }{ { "With override and append slice", - []func(*Config){WithOverride, WithAppendSlice}, + []func(*mergo.Config){mergo.WithOverride, mergo.WithAppendSlice}, "cannot append two slices with different type", }, { "With override and type check", - []func(*Config){WithOverride, WithTypeCheck}, + []func(*mergo.Config){mergo.WithOverride, mergo.WithTypeCheck}, "cannot override two slices with different type", }, } @@ -920,7 +923,7 @@ func TestMergeMapWithInnerSliceOfDifferentType(t *testing.T) { "foo": []int{1, 2}, } - if err := Merge(&src, &dst, tc.options...); err == nil || !strings.Contains(err.Error(), tc.err) { + if err := mergo.Merge(&src, &dst, tc.options...); err == nil || !strings.Contains(err.Error(), tc.err) { t.Errorf("expected %q, got %q", tc.err, err) } }) @@ -931,7 +934,7 @@ func TestMergeSlicesIsNotSupported(t *testing.T) { src := []string{"a", "b"} dst := []int{1, 2} - if err := Merge(&src, &dst, WithOverride, WithAppendSlice); err != ErrNotSupported { - t.Errorf("expected %q, got %q", ErrNotSupported, err) + if err := mergo.Merge(&src, &dst, mergo.WithOverride, mergo.WithAppendSlice); err != mergo.ErrNotSupported { + t.Errorf("expected %q, got %q", mergo.ErrNotSupported, err) } } diff --git a/pr80_test.go b/pr80_test.go index 978a8fe..64f2a78 100644 --- a/pr80_test.go +++ b/pr80_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type mapInterface map[string]interface{} @@ -9,7 +11,7 @@ type mapInterface map[string]interface{} func TestMergeMapsEmptyString(t *testing.T) { a := mapInterface{"s": ""} b := mapInterface{"s": "foo"} - if err := Merge(&a, b); err != nil { + if err := mergo.Merge(&a, b); err != nil { t.Error(err) } if a["s"] != "foo" { diff --git a/pr81_test.go b/pr81_test.go index 10e0455..30abe74 100644 --- a/pr81_test.go +++ b/pr81_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) func TestMapInterfaceWithMultipleLayer(t *testing.T) { @@ -18,7 +20,7 @@ func TestMapInterfaceWithMultipleLayer(t *testing.T) { }, } - if err := Map(&m1, m2, WithOverride); err != nil { + if err := mergo.Map(&m1, m2, mergo.WithOverride); err != nil { t.Errorf("Error merging: %v", err) } diff --git a/v039_bugs_test.go b/v039_bugs_test.go index db0cca9..c470ed0 100644 --- a/v039_bugs_test.go +++ b/v039_bugs_test.go @@ -1,7 +1,9 @@ -package mergo +package mergo_test import ( "testing" + + "github.com/imdario/mergo" ) type inner struct { @@ -22,7 +24,7 @@ func TestV039Issue139(t *testing.T) { inner: inner{A: 10}, B: 20, } - err := MergeWithOverwrite(&dst, src) + err := mergo.MergeWithOverwrite(&dst, src) if err != nil { panic(err.Error()) } @@ -45,7 +47,7 @@ func TestV039Issue152(t *testing.T) { "field1": "wrong", }, } - if err := Map(&dst, src, WithOverride); err != nil { + if err := mergo.Map(&dst, src, mergo.WithOverride); err != nil { t.Error(err) } } @@ -81,7 +83,7 @@ func TestV039Issue146(t *testing.T) { }, }, } - if err := Merge(&dst, src, WithOverride); err != nil { + if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil { t.Error(err) } if dst.B["foo"].D == nil {