Debian Codegraph @

parsing/xml/writer_internal_test.go

raw symbols SCIP
1 package xml
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/tomwright/dasel/v3/model"
8 "github.com/tomwright/dasel/v3/parsing"
9 )
10
11 // TestXmlWriter_CommentValidation tests validation of comment content during write
12 func TestXmlWriter_CommentValidation(t *testing.T) {
13 t.Run("reject comment containing double dash sequence", func(t *testing.T) {
14 w, err := newXMLWriter(parsing.DefaultWriterOptions())
15 if err != nil {
16 t.Fatalf("Unexpected error creating writer: %s", err)
17 }
18
19 // Create a child value with an invalid comment containing -- sequence
20 // The comments are attached to child elements, not the root
21 childValue := model.NewMapValue()
22 childValue.SetMetadataValue("xml_comments", []*xmlComment{{Text: "invalid--comment"}})
23 if err := childValue.SetMapKey("item", model.NewStringValue("text")); err != nil {
24 t.Fatalf("Unexpected error setting map key: %s", err)
25 }
26
27 // Create root value containing the child
28 rootValue := model.NewMapValue()
29 if err := rootValue.SetMapKey("child", childValue); err != nil {
30 t.Fatalf("Unexpected error setting map key: %s", err)
31 }
32
33 _, err = w.Write(rootValue)
34 if err == nil {
35 t.Errorf("Expected error for comment containing double dash")
36 }
37 if err != nil && !strings.Contains(err.Error(), "cannot contain '--'") {
38 t.Errorf("Expected error about double dash sequence, got: %s", err)
39 }
40 })
41
42 t.Run("accept valid comment without double dash", func(t *testing.T) {
43 w, err := newXMLWriter(parsing.DefaultWriterOptions())
44 if err != nil {
45 t.Fatalf("Unexpected error creating writer: %s", err)
46 }
47
48 // Create a child value with a valid comment
49 childValue := model.NewMapValue()
50 childValue.SetMetadataValue("xml_comments", []*xmlComment{{Text: "valid comment with single - dash"}})
51 if err := childValue.SetMapKey("item", model.NewStringValue("text")); err != nil {
52 t.Fatalf("Unexpected error setting map key: %s", err)
53 }
54
55 // Create root value containing the child
56 rootValue := model.NewMapValue()
57 if err := rootValue.SetMapKey("child", childValue); err != nil {
58 t.Fatalf("Unexpected error setting map key: %s", err)
59 }
60
61 output, err := w.Write(rootValue)
62 if err != nil {
63 t.Fatalf("Unexpected error writing XML: %s", err)
64 }
65
66 outputStr := string(output)
67 if !strings.Contains(outputStr, "<!--valid comment with single - dash-->") {
68 t.Errorf("Expected output to contain valid comment, got:\n%s", outputStr)
69 }
70 })
71 }
72
73 // Test_isValidXMLName tests XML name validation per the XML 1.0 spec.
74 func Test_isValidXMLName(t *testing.T) {
75 valid := []string{"foo", "Foo", "_bar", "ns:local", "a1", "a-b", "a.b", "über"}
76 for _, name := range valid {
77 if !isValidXMLName(name) {
78 t.Errorf("Expected %q to be a valid XML name", name)
79 }
80 }
81
82 invalid := []string{"", "<", ">", "&", "foo bar", "1abc", "-abc", ".abc", "a<b", "a>b", "a&b"}
83 for _, name := range invalid {
84 if isValidXMLName(name) {
85 t.Errorf("Expected %q to be an invalid XML name", name)
86 }
87 }
88 }
89
90 // Test_valueToString tests the valueToString function for all supported types
91 func Test_valueToString(t *testing.T) {
92 t.Run("null value returns empty string", func(t *testing.T) {
93 v := model.NewNullValue()
94 result, err := valueToString(v)
95 if err != nil {
96 t.Errorf("Unexpected error for null value: %s", err)
97 }
98 if result != "" {
99 t.Errorf("Expected empty string for null value, got: %q", result)
100 }
101 })
102
103 t.Run("string value", func(t *testing.T) {
104 v := model.NewStringValue("hello world")
105 result, err := valueToString(v)
106 if err != nil {
107 t.Errorf("Unexpected error for string value: %s", err)
108 }
109 if result != "hello world" {
110 t.Errorf("Expected 'hello world', got: %q", result)
111 }
112 })
113
114 t.Run("empty string value", func(t *testing.T) {
115 v := model.NewStringValue("")
116 result, err := valueToString(v)
117 if err != nil {
118 t.Errorf("Unexpected error for empty string value: %s", err)
119 }
120 if result != "" {
121 t.Errorf("Expected empty string, got: %q", result)
122 }
123 })
124
125 t.Run("int value positive", func(t *testing.T) {
126 v := model.NewIntValue(42)
127 result, err := valueToString(v)
128 if err != nil {
129 t.Errorf("Unexpected error for int value: %s", err)
130 }
131 if result != "42" {
132 t.Errorf("Expected '42', got: %q", result)
133 }
134 })
135
136 t.Run("int value negative", func(t *testing.T) {
137 v := model.NewIntValue(-123)
138 result, err := valueToString(v)
139 if err != nil {
140 t.Errorf("Unexpected error for negative int value: %s", err)
141 }
142 if result != "-123" {
143 t.Errorf("Expected '-123', got: %q", result)
144 }
145 })
146
147 t.Run("int value zero", func(t *testing.T) {
148 v := model.NewIntValue(0)
149 result, err := valueToString(v)
150 if err != nil {
151 t.Errorf("Unexpected error for zero int value: %s", err)
152 }
153 if result != "0" {
154 t.Errorf("Expected '0', got: %q", result)
155 }
156 })
157
158 t.Run("float value", func(t *testing.T) {
159 v := model.NewFloatValue(3.14159)
160 result, err := valueToString(v)
161 if err != nil {
162 t.Errorf("Unexpected error for float value: %s", err)
163 }
164 if result != "3.14159" {
165 t.Errorf("Expected '3.14159', got: %q", result)
166 }
167 })
168
169 t.Run("float value negative", func(t *testing.T) {
170 v := model.NewFloatValue(-2.5)
171 result, err := valueToString(v)
172 if err != nil {
173 t.Errorf("Unexpected error for negative float value: %s", err)
174 }
175 if result != "-2.5" {
176 t.Errorf("Expected '-2.5', got: %q", result)
177 }
178 })
179
180 t.Run("float value zero", func(t *testing.T) {
181 v := model.NewFloatValue(0.0)
182 result, err := valueToString(v)
183 if err != nil {
184 t.Errorf("Unexpected error for zero float value: %s", err)
185 }
186 if result != "0" {
187 t.Errorf("Expected '0', got: %q", result)
188 }
189 })
190
191 t.Run("bool value true", func(t *testing.T) {
192 v := model.NewBoolValue(true)
193 result, err := valueToString(v)
194 if err != nil {
195 t.Errorf("Unexpected error for bool true value: %s", err)
196 }
197 if result != "true" {
198 t.Errorf("Expected 'true', got: %q", result)
199 }
200 })
201
202 t.Run("bool value false", func(t *testing.T) {
203 v := model.NewBoolValue(false)
204 result, err := valueToString(v)
205 if err != nil {
206 t.Errorf("Unexpected error for bool false value: %s", err)
207 }
208 if result != "false" {
209 t.Errorf("Expected 'false', got: %q", result)
210 }
211 })
212
213 t.Run("map value returns error", func(t *testing.T) {
214 v := model.NewMapValue()
215 _, err := valueToString(v)
216 if err == nil {
217 t.Errorf("Expected error for map value")
218 }
219 if err != nil && !strings.Contains(err.Error(), "cannot format type") {
220 t.Errorf("Expected error about formatting type, got: %s", err)
221 }
222 })
223
224 t.Run("slice value returns error", func(t *testing.T) {
225 v := model.NewSliceValue()
226 _, err := valueToString(v)
227 if err == nil {
228 t.Errorf("Expected error for slice value")
229 }
230 if err != nil && !strings.Contains(err.Error(), "cannot format type") {
231 t.Errorf("Expected error about formatting type, got: %s", err)
232 }
233 })
234 }