Debian Codegraph @

internal/libyaml/node_test.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Tests for node.go functions and methods.
5
6 package libyaml
7
8 import (
9 "reflect"
10 "testing"
11
12 "go.yaml.in/yaml/v4/internal/testutil/assert"
13 "go.yaml.in/yaml/v4/internal/testutil/datatest"
14 )
15
16 func TestNode(t *testing.T) {
17 handlers := map[string]TestHandler{
18 "isZero": runIsZeroTest,
19 "set-string": runSetStringTest,
20 "set-string-binary": runSetStringBinaryTest,
21 "short-tag": runShortTagTest,
22 "long-tag": runLongTagTest,
23 "node-is-zero": runNodeIsZeroTest,
24 "should-literal": runShouldLiteralTest,
25 }
26
27 RunTestCases(t, "node.yaml", handlers)
28 }
29
30 // runIsZeroTest tests the isZero function
31 func runIsZeroTest(t *testing.T, tc TestCase) {
32 t.Helper()
33
34 var v reflect.Value
35
36 // Handle special modifiers in 'also' field
37 switch tc.Also {
38 case "slice":
39 // Nil slice case
40 if tc.From == nil {
41 v = reflect.ValueOf(([]int)(nil))
42 } else {
43 // Convert from to slice
44 if slice, ok := tc.From.([]any); ok {
45 v = reflect.ValueOf(slice)
46 } else {
47 t.Fatalf("expected slice, got %T", tc.From)
48 }
49 }
50 case "map":
51 // Nil map case
52 if tc.From == nil {
53 v = reflect.ValueOf((map[string]any)(nil))
54 } else {
55 // Convert from to map
56 if m, ok := tc.From.(map[string]any); ok {
57 v = reflect.ValueOf(m)
58 } else {
59 t.Fatalf("expected map, got %T", tc.From)
60 }
61 }
62 default:
63 // Regular value
64 v = reflect.ValueOf(tc.From)
65 }
66
67 got := isZero(v)
68 want := datatest.WantBool(t, tc.Want, false)
69
70 assert.Equalf(t, want, got, "isZero() = %v, want %v", got, want)
71 }
72
73 // runSetStringTest tests the SetString method
74 func runSetStringTest(t *testing.T, tc TestCase) {
75 t.Helper()
76
77 str, ok := tc.From.(string)
78 if !ok {
79 t.Fatalf("from should be string, got %T", tc.From)
80 }
81
82 node := &Node{}
83 node.SetString(str)
84
85 wantMap, ok := tc.Want.(map[string]any)
86 if !ok {
87 t.Fatalf("want should be a map, got %T", tc.Want)
88 }
89
90 // Check Kind
91 if wantKind, ok := wantMap["kind"].(string); ok {
92 gotKind := kindToString(node.Kind)
93 assert.Equalf(t, wantKind, gotKind, "Kind = %v, want %v", gotKind, wantKind)
94 }
95
96 // Check Tag
97 if wantTag, ok := wantMap["tag"].(string); ok {
98 assert.Equalf(t, wantTag, node.Tag, "Tag = %v, want %v", node.Tag, wantTag)
99 }
100
101 // Check Value
102 if wantValue, ok := wantMap["value"].(string); ok {
103 assert.Equalf(t, wantValue, node.Value, "Value = %v, want %v", node.Value, wantValue)
104 }
105
106 // Check Style
107 if wantStyle, ok := wantMap["style"].(string); ok {
108 gotStyle := styleToString(node.Style)
109 assert.Equalf(t, wantStyle, gotStyle, "Style = %v, want %v", gotStyle, wantStyle)
110 }
111 }
112
113 // runSetStringBinaryTest tests SetString with invalid UTF-8
114 func runSetStringBinaryTest(t *testing.T, tc TestCase) {
115 t.Helper()
116
117 // Get binary input from hex
118 input := HexToBytes(t, tc.InputHex)
119 str := string(input)
120
121 node := &Node{}
122 node.SetString(str)
123
124 wantMap, ok := tc.Want.(map[string]any)
125 if !ok {
126 t.Fatalf("want should be a map, got %T", tc.Want)
127 }
128
129 // Check Kind
130 if wantKind, ok := wantMap["kind"].(string); ok {
131 gotKind := kindToString(node.Kind)
132 assert.Equalf(t, wantKind, gotKind, "Kind = %v, want %v", gotKind, wantKind)
133 }
134
135 // Check Tag
136 if wantTag, ok := wantMap["tag"].(string); ok {
137 assert.Equalf(t, wantTag, node.Tag, "Tag = %v, want %v", node.Tag, wantTag)
138 }
139
140 // For binary data, we just verify it's base64 encoded (not checking exact value)
141 if node.Tag == binaryTag {
142 assert.Truef(t, len(node.Value) > 0, "binary value should not be empty")
143 }
144
145 // Check Style
146 if wantStyle, ok := wantMap["style"].(string); ok {
147 gotStyle := styleToString(node.Style)
148 assert.Equalf(t, wantStyle, gotStyle, "Style = %v, want %v", gotStyle, wantStyle)
149 }
150 }
151
152 // runShortTagTest tests the shortTag function
153 func runShortTagTest(t *testing.T, tc TestCase) {
154 t.Helper()
155
156 node := nodeFromSpec(t, tc.Node)
157 got := node.ShortTag()
158 want, ok := tc.Want.(string)
159 if !ok {
160 t.Fatalf("want should be string, got %T", tc.Want)
161 }
162
163 assert.Equalf(t, want, got, "ShortTag() = %v, want %v", got, want)
164 }
165
166 // runLongTagTest tests the longTag function
167 func runLongTagTest(t *testing.T, tc TestCase) {
168 t.Helper()
169
170 node := nodeFromSpec(t, tc.Node)
171 got := node.LongTag()
172 want, ok := tc.Want.(string)
173 if !ok {
174 t.Fatalf("want should be string, got %T", tc.Want)
175 }
176
177 assert.Equalf(t, want, got, "LongTag() = %v, want %v", got, want)
178 }
179
180 // runNodeIsZeroTest tests the Node.IsZero method
181 func runNodeIsZeroTest(t *testing.T, tc TestCase) {
182 t.Helper()
183
184 node := nodeFromSpec(t, tc.Node)
185 got := node.IsZero()
186 want := datatest.WantBool(t, tc.Want, false)
187
188 assert.Equalf(t, want, got, "Node.IsZero() = %v, want %v", got, want)
189 }
190
191 // runShouldLiteralTest tests the shouldUseLiteralStyle helper
192 func runShouldLiteralTest(t *testing.T, tc TestCase) {
193 t.Helper()
194
195 str, ok := tc.From.(string)
196 if !ok {
197 t.Fatalf("from should be string, got %T", tc.From)
198 }
199
200 got := shouldUseLiteralStyle(str)
201 want := datatest.WantBool(t, tc.Want, false)
202
203 assert.Equalf(t, want, got, "shouldUseLiteralStyle() = %v, want %v", got, want)
204 }
205
206 // nodeFromSpec creates a Node from a NodeSpec
207 func nodeFromSpec(t *testing.T, spec NodeSpec) *Node {
208 t.Helper()
209
210 node := &Node{}
211
212 // Set Kind
213 if spec.Kind != "" {
214 node.Kind = parseKind(t, spec.Kind)
215 }
216
217 // Set Tag
218 node.Tag = spec.Tag
219
220 // Set Value
221 node.Value = spec.Value
222
223 // Set Style
224 if spec.Style != "" {
225 node.Style = parseStyle(t, spec.Style)
226 }
227
228 return node
229 }
230
231 // parseKind converts a string to Kind
232 func parseKind(t *testing.T, s string) Kind {
233 t.Helper()
234 switch s {
235 case "Document":
236 return DocumentNode
237 case "Sequence":
238 return SequenceNode
239 case "Mapping":
240 return MappingNode
241 case "Scalar":
242 return ScalarNode
243 case "Alias":
244 return AliasNode
245 case "Stream":
246 return StreamNode
247 case "":
248 return 0
249 default:
250 t.Fatalf("unknown kind: %s", s)
251 return 0
252 }
253 }
254
255 // parseStyle converts a string to Style
256 func parseStyle(t *testing.T, s string) Style {
257 t.Helper()
258 switch s {
259 case "Tagged":
260 return TaggedStyle
261 case "DoubleQuoted":
262 return DoubleQuotedStyle
263 case "SingleQuoted":
264 return SingleQuotedStyle
265 case "Literal":
266 return LiteralStyle
267 case "Folded":
268 return FoldedStyle
269 case "Flow":
270 return FlowStyle
271 case "":
272 return 0
273 default:
274 t.Fatalf("unknown style: %s", s)
275 return 0
276 }
277 }
278
279 // kindToString converts Kind to string for comparison
280 func kindToString(k Kind) string {
281 switch k {
282 case DocumentNode:
283 return "DocumentNode"
284 case SequenceNode:
285 return "SequenceNode"
286 case MappingNode:
287 return "MappingNode"
288 case ScalarNode:
289 return "ScalarNode"
290 case AliasNode:
291 return "AliasNode"
292 case StreamNode:
293 return "StreamNode"
294 case 0:
295 return ""
296 default:
297 return "unknown"
298 }
299 }
300
301 // styleToString converts Style to string for comparison
302 func styleToString(s Style) string {
303 switch s {
304 case TaggedStyle:
305 return "tagged"
306 case DoubleQuotedStyle:
307 return "double-quoted"
308 case SingleQuotedStyle:
309 return "single-quoted"
310 case LiteralStyle:
311 return "literal"
312 case FoldedStyle:
313 return "folded"
314 case FlowStyle:
315 return "flow"
316 case 0:
317 return "plain"
318 default:
319 return "unknown"
320 }
321 }