Debian Codegraph @

internal/libyaml/composer_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 Composer stage
5
6 package libyaml
7
8 import (
9 "fmt"
10 "testing"
11 )
12
13 // checkComposedNode recursively validates a composed node against expected structure
14 func checkComposedNode(t *testing.T, node *Node, wantMap map[string]any, path string) {
15 t.Helper()
16
17 // Check kind
18 if kindStr, ok := wantMap["kind"].(string); ok {
19 var expectedKind Kind
20 switch kindStr {
21 case "Scalar":
22 expectedKind = ScalarNode
23 case "Sequence":
24 expectedKind = SequenceNode
25 case "Mapping":
26 expectedKind = MappingNode
27 }
28 if node.Kind != expectedKind {
29 t.Fatalf("%s: got kind %v; want %v", path, node.Kind, expectedKind)
30 }
31 }
32
33 // Check tag
34 if wantTag, ok := wantMap["tag"].(string); ok {
35 if node.Tag != wantTag {
36 t.Fatalf("%s: got tag %q; want %q", path, node.Tag, wantTag)
37 }
38 }
39
40 // Check value (for scalars)
41 if wantValue, ok := wantMap["value"].(string); ok {
42 if node.Value != wantValue {
43 t.Fatalf("%s: got value %q; want %q", path, node.Value, wantValue)
44 }
45 }
46
47 // Check style
48 if wantStyle, ok := wantMap["style"].(string); ok {
49 var expectedStyle Style
50 switch wantStyle {
51 case "Single":
52 expectedStyle = SingleQuotedStyle
53 case "Double":
54 expectedStyle = DoubleQuotedStyle
55 case "Literal":
56 expectedStyle = LiteralStyle
57 case "Folded":
58 expectedStyle = FoldedStyle
59 case "Flow":
60 expectedStyle = FlowStyle
61 }
62 if expectedStyle != 0 && node.Style&expectedStyle == 0 {
63 t.Fatalf("%s: expected style %v but got %v", path, expectedStyle, node.Style)
64 }
65 }
66
67 // Check content (for collections)
68 if wantContent, ok := wantMap["content"].([]any); ok {
69 if len(node.Content) != len(wantContent) {
70 t.Fatalf("%s: got %d children; want %d", path, len(node.Content), len(wantContent))
71 }
72 for i, wantChild := range wantContent {
73 if wantChildMap, ok := wantChild.(map[string]any); ok {
74 childPath := fmt.Sprintf("%s[%d]", path, i)
75 checkComposedNode(t, node.Content[i], wantChildMap, childPath)
76 }
77 }
78 }
79 }
80
81 func TestComposer(t *testing.T) {
82 RunTestCases(t, "composer.yaml", map[string]TestHandler{
83 "compose-scalar": func(t *testing.T, tc TestCase) {
84 t.Helper()
85
86 // Parse YAML from tc.From (YAML input string)
87 yaml := tc.From.(string)
88 c := NewComposer([]byte(yaml), nil)
89 defer c.Destroy()
90
91 // Get document node
92 doc := c.Compose()
93 if doc == nil || doc.Kind != DocumentNode {
94 t.Fatal("expected DocumentNode")
95 }
96 if len(doc.Content) == 0 {
97 t.Fatal("expected content in document")
98 }
99 node := doc.Content[0]
100
101 // Check node against want spec
102 wantMap := tc.Want.(map[string]any)
103 checkComposedNode(t, node, wantMap, "root")
104 },
105
106 "compose-collection": func(t *testing.T, tc TestCase) {
107 t.Helper()
108
109 // Parse YAML from tc.From (YAML input string)
110 yaml := tc.From.(string)
111 c := NewComposer([]byte(yaml), nil)
112 defer c.Destroy()
113
114 // Get document node
115 doc := c.Compose()
116 if doc == nil || doc.Kind != DocumentNode {
117 t.Fatal("expected DocumentNode")
118 }
119 if len(doc.Content) == 0 {
120 t.Fatal("expected content in document")
121 }
122 node := doc.Content[0]
123
124 // Check node against want spec
125 wantMap := tc.Want.(map[string]any)
126 checkComposedNode(t, node, wantMap, "root")
127 },
128
129 "compose-style": func(t *testing.T, tc TestCase) {
130 t.Helper()
131
132 // Parse YAML from tc.From (YAML input string)
133 yaml := tc.From.(string)
134 c := NewComposer([]byte(yaml), nil)
135 defer c.Destroy()
136
137 // Get document node
138 doc := c.Compose()
139 if doc == nil || doc.Kind != DocumentNode {
140 t.Fatal("expected DocumentNode")
141 }
142 if len(doc.Content) == 0 {
143 t.Fatal("expected content in document")
144 }
145 node := doc.Content[0]
146
147 // Check node against want spec
148 wantMap := tc.Want.(map[string]any)
149 checkComposedNode(t, node, wantMap, "root")
150 },
151 })
152 }