Repository navigation
Expand file tree
/
Copy pathmerge_tree_test.go
More file actions
220 lines (160 loc) · 4.84 KB
/
Copy pathmerge_tree_test.go
File metadata and controls
220 lines (160 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package config //nolint:testpackage // exercises the unexported mergeTreeInto helper
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-config/v2/tree"
)
const (
srcFirst = "first"
srcSecond = "second"
)
// TestMergeTreeInto_MapRecurse verifies that merging a src tree whose child is a map
// recurses into the dst counterpart rather than replacing it.
func TestMergeTreeInto_MapRecurse(t *testing.T) {
t.Parallel()
// dst: { server: { port: 8080, host: "localhost" } }
dst := tree.New()
server := tree.New()
port := tree.New()
port.Value = 8080
port.Source = srcFirst
host := tree.New()
host.Value = "localhost"
host.Source = srcFirst
server.SetChild("port", port)
server.SetChild("host", host)
dst.SetChild("server", server)
// src: { server: { port: 9090, ssl: true } }
src := tree.New()
srcServer := tree.New()
srcPort := tree.New()
srcPort.Value = 9090
srcPort.Source = srcSecond
srcSSL := tree.New()
srcSSL.Value = true
srcSSL.Source = srcSecond
srcServer.SetChild("port", srcPort)
srcServer.SetChild("ssl", srcSSL)
src.SetChild("server", srcServer)
mergeTreeInto(dst, src)
serverNode := dst.Child("server")
require.NotNil(t, serverNode)
// Three keys: port, host, ssl.
assert.Len(t, serverNode.ChildrenKeys(), 3)
portNode := serverNode.Child("port")
require.NotNil(t, portNode)
assert.Equal(t, 9090, portNode.Value)
assert.Equal(t, srcSecond, portNode.Source)
hostNode := serverNode.Child("host")
require.NotNil(t, hostNode)
assert.Equal(t, "localhost", hostNode.Value)
assert.Equal(t, srcFirst, hostNode.Source)
sslNode := serverNode.Child("ssl")
require.NotNil(t, sslNode)
assert.Equal(t, true, sslNode.Value)
assert.Equal(t, srcSecond, sslNode.Source)
}
// TestMergeTreeInto_LeafReplace verifies that a src leaf replaces a dst leaf.
func TestMergeTreeInto_LeafReplace(t *testing.T) {
t.Parallel()
dst := tree.New()
dstLeaf := tree.New()
dstLeaf.Value = "old"
dstLeaf.Source = srcFirst
dstLeaf.Revision = "1"
dst.SetChild("key", dstLeaf)
src := tree.New()
srcLeaf := tree.New()
srcLeaf.Value = "new"
srcLeaf.Source = srcSecond
srcLeaf.Revision = "2"
src.SetChild("key", srcLeaf)
mergeTreeInto(dst, src)
keyNode := dst.Child("key")
require.NotNil(t, keyNode)
assert.Equal(t, "new", keyNode.Value)
assert.Equal(t, srcSecond, keyNode.Source)
assert.Equal(t, "2", keyNode.Revision)
}
// TestMergeTreeInto_ArrayCarry verifies that isArray is propagated from src to dst.
func TestMergeTreeInto_ArrayCarry(t *testing.T) {
t.Parallel()
dst := tree.New()
src := tree.New()
srcArr := tree.New()
srcArr.MarkArray()
srcArr.Source = srcFirst
el0 := tree.New()
el0.Value = "a"
el0.Source = srcFirst
srcArr.SetChild("0", el0)
src.SetChild("roles", srcArr)
mergeTreeInto(dst, src)
rolesNode := dst.Child("roles")
require.NotNil(t, rolesNode)
assert.True(t, rolesNode.IsArray(), "isArray must be carried from src")
assert.Equal(t, srcFirst, rolesNode.Source)
el := rolesNode.Child("0")
require.NotNil(t, el)
assert.Equal(t, "a", el.Value)
}
// TestMergeTreeInto_OrderCarry verifies that when src.OrderSet() == true and
// dst.OrderSet() == false, the dst children are reordered to match src and
// dst.OrderSet() becomes true.
func TestMergeTreeInto_OrderCarry(t *testing.T) {
t.Parallel()
// dst has keys inserted in order: c, a, b.
dst := tree.New()
dstMap := tree.New()
dstC := tree.New()
dstC.Value = "C"
dstA := tree.New()
dstA.Value = "A"
dstB := tree.New()
dstB.Value = "B"
dstMap.SetChild("c", dstC)
dstMap.SetChild("a", dstA)
dstMap.SetChild("b", dstB)
// dst order NOT set.
dst.SetChild("map", dstMap)
// src has the same keys but declares order: a, b, c and marks OrderSet.
src := tree.New()
srcMap := tree.New()
srcA := tree.New()
srcA.Value = "A2"
srcA.Source = srcSecond
srcB := tree.New()
srcB.Value = "B2"
srcB.Source = srcSecond
srcC := tree.New()
srcC.Value = "C2"
srcC.Source = srcSecond
srcMap.SetChild("a", srcA)
srcMap.SetChild("b", srcB)
srcMap.SetChild("c", srcC)
srcMap.SetOrderSet(true)
src.SetChild("map", srcMap)
mergeTreeInto(dst, src)
mapNode := dst.Child("map")
require.NotNil(t, mapNode)
assert.True(t, mapNode.OrderSet(), "dst should inherit OrderSet from src")
keys := mapNode.ChildrenKeys()
assert.Equal(t, []string{"a", "b", "c"}, keys, "keys should be reordered to src order")
}
// TestMergeTreeInto_AnnotationCarry verifies that annotation is cloned from src
// children into dst.
func TestMergeTreeInto_AnnotationCarry(t *testing.T) {
t.Parallel()
dst := tree.New()
src := tree.New()
srcLeaf := tree.New()
srcLeaf.Value = 42
srcLeaf.Source = srcFirst
srcLeaf.SetAnnotation("yaml-annotation")
src.SetChild("key", srcLeaf)
mergeTreeInto(dst, src)
keyNode := dst.Child("key")
require.NotNil(t, keyNode)
assert.Equal(t, "yaml-annotation", keyNode.Annotation())
}