Debian Codegraph @

internal/libyaml/desolver_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 Desolver stage
5
6 package libyaml
7
8 import (
9 "testing"
10
11 "go.yaml.in/yaml/v4/internal/testutil/assert"
12 )
13
14 func TestDesolver(t *testing.T) {
15 RunTestCases(t, "desolver.yaml", map[string]TestHandler{
16 "desolve-inferable": func(t *testing.T, tc TestCase) {
17 t.Helper()
18
19 node := &Node{
20 Kind: ScalarNode,
21 Tag: tc.Node.Tag,
22 Value: tc.Node.Value,
23 }
24
25 d := NewDesolver(nil)
26 d.Desolve(node)
27
28 // Extract want fields from tc.Want (type any)
29 wantMap := tc.Want.(map[string]any)
30 wantTag := wantMap["tag"].(string)
31
32 // Check tag
33 assert.Equal(t, wantTag, node.Tag)
34
35 // Check style
36 if wantStyle, ok := wantMap["style"].(string); ok {
37 hasQuote := node.Style&(SingleQuotedStyle|DoubleQuotedStyle) != 0
38 switch wantStyle {
39 case "Plain":
40 assert.False(t, hasQuote)
41 case "Single":
42 assert.True(t, hasQuote)
43 }
44 }
45 },
46
47 "desolve-preserve": func(t *testing.T, tc TestCase) {
48 t.Helper()
49
50 node := &Node{
51 Kind: ScalarNode,
52 Tag: tc.Node.Tag,
53 }
54
55 // Handle kind for collection tests
56 if tc.Node.Kind != "" {
57 switch tc.Node.Kind {
58 case "Mapping":
59 node.Kind = MappingNode
60 case "Sequence":
61 node.Kind = SequenceNode
62 }
63 } else {
64 // Scalar node needs value
65 node.Value = tc.Node.Value
66 }
67
68 // Handle style for explicitly tagged tests
69 if tc.Node.Style == "Tagged" {
70 node.Style = TaggedStyle
71 }
72
73 d := NewDesolver(nil)
74 d.Desolve(node)
75
76 // Extract want fields
77 wantMap := tc.Want.(map[string]any)
78 wantTag := wantMap["tag"].(string)
79
80 // Check tag is preserved
81 assert.Equal(t, wantTag, node.Tag)
82
83 // Check style if present
84 if wantStyle, ok := wantMap["style"].(string); ok {
85 hasQuote := node.Style&(SingleQuotedStyle|DoubleQuotedStyle) != 0
86 switch wantStyle {
87 case "Plain":
88 assert.False(t, hasQuote)
89 case "Single":
90 assert.True(t, hasQuote)
91 }
92 }
93 },
94
95 "desolve-string-quoting": func(t *testing.T, tc TestCase) {
96 t.Helper()
97
98 node := &Node{
99 Kind: ScalarNode,
100 Tag: tc.Node.Tag,
101 Value: tc.Node.Value,
102 }
103
104 d := NewDesolver(nil)
105 d.Desolve(node)
106
107 // Extract want fields
108 wantMap := tc.Want.(map[string]any)
109 wantTag := wantMap["tag"].(string)
110
111 // Check tag removed
112 assert.Equal(t, wantTag, node.Tag)
113
114 // Check style
115 if wantStyle, ok := wantMap["style"].(string); ok {
116 hasQuote := node.Style&(SingleQuotedStyle|DoubleQuotedStyle) != 0
117 switch wantStyle {
118 case "Plain":
119 assert.False(t, hasQuote)
120 case "Single":
121 assert.True(t, hasQuote)
122 }
123 }
124 },
125 })
126 }