-
Notifications
You must be signed in to change notification settings - Fork 0
/
yml_test.go
149 lines (138 loc) · 3.03 KB
/
yml_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 yaml2json
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestConvert(t *testing.T) {
tests := []struct {
name, yaml, json string
}{{
name: "map list",
yaml: `- name: Jack
- name: Jill
`,
json: `[{"name":"Jack"},{"name":"Jill"}]`,
}, {
name: "single item map obj",
yaml: `name: Jack`,
json: `{"name":"Jack"}`,
}, {
name: "object as map",
yaml: `name: Jack
job: Butcher
`,
json: `{"job":"Butcher","name":"Jack"}`,
}, {
name: "object list",
yaml: `- name: Jack
job: Butcher
- name: Jill
job: Cook
obj:
empty: false
data: |
some data 123
with new line
`,
json: `[{"job":"Butcher","name":"Jack"},{"job":"Cook","name":"Jill","obj":{"data":"some data 123\nwith new line\n","empty":false}}]`,
}, {
name: "advanced yaml with alias",
yaml: `vars:
- &node_image 'node:16-alpine'
- &when_path
# web source code
- "web/**"
- some
pipeline:
deps:
image: *node_image
commands:
- "cd web/"
- yarn install
when:
path: *when_path
`,
json: `{"pipeline":{"deps":{"commands":["cd web/","yarn install"],"image":"node:16-alpine","when":{"path":["web/**","some"]}}},"vars":["node:16-alpine",["web/**","some"]]}`,
}, {
name: "map merging",
yaml: `
variables: &var
target: dist
recursive: false
try: true
one:
<<: *var
name: awesome
two:
<<: *var
try: false
`,
json: `{"one":{"name":"awesome","recursive":false,"target":"dist","try":true},"two":{"recursive":false,"target":"dist","try":false},"variables":{"recursive":false,"target":"dist","try":true}}`,
}, {
name: "map merging array",
yaml: `one: &one
name: awesome
two: &two
try: false
comb:
<<: [*one, *two]`,
json: `{"comb":{"name":"awesome","try":false},"one":{"name":"awesome"},"two":{"try":false}}`,
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := Convert([]byte(tc.yaml))
assert.NoError(t, err)
assert.EqualValues(t, tc.json, string(result))
})
}
}
func TestStreamConvert(t *testing.T) {
tests := []struct {
name, yaml, json string
}{{
name: "empty doc",
yaml: `---`,
json: "null\n",
}, {
name: "values",
yaml: `values:
- int: 5
- float: 6.8523015e+5
- none: null
`,
json: `{"values":[{"int":5},{"float":685230.15},{"none":null}]}` + "\n",
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := bytes.NewReader([]byte(tc.yaml))
w := new(strings.Builder)
err := StreamConvert(r, w)
assert.NoError(t, err)
assert.EqualValues(t, tc.json, w.String())
})
}
}
func TestErrors(t *testing.T) {
tests := []struct {
yaml string
error string
}{{
yaml: ``,
error: `EOF`,
}}
for _, tc := range tests {
r := bytes.NewReader([]byte(tc.yaml))
w := new(strings.Builder)
err := StreamConvert(r, w)
if assert.Error(t, err) {
assert.EqualValues(t, tc.error, err.Error())
}
}
// test max depth
_, err := toJSON(nil, maxDepth)
assert.ErrorIs(t, err, ErrMaxDepth)
}