Debian Codegraph @

parsing/xml/reader_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 "errorsscip-go gomod github.com/golang/go/src go1.25 errors/"
5 "stringsscip-go gomod github.com/golang/go/src go1.25 strings/"
6 "testingscip-go gomod github.com/golang/go/src go1.25 testing/"
7
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/jsonscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/"
10 "github.com/tomwright/dasel/v3/parsing/xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/"
11 )
12
13 func TestXmlReader_Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXmlReader_Read().(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 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#) {
15 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().())
16 if err != nil {
17 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
18 }
19
20 w, err := jsonscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/.JSONscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/JSON..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().())
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 <In_N_Document>
28 <N_Document>1024</N_Document>
29 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
30 </In_N_Document>
31 <Out_N_Document>
32 <N_Document>2043</N_Document>
33 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
34 </Out_N_Document>
35 <Content>Skzzkz</Content>
36 <DSP>true</DSP>
37 </Document>
38 `))
39 if err != nil {
40 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
41 }
42
43 jsonBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
44 if err != nil {
45 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
46 }
47
48 expected := `{
49 "Document": {
50 "Sender": "Ivanov",
51 "In_N_Document": {
52 "N_Document": "1024",
53 "Date_Reg": "2024-06-21T15:07:29.0451517+03:00"
54 },
55 "Out_N_Document": {
56 "N_Document": "2043",
57 "Date_Reg": "2024-05-01T00:00:00"
58 },
59 "Content": "Skzzkz",
60 "DSP": "true"
61 }
62 }
63 `
64 if string(jsonBytes) != expected {
65 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes))
66 }
67 })
68
69 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#) {
70 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().())
71 if err != nil {
72 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
73 }
74
75 w, err := jsonscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/.JSONscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/json`/JSON..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().())
76 if err != nil {
77 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
78 }
79
80 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"?>
81 <Document>
82 <Sender>Ivanov</Sender>
83 <In_N_Document>
84 <N_Document>1024</N_Document>
85 <Date_Reg>2024-06-21T15:07:29.0451517+03:00</Date_Reg>
86 </In_N_Document>
87 <Out_N_Document>
88 <N_Document>2043</N_Document>
89 <Date_Reg>2024-05-01T00:00:00</Date_Reg>
90 </Out_N_Document>
91 <Content>Skzzkz</Content>
92 <DSP>true</DSP>
93 </Document>
94 `))
95 if err != nil {
96 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
97 }
98
99 jsonBytes, err := w.Writescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Writer#Write.(data)
100 if err != nil {
101 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
102 }
103
104 expected := `{
105 "Document": {
106 "Sender": "Ivanov",
107 "In_N_Document": {
108 "N_Document": "1024",
109 "Date_Reg": "2024-06-21T15:07:29.0451517+03:00"
110 },
111 "Out_N_Document": {
112 "N_Document": "2043",
113 "Date_Reg": "2024-05-01T00:00:00"
114 },
115 "Content": "Skzzkz",
116 "DSP": "true"
117 }
118 }
119 `
120 if string(jsonBytes) != expected {
121 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes))
122 }
123 })
124
125 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("cdata tag", 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 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().())
127 if err != nil {
128 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
129 }
130
131 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<foo>
132 <![CDATA[<bar>baz</bar>]]>
133 </foo>
134 `))
135 if err != nil {
136 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
137 }
138
139 x, err := data.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("foo")
140 if err != nil {
141 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
142 }
143 got, err := x.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
144 if err != nil {
145 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
146 }
147
148 exp := "<bar>baz</bar>"
149 if exp != got {
150 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected value %q but got %q", exp, got)
151 }
152 })
153
154 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("empty cdata tag", 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#) {
155 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().())
156 if err != nil {
157 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
158 }
159
160 data, err := r.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(`<foo>
161 <![CDATA[]]>
162 </foo>
163 `))
164 if err != nil {
165 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
166 }
167
168 x, err := data.GetMapKeyscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#GetMapKey().("foo")
169 if err != nil {
170 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
171 }
172 got, err := x.StringValuescip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/model`/Value#StringValue().()
173 if err != nil {
174 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Unexpected error: %s", err)
175 }
176
177 exp := ""
178 if exp != got {
179 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("Expected value %q but got %q", exp, got)
180 }
181 })
182 }
183
184 func TestXMLReader_DepthLimitscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml_test`/TestXMLReader_DepthLimit().(t *testingscip-go gomod github.com/golang/go/src go1.25 testing/.Tscip-go gomod github.com/golang/go/src go1.25 testing/T#) {
185 reader, 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().())
186 if err != nil {
187 t.Fatalscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatal().(err)
188 }
189
190 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("deeply nested elements exceeding limit return 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#) {
191 // 10_001 levels — one past the 10_000 limit. Each open tag is 3 bytes (<a>),
192 // so total ~60 KB, well under the 10 MB size cap.
193 const depth = 10_001
194 open := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25 strings/Repeat().("<a>", depth)
195 close := stringsscip-go gomod github.com/golang/go/src go1.25 strings/.Repeatscip-go gomod github.com/golang/go/src go1.25 strings/Repeat().("</a>", depth)
196 _, err := reader.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.([]byte(open + close))
197 if !errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Isscip-go gomod github.com/golang/go/src go1.25 errors/Is().(err, xmlscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/.ErrXMLMaxDepthExceededscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing/xml`/ErrXMLMaxDepthExceeded.) {
198 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("expected ErrXMLMaxDepthExceeded, got %v", err)
199 }
200 })
201
202 t.Runscip-go gomod github.com/golang/go/src go1.25 testing/T#Run().("normal nesting within limit succeeds", 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 data := []byte(`<root><a><b><c>hello</c></b></a></root>`)
204 _, err := reader.Readscip-go gomod github.com/tomwright/dasel/v3 . `github.com/tomwright/dasel/v3/parsing`/Reader#Read.(data)
205 if err != nil {
206 t.Fatalfscip-go gomod github.com/golang/go/src go1.25 testing/common#Fatalf().("unexpected error: %v", err)
207 }
208 })
209 }