Debian Codegraph @

parsing/xml/writer_test.go

raw symbols SCIP
1 package xml_testscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/
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 "github.com/tomwright/dasel/v3/parsing/xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/"
10 )
11
12 func TestXmlWriter_Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXmlWriter_Compact().(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 r, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
14 if err != nil {
15 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
16 }
17
18 opts := 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().()
19 opts.Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. = true
20 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(opts)
21 if err != nil {
22 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
23 }
24
25 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<Document>
26 <Sender>Ivanov</Sender>
27 <Content>Hello</Content>
28 </Document>
29 `))
30 if err != nil {
31 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
32 }
33
34 xmlBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
35 if err != nil {
36 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
37 }
38
39 expected := "<Document><Sender>Ivanov</Sender><Content>Hello</Content></Document>\n"
40 if string(xmlBytes) != expected {
41 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes))
42 }
43 }
44
45 func TestXmlWriter_CompactWithPIscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXmlWriter_CompactWithPI().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
46 r, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
47 if err != nil {
48 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
49 }
50
51 opts := 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().()
52 opts.Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. = true
53 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(opts)
54 if err != nil {
55 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
56 }
57
58 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<?xml version="1.0" encoding="utf-8"?>
59 <Root>
60 <Name>Test</Name>
61 </Root>
62 `))
63 if err != nil {
64 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
65 }
66
67 xmlBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
68 if err != nil {
69 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
70 }
71
72 expected := `<?xml version="1.0" encoding="utf-8"?><Root><Name>Test</Name></Root>` + "\n"
73 if string(xmlBytes) != expected {
74 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes))
75 }
76 }
77
78 func TestXmlWriter_CompactWithCommentsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXmlWriter_CompactWithComments().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
79 r, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
80 if err != nil {
81 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
82 }
83
84 opts := 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().()
85 opts.Compactscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/WriterOptions#Compact. = true
86 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(opts)
87 if err != nil {
88 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
89 }
90
91 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<!-- top comment -->
92 <Root>
93 <!-- child comment -->
94 <Name>Test</Name>
95 </Root>
96 `))
97 if err != nil {
98 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
99 }
100
101 xmlBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
102 if err != nil {
103 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
104 }
105
106 got := string(xmlBytes)
107 // Compact output should contain all elements and comments
108 for _, substr := range []string{"<!-- top comment -->", "<!-- child comment -->", "<Root>", "<Name>Test</Name>", "</Root>"} {
109 if !stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Containsscip-go gomod github.com/golang/go/src go1.25 strings/Contains().(got, substr) {
110 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected output to contain %q, got:\n%s", substr, got)
111 }
112 }
113 // Should not contain indentation whitespace (check content before trailing newline)
114 trimmed := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25 strings/TrimSuffix().(got, "\n")
115 if stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Containsscip-go gomod github.com/golang/go/src go1.25 strings/Contains().(trimmed, "\n ") {
116 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Compact output should not contain indentation, got:\n%s", got)
117 }
118 }
119
120 func TestXmlReader_Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXmlReader_Write().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
121 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("nested xml elements", 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#) {
122 r, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
123 if err != nil {
124 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
125 }
126
127 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
128 if err != nil {
129 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
130 }
131
132 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<Document>
133 <Sender>Ivanov</Sender>
134 <In_N_Document>
135 <N_Document>1024</N_Document>
136 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
137 </In_N_Document>
138 <Out_N_Document>
139 <N_Document>2043</N_Document>
140 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
141 </Out_N_Document>
142 <Content>Skzzkz</Content>
143 <DSP>true</DSP>
144 </Document>
145 `))
146 if err != nil {
147 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
148 }
149
150 xmlBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
151 if err != nil {
152 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
153 }
154
155 expected := `<Document>
156 <Sender>Ivanov</Sender>
157 <In_N_Document>
158 <N_Document>1024</N_Document>
159 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
160 </In_N_Document>
161 <Out_N_Document>
162 <N_Document>2043</N_Document>
163 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
164 </Out_N_Document>
165 <Content>Skzzkz</Content>
166 <DSP>true</DSP>
167 </Document>
168 `
169 if string(xmlBytes) != expected {
170 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes))
171 }
172 })
173
174 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("nested xml elements with processing instruction", 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#) {
175 r, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewReaderscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewReader().(parsingscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/.DefaultReaderOptionsscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/DefaultReaderOptions().())
176 if err != nil {
177 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
178 }
179
180 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
181 if err != nil {
182 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
183 }
184
185 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
186 <Document>
187 <Sender>Ivanov</Sender>
188 <In_N_Document>
189 <N_Document>1024</N_Document>
190 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
191 </In_N_Document>
192 <Out_N_Document>
193 <N_Document>2043</N_Document>
194 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
195 </Out_N_Document>
196 <Content>Skzzkz</Content>
197 <DSP>true</DSP>
198 </Document>
199 `))
200 if err != nil {
201 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
202 }
203
204 doc, err := data.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("Document")
205 if err != nil {
206 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
207 }
208 docProcessingInstructions, ok := doc.MetadataValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#MetadataValue().("xml_processing_instructions")
209 if !ok || docProcessingInstructions == nil {
210 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected processing instructions on Document element")
211 }
212
213 jsonBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
214 if err != nil {
215 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
216 }
217
218 expected := `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
219 <Document>
220 <Sender>Ivanov</Sender>
221 <In_N_Document>
222 <N_Document>1024</N_Document>
223 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
224 </In_N_Document>
225 <Out_N_Document>
226 <N_Document>2043</N_Document>
227 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
228 </Out_N_Document>
229 <Content>Skzzkz</Content>
230 <DSP>true</DSP>
231 </Document>
232 `
233 if string(jsonBytes) != expected {
234 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes))
235 }
236 })
237
238 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("encode attributes", 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#) {
239 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
240 if err != nil {
241 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
242 }
243
244 toEncode := 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().()
245 foo := 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().()
246 _ = foo.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("-fiz", 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"))
247 _ = foo.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("bar", 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().(""))
248 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("foo", foo)
249
250 got, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
251 if err != nil {
252 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
253 }
254 exp := []byte(`<foo fiz="hello">
255 <bar></bar>
256 </foo>
257 `)
258 if string(got) != string(exp) {
259 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("Expected:\n%s\nGot:\n%s", string(exp), string(got))
260 }
261 })
262
263 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("invalid element name", 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#) {
264 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
265 if err != nil {
266 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
267 }
268
269 toEncode := 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().()
270 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("<", 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().("value"))
271 _, err = w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
272 if err == nil {
273 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("Expected error for invalid XML element name, got nil")
274 }
275 if !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(), `"<"`) || !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(), "not a valid XML element name") {
276 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected error to mention the offending key, got: %s", err)
277 }
278 })
279
280 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("invalid element name ampersand", 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#) {
281 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
282 if err != nil {
283 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
284 }
285
286 toEncode := 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().()
287 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("&", 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().("value"))
288 _, err = w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
289 if err == nil {
290 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("Expected error for invalid XML element name, got nil")
291 }
292 if !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(), `"&"`) || !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(), "not a valid XML element name") {
293 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected error to mention the offending key, got: %s", err)
294 }
295 })
296
297 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("invalid element name with space", 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#) {
298 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
299 if err != nil {
300 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
301 }
302
303 toEncode := 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().()
304 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("foo bar", 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().("value"))
305 _, err = w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
306 if err == nil {
307 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("Expected error for invalid XML element name, got nil")
308 }
309 if !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(), `"foo bar"`) || !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(), "not a valid XML element name") {
310 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected error to mention the offending key, got: %s", err)
311 }
312 })
313
314 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("invalid attribute name", 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#) {
315 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
316 if err != nil {
317 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
318 }
319
320 toEncode := 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().()
321 child := 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().()
322 _ = child.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("-<", 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().("value"))
323 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("foo", child)
324 _, err = w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
325 if err == nil {
326 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().("Expected error for invalid XML attribute name, got nil")
327 }
328 if !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(), "invalid XML attribute name") || !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(), `"-<"`) {
329 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected error to mention the offending attribute key, got: %s", err)
330 }
331 })
332
333 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("encode cdata", 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#) {
334 w, err := xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.XMLscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/XML..NewWriterscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Format#NewWriter().(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().())
335 if err != nil {
336 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
337 }
338 toEncode := 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().()
339 _ = toEncode.SetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#SetMapKey().("foo", 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().("<bar>baz</bar>"))
340 got, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(toEncode)
341 if err != nil {
342 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
343 }
344 // TODO : Change this to use CDATA sections rather than escaping.
345 exp := []byte(`<foo>&lt;bar&gt;baz&lt;/bar&gt;</foo>
346 `)
347 if string(got) != string(exp) {
348 t.Errorfscip-go gomod github.com/golang/go/src go1.25 testing/common#Errorf().("Expected:\n%s\nGot:\n%s", string(exp), string(got))
349 }
350 })
351 }