Debian Codegraph @

node_test.go

raw symbols SCIP
1 // Copyright 2011-2019 Canonical Ltd
2 // Copyright 2025 The go-yaml Project Contributors
3 // SPDX-License-Identifier: Apache-2.0
4
5 // Tests for Node operations, including encoding/decoding with Node,
6 // node manipulation, and style handling.
7
8 package yaml_test
9
10 import (
11 "bytes"
12 "fmt"
13 "reflect"
14 "testing"
15
16 "go.yaml.in/yaml/v4"
17 "go.yaml.in/yaml/v4/internal/libyaml"
18 "go.yaml.in/yaml/v4/internal/testutil/assert"
19 "go.yaml.in/yaml/v4/internal/testutil/datatest"
20 )
21
22 func assertNodeEqual(t *testing.T, want *yaml.Node, got *yaml.Node) {
23 t.Helper()
24
25 if reflect.DeepEqual(got, want) {
26 // fast path
27 return
28 }
29
30 if got.Tag != want.Tag {
31 t.Errorf("Tag mismatch: want: %q got: %q", want.Tag, got.Tag)
32 }
33
34 if got.Kind != want.Kind {
35 t.Errorf("Kind mismatch: want: %q got: %q", want.Kind, got.Kind)
36 }
37
38 if got.Style != want.Style {
39 t.Errorf("Style mismatch: want: %q got: %q", want.Style, got.Style)
40 }
41
42 if got.HeadComment != want.HeadComment {
43 t.Errorf("HeadComment mismatch: want: %#v got: %#v", want.HeadComment, got.HeadComment)
44 }
45
46 if got.LineComment != want.LineComment {
47 t.Errorf("LineComment mismatch: want: %#v got: %#v", want.LineComment, got.LineComment)
48 }
49
50 if got.FootComment != want.FootComment {
51 t.Errorf("FootComment mismatch: want: %#v got: %#v", want.FootComment, got.FootComment)
52 }
53
54 if got.Value != want.Value {
55 t.Errorf("Value mismatch: want: %q got: %q", want.Value, got.Value)
56 }
57
58 if got.Anchor != want.Anchor {
59 t.Errorf("Anchor mismatch: want: %q got: %q", want.Anchor, got.Anchor)
60 }
61
62 if got.Line != want.Line {
63 t.Errorf("Line mismatch: want: %d got: %d", want.Line, got.Line)
64 }
65
66 if got.Column != want.Column {
67 t.Errorf("Column mismatch: want: %d got: %d", want.Column, got.Column)
68 }
69
70 if !reflect.DeepEqual(got.Content, want.Content) {
71 // Content differs
72
73 if len(got.Content) != len(want.Content) {
74 t.Errorf("Content length mismatch:\nwant: %d\ngot: %d", len(want.Content), len(got.Content))
75 }
76
77 for i := 0; i < len(want.Content) && i < len(got.Content); i++ {
78 assertNodeEqual(t, want.Content[i], got.Content[i])
79 }
80 }
81
82 if t.Failed() {
83 // we already reported an error, there is no need to report it again.
84 return
85 }
86
87 // this error message is harder to read, and is only shown if no other errors were reported.
88 t.Errorf("nodes differ:\nwant:\n%#v\ngot:\n%#v", want, got)
89 }
90
91 var setStringTests = []struct {
92 str string
93 yaml string
94 node yaml.Node
95 }{
96 {
97 "something simple",
98 "something simple\n",
99 yaml.Node{
100 Kind: yaml.ScalarNode,
101 Value: "something simple",
102 Tag: "!!str",
103 },
104 }, {
105 `"quoted value"`,
106 "'\"quoted value\"'\n",
107 yaml.Node{
108 Kind: yaml.ScalarNode,
109 Value: `"quoted value"`,
110 Tag: "!!str",
111 },
112 }, {
113 "multi\nline",
114 "|-\n multi\n line\n",
115 yaml.Node{
116 Kind: yaml.ScalarNode,
117 Value: "multi\nline",
118 Tag: "!!str",
119 Style: yaml.LiteralStyle,
120 },
121 }, {
122 "123",
123 "\"123\"\n",
124 yaml.Node{
125 Kind: yaml.ScalarNode,
126 Value: "123",
127 Tag: "!!str",
128 },
129 }, {
130 "multi\nline\n",
131 "|\n multi\n line\n",
132 yaml.Node{
133 Kind: yaml.ScalarNode,
134 Value: "multi\nline\n",
135 Tag: "!!str",
136 Style: yaml.LiteralStyle,
137 },
138 }, {
139 "\x80\x81\x82",
140 "!!binary gIGC\n",
141 yaml.Node{
142 Kind: yaml.ScalarNode,
143 Value: "gIGC",
144 Tag: "!!binary",
145 },
146 },
147 }
148
149 func TestSetString(t *testing.T) {
150 t.Setenv("TZ", "UTC")
151 for _, item := range setStringTests {
152 item := item
153 t.Run("", func(t *testing.T) {
154 t.Logf("str: %q", item.str)
155
156 var node yaml.Node
157 node.SetString(item.str)
158
159 assertNodeEqual(t, &item.node, &node)
160
161 buf := bytes.Buffer{}
162 enc := yaml.NewEncoder(&buf)
163 enc.SetIndent(2)
164 err := enc.Encode(&item.node)
165 assert.NoError(t, err)
166 err = enc.Close()
167 assert.NoError(t, err)
168 assert.Equal(t, item.yaml, buf.String())
169
170 var doc yaml.Node
171 err = yaml.Unmarshal([]byte(item.yaml), &doc)
172 assert.NoError(t, err)
173
174 var str string
175 err = node.Load(&str)
176 assert.NoError(t, err)
177 assert.Equal(t, item.str, str)
178 })
179 }
180 }
181
182 var nodeEncodeDecodeTests = []struct {
183 value any
184 yaml string
185 node yaml.Node
186 }{{
187 "something simple",
188 "something simple\n",
189 yaml.Node{
190 Kind: yaml.ScalarNode,
191 Value: "something simple",
192 Tag: "!!str",
193 },
194 }, {
195 `"quoted value"`,
196 "'\"quoted value\"'\n",
197 yaml.Node{
198 Kind: yaml.ScalarNode,
199 Style: yaml.SingleQuotedStyle,
200 Value: `"quoted value"`,
201 Tag: "!!str",
202 },
203 }, {
204 123,
205 "123",
206 yaml.Node{
207 Kind: yaml.ScalarNode,
208 Value: `123`,
209 Tag: "!!int",
210 },
211 }, {
212 []any{1, 2},
213 "[1, 2]",
214 yaml.Node{
215 Kind: yaml.SequenceNode,
216 Tag: "!!seq",
217 Content: []*yaml.Node{{
218 Kind: yaml.ScalarNode,
219 Value: "1",
220 Tag: "!!int",
221 }, {
222 Kind: yaml.ScalarNode,
223 Value: "2",
224 Tag: "!!int",
225 }},
226 },
227 }, {
228 map[string]any{"a": "b"},
229 "a: b",
230 yaml.Node{
231 Kind: yaml.MappingNode,
232 Tag: "!!map",
233 Content: []*yaml.Node{{
234 Kind: yaml.ScalarNode,
235 Value: "a",
236 Tag: "!!str",
237 }, {
238 Kind: yaml.ScalarNode,
239 Value: "b",
240 Tag: "!!str",
241 }},
242 },
243 }}
244
245 func TestNodeEncodeDecode(t *testing.T) {
246 for _, item := range nodeEncodeDecodeTests {
247 item := item
248 t.Run("", func(t *testing.T) {
249 t.Logf("Encode/Decode test value: %#v", item.value)
250
251 var v any
252 err := item.node.Load(&v)
253 assert.NoError(t, err)
254 assert.DeepEqual(t, item.value, v)
255
256 var n yaml.Node
257 err = n.Dump(item.value)
258 assert.NoError(t, err)
259 assert.DeepEqual(t, item.node, n)
260 })
261 }
262 }
263
264 func TestNodeZeroEncodeDecode(t *testing.T) {
265 // Zero node value behaves as nil when encoding...
266 var n yaml.Node
267 data, err := yaml.Marshal(&n)
268 assert.NoError(t, err)
269 assert.Equal(t, "null\n", string(data))
270
271 // ... and decoding.
272 v := &struct{}{}
273 err = n.Load(&v)
274 assert.NoError(t, err)
275 assert.IsNil(t, v)
276
277 // ... and even when looking for its tag.
278 assert.Equal(t, "!!null", n.ShortTag())
279
280 // Kind zero is still unknown, though.
281 n.Line = 1
282 _, err = yaml.Marshal(&n)
283 assert.ErrorMatches(t, "go-yaml dump error in serializer: cannot represent node with unknown kind 0", err)
284 err = n.Load(&v)
285 assert.ErrorMatches(t, `go-yaml load error in constructor at L1: cannot construct node with unknown kind: '0'`, err)
286 }
287
288 func TestNodeOmitEmpty(t *testing.T) {
289 var v struct {
290 A int
291 B yaml.Node `yaml:",omitempty"`
292 }
293 v.A = 1
294 data, err := yaml.Marshal(&v)
295 assert.NoError(t, err)
296 assert.Equal(t, "a: 1\n", string(data))
297
298 v.B.Line = 1
299 _, err = yaml.Marshal(&v)
300 assert.ErrorMatches(t, "go-yaml dump error in serializer: cannot represent node with unknown kind 0", err)
301 }
302
303 // NodeInfo represents the information about a YAML node in a test-friendly format
304 type NodeInfo struct {
305 Kind string `yaml:"kind"`
306 Style string `yaml:"style,omitempty"`
307 Anchor string `yaml:"anchor,omitempty"`
308 Tag string `yaml:"tag,omitempty"`
309 Head string `yaml:"head,omitempty"`
310 Line string `yaml:"line,omitempty"`
311 Foot string `yaml:"foot,omitempty"`
312 Text string `yaml:"text,omitempty"`
313 Content []*NodeInfo `yaml:"content,omitempty"`
314 }
315
316 // isStandardTag checks if a tag is a standard YAML tag
317 func isStandardTag(tag string) bool {
318 switch tag {
319 case "!!null", "!!bool", "!!int", "!!float", "!!str", "!!seq", "!!map":
320 return true
321 }
322 return false
323 }
324
325 // parseNodeInfo converts a NodeInfo structure into a yaml.Node
326 func parseNodeInfo(info *NodeInfo) (*yaml.Node, error) {
327 if info == nil {
328 return nil, fmt.Errorf("nil NodeInfo")
329 }
330
331 node := &yaml.Node{}
332
333 // Parse Kind
334 switch info.Kind {
335 case "Document":
336 node.Kind = yaml.DocumentNode
337 case "Sequence":
338 node.Kind = yaml.SequenceNode
339 case "Mapping":
340 node.Kind = yaml.MappingNode
341 case "Scalar":
342 node.Kind = yaml.ScalarNode
343 case "Alias":
344 node.Kind = yaml.AliasNode
345 default:
346 return nil, fmt.Errorf("unknown node kind: %s", info.Kind)
347 }
348
349 // Parse Style
350 if info.Style != "" {
351 switch info.Style {
352 case "Double":
353 node.Style = yaml.DoubleQuotedStyle
354 case "Single":
355 node.Style = yaml.SingleQuotedStyle
356 case "Literal":
357 node.Style = yaml.LiteralStyle
358 case "Folded":
359 node.Style = yaml.FoldedStyle
360 case "Flow":
361 node.Style = yaml.FlowStyle
362 case "Tagged":
363 node.Style = yaml.TaggedStyle
364 default:
365 return nil, fmt.Errorf("unknown style: %s", info.Style)
366 }
367 }
368
369 // Set other fields
370 node.Anchor = info.Anchor
371 node.Tag = info.Tag
372 node.HeadComment = info.Head
373 node.LineComment = info.Line
374 node.FootComment = info.Foot
375
376 // Add TaggedStyle bit for custom tags (not standard YAML tags)
377 if info.Tag != "" && !isStandardTag(info.Tag) && node.Style != 0 {
378 node.Style |= yaml.TaggedStyle
379 }
380
381 // Set value for scalar nodes
382 if node.Kind == yaml.ScalarNode {
383 node.Value = info.Text
384 }
385
386 // Parse content for non-scalar nodes
387 if info.Content != nil {
388 node.Content = make([]*yaml.Node, len(info.Content))
389 for i, childInfo := range info.Content {
390 childNode, err := parseNodeInfo(childInfo)
391 if err != nil {
392 return nil, fmt.Errorf("content[%d]: %w", i, err)
393 }
394 node.Content[i] = childNode
395 }
396 }
397
398 return node, nil
399 }
400
401 // formatNodeInfo converts a yaml.Node into a NodeInfo structure for comparison
402 func formatNodeInfo(n yaml.Node) *NodeInfo {
403 info := &NodeInfo{
404 Kind: formatKindForTest(n.Kind),
405 }
406
407 if style := formatStyleForTest(n.Style); style != "" {
408 info.Style = style
409 }
410 if n.Anchor != "" {
411 info.Anchor = n.Anchor
412 }
413 if tag := formatTagForTest(n.Tag, n.Style); tag != "" {
414 info.Tag = tag
415 }
416 if n.HeadComment != "" {
417 info.Head = n.HeadComment
418 }
419 if n.LineComment != "" {
420 info.Line = n.LineComment
421 }
422 if n.FootComment != "" {
423 info.Foot = n.FootComment
424 }
425
426 if info.Kind == "Scalar" {
427 info.Text = n.Value
428 } else if n.Content != nil {
429 info.Content = make([]*NodeInfo, len(n.Content))
430 for i, node := range n.Content {
431 info.Content[i] = formatNodeInfo(*node)
432 }
433 }
434
435 return info
436 }
437
438 // formatKindForTest converts a YAML node kind into its string representation.
439 func formatKindForTest(k yaml.Kind) string {
440 switch k {
441 case yaml.DocumentNode:
442 return "Document"
443 case yaml.SequenceNode:
444 return "Sequence"
445 case yaml.MappingNode:
446 return "Mapping"
447 case yaml.ScalarNode:
448 return "Scalar"
449 case yaml.AliasNode:
450 return "Alias"
451 default:
452 return "Unknown"
453 }
454 }
455
456 // formatStyleForTest converts a YAML node style into its string representation.
457 func formatStyleForTest(s yaml.Style) string {
458 // Strip out TaggedStyle bit - it's implicit when we have a custom tag
459 baseStyle := s &^ yaml.TaggedStyle
460
461 switch baseStyle {
462 case yaml.DoubleQuotedStyle:
463 return "Double"
464 case yaml.SingleQuotedStyle:
465 return "Single"
466 case yaml.LiteralStyle:
467 return "Literal"
468 case yaml.FoldedStyle:
469 return "Folded"
470 case yaml.FlowStyle:
471 return "Flow"
472 }
473 return ""
474 }
475
476 // formatTagForTest converts a YAML tag string to its string representation.
477 func formatTagForTest(tag string, style yaml.Style) string {
478 // Check if the tag was explicit in the input
479 tagWasExplicit := style&yaml.TaggedStyle != 0
480
481 // Show !!str only if it was explicit in the input
482 switch tag {
483 case "!!str", "!!map", "!!seq":
484 if tagWasExplicit {
485 return tag
486 }
487 return ""
488 }
489
490 // Show all other tags
491 return tag
492 }
493
494 // runNodeTestCase executes a single node test case
495 func runNodeTestCase(t *testing.T, tc map[string]any) {
496 t.Helper()
497
498 name := tc["name"].(string)
499 yamlInput := tc["yaml"].(string)
500
501 // Get the expected node structure
502 nodeInfoData, ok := tc["node"]
503 if !ok {
504 t.Fatal("test case missing 'node' field")
505 }
506
507 // Convert the node data to NodeInfo
508 var expectedInfo NodeInfo
509 nodeBytes, err := yaml.Marshal(nodeInfoData)
510 assert.NoError(t, err)
511 err = yaml.Unmarshal(nodeBytes, &expectedInfo)
512 assert.NoError(t, err)
513
514 // Parse expected NodeInfo into yaml.Node
515 expectedNode, err := parseNodeInfo(&expectedInfo)
516 assert.NoError(t, err)
517
518 // Check if decode/encode should be skipped
519 decodeTest := true
520 encodeTest := true
521 if skipDecode, ok := tc["decode"].(bool); ok && !skipDecode {
522 decodeTest = false
523 }
524 if skipEncode, ok := tc["encode"].(bool); ok && !skipEncode {
525 encodeTest = false
526 }
527
528 if decodeTest {
529 var actualNode yaml.Node
530 err := yaml.Unmarshal([]byte(yamlInput), &actualNode)
531 assert.NoError(t, err)
532
533 // Compare using NodeInfo for better error messages
534 actualInfo := formatNodeInfo(actualNode)
535 assertNodeInfoEqual(t, &expectedInfo, actualInfo, name)
536 }
537
538 if encodeTest {
539 // Encode the expected node with 2-space indent
540 buf := bytes.Buffer{}
541 enc := yaml.NewEncoder(&buf)
542 enc.SetIndent(2)
543 err := enc.Encode(expectedNode)
544 assert.NoError(t, err)
545 err = enc.Close()
546 assert.NoError(t, err)
547
548 assert.Equal(t, yamlInput, buf.String())
549 }
550 }
551
552 // assertNodeInfoEqual compares two NodeInfo structures and reports differences
553 func assertNodeInfoEqual(t *testing.T, expected, actual *NodeInfo, context string) {
554 t.Helper()
555
556 if expected == nil && actual == nil {
557 return
558 }
559 if expected == nil {
560 t.Fatalf("%s: expected nil, got %+v", context, actual)
561 return
562 }
563 if actual == nil {
564 t.Fatalf("%s: expected %+v, got nil", context, expected)
565 return
566 }
567
568 assert.Equalf(t, expected.Kind, actual.Kind, "%s: Kind mismatch", context)
569 assert.Equalf(t, expected.Style, actual.Style, "%s: Style mismatch", context)
570 assert.Equalf(t, expected.Anchor, actual.Anchor, "%s: Anchor mismatch", context)
571 assert.Equalf(t, expected.Tag, actual.Tag, "%s: Tag mismatch", context)
572 assert.Equalf(t, expected.Head, actual.Head, "%s: Head comment mismatch", context)
573 assert.Equalf(t, expected.Line, actual.Line, "%s: Line comment mismatch", context)
574 assert.Equalf(t, expected.Foot, actual.Foot, "%s: Foot comment mismatch", context)
575 assert.Equalf(t, expected.Text, actual.Text, "%s: Text mismatch", context)
576
577 if len(expected.Content) != len(actual.Content) {
578 t.Fatalf("%s: Content length mismatch: expected %d, got %d",
579 context, len(expected.Content), len(actual.Content))
580 }
581
582 for i := range expected.Content {
583 assertNodeInfoEqual(t, expected.Content[i], actual.Content[i],
584 fmt.Sprintf("%s.content[%d]", context, i))
585 }
586 }
587
588 func TestNodeFromYAML(t *testing.T) {
589 t.Setenv("TZ", "UTC")
590 datatest.RunTestCases(t, func() ([]map[string]any, error) {
591 return datatest.LoadTestCasesFromFile("testdata/node.yaml", libyaml.LoadAny)
592 }, map[string]datatest.TestHandler{
593 "node-test": runNodeTestCase,
594 })
595 }
596
597 func TestNodeLoad(t *testing.T) {
598 // Test basic Load functionality
599 node := &yaml.Node{
600 Kind: yaml.MappingNode,
601 Tag: "!!map",
602 Content: []*yaml.Node{
603 {Kind: yaml.ScalarNode, Value: "name", Tag: "!!str"},
604 {Kind: yaml.ScalarNode, Value: "test", Tag: "!!str"},
605 },
606 }
607
608 var result map[string]string
609 err := node.Load(&result)
610 assert.NoError(t, err)
611 assert.Equal(t, "test", result["name"])
612 }
613
614 func TestNodeLoadWithKnownFields(t *testing.T) {
615 // Test that KnownFields option is respected
616 node := &yaml.Node{
617 Kind: yaml.MappingNode,
618 Tag: "!!map",
619 Content: []*yaml.Node{
620 {Kind: yaml.ScalarNode, Value: "known", Tag: "!!str"},
621 {Kind: yaml.ScalarNode, Value: "value", Tag: "!!str"},
622 {Kind: yaml.ScalarNode, Value: "unknown", Tag: "!!str"},
623 {Kind: yaml.ScalarNode, Value: "other", Tag: "!!str"},
624 },
625 }
626
627 type Target struct {
628 Known string `yaml:"known"`
629 }
630
631 // Without KnownFields - should succeed
632 var result1 Target
633 err := node.Load(&result1)
634 assert.NoError(t, err)
635 assert.Equal(t, "value", result1.Known)
636
637 // With KnownFields - should fail
638 var result2 Target
639 err = node.Load(&result2, yaml.WithKnownFields())
640 assert.NotNil(t, err)
641 assert.ErrorMatches(t, ".*unknown not found.*", err)
642 }
643
644 func TestNodeLoadPreservesKnownFieldsInUnmarshaler(t *testing.T) {
645 // This test validates the fix for Issue #460
646 type strictConfig struct {
647 Name string `yaml:"name"`
648 Port int `yaml:"port"`
649 }
650
651 // Custom unmarshaler using Load with KnownFields
652 type Config struct {
653 strictConfig
654 }
655
656 var unmarshalCalled bool
657 unmarshaler := struct {
658 Config
659 }{}
660
661 // Override UnmarshalYAML to use node.Load
662 oldUnmarshal := func(node *yaml.Node) error {
663 unmarshalCalled = true
664 type plain strictConfig
665 return node.Load((*plain)(&unmarshaler.strictConfig), yaml.WithKnownFields())
666 }
667
668 // Valid YAML - should succeed
669 validYAML := []byte(`
670 name: test
671 port: 8080
672 `)
673
674 var validNode yaml.Node
675 err := yaml.Unmarshal(validYAML, &validNode)
676 assert.NoError(t, err)
677
678 err = oldUnmarshal(validNode.Content[0])
679 assert.NoError(t, err)
680 assert.True(t, unmarshalCalled)
681 assert.Equal(t, "test", unmarshaler.Name)
682 assert.Equal(t, 8080, unmarshaler.Port)
683
684 // Invalid YAML with unknown field - should fail
685 invalidYAML := []byte(`
686 name: test
687 port: 8080
688 unknown: field
689 `)
690
691 var invalidNode yaml.Node
692 err = yaml.Unmarshal(invalidYAML, &invalidNode)
693 assert.NoError(t, err)
694
695 unmarshalCalled = false
696 err = oldUnmarshal(invalidNode.Content[0])
697 assert.NotNil(t, err)
698 assert.True(t, unmarshalCalled)
699 assert.ErrorMatches(t, ".*unknown not found.*", err)
700 }
701
702 func TestNodeDump(t *testing.T) {
703 // Test basic Dump functionality
704 value := map[string]string{"name": "test"}
705
706 var node yaml.Node
707 err := node.Dump(value)
708 assert.NoError(t, err)
709 assert.Equal(t, yaml.MappingNode, node.Kind)
710 assert.Equal(t, "!!map", node.Tag)
711 assert.Equal(t, 2, len(node.Content))
712 }
713
714 func TestNodeDumpWithOptions(t *testing.T) {
715 // Test Dump with encoder options
716 type Config struct {
717 Name string `yaml:"name"`
718 Port int `yaml:"port"`
719 }
720 value := Config{Name: "myapp", Port: 8080}
721
722 // Dump with V4 (default)
723 var node1 yaml.Node
724 err := node1.Dump(value, yaml.WithV4Defaults())
725 assert.NoError(t, err)
726 assert.Equal(t, yaml.MappingNode, node1.Kind)
727
728 // Dump with V3
729 var node2 yaml.Node
730 err = node2.Dump(value, yaml.WithV3Defaults())
731 assert.NoError(t, err)
732 assert.Equal(t, yaml.MappingNode, node2.Kind)
733
734 // Both should produce valid nodes with same content structure
735 assert.Equal(t, len(node1.Content), len(node2.Content))
736 }
737
738 func TestNodeLoadWithUniqueKeys(t *testing.T) {
739 // Test that UniqueKeys option is respected
740 node := &yaml.Node{
741 Kind: yaml.MappingNode,
742 Tag: "!!map",
743 Content: []*yaml.Node{
744 {Kind: yaml.ScalarNode, Value: "key", Tag: "!!str"},
745 {Kind: yaml.ScalarNode, Value: "value1", Tag: "!!str"},
746 {Kind: yaml.ScalarNode, Value: "key", Tag: "!!str"},
747 {Kind: yaml.ScalarNode, Value: "value2", Tag: "!!str"},
748 },
749 }
750
751 // With UniqueKeys (default) - should fail on duplicate
752 var result1 map[string]string
753 err := node.Load(&result1, yaml.WithUniqueKeys())
754 assert.NotNil(t, err)
755 assert.ErrorMatches(t, ".*already defined.*", err)
756
757 // Without UniqueKeys - should succeed (last value wins)
758 var result2 map[string]string
759 err = node.Load(&result2, yaml.WithUniqueKeys(false))
760 assert.NoError(t, err)
761 assert.Equal(t, "value2", result2["key"])
762 }
763
764 func TestNodeLoadInvalidOptions(t *testing.T) {
765 node := &yaml.Node{
766 Kind: yaml.ScalarNode,
767 Value: "test",
768 Tag: "!!str",
769 }
770
771 // Test with invalid indent option (should fail during applyOptions)
772 var result string
773 err := node.Load(&result, yaml.WithIndent(100))
774 assert.NotNil(t, err)
775 assert.ErrorMatches(t, ".*indent must be.*", err)
776 }
777
778 func TestNodeDumpInvalidOptions(t *testing.T) {
779 value := "test"
780
781 // Test with invalid indent option
782 var node yaml.Node
783 err := node.Dump(value, yaml.WithIndent(100))
784 assert.NotNil(t, err)
785 assert.ErrorMatches(t, ".*indent must be.*", err)
786 }