Debian Codegraph @

yaml_test.go

raw symbols SCIP
1 package yaml_test
2
3 import (
4 "fmt"
5 "io"
6 "reflect"
7 "strings"
8 "testing"
9
10 "github.com/goccy/go-yaml"
11 "github.com/goccy/go-yaml/parser"
12 )
13
14 func TestRoundTripWithComment(t *testing.T) {
15 yml := `
16 # head comment
17 key: value # line comment
18 `
19 var v struct {
20 Key string
21 }
22 comments := yaml.CommentMap{}
23
24 if err := yaml.UnmarshalWithOptions([]byte(yml), &v, yaml.Strict(), yaml.CommentToMap(comments)); err != nil {
25 t.Fatal(err)
26 }
27 out, err := yaml.MarshalWithOptions(v, yaml.WithComment(comments))
28 if err != nil {
29 t.Fatal(err)
30 }
31 got := "\n" + string(out)
32 if yml != got {
33 t.Fatalf("failed to get round tripped yaml: %s", got)
34 }
35 }
36
37 func TestStreamDecodingWithComment(t *testing.T) {
38 yml := `
39 # comment
40 ---
41 a:
42 b:
43 c: # comment
44 ---
45 foo: bar # comment
46 ---
47 - a
48 - b
49 - c # comment
50 `
51 cm := yaml.CommentMap{}
52 dec := yaml.NewDecoder(strings.NewReader(yml), yaml.CommentToMap(cm))
53 var commentPathsWithDocIndex [][]string
54 for {
55 var v any
56 if err := dec.Decode(&v); err != nil {
57 if err == io.EOF {
58 break
59 }
60 t.Fatal(err)
61 }
62 paths := make([]string, 0, len(cm))
63 for k := range cm {
64 paths = append(paths, k)
65 }
66 commentPathsWithDocIndex = append(commentPathsWithDocIndex, paths)
67 for k := range cm {
68 delete(cm, k)
69 }
70 }
71 if !reflect.DeepEqual(commentPathsWithDocIndex, [][]string{
72 {"$.a.b.c"},
73 {"$.foo"},
74 {"$[2]"},
75 }) {
76 t.Fatalf("failed to get comment: %v", commentPathsWithDocIndex)
77 }
78 }
79
80 func TestDecodeKeepAddress(t *testing.T) {
81 var data = `
82 a: &a [_]
83 b: &b [*a,*a]
84 c: &c [*b,*b]
85 d: &d [*c,*c]
86 `
87 var v map[string]any
88 if err := yaml.Unmarshal([]byte(data), &v); err != nil {
89 t.Fatal(err)
90 }
91 a := v["a"]
92 b := v["b"]
93 for _, vv := range v["b"].([]any) {
94 if fmt.Sprintf("%p", a) != fmt.Sprintf("%p", vv) {
95 t.Fatalf("failed to decode b element as keep address")
96 }
97 }
98 for _, vv := range v["c"].([]any) {
99 if fmt.Sprintf("%p", b) != fmt.Sprintf("%p", vv) {
100 t.Fatalf("failed to decode c element as keep address")
101 }
102 }
103 }
104
105 func TestSmartAnchor(t *testing.T) {
106 var data = `
107 a: &a [_,_,_,_,_,_,_,_,_,_,_,_,_,_,_]
108 b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a,*a]
109 c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b,*b]
110 d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c,*c]
111 e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d,*d]
112 f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e,*e]
113 g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f,*f]
114 h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g,*g]
115 i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h,*h]
116 `
117 var v any
118 if err := yaml.Unmarshal([]byte(data), &v); err != nil {
119 t.Fatal(err)
120 }
121 got, err := yaml.MarshalWithOptions(v, yaml.WithSmartAnchor())
122 if err != nil {
123 t.Fatal(err)
124 }
125 expected := `
126 a: &a
127 - _
128 - _
129 - _
130 - _
131 - _
132 - _
133 - _
134 - _
135 - _
136 - _
137 - _
138 - _
139 - _
140 - _
141 - _
142 b: &b
143 - *a
144 - *a
145 - *a
146 - *a
147 - *a
148 - *a
149 - *a
150 - *a
151 - *a
152 - *a
153 c: &c
154 - *b
155 - *b
156 - *b
157 - *b
158 - *b
159 - *b
160 - *b
161 - *b
162 - *b
163 - *b
164 d: &d
165 - *c
166 - *c
167 - *c
168 - *c
169 - *c
170 - *c
171 - *c
172 - *c
173 - *c
174 - *c
175 e: &e
176 - *d
177 - *d
178 - *d
179 - *d
180 - *d
181 - *d
182 - *d
183 - *d
184 - *d
185 - *d
186 f: &f
187 - *e
188 - *e
189 - *e
190 - *e
191 - *e
192 - *e
193 - *e
194 - *e
195 - *e
196 - *e
197 g: &g
198 - *f
199 - *f
200 - *f
201 - *f
202 - *f
203 - *f
204 - *f
205 - *f
206 - *f
207 - *f
208 h: &h
209 - *g
210 - *g
211 - *g
212 - *g
213 - *g
214 - *g
215 - *g
216 - *g
217 - *g
218 - *g
219 i:
220 - *h
221 - *h
222 - *h
223 - *h
224 - *h
225 - *h
226 - *h
227 - *h
228 - *h
229 - *h
230 `
231 if strings.TrimPrefix(expected, "\n") != string(got) {
232 t.Fatalf("failed to encode: %s", string(got))
233 }
234 }
235
236 func TestCustomErrorMessage(t *testing.T) {
237 data := `
238 foo:
239 bar:
240 foo: 2
241 baz:
242 foo: 3
243 foo: 2
244 `
245 if _, err := parser.ParseBytes([]byte(data), 0); err == nil {
246 t.Fatalf("expected error")
247 } else {
248 yamlErr, ok := err.(yaml.Error)
249 if !ok {
250 t.Fatalf("failed to get yaml.Error from error: %T", err)
251 }
252 expected := `
253 [7:1] custom message
254 4 | foo: 2
255 5 | baz:
256 6 | foo: 3
257 > 7 | foo: 2
258 ^
259 `
260 got := "\n" + yaml.FormatErrorWithToken("custom message", yamlErr.GetToken(), false, true)
261 if expected != got {
262 t.Fatalf("unexpected error message:\nexpected:\n%s\nbut got:\n%s", expected, got)
263 }
264 }
265 }