-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerge_test.go
More file actions
217 lines (186 loc) · 3.83 KB
/
merge_test.go
File metadata and controls
217 lines (186 loc) · 3.83 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
// SPDX-FileCopyrightText: 2026 The templig contributors.
// SPDX-License-Identifier: MPL-2.0
package templig_test
import (
"bytes"
"testing"
"go.yaml.in/yaml/v4"
"github.com/AlphaOne1/templig"
)
func TestMerge(t *testing.T) {
t.Parallel()
tests := []struct {
name string
a string
b string
want string
wantErr bool
}{
{ // 0
name: "sequence concat",
a: `["a", 2, 3, 4]`,
b: `["b", 5, 6]`,
want: `["a", 2, 3, 4, "b", 5, 6]`,
wantErr: false,
},
{ // 1
name: "mapping concat",
a: `{"a": 2, "b": 3, "c": 4}`,
b: `{"d": 5, "e": 6}`,
want: `{"a": 2, "b": 3, "c": 4, "d": 5, "e": 6}`,
wantErr: false,
},
{ // 2
name: "mapping concat with overlap",
a: `{"a": 2, "b": 3, "c": 4}`,
b: `{"b": 5, "c": 6, "d": 7}`,
want: `{"a": 2, "b": 5, "c": 6, "d": 7}`,
wantErr: false,
},
{ // 3
name: "mapping recursive",
a: `{"a": {"a": 1, "b": 2}}`,
b: `{"a": {"a": 2, "c": 3}}`,
want: `{"a": {"a": 2, "b": 2, "c": 3}}`,
wantErr: false,
},
{ // 4
name: "mapping recursive",
a: `{"a": {"a": 1, "b": 2}}`,
b: `{"a": ["a", 2]}`,
want: ``,
wantErr: true,
},
{ // 5
name: "alias contained",
a: `
a: &ref
a: 3
b: *ref`,
b: `
a:
a: 4`,
want: `a: &ref
a: 4
b: *ref`,
wantErr: false,
},
{ // 6
name: "alias modified",
a: `
a: &ref
a: 3
b: *ref`,
b: `
b:
a: 5`,
want: `a: &ref
a: 3
b:
a: 5`,
wantErr: false,
},
{ // 7
name: "alias in merge",
a: `
a: &ref
a: 3
b: *ref`,
b: `
x-blah: &blah
a: 5
b: *blah`,
want: `a: &ref
a: 3
b:
a: 5
x-blah: &blah
a: 5`,
wantErr: false,
},
{ // 8
name: "anchor mismatch",
a: `
a: &ref0
a: 3`,
b: `
a: &ref1
a: 3`,
want: ``,
wantErr: true,
},
}
for testNum, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var nodeA yaml.Node
var nodeB yaml.Node
test.want += "\n"
if aErr := yaml.Unmarshal([]byte(test.a), &nodeA); aErr != nil {
t.Errorf("%v - %v: could not unmarshal a: %v", testNum, test.name, aErr)
}
if bErr := yaml.Unmarshal([]byte(test.b), &nodeB); bErr != nil {
t.Errorf("%v - %v: could not unmarshal b: %v", testNum, test.name, bErr)
}
result, resultErr := templig.MergeYAMLNodes(&nodeA, &nodeB)
if !test.wantErr && resultErr != nil {
t.Errorf("%v - %v: could not merge: %v", testNum, test.name, resultErr)
return
}
if test.wantErr && resultErr == nil {
t.Errorf("%v - %v: should not have been able to merge", testNum, test.name)
return
}
if resultErr != nil {
return
}
buf := bytes.Buffer{}
buf.Reset()
if err := yaml.NewEncoder(&buf).Encode(result.Content[0]); err != nil {
t.Errorf("%v - %v: could not marshal document to yaml: %v",
testNum, test.name, err)
}
if buf.String() != test.want {
t.Errorf("%v - %v: result mismatch, wanted:\n%v\nbut got:\n%v",
testNum, test.name, test.want, buf.String())
}
})
}
}
func TestStrangeDocumentNodes(t *testing.T) {
t.Parallel()
nodeA := yaml.Node{
Kind: yaml.DocumentNode,
}
nodeB := yaml.Node{
Kind: yaml.DocumentNode,
Content: []*yaml.Node{
{
Kind: yaml.ScalarNode,
},
},
}
res, resErr := templig.MergeYAMLNodes(&nodeA, &nodeB)
if res != nil {
t.Errorf("merging strange document nodes must produce nil")
}
if resErr == nil {
t.Errorf("expected an error merging strange document nodes")
}
}
func TestUnknownNodeKind(t *testing.T) {
t.Parallel()
a := yaml.Node{
Kind: 0,
}
b := yaml.Node{
Kind: 0,
}
res, resErr := templig.MergeYAMLNodes(&a, &b)
if res != nil {
t.Errorf("merging unknown node kind must produce nil")
}
if resErr == nil {
t.Errorf("expected an error merging unknown node kind")
}
}