Debian Codegraph @

parsing/xml/writer_internal_test.go

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