-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathyaml_unmarshal_test.go
More file actions
145 lines (112 loc) · 3.04 KB
/
Copy pathyaml_unmarshal_test.go
File metadata and controls
145 lines (112 loc) · 3.04 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
package sequencedmap_test
import (
"testing"
"github.com/speakeasy-api/openapi/sequencedmap"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestSequencedMap_Standard_Yaml_Unmarshal(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[string, string] `yaml:"map"`
}
yamlStr := `map:
key 1: value 1
key 2: value 2
key 3: value 3
`
var ts TestStruct
err := yaml.Unmarshal([]byte(yamlStr), &ts)
require.NoError(t, err)
assert.Equal(t, TestStruct{
Map: sequencedmap.New(sequencedmap.NewElem("key 1", "value 1"), sequencedmap.NewElem("key 2", "value 2"), sequencedmap.NewElem("key 3", "value 3")),
}, ts)
}
func TestSequencedMap_Yaml_Marshal_Unmarshal_RoundTrip(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[string, string] `yaml:"map"`
}
// Create original data
original := TestStruct{
Map: sequencedmap.New(
sequencedmap.NewElem("first", "value1"),
sequencedmap.NewElem("second", "value2"),
sequencedmap.NewElem("third", "value3"),
),
}
// Marshal to YAML
yamlData, err := yaml.Marshal(original)
require.NoError(t, err)
// Unmarshal back
var result TestStruct
err = yaml.Unmarshal(yamlData, &result)
require.NoError(t, err)
// Verify the round trip preserved order and values
assert.Equal(t, original, result)
// Verify order is preserved
keys := make([]string, 0)
for key := range result.Map.All() {
keys = append(keys, key)
}
assert.Equal(t, []string{"first", "second", "third"}, keys)
}
func TestSequencedMap_Yaml_Unmarshal_EmptyMap(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[string, string] `yaml:"map"`
}
yamlStr := `map: {}`
var ts TestStruct
err := yaml.Unmarshal([]byte(yamlStr), &ts)
require.NoError(t, err)
assert.Equal(t, TestStruct{
Map: sequencedmap.New[string, string](),
}, ts)
assert.Equal(t, 0, ts.Map.Len())
}
func TestSequencedMap_Yaml_Unmarshal_NilMap(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[string, string] `yaml:"map"`
}
yamlStr := `map: null`
var ts TestStruct
err := yaml.Unmarshal([]byte(yamlStr), &ts)
require.NoError(t, err)
assert.Nil(t, ts.Map)
}
func TestSequencedMap_Yaml_Unmarshal_IntegerKeys(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[int, string] `yaml:"map"`
}
yamlStr := `map:
1: first
2: second
3: third
`
var ts TestStruct
err := yaml.Unmarshal([]byte(yamlStr), &ts)
require.NoError(t, err)
expected := TestStruct{
Map: sequencedmap.New(
sequencedmap.NewElem(1, "first"),
sequencedmap.NewElem(2, "second"),
sequencedmap.NewElem(3, "third"),
),
}
assert.Equal(t, expected, ts)
}
func TestSequencedMap_Yaml_Unmarshal_Error_InvalidYaml(t *testing.T) {
t.Parallel()
type TestStruct struct {
Map *sequencedmap.Map[string, string] `yaml:"map"`
}
yamlStr := `map: "not a map"`
var ts TestStruct
err := yaml.Unmarshal([]byte(yamlStr), &ts)
require.Error(t, err)
assert.Contains(t, err.Error(), "cannot unmarshal")
}