-
Notifications
You must be signed in to change notification settings - Fork 0
/
xyaml_test.go
149 lines (139 loc) · 3.86 KB
/
xyaml_test.go
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
// SPDX-FileCopyrightText: 2023 6543 <[email protected]>
// SPDX-License-Identifier: MIT
package xyaml_test
import (
"math"
"reflect"
"testing"
"codeberg.org/6543/xyaml"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
func TestStdDecode(t *testing.T) {
tests := []struct {
data string
value interface{}
}{{
"value: hi",
map[string]string{"value": "hi"},
}, {
"boolean: TRUE",
map[string]interface{}{"boolean": true},
}, {
"a: {b: c}",
&struct{ A *map[string]string }{&map[string]string{"b": "c"}},
}, {
"float32_max: 3.40282346638528859811704183484516925440e+38",
map[string]float32{"float32_max": math.MaxFloat32},
}, {
"a: [1, 2]",
&struct{ A [2]int }{[2]int{1, 2}},
}, {
"a: {b: c}",
&struct{ A *struct{ B string } }{&struct{ B string }{"c"}},
}, {
"a:\n - 1\n - &ref 2\nb:\n - *ref\n - 3",
map[string][]string{"a": {"1", "2"}, "b": {"2", "3"}},
}}
for _, tt := range tests {
t.Run(tt.data, func(t *testing.T) {
newValue := reflect.New(reflect.ValueOf(tt.value).Type())
xyaml.Unmarshal([]byte(tt.data), newValue.Interface())
assert.EqualValues(t, tt.value, newValue.Elem().Interface())
})
}
}
func TestMergeSequences(t *testing.T) {
// https://github.com/yaml/yaml/issues/48
tests := []struct {
name, in, out string
}{{
name: "merging a sequence",
in: `array1: &my_array_alias
- foo
- bar
array2:
- <<: *my_array_alias
- zap`,
out: "array1:\n - foo\n - bar\narray2:\n - foo\n - bar\n - zap\n",
}, {
name: "merging two sequences",
in: `array1: &my_array_alias
- foo
- bar
array2:
- first
- <<: [*my_array_alias, *my_array_alias]
- zap`,
out: "array1:\n - foo\n - bar\narray2:\n - first\n - foo\n - bar\n - foo\n - bar\n - zap\n",
}, {
name: "merge sequences independent",
in: `array1: &alias1
- one
- two
result:
- 1
- <<: *alias1
- 2
- <<: *alias1
`,
out: "array1:\n - one\n - two\nresult:\n - \"1\"\n - one\n - two\n - \"2\"\n - one\n - two\n",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := make(map[string][]string)
assert.NoError(t, xyaml.Unmarshal([]byte(tt.in), out))
newData, err := xyaml.Marshal(out)
assert.NoError(t, err)
assert.EqualValues(t, tt.out, string(newData))
})
}
}
func TestMergeSequenceErrors(t *testing.T) {
tests := []struct {
name, in, errStg string
err error
}{{
name: "test",
in: `array2:
- <<: a, b
- zap`,
err: xyaml.ErrSequenceMerge,
errStg: "sequence merge failed: merge map node did not contain an alias node",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
node := new(yaml.Node)
err := xyaml.Unmarshal([]byte(tt.in), node)
if assert.ErrorIs(t, err, tt.err) && tt.errStg != "" {
assert.EqualValues(t, tt.errStg, err.Error())
}
})
}
}
func TestMergeMap(t *testing.T) {
tests := []struct {
name, in, out string
}{{
name: "merge unique maps",
in: "letters: &letters\n a: \"A\"\nnumbers: &numbers\n one: \"1\"\ncombined:\n <<: [ *letters, *numbers ]",
out: "combined:\n a: A\n one: \"1\"\nletters:\n a: A\nnumbers:\n one: \"1\"\n",
}, {
name: "extend maps",
in: "base: &base\n foo: FOO\n bar: BAR\nextended:\n <<: *base\n zap: ZAP",
out: "base:\n bar: BAR\n foo: FOO\nextended:\n bar: BAR\n foo: FOO\n zap: ZAP\n",
}, {
name: "overwrite vales",
in: "\nbase: &base\n val: ONE\n next: 1\n zap: 3\noverwrite:\n next: 2\n <<: *base\n val: TWO\n",
out: "base:\n next: \"1\"\n val: ONE\n zap: \"3\"\noverwrite:\n next: \"2\"\n val: TWO\n zap: \"3\"\n",
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := make(map[string]map[string]string)
assert.NoError(t, xyaml.Unmarshal([]byte(tt.in), out))
newData, err := xyaml.Marshal(out)
assert.NoError(t, err)
assert.EqualValues(t, tt.out, string(newData))
})
}
}