Debian Codegraph @

internal/libyaml/serializer_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for the Serializer stage
5
6 package libyaml
7
8 import (
9 "bytes"
10 "testing"
11
12 "go.yaml.in/yaml/v4/internal/testutil/assert"
13 )
14
15 // buildNodeFromSpec recursively builds a Node from a NodeSpec
16 func buildNodeFromSpec(spec map[string]any) *Node {
17 node := &Node{}
18
19 // Set kind
20 if kindStr, ok := spec["kind"].(string); ok {
21 switch kindStr {
22 case "Document":
23 node.Kind = DocumentNode
24 case "Scalar":
25 node.Kind = ScalarNode
26 case "Sequence":
27 node.Kind = SequenceNode
28 case "Mapping":
29 node.Kind = MappingNode
30 }
31 }
32
33 // Set value
34 if value, ok := spec["value"].(string); ok {
35 node.Value = value
36 }
37
38 // Set style
39 if styleStr, ok := spec["style"].(string); ok {
40 switch styleStr {
41 case "Single":
42 node.Style = SingleQuotedStyle
43 case "Double":
44 node.Style = DoubleQuotedStyle
45 case "Flow":
46 node.Style = FlowStyle
47 }
48 }
49
50 // Set content (recursive)
51 if contentData, ok := spec["content"].([]any); ok {
52 for _, item := range contentData {
53 if itemMap, ok := item.(map[string]any); ok {
54 child := buildNodeFromSpec(itemMap)
55 node.Content = append(node.Content, child)
56 }
57 }
58 }
59
60 return node
61 }
62
63 func TestSerializer(t *testing.T) {
64 RunTestCases(t, "serializer.yaml", map[string]TestHandler{
65 "serialize-scalar": func(t *testing.T, tc TestCase) {
66 t.Helper()
67
68 // Build node from nested spec
69 nodeData, ok := tc.Node.Content.([]any)
70 if !ok || len(nodeData) == 0 {
71 t.Fatal("expected content in node spec")
72 }
73
74 // Create document with scalar
75 doc := &Node{Kind: DocumentNode}
76 for _, item := range nodeData {
77 if itemMap, ok := item.(map[string]any); ok {
78 doc.Content = append(doc.Content, buildNodeFromSpec(itemMap))
79 }
80 }
81
82 var buf bytes.Buffer
83 s := NewSerializer(&buf, DefaultOptions)
84 s.Serialize(doc)
85 s.Finish()
86
87 // Check output
88 assert.Equal(t, tc.Want.(string), buf.String())
89 },
90
91 "serialize-collection": func(t *testing.T, tc TestCase) {
92 t.Helper()
93
94 // Build node from nested spec
95 nodeData, ok := tc.Node.Content.([]any)
96 if !ok || len(nodeData) == 0 {
97 t.Fatal("expected content in node spec")
98 }
99
100 // Create document
101 doc := &Node{Kind: DocumentNode}
102 for _, item := range nodeData {
103 if itemMap, ok := item.(map[string]any); ok {
104 doc.Content = append(doc.Content, buildNodeFromSpec(itemMap))
105 }
106 }
107
108 // Serialize with appropriate indent
109 opts := DefaultOptions
110 if tc.Indent > 0 {
111 opts.Indent = tc.Indent
112 }
113
114 var buf bytes.Buffer
115 s := NewSerializer(&buf, opts)
116 s.Serialize(doc)
117 s.Finish()
118
119 // Check output
120 assert.Equal(t, tc.Want.(string), buf.String())
121 },
122
123 "serialize-style": func(t *testing.T, tc TestCase) {
124 t.Helper()
125
126 // Build node from nested spec
127 nodeData, ok := tc.Node.Content.([]any)
128 if !ok || len(nodeData) == 0 {
129 t.Fatal("expected content in node spec")
130 }
131
132 // Create document
133 doc := &Node{Kind: DocumentNode}
134 for _, item := range nodeData {
135 if itemMap, ok := item.(map[string]any); ok {
136 doc.Content = append(doc.Content, buildNodeFromSpec(itemMap))
137 }
138 }
139
140 var buf bytes.Buffer
141 s := NewSerializer(&buf, DefaultOptions)
142 s.Serialize(doc)
143 s.Finish()
144
145 // Check output
146 assert.Equal(t, tc.Want.(string), buf.String())
147 },
148 })
149 }