0 | |
Index: golang-github-imdario-mergo/mergo_test.go
|
1 | |
===================================================================
|
2 | |
--- golang-github-imdario-mergo.orig/mergo_test.go
|
3 | |
+++ golang-github-imdario-mergo/mergo_test.go
|
4 | |
@@ -6,11 +6,8 @@
|
5 | |
package mergo
|
6 | |
|
7 | |
import (
|
8 | |
- "io/ioutil"
|
9 | |
"reflect"
|
10 | |
"testing"
|
11 | |
-
|
12 | |
- "gopkg.in/yaml.v1"
|
13 | |
)
|
14 | |
|
15 | |
type simpleTest struct {
|
16 | |
@@ -311,131 +308,3 @@ func TestMaps(t *testing.T) {
|
17 | |
t.Fatalf(`n overwritten in m: m["c"].Value(%d) != n["c"].Value(%d)`, m["c"].Value, n["c"].Value)
|
18 | |
}
|
19 | |
}
|
20 | |
-
|
21 | |
-func TestYAMLMaps(t *testing.T) {
|
22 | |
- thing := loadYAML("testdata/thing.yml")
|
23 | |
- license := loadYAML("testdata/license.yml")
|
24 | |
- ft := thing["fields"].(map[interface{}]interface{})
|
25 | |
- fl := license["fields"].(map[interface{}]interface{})
|
26 | |
- expectedLength := len(ft) + len(fl)
|
27 | |
- if err := Merge(&license, thing); err != nil {
|
28 | |
- t.Fatal(err.Error())
|
29 | |
- }
|
30 | |
- currentLength := len(license["fields"].(map[interface{}]interface{}))
|
31 | |
- if currentLength != expectedLength {
|
32 | |
- t.Fatalf(`thing not merged in license properly, license must have %d elements instead of %d`, expectedLength, currentLength)
|
33 | |
- }
|
34 | |
- fields := license["fields"].(map[interface{}]interface{})
|
35 | |
- if _, ok := fields["id"]; !ok {
|
36 | |
- t.Fatalf(`thing not merged in license properly, license must have a new id field from thing`)
|
37 | |
- }
|
38 | |
-}
|
39 | |
-
|
40 | |
-func TestTwoPointerValues(t *testing.T) {
|
41 | |
- a := &simpleTest{}
|
42 | |
- b := &simpleTest{42}
|
43 | |
- if err := Merge(a, b); err != nil {
|
44 | |
- t.Fatalf(`Boom. You crossed the streams: %s`, err)
|
45 | |
- }
|
46 | |
-}
|
47 | |
-
|
48 | |
-func TestMap(t *testing.T) {
|
49 | |
- a := complexTest{}
|
50 | |
- a.Id = "athing"
|
51 | |
- c := moreComplextText{a, simpleTest{}, simpleTest{}}
|
52 | |
- b := map[string]interface{}{
|
53 | |
- "ct": map[string]interface{}{
|
54 | |
- "st": map[string]interface{}{
|
55 | |
- "value": 42,
|
56 | |
- },
|
57 | |
- "sz": 1,
|
58 | |
- "id": "bthing",
|
59 | |
- },
|
60 | |
- "st": &simpleTest{144}, // Mapping a reference
|
61 | |
- "zt": simpleTest{299}, // Mapping a missing field (zt doesn't exist)
|
62 | |
- "nt": simpleTest{3},
|
63 | |
- }
|
64 | |
- if err := Map(&c, b); err != nil {
|
65 | |
- t.FailNow()
|
66 | |
- }
|
67 | |
- m := b["ct"].(map[string]interface{})
|
68 | |
- n := m["st"].(map[string]interface{})
|
69 | |
- o := b["st"].(*simpleTest)
|
70 | |
- p := b["nt"].(simpleTest)
|
71 | |
- if c.Ct.St.Value != 42 {
|
72 | |
- t.Fatalf("b not merged in properly: c.Ct.St.Value(%d) != b.Ct.St.Value(%d)", c.Ct.St.Value, n["value"])
|
73 | |
- }
|
74 | |
- if c.St.Value != 144 {
|
75 | |
- t.Fatalf("b not merged in properly: c.St.Value(%d) != b.St.Value(%d)", c.St.Value, o.Value)
|
76 | |
- }
|
77 | |
- if c.Nt.Value != 3 {
|
78 | |
- t.Fatalf("b not merged in properly: c.Nt.Value(%d) != b.Nt.Value(%d)", c.St.Value, p.Value)
|
79 | |
- }
|
80 | |
- if c.Ct.sz == 1 {
|
81 | |
- t.Fatalf("a's private field sz not preserved from merge: c.Ct.sz(%d) == b.Ct.sz(%d)", c.Ct.sz, m["sz"])
|
82 | |
- }
|
83 | |
- if c.Ct.Id == m["id"] {
|
84 | |
- t.Fatalf("a's field Id merged unexpectedly: c.Ct.Id(%s) == b.Ct.Id(%s)", c.Ct.Id, m["id"])
|
85 | |
- }
|
86 | |
-}
|
87 | |
-
|
88 | |
-func TestSimpleMap(t *testing.T) {
|
89 | |
- a := simpleTest{}
|
90 | |
- b := map[string]interface{}{
|
91 | |
- "value": 42,
|
92 | |
- }
|
93 | |
- if err := Map(&a, b); err != nil {
|
94 | |
- t.FailNow()
|
95 | |
- }
|
96 | |
- if a.Value != 42 {
|
97 | |
- t.Fatalf("b not merged in properly: a.Value(%d) != b.Value(%v)", a.Value, b["value"])
|
98 | |
- }
|
99 | |
-}
|
100 | |
-
|
101 | |
-type pointerMapTest struct {
|
102 | |
- A int
|
103 | |
- hidden int
|
104 | |
- B *simpleTest
|
105 | |
-}
|
106 | |
-
|
107 | |
-func TestBackAndForth(t *testing.T) {
|
108 | |
- pt := pointerMapTest{42, 1, &simpleTest{66}}
|
109 | |
- m := make(map[string]interface{})
|
110 | |
- if err := Map(&m, pt); err != nil {
|
111 | |
- t.FailNow()
|
112 | |
- }
|
113 | |
- var (
|
114 | |
- v interface{}
|
115 | |
- ok bool
|
116 | |
- )
|
117 | |
- if v, ok = m["a"]; v.(int) != pt.A || !ok {
|
118 | |
- t.Fatalf("pt not merged in properly: m[`a`](%d) != pt.A(%d)", v, pt.A)
|
119 | |
- }
|
120 | |
- if v, ok = m["b"]; !ok {
|
121 | |
- t.Fatalf("pt not merged in properly: B is missing in m")
|
122 | |
- }
|
123 | |
- var st *simpleTest
|
124 | |
- if st = v.(*simpleTest); st.Value != 66 {
|
125 | |
- t.Fatalf("something went wrong while mapping pt on m, B wasn't copied")
|
126 | |
- }
|
127 | |
- bpt := pointerMapTest{}
|
128 | |
- if err := Map(&bpt, m); err != nil {
|
129 | |
- t.Fatal(err)
|
130 | |
- }
|
131 | |
- if bpt.A != pt.A {
|
132 | |
- t.Fatalf("pt not merged in properly: bpt.A(%d) != pt.A(%d)", bpt.A, pt.A)
|
133 | |
- }
|
134 | |
- if bpt.hidden == pt.hidden {
|
135 | |
- t.Fatalf("pt unexpectedly merged: bpt.hidden(%d) == pt.hidden(%d)", bpt.hidden, pt.hidden)
|
136 | |
- }
|
137 | |
- if bpt.B.Value != pt.B.Value {
|
138 | |
- t.Fatalf("pt not merged in properly: bpt.B.Value(%d) != pt.B.Value(%d)", bpt.B.Value, pt.B.Value)
|
139 | |
- }
|
140 | |
-}
|
141 | |
-
|
142 | |
-func loadYAML(path string) (m map[string]interface{}) {
|
143 | |
- m = make(map[string]interface{})
|
144 | |
- raw, _ := ioutil.ReadFile(path)
|
145 | |
- _ = yaml.Unmarshal(raw, &m)
|
146 | |
- return
|
147 | |
-}
|