| 1 | package xml_test |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/tomwright/dasel/v3/model" |
| 8 | "github.com/tomwright/dasel/v3/parsing" |
| 9 | "github.com/tomwright/dasel/v3/parsing/xml" |
| 10 | ) |
| 11 | |
| 12 | func TestXmlWriter_Compact(t *testing.T) { |
| 13 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 14 | if err != nil { |
| 15 | t.Fatalf("Unexpected error: %s", err) |
| 16 | } |
| 17 | |
| 18 | opts := parsing.DefaultWriterOptions() |
| 19 | opts.Compact = true |
| 20 | w, err := xml.XML.NewWriter(opts) |
| 21 | if err != nil { |
| 22 | t.Fatalf("Unexpected error: %s", err) |
| 23 | } |
| 24 | |
| 25 | data, err := r.Read([]byte(`<Document> |
| 26 | <Sender>Ivanov</Sender> |
| 27 | <Content>Hello</Content> |
| 28 | </Document> |
| 29 | `)) |
| 30 | if err != nil { |
| 31 | t.Fatalf("Unexpected error: %s", err) |
| 32 | } |
| 33 | |
| 34 | xmlBytes, err := w.Write(data) |
| 35 | if err != nil { |
| 36 | t.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.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes)) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func TestXmlWriter_CompactWithPI(t *testing.T) { |
| 46 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 47 | if err != nil { |
| 48 | t.Fatalf("Unexpected error: %s", err) |
| 49 | } |
| 50 | |
| 51 | opts := parsing.DefaultWriterOptions() |
| 52 | opts.Compact = true |
| 53 | w, err := xml.XML.NewWriter(opts) |
| 54 | if err != nil { |
| 55 | t.Fatalf("Unexpected error: %s", err) |
| 56 | } |
| 57 | |
| 58 | data, err := r.Read([]byte(`<?xml version="1.0" encoding="utf-8"?> |
| 59 | <Root> |
| 60 | <Name>Test</Name> |
| 61 | </Root> |
| 62 | `)) |
| 63 | if err != nil { |
| 64 | t.Fatalf("Unexpected error: %s", err) |
| 65 | } |
| 66 | |
| 67 | xmlBytes, err := w.Write(data) |
| 68 | if err != nil { |
| 69 | t.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.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes)) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | func TestXmlWriter_CompactWithComments(t *testing.T) { |
| 79 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 80 | if err != nil { |
| 81 | t.Fatalf("Unexpected error: %s", err) |
| 82 | } |
| 83 | |
| 84 | opts := parsing.DefaultWriterOptions() |
| 85 | opts.Compact = true |
| 86 | w, err := xml.XML.NewWriter(opts) |
| 87 | if err != nil { |
| 88 | t.Fatalf("Unexpected error: %s", err) |
| 89 | } |
| 90 | |
| 91 | data, err := r.Read([]byte(`<!-- top comment --> |
| 92 | <Root> |
| 93 | <!-- child comment --> |
| 94 | <Name>Test</Name> |
| 95 | </Root> |
| 96 | `)) |
| 97 | if err != nil { |
| 98 | t.Fatalf("Unexpected error: %s", err) |
| 99 | } |
| 100 | |
| 101 | xmlBytes, err := w.Write(data) |
| 102 | if err != nil { |
| 103 | t.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 !strings.Contains(got, substr) { |
| 110 | t.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 := strings.TrimSuffix(got, "\n") |
| 115 | if strings.Contains(trimmed, "\n ") { |
| 116 | t.Fatalf("Compact output should not contain indentation, got:\n%s", got) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestXmlReader_Write(t *testing.T) { |
| 121 | t.Run("nested xml elements", func(t *testing.T) { |
| 122 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 123 | if err != nil { |
| 124 | t.Fatalf("Unexpected error: %s", err) |
| 125 | } |
| 126 | |
| 127 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 128 | if err != nil { |
| 129 | t.Fatalf("Unexpected error: %s", err) |
| 130 | } |
| 131 | |
| 132 | data, err := r.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.Fatalf("Unexpected error: %s", err) |
| 148 | } |
| 149 | |
| 150 | xmlBytes, err := w.Write(data) |
| 151 | if err != nil { |
| 152 | t.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.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(xmlBytes)) |
| 171 | } |
| 172 | }) |
| 173 | |
| 174 | t.Run("nested xml elements with processing instruction", func(t *testing.T) { |
| 175 | r, err := xml.XML.NewReader(parsing.DefaultReaderOptions()) |
| 176 | if err != nil { |
| 177 | t.Fatalf("Unexpected error: %s", err) |
| 178 | } |
| 179 | |
| 180 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 181 | if err != nil { |
| 182 | t.Fatalf("Unexpected error: %s", err) |
| 183 | } |
| 184 | |
| 185 | data, err := r.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.Fatalf("Unexpected error: %s", err) |
| 202 | } |
| 203 | |
| 204 | doc, err := data.GetMapKey("Document") |
| 205 | if err != nil { |
| 206 | t.Fatalf("Unexpected error: %s", err) |
| 207 | } |
| 208 | docProcessingInstructions, ok := doc.MetadataValue("xml_processing_instructions") |
| 209 | if !ok || docProcessingInstructions == nil { |
| 210 | t.Fatalf("Expected processing instructions on Document element") |
| 211 | } |
| 212 | |
| 213 | jsonBytes, err := w.Write(data) |
| 214 | if err != nil { |
| 215 | t.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.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(jsonBytes)) |
| 235 | } |
| 236 | }) |
| 237 | |
| 238 | t.Run("encode attributes", func(t *testing.T) { |
| 239 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 240 | if err != nil { |
| 241 | t.Fatalf("Unexpected error: %s", err) |
| 242 | } |
| 243 | |
| 244 | toEncode := model.NewMapValue() |
| 245 | foo := model.NewMapValue() |
| 246 | _ = foo.SetMapKey("-fiz", model.NewStringValue("hello")) |
| 247 | _ = foo.SetMapKey("bar", model.NewStringValue("")) |
| 248 | _ = toEncode.SetMapKey("foo", foo) |
| 249 | |
| 250 | got, err := w.Write(toEncode) |
| 251 | if err != nil { |
| 252 | t.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.Errorf("Expected:\n%s\nGot:\n%s", string(exp), string(got)) |
| 260 | } |
| 261 | }) |
| 262 | |
| 263 | t.Run("invalid element name", func(t *testing.T) { |
| 264 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 265 | if err != nil { |
| 266 | t.Fatalf("Unexpected error: %s", err) |
| 267 | } |
| 268 | |
| 269 | toEncode := model.NewMapValue() |
| 270 | _ = toEncode.SetMapKey("<", model.NewStringValue("value")) |
| 271 | _, err = w.Write(toEncode) |
| 272 | if err == nil { |
| 273 | t.Fatal("Expected error for invalid XML element name, got nil") |
| 274 | } |
| 275 | if !strings.Contains(err.Error(), `"<"`) || !strings.Contains(err.Error(), "not a valid XML element name") { |
| 276 | t.Fatalf("Expected error to mention the offending key, got: %s", err) |
| 277 | } |
| 278 | }) |
| 279 | |
| 280 | t.Run("invalid element name ampersand", func(t *testing.T) { |
| 281 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 282 | if err != nil { |
| 283 | t.Fatalf("Unexpected error: %s", err) |
| 284 | } |
| 285 | |
| 286 | toEncode := model.NewMapValue() |
| 287 | _ = toEncode.SetMapKey("&", model.NewStringValue("value")) |
| 288 | _, err = w.Write(toEncode) |
| 289 | if err == nil { |
| 290 | t.Fatal("Expected error for invalid XML element name, got nil") |
| 291 | } |
| 292 | if !strings.Contains(err.Error(), `"&"`) || !strings.Contains(err.Error(), "not a valid XML element name") { |
| 293 | t.Fatalf("Expected error to mention the offending key, got: %s", err) |
| 294 | } |
| 295 | }) |
| 296 | |
| 297 | t.Run("invalid element name with space", func(t *testing.T) { |
| 298 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 299 | if err != nil { |
| 300 | t.Fatalf("Unexpected error: %s", err) |
| 301 | } |
| 302 | |
| 303 | toEncode := model.NewMapValue() |
| 304 | _ = toEncode.SetMapKey("foo bar", model.NewStringValue("value")) |
| 305 | _, err = w.Write(toEncode) |
| 306 | if err == nil { |
| 307 | t.Fatal("Expected error for invalid XML element name, got nil") |
| 308 | } |
| 309 | if !strings.Contains(err.Error(), `"foo bar"`) || !strings.Contains(err.Error(), "not a valid XML element name") { |
| 310 | t.Fatalf("Expected error to mention the offending key, got: %s", err) |
| 311 | } |
| 312 | }) |
| 313 | |
| 314 | t.Run("invalid attribute name", func(t *testing.T) { |
| 315 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 316 | if err != nil { |
| 317 | t.Fatalf("Unexpected error: %s", err) |
| 318 | } |
| 319 | |
| 320 | toEncode := model.NewMapValue() |
| 321 | child := model.NewMapValue() |
| 322 | _ = child.SetMapKey("-<", model.NewStringValue("value")) |
| 323 | _ = toEncode.SetMapKey("foo", child) |
| 324 | _, err = w.Write(toEncode) |
| 325 | if err == nil { |
| 326 | t.Fatal("Expected error for invalid XML attribute name, got nil") |
| 327 | } |
| 328 | if !strings.Contains(err.Error(), "invalid XML attribute name") || !strings.Contains(err.Error(), `"-<"`) { |
| 329 | t.Fatalf("Expected error to mention the offending attribute key, got: %s", err) |
| 330 | } |
| 331 | }) |
| 332 | |
| 333 | t.Run("encode cdata", func(t *testing.T) { |
| 334 | w, err := xml.XML.NewWriter(parsing.DefaultWriterOptions()) |
| 335 | if err != nil { |
| 336 | t.Fatalf("Unexpected error: %s", err) |
| 337 | } |
| 338 | toEncode := model.NewMapValue() |
| 339 | _ = toEncode.SetMapKey("foo", model.NewStringValue("<bar>baz</bar>")) |
| 340 | got, err := w.Write(toEncode) |
| 341 | if err != nil { |
| 342 | t.Fatalf("Unexpected error: %s", err) |
| 343 | } |
| 344 | // TODO : Change this to use CDATA sections rather than escaping. |
| 345 | exp := []byte(`<foo><bar>baz</bar></foo> |
| 346 | `) |
| 347 | if string(got) != string(exp) { |
| 348 | t.Errorf("Expected:\n%s\nGot:\n%s", string(exp), string(got)) |
| 349 | } |
| 350 | }) |
| 351 | } |