Debian Codegraph @

internal/libyaml/options_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for options.go functions and methods.
5
6 package libyaml
7
8 import (
9 "regexp"
10 "testing"
11
12 "go.yaml.in/yaml/v4/internal/testutil/assert"
13 )
14
15 func TestOptions(t *testing.T) {
16 handlers := map[string]TestHandler{
17 "with-indent": runWithIndentTest,
18 "with-compact-seq-indent": runWithCompactSeqIndentTest,
19 "with-known-fields": runWithKnownFieldsTest,
20 "with-single-document": runWithSingleDocumentTest,
21 "with-stream-nodes": runWithStreamNodesTest,
22 "with-all-documents": runWithAllDocumentsTest,
23 "with-line-width": runWithLineWidthTest,
24 "with-unicode": runWithUnicodeTest,
25 "with-unique-keys": runWithUniqueKeysTest,
26 "with-canonical": runWithCanonicalTest,
27 "with-line-break": runWithLineBreakTest,
28 "with-explicit-start": runWithExplicitStartTest,
29 "with-explicit-end": runWithExplicitEndTest,
30 "with-flow-simple-collections": runWithFlowSimpleCollectionsTest,
31 "with-quote-preference": runWithQuotePreferenceTest,
32 "apply-options": runApplyOptionsTest,
33 }
34
35 RunTestCases(t, "options.yaml", handlers)
36 }
37
38 // runWithIndentTest tests WithIndent
39 func runWithIndentTest(t *testing.T, tc TestCase) {
40 t.Helper()
41
42 indent, ok := tc.From.(int)
43 if !ok {
44 t.Fatalf("from should be int, got %T", tc.From)
45 }
46
47 opt := WithIndent(indent)
48 opts := &Options{}
49 err := opt(opts)
50
51 if tc.Like != "" {
52 // Expect error
53 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
54 if err != nil {
55 matched, _ := regexp.MatchString(tc.Like, err.Error())
56 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
57 }
58 } else {
59 // Expect success
60 assert.NoErrorf(t, err, "WithIndent(%d) error: %v", indent, err)
61 checkWantFields(t, opts, tc.Want)
62 }
63 }
64
65 // runWithCompactSeqIndentTest tests WithCompactSeqIndent
66 func runWithCompactSeqIndentTest(t *testing.T, tc TestCase) {
67 t.Helper()
68
69 args := parseBoolSlice(t, tc.From)
70 opt := WithCompactSeqIndent(args...)
71 opts := &Options{}
72 err := opt(opts)
73
74 if tc.Like != "" {
75 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
76 if err != nil {
77 matched, _ := regexp.MatchString(tc.Like, err.Error())
78 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
79 }
80 } else {
81 assert.NoErrorf(t, err, "WithCompactSeqIndent error: %v", err)
82 checkWantFields(t, opts, tc.Want)
83 }
84 }
85
86 // runWithKnownFieldsTest tests WithKnownFields
87 func runWithKnownFieldsTest(t *testing.T, tc TestCase) {
88 t.Helper()
89
90 args := parseBoolSlice(t, tc.From)
91 opt := WithKnownFields(args...)
92 opts := &Options{}
93 err := opt(opts)
94
95 if tc.Like != "" {
96 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
97 if err != nil {
98 matched, _ := regexp.MatchString(tc.Like, err.Error())
99 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
100 }
101 } else {
102 assert.NoErrorf(t, err, "WithKnownFields error: %v", err)
103 checkWantFields(t, opts, tc.Want)
104 }
105 }
106
107 // runWithSingleDocumentTest tests WithSingleDocument
108 func runWithSingleDocumentTest(t *testing.T, tc TestCase) {
109 t.Helper()
110
111 args := parseBoolSlice(t, tc.From)
112 opt := WithSingleDocument(args...)
113 opts := &Options{}
114 err := opt(opts)
115
116 if tc.Like != "" {
117 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
118 if err != nil {
119 matched, _ := regexp.MatchString(tc.Like, err.Error())
120 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
121 }
122 } else {
123 assert.NoErrorf(t, err, "WithSingleDocument error: %v", err)
124 checkWantFields(t, opts, tc.Want)
125 }
126 }
127
128 // runWithStreamNodesTest tests WithStreamNodes
129 func runWithStreamNodesTest(t *testing.T, tc TestCase) {
130 t.Helper()
131
132 args := parseBoolSlice(t, tc.From)
133 opt := WithStreamNodes(args...)
134 opts := &Options{}
135 err := opt(opts)
136
137 if tc.Like != "" {
138 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
139 if err != nil {
140 matched, _ := regexp.MatchString(tc.Like, err.Error())
141 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
142 }
143 } else {
144 assert.NoErrorf(t, err, "WithStreamNodes error: %v", err)
145 checkWantFields(t, opts, tc.Want)
146 }
147 }
148
149 // runWithAllDocumentsTest tests WithAllDocuments
150 func runWithAllDocumentsTest(t *testing.T, tc TestCase) {
151 t.Helper()
152
153 args := parseBoolSlice(t, tc.From)
154 opt := WithAllDocuments(args...)
155 opts := &Options{}
156 err := opt(opts)
157
158 if tc.Like != "" {
159 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
160 if err != nil {
161 matched, _ := regexp.MatchString(tc.Like, err.Error())
162 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
163 }
164 } else {
165 assert.NoErrorf(t, err, "WithAllDocuments error: %v", err)
166 checkWantFields(t, opts, tc.Want)
167 }
168 }
169
170 // runWithLineWidthTest tests WithLineWidth
171 func runWithLineWidthTest(t *testing.T, tc TestCase) {
172 t.Helper()
173
174 width, ok := tc.From.(int)
175 if !ok {
176 t.Fatalf("from should be int, got %T", tc.From)
177 }
178
179 opt := WithLineWidth(width)
180 opts := &Options{}
181 err := opt(opts)
182
183 assert.NoErrorf(t, err, "WithLineWidth(%d) error: %v", width, err)
184 checkWantFields(t, opts, tc.Want)
185 }
186
187 // runWithUnicodeTest tests WithUnicode
188 func runWithUnicodeTest(t *testing.T, tc TestCase) {
189 t.Helper()
190
191 args := parseBoolSlice(t, tc.From)
192 opt := WithUnicode(args...)
193 opts := &Options{}
194 err := opt(opts)
195
196 if tc.Like != "" {
197 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
198 if err != nil {
199 matched, _ := regexp.MatchString(tc.Like, err.Error())
200 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
201 }
202 } else {
203 assert.NoErrorf(t, err, "WithUnicode error: %v", err)
204 checkWantFields(t, opts, tc.Want)
205 }
206 }
207
208 // runWithUniqueKeysTest tests WithUniqueKeys
209 func runWithUniqueKeysTest(t *testing.T, tc TestCase) {
210 t.Helper()
211
212 args := parseBoolSlice(t, tc.From)
213 opt := WithUniqueKeys(args...)
214 opts := &Options{}
215 err := opt(opts)
216
217 if tc.Like != "" {
218 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
219 if err != nil {
220 matched, _ := regexp.MatchString(tc.Like, err.Error())
221 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
222 }
223 } else {
224 assert.NoErrorf(t, err, "WithUniqueKeys error: %v", err)
225 checkWantFields(t, opts, tc.Want)
226 }
227 }
228
229 // runWithCanonicalTest tests WithCanonical
230 func runWithCanonicalTest(t *testing.T, tc TestCase) {
231 t.Helper()
232
233 args := parseBoolSlice(t, tc.From)
234 opt := WithCanonical(args...)
235 opts := &Options{}
236 err := opt(opts)
237
238 if tc.Like != "" {
239 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
240 if err != nil {
241 matched, _ := regexp.MatchString(tc.Like, err.Error())
242 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
243 }
244 } else {
245 assert.NoErrorf(t, err, "WithCanonical error: %v", err)
246 checkWantFields(t, opts, tc.Want)
247 }
248 }
249
250 // runWithLineBreakTest tests WithLineBreak
251 func runWithLineBreakTest(t *testing.T, tc TestCase) {
252 t.Helper()
253
254 lineBreak := parseLineBreak(t, tc.From)
255 opt := WithLineBreak(lineBreak)
256 opts := &Options{}
257 err := opt(opts)
258
259 assert.NoErrorf(t, err, "WithLineBreak error: %v", err)
260 checkWantFields(t, opts, tc.Want)
261 }
262
263 // runWithExplicitStartTest tests WithExplicitStart
264 func runWithExplicitStartTest(t *testing.T, tc TestCase) {
265 t.Helper()
266
267 args := parseBoolSlice(t, tc.From)
268 opt := WithExplicitStart(args...)
269 opts := &Options{}
270 err := opt(opts)
271
272 if tc.Like != "" {
273 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
274 if err != nil {
275 matched, _ := regexp.MatchString(tc.Like, err.Error())
276 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
277 }
278 } else {
279 assert.NoErrorf(t, err, "WithExplicitStart error: %v", err)
280 checkWantFields(t, opts, tc.Want)
281 }
282 }
283
284 // runWithExplicitEndTest tests WithExplicitEnd
285 func runWithExplicitEndTest(t *testing.T, tc TestCase) {
286 t.Helper()
287
288 args := parseBoolSlice(t, tc.From)
289 opt := WithExplicitEnd(args...)
290 opts := &Options{}
291 err := opt(opts)
292
293 if tc.Like != "" {
294 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
295 if err != nil {
296 matched, _ := regexp.MatchString(tc.Like, err.Error())
297 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
298 }
299 } else {
300 assert.NoErrorf(t, err, "WithExplicitEnd error: %v", err)
301 checkWantFields(t, opts, tc.Want)
302 }
303 }
304
305 // runWithFlowSimpleCollectionsTest tests WithFlowSimpleCollections
306 func runWithFlowSimpleCollectionsTest(t *testing.T, tc TestCase) {
307 t.Helper()
308
309 args := parseBoolSlice(t, tc.From)
310 opt := WithFlowSimpleCollections(args...)
311 opts := &Options{}
312 err := opt(opts)
313
314 if tc.Like != "" {
315 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
316 if err != nil {
317 matched, _ := regexp.MatchString(tc.Like, err.Error())
318 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
319 }
320 } else {
321 assert.NoErrorf(t, err, "WithFlowSimpleCollections error: %v", err)
322 checkWantFields(t, opts, tc.Want)
323 }
324 }
325
326 // runWithQuotePreferenceTest tests WithQuotePreference
327 func runWithQuotePreferenceTest(t *testing.T, tc TestCase) {
328 t.Helper()
329
330 style := parseQuoteStyle(t, tc.From)
331 opt := WithQuotePreference(style)
332 opts := &Options{}
333 err := opt(opts)
334
335 if tc.Like != "" {
336 assert.NotNilf(t, err, "expected error matching %q", tc.Like)
337 if err != nil {
338 matched, _ := regexp.MatchString(tc.Like, err.Error())
339 assert.Truef(t, matched, "error %q should match %q", err.Error(), tc.Like)
340 }
341 } else {
342 assert.NoErrorf(t, err, "WithQuotePreference error: %v", err)
343 checkWantFields(t, opts, tc.Want)
344 }
345 }
346
347 // runApplyOptionsTest tests ApplyOptions
348 func runApplyOptionsTest(t *testing.T, tc TestCase) {
349 t.Helper()
350
351 // Test with no options to verify v4 defaults
352 opts, err := ApplyOptions()
353
354 assert.NoErrorf(t, err, "ApplyOptions error: %v", err)
355 if opts != nil {
356 checkWantFields(t, opts, tc.Want)
357 }
358 }
359
360 // Helper functions
361
362 // parseBoolSlice converts tc.From to []bool
363 func parseBoolSlice(t *testing.T, from any) []bool {
364 t.Helper()
365
366 slice, ok := from.([]any)
367 if !ok {
368 t.Fatalf("from should be []any, got %T", from)
369 }
370
371 result := make([]bool, len(slice))
372 for i, v := range slice {
373 b, ok := v.(bool)
374 if !ok {
375 t.Fatalf("from[%d] should be bool, got %T", i, v)
376 }
377 result[i] = b
378 }
379 return result
380 }
381
382 // parseLineBreak converts string or int to LineBreak
383 func parseLineBreak(t *testing.T, from any) LineBreak {
384 t.Helper()
385
386 switch v := from.(type) {
387 case string:
388 switch v {
389 case "LN_BREAK":
390 return LN_BREAK
391 case "CR_BREAK":
392 return CR_BREAK
393 case "CRLN_BREAK":
394 return CRLN_BREAK
395 default:
396 t.Fatalf("unknown LineBreak constant: %s", v)
397 }
398 case int:
399 return LineBreak(v)
400 default:
401 t.Fatalf("from should be string or int, got %T", from)
402 }
403 return 0
404 }
405
406 // parseQuoteStyle converts string or int to QuoteStyle
407 func parseQuoteStyle(t *testing.T, from any) QuoteStyle {
408 t.Helper()
409
410 switch v := from.(type) {
411 case string:
412 switch v {
413 case "QuoteSingle":
414 return QuoteSingle
415 case "QuoteDouble":
416 return QuoteDouble
417 case "QuoteLegacy":
418 return QuoteLegacy
419 default:
420 t.Fatalf("unknown QuoteStyle constant: %s", v)
421 }
422 case int:
423 return QuoteStyle(v)
424 default:
425 t.Fatalf("from should be string or int, got %T", from)
426 }
427 return 0
428 }
429
430 // checkWantFields verifies expected fields in Options
431 func checkWantFields(t *testing.T, opts *Options, want any) {
432 t.Helper()
433
434 if want == nil {
435 return
436 }
437
438 wantMap, ok := want.(map[string]any)
439 if !ok {
440 t.Fatalf("want should be map, got %T", want)
441 }
442
443 for key, expectedValue := range wantMap {
444 switch key {
445 case "indent":
446 expected, ok := expectedValue.(int)
447 if !ok {
448 t.Fatalf("want.indent should be int, got %T", expectedValue)
449 }
450 assert.Equalf(t, expected, opts.Indent, "Indent = %d, want %d", opts.Indent, expected)
451
452 case "compact_seq_indent":
453 expected, ok := expectedValue.(bool)
454 if !ok {
455 t.Fatalf("want.compact_seq_indent should be bool, got %T", expectedValue)
456 }
457 assert.Equalf(t, expected, opts.CompactSeqIndent, "CompactSeqIndent = %v, want %v", opts.CompactSeqIndent, expected)
458
459 case "known_fields":
460 expected, ok := expectedValue.(bool)
461 if !ok {
462 t.Fatalf("want.known_fields should be bool, got %T", expectedValue)
463 }
464 assert.Equalf(t, expected, opts.KnownFields, "KnownFields = %v, want %v", opts.KnownFields, expected)
465
466 case "single_document":
467 expected, ok := expectedValue.(bool)
468 if !ok {
469 t.Fatalf("want.single_document should be bool, got %T", expectedValue)
470 }
471 assert.Equalf(t, expected, opts.SingleDocument, "SingleDocument = %v, want %v", opts.SingleDocument, expected)
472
473 case "stream_nodes":
474 expected, ok := expectedValue.(bool)
475 if !ok {
476 t.Fatalf("want.stream_nodes should be bool, got %T", expectedValue)
477 }
478 assert.Equalf(t, expected, opts.StreamNodes, "StreamNodes = %v, want %v", opts.StreamNodes, expected)
479
480 case "all_documents":
481 expected, ok := expectedValue.(bool)
482 if !ok {
483 t.Fatalf("want.all_documents should be bool, got %T", expectedValue)
484 }
485 assert.Equalf(t, expected, opts.AllDocuments, "AllDocuments = %v, want %v", opts.AllDocuments, expected)
486
487 case "line_width":
488 expected, ok := expectedValue.(int)
489 if !ok {
490 t.Fatalf("want.line_width should be int, got %T", expectedValue)
491 }
492 assert.Equalf(t, expected, opts.LineWidth, "LineWidth = %d, want %d", opts.LineWidth, expected)
493
494 case "unicode":
495 expected, ok := expectedValue.(bool)
496 if !ok {
497 t.Fatalf("want.unicode should be bool, got %T", expectedValue)
498 }
499 assert.Equalf(t, expected, opts.Unicode, "Unicode = %v, want %v", opts.Unicode, expected)
500
501 case "unique_keys":
502 expected, ok := expectedValue.(bool)
503 if !ok {
504 t.Fatalf("want.unique_keys should be bool, got %T", expectedValue)
505 }
506 assert.Equalf(t, expected, opts.UniqueKeys, "UniqueKeys = %v, want %v", opts.UniqueKeys, expected)
507
508 case "canonical":
509 expected, ok := expectedValue.(bool)
510 if !ok {
511 t.Fatalf("want.canonical should be bool, got %T", expectedValue)
512 }
513 assert.Equalf(t, expected, opts.Canonical, "Canonical = %v, want %v", opts.Canonical, expected)
514
515 case "line_break":
516 expectedStr, ok := expectedValue.(string)
517 if !ok {
518 t.Fatalf("want.line_break should be string, got %T", expectedValue)
519 }
520 expected := parseLineBreak(t, expectedStr)
521 assert.Equalf(t, expected, opts.LineBreak, "LineBreak = %v, want %v", opts.LineBreak, expected)
522
523 case "explicit_start":
524 expected, ok := expectedValue.(bool)
525 if !ok {
526 t.Fatalf("want.explicit_start should be bool, got %T", expectedValue)
527 }
528 assert.Equalf(t, expected, opts.ExplicitStart, "ExplicitStart = %v, want %v", opts.ExplicitStart, expected)
529
530 case "explicit_end":
531 expected, ok := expectedValue.(bool)
532 if !ok {
533 t.Fatalf("want.explicit_end should be bool, got %T", expectedValue)
534 }
535 assert.Equalf(t, expected, opts.ExplicitEnd, "ExplicitEnd = %v, want %v", opts.ExplicitEnd, expected)
536
537 case "flow_simple_collections":
538 expected, ok := expectedValue.(bool)
539 if !ok {
540 t.Fatalf("want.flow_simple_collections should be bool, got %T", expectedValue)
541 }
542 assert.Equalf(t, expected, opts.FlowSimpleCollections, "FlowSimpleCollections = %v, want %v", opts.FlowSimpleCollections, expected)
543
544 case "quote_preference":
545 expectedStr, ok := expectedValue.(string)
546 if !ok {
547 t.Fatalf("want.quote_preference should be string, got %T", expectedValue)
548 }
549 expected := parseQuoteStyle(t, expectedStr)
550 assert.Equalf(t, expected, opts.QuotePreference, "QuotePreference = %v, want %v", opts.QuotePreference, expected)
551
552 default:
553 t.Fatalf("unknown want field: %s", key)
554 }
555 }
556 }