Debian Codegraph @

internal/libyaml/resolver_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 Resolver stage
5
6 package libyaml
7
8 import (
9 "testing"
10 )
11
12 func TestResolver(t *testing.T) {
13 RunTestCases(t, "resolver.yaml", map[string]TestHandler{
14 "resolve-default": func(t *testing.T, tc TestCase) {
15 t.Helper()
16
17 // Create node from test case
18 node := &Node{
19 Tag: tc.Node.Tag,
20 Value: tc.Node.Value,
21 }
22
23 // Parse kind
24 switch tc.Node.Kind {
25 case "Scalar":
26 node.Kind = ScalarNode
27 case "Sequence":
28 node.Kind = SequenceNode
29 case "Mapping":
30 node.Kind = MappingNode
31 default:
32 node.Kind = ScalarNode // default
33 }
34
35 // Parse style
36 switch tc.Node.Style {
37 case "Single":
38 node.Style = SingleQuotedStyle
39 case "Double":
40 node.Style = DoubleQuotedStyle
41 case "Literal":
42 node.Style = LiteralStyle
43 case "Folded":
44 node.Style = FoldedStyle
45 case "Flow":
46 node.Style = FlowStyle
47 case "Tagged":
48 node.Style = TaggedStyle
49 }
50
51 // Resolve the node
52 r := NewResolver(nil)
53 r.Resolve(node)
54
55 // Extract want fields
56 wantMap := tc.Want.(map[string]any)
57 wantTag := wantMap["tag"].(string)
58
59 // Check tag
60 if node.Tag != wantTag {
61 t.Fatalf("got tag %q; want %q", node.Tag, wantTag)
62 }
63 },
64
65 "resolve-infer": func(t *testing.T, tc TestCase) {
66 t.Helper()
67
68 // Create scalar node from test case
69 node := &Node{
70 Kind: ScalarNode,
71 Tag: tc.Node.Tag,
72 Value: tc.Node.Value,
73 }
74
75 // Resolve the node
76 r := NewResolver(nil)
77 r.Resolve(node)
78
79 // Extract want fields
80 wantMap := tc.Want.(map[string]any)
81 wantTag := wantMap["tag"].(string)
82
83 // Check tag
84 if node.Tag != wantTag {
85 t.Fatalf("got tag %q; want %q", node.Tag, wantTag)
86 }
87 },
88
89 "resolve-preserve": func(t *testing.T, tc TestCase) {
90 t.Helper()
91
92 // Create scalar node with pre-existing tag
93 node := &Node{
94 Kind: ScalarNode,
95 Tag: tc.Node.Tag,
96 Value: tc.Node.Value,
97 }
98
99 // Resolve the node
100 r := NewResolver(nil)
101 r.Resolve(node)
102
103 // Extract want fields
104 wantMap := tc.Want.(map[string]any)
105 wantTag := wantMap["tag"].(string)
106
107 // Check tag is preserved
108 if node.Tag != wantTag {
109 t.Fatalf("got tag %q; want %q (tag should be preserved)", node.Tag, wantTag)
110 }
111 },
112 })
113 }